66 CloudFormationCustomResourceUpdateEvent ,
77} from 'aws-lambda' ;
88import { errorHandler } from '@aws-accelerator/custom-resource-runtime-cfn-response' ;
9- import { throttlingBackOff } from '@aws-accelerator/custom-resource-cfn-utils' ;
109
1110const macie = new AWS . Macie2 ( ) ;
1211
@@ -31,50 +30,60 @@ async function onEvent(event: CloudFormationCustomResourceEvent) {
3130 }
3231}
3332
34- function getPhysicalId ( event : CloudFormationCustomResourceEvent ) : string {
35- const properties = ( event . ResourceProperties as unknown ) as HandlerProperties ;
36-
37- return `${ properties . accountId } ` ;
38- }
39-
4033async function onCreateOrUpdate (
4134 event : CloudFormationCustomResourceCreateEvent | CloudFormationCustomResourceUpdateEvent ,
4235) {
43- const properties = ( event . ResourceProperties as unknown ) as HandlerProperties ;
44- const response = await enableOrgAdmin ( properties ) ;
36+ const accountId = event . ResourceProperties . accountId ;
37+ const sleepTime = 30000 ;
38+ const retryCount = 10 ;
39+ await enableOrgAdmin ( accountId ) ;
40+ let macieAdminEnabled = await isMacieAdminEnabled ( accountId ) ;
41+ let retries = 0 ;
42+ while ( ! macieAdminEnabled && retries < retryCount ) {
43+ console . warn (
44+ `Macie Admin not enabled. Retrying in ${ sleepTime / 1000 } seconds. Retry: ${ retries + 1 } of ${ retryCount } ` ,
45+ ) ;
46+ await sleep ( sleepTime ) ;
47+ await enableOrgAdmin ( accountId ) ;
48+ macieAdminEnabled = await isMacieAdminEnabled ( accountId ) ;
49+ retries ++ ;
50+ }
4551 return {
46- physicalResourceId : getPhysicalId ( event ) ,
52+ physicalResourceId : accountId ,
4753 data : { } ,
4854 } ;
4955}
5056
51- async function enableOrgAdmin ( properties : HandlerProperties ) {
52- try {
53- const enableAdmin = await throttlingBackOff ( ( ) =>
54- macie
55- . enableOrganizationAdminAccount ( {
56- adminAccountId : properties . accountId ,
57- } )
58- . promise ( ) ,
59- ) ;
57+ async function isMacieAdminEnabled ( accountId : string ) {
58+ console . log ( `Checking if Macie Administration is enabled for account ${ accountId } ` ) ;
59+ const adminList = await macie . listOrganizationAdminAccounts ( ) . promise ( ) ;
60+ const isAccountAdded = adminList . adminAccounts ?. filter ( account => {
61+ return account . accountId === accountId ;
62+ } ) ;
63+ if ( isAccountAdded ! . length === 0 ) {
64+ console . log ( 'Account has not been added.' ) ;
65+ } else {
66+ console . log ( 'Account has been added.' ) ;
67+ }
68+ return isAccountAdded ! . length > 0 ;
69+ }
6070
61- return enableAdmin ;
71+ async function enableOrgAdmin ( accountId : string ) {
72+ console . info ( `Enabling Macie Admin Account ${ accountId } ` ) ;
73+ try {
74+ const macieAdmin = await macie
75+ . enableOrganizationAdminAccount ( {
76+ adminAccountId : accountId ,
77+ } )
78+ . promise ( ) ;
79+ console . info ( macieAdmin ) ;
6280 } catch ( e ) {
63- const message = `${ e } ` ;
64- if (
65- message . includes (
66- 'The request failed because an account is already enabled as the Macie delegated administrator for the organization' ,
67- )
68- ) {
69- console . warn ( e ) ;
70- } else if (
71- message . includes (
72- `The request failed because there's already a delegated Macie administrator account for your organization` ,
73- )
74- ) {
75- console . warn ( e ) ;
76- } else {
77- throw e ;
78- }
81+ console . warn ( 'Could not enable Macie Admin account' ) ;
82+ console . warn ( e ) ;
83+ return ;
7984 }
8085}
86+
87+ async function sleep ( ms : number ) {
88+ return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
89+ }
0 commit comments