All files / lib aws-organizations-stack.ts

93.18% Statements 41/44
70.59% Branches 12/17
80% Functions 4/5
93.18% Lines 41/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170                                1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                             1x             8x   8x   8x   8x     12x       12x   12x             12x           12x 12x     8x       8x       4x 4x   4x   4x 4x   4x 4x   4x 3x 3x       4x   4x 8x     4x 4x     4x 4x   4x        
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
 
import * as cdk from '@aws-cdk/core';
import * as sns from '@aws-cdk/aws-sns';
import * as subs from '@aws-cdk/aws-sns-subscriptions';
import {Organization} from './organization';
import {OrganizationalUnit} from './organizational-unit';
import {Account} from './account';
import {SecureRootUser} from './secure-root-user';
import {OrganizationTrail} from './organization-trail';
import {version} from '../package.json';
import ValidateEmailStack from './validate-email';
 
/**
 * AWS Account input details
 */
export interface AccountSpec {
 
  /**
   * The name of the AWS account
   */
  readonly name: string,
 
  /**
   * The email associated to the AWS account
   */
  readonly email?: string
}
 
/**
 * Organizational Unit Input details
 */
export interface OUSpec {
  /**
   * Name of the Organizational Unit
   */
  readonly name: string,
 
  /**
   * Accounts' specification inside in this Organizational Unit
   */
  readonly accounts: AccountSpec[],
 
  /**
   * Specification of sub Organizational Unit
   */
  readonly nestedOU?: OUSpec[]
}
 
/**
 * Properties for AWS SDLC Organizations Stack
 * @experimental
 */
export interface AwsOrganizationsStackProps extends cdk.StackProps {
 
  /**
   * Email address of the Root account
   */
  readonly email: string,
 
  /**
   * Specification of the sub Organizational Unit
   */
  readonly nestedOU: OUSpec[],
 
  /**
   * Enable Email Verification Process
   */
  readonly forceEmailVerification?: boolean,
}
 
/**
 * A Stack creating the Software Development Life Cycle (SDLC) Organization
 */
export class AwsOrganizationsStack extends cdk.Stack {
 
  private readonly emailPrefix?: string;
  private readonly domain?: string;
 
  private createOrganizationTree(oUSpec: OUSpec, parentId: string, previousSequentialConstruct: cdk.IDependable): cdk.IDependable {
 
    let organizationalUnit = new OrganizationalUnit(this, `${oUSpec.name}-OU`, {Name: oUSpec.name, ParentId: parentId});
    //adding an explicit dependency as CloudFormation won't infer that Organization, Organizational Units and Accounts must be created or modified sequentially
    organizationalUnit.node.addDependency(previousSequentialConstruct);
 
    previousSequentialConstruct = organizationalUnit;
 
    oUSpec.accounts.forEach(accountSpec => {
 
      let accountEmail: string;
      Iif(accountSpec.email)
      {
        accountEmail = accountSpec.email;
      }
      else Eif(this.emailPrefix && this.domain)
      {
        accountEmail = `${this.emailPrefix}+${accountSpec.name}-${cdk.Stack.of(this).account}@${this.domain}`
      }
      else
      {
        throw new Error(`Master account email must be provided or an account email for account ${accountSpec.name}`)
      }
 
      let account = new Account(this, accountSpec.name, {
        email: accountEmail,
        name: accountSpec.name,
        parentOrganizationalUnitId: organizationalUnit.id
      });
      //adding an explicit dependency as CloudFormation won't infer that Organization, Organizational Units and Accounts must be created or modified sequentially
      account.node.addDependency(previousSequentialConstruct);
      previousSequentialConstruct = account;
    });
 
    oUSpec.nestedOU?.forEach(nestedOU => {
      previousSequentialConstruct = this.createOrganizationTree(nestedOU, organizationalUnit.id, previousSequentialConstruct);
    });
 
    return previousSequentialConstruct;
  }
 
  constructor(scope: cdk.Construct, id: string, props: AwsOrganizationsStackProps) {
    super(scope, id, {description: `Software development Landing Zone (uksb-1r7an8o45) (version:${version})`, ...props});
    const {email, nestedOU, forceEmailVerification = true} = props;
 
    Eif(nestedOU.length > 0)
    {
      let org = new Organization(this, "Organization");
      Eif(email)
      {
        this.emailPrefix = email.split('@', 2)[0];
        this.domain = email.split('@', 2)[1];
 
        if(forceEmailVerification) {
          const validateEmail = new ValidateEmailStack(this, 'EmailValidation', { email });
          org.node.addDependency(validateEmail);
        }
      }
 
      let previousSequentialConstruct: cdk.IDependable = org;
 
      nestedOU.forEach(nestedOU => {
        previousSequentialConstruct = this.createOrganizationTree(nestedOU, org.rootId, previousSequentialConstruct);
      });
 
      let orgTrail = new OrganizationTrail(this, 'OrganizationTrail', {OrganizationId: org.id});
      orgTrail.node.addDependency(org);
    }
 
    const secureRootUserConfigTopic = new sns.Topic(this, 'SecureRootUserConfigTopic');
    secureRootUserConfigTopic.addSubscription(new subs.EmailSubscription(email));
 
    new SecureRootUser(this, 'SecureRootUser', secureRootUserConfigTopic);
 
  }
}