All files / lib aws-organizations-stack.ts

93.48% Statements 43/46
69.57% Branches 16/23
80% Functions 4/5
93.48% Lines 43/46

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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                                2x 2x 2x 2x 2x 2x 2x 2x 2x                                                                                                                                                                                 2x       6x       12x   12x   12x   12x   18x       18x   18x             18x                   18x 18x     18x 18x       12x       12x       6x 6x   6x   6x 6x   6x 6x   6x 5x 5x       6x 6x   6x   6x 12x           6x 1x             6x      
/*
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 {Organization} from './organization';
import {OrganizationalUnit} from './organizational-unit';
import {Account, AccountType} from './account';
import {SecureRootUser} from './secure-root-user';
import {OrganizationTrail} from './organization-trail';
import {version} from '../package.json';
import { RootDns } from './dns';
import { ValidateEmail } 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
  /**
   * The account type
   */
  readonly type?: AccountType;
  /**
   * The (optional) Stage name to be used in CI/CD pipeline
   */
  readonly stageName?: string;
  /**
   * The (optional) Stage deployment order
   */
  readonly stageOrder?: number;
  /**
   *  List of your services that will be hosted in this account. Set it to [ALL] if you don't plan to have dedicated account for each service.
   */
  readonly hostedServices?: 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[],
 
  /**
   * The main DNS domain name to manage
   */
  readonly rootHostedZoneDNSName?: string,
 
  /**
   * A boolean used to decide if domain should be requested through this delpoyment or if already registered through a third party
   */
  readonly thirdPartyProviderDNSUsed?: boolean,
 
  /**
  * 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 readonly stageAccounts: Account[] = []; 
 
  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,
        type: accountSpec.type,
        stageName: accountSpec.stageName,
        stageOrder: accountSpec.stageOrder,
        hostedServices: accountSpec.hostedServices
      });
      // 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;
 
      // Building stageAccounts array to be used for DNS delegation system
      Eif(['Prod', 'SDLC'].includes(oUSpec.name)) {
        this.stageAccounts.push(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 ValidateEmail(this, 'EmailValidation', { email });
          org.node.addDependency(validateEmail);
        }
      }
 
      let orgTrail = new OrganizationTrail(this, 'OrganizationTrail', {OrganizationId: org.id});
      orgTrail.node.addDependency(org);
 
      let previousSequentialConstruct: cdk.IDependable = orgTrail;
 
      nestedOU.forEach(nestedOU => {
        previousSequentialConstruct = this.createOrganizationTree(nestedOU, org.rootId, previousSequentialConstruct);
      });
 
 
    }
 
    if(props.rootHostedZoneDNSName){
      new RootDns(this, 'RootDNS', {
        stagesAccounts: this.stageAccounts,
        rootHostedZoneDNSName: props.rootHostedZoneDNSName,
        thirdPartyProviderDNSUsed: props.thirdPartyProviderDNSUsed?props.thirdPartyProviderDNSUsed:true
      });
    }
    
    new SecureRootUser(this, 'SecureRootUser', email);
  }
}