All files / lib account.ts

88.24% Statements 15/17
50% Branches 2/4
50% Functions 1/2
88.24% Lines 15/17

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                                2x 2x 2x 2x                     2x   4x   4x   4x                         4x   4x   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 core from "@aws-cdk/core";
import { AccountProvider } from "./account-provider";
import * as cr from "@aws-cdk/custom-resources";
import * as ssm from "@aws-cdk/aws-ssm"; 
 
export interface IAccountProps {
  email: string;
  name: string;
  parentOrganizationalUnitId?: string;
  parentOrganizationalUnitName?: string;
  id?: string;
 
}
 
export class Account extends core.Construct {
  constructor(scope: core.Construct, id: string, accountProps: IAccountProps) {
    super(scope, id);
 
    const accountProvider = AccountProvider.getOrCreate(this);
 
    let account = new core.CustomResource(
      this,
      `Account-${accountProps.name}`,
      {
        serviceToken: accountProvider.provider.serviceToken,
        resourceType: "Custom::AccountCreation",
        properties: {
          Email: accountProps.email,
          AccountName: accountProps.name,
        },
      }
    );
 
    let accountId = account.getAtt("AccountId").toString();
    
    accountProps.id = accountId;
    
    new ssm.StringParameter(this, `${accountProps.name}-AccountDetails`, {
      description: `Details of ${accountProps.name}`,
      parameterName: `/accounts/${accountProps.name}`,
      stringValue: JSON.stringify(accountProps),
    });
    
    Eif (accountProps.parentOrganizationalUnitId) {
      let parent = new cr.AwsCustomResource(this, "ListParentsCustomResource", {
        onCreate: {
          service: "Organizations",
          action: "listParents",
          physicalResourceId: cr.PhysicalResourceId.fromResponse(
            "Parents.0.Id"
          ),
          region: "us-east-1", //AWS Organizations API are only available in us-east-1 for root actions
          parameters: {
            ChildId: accountId,
          },
        },
        onUpdate: {
          service: "Organizations",
          action: "listParents",
          physicalResourceId: cr.PhysicalResourceId.fromResponse(
            "Parents.0.Id"
          ),
          region: "us-east-1", //AWS Organizations API are only available in us-east-1 for root actions
          parameters: {
            ChildId: accountId,
          },
        },
        onDelete: {
          service: "Organizations",
          action: "listParents",
          physicalResourceId: cr.PhysicalResourceId.fromResponse(
            "Parents.0.Id"
          ),
          region: "us-east-1", //AWS Organizations API are only available in us-east-1 for root actions
          parameters: {
            ChildId: accountId,
          },
        },
        policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
          resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE,
        }),
      });
 
      new cr.AwsCustomResource(
        this,
        "MoveAccountCustomResource",
        {
          onCreate: {
            service: "Organizations",
            action: "moveAccount",
            physicalResourceId: cr.PhysicalResourceId.of(accountId),
            region: "us-east-1", //AWS Organizations API are only available in us-east-1 for root actions
            parameters: {
              AccountId: accountId,
              DestinationParentId: accountProps.parentOrganizationalUnitId,
              SourceParentId: parent.getResponseField("Parents.0.Id"),
            },
          },
          policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
            resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE,
          }),
        }
      );
      Iif (accountProps.name === 'CICD') {
        // Be careful extending this to other account, we are limited to 3 delegated admin per service
        this.registerAsDelegatedAdministrator(accountId);
      }
    }
  }
 
  registerAsDelegatedAdministrator(accountId: string) {
    new cr.AwsCustomResource(this, 
      "registerDelegatedAdministrator", 
      {
        onCreate: {
          service: 'Organizations',
          action: 'registerDelegatedAdministrator', // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Organizations.html#registerDelegatedAdministrator-property
          physicalResourceId: cr.PhysicalResourceId.of('registerDelegatedAdministrator'),
          region: 'us-east-1', //AWS Organizations API are only available in us-east-1 for root actions
          parameters: {
            AccountId: accountId,
            ServicePrincipal: 'ssm.amazonaws.com'
          }
        },
        onDelete: {
          service: 'Organizations',
          action: 'deregisterDelegatedAdministrator', // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Organizations.html#deregisterDelegatedAdministrator-property
          physicalResourceId: cr.PhysicalResourceId.of('registerDelegatedAdministrator'),
          region: 'us-east-1', //AWS Organizations API are only available in us-east-1 for root actions
          parameters: {
            AccountId: accountId,
            ServicePrincipal: 'ssm.amazonaws.com'
          }
        },
        installLatestAwsSdk: false,
        policy: cr.AwsCustomResourcePolicy.fromSdkCalls(
          {
            resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE
          }
        )
      }
    );
  }
}