@@ -30,26 +30,18 @@ async function onEvent(event: CloudFormationCustomResourceEvent) {
3030
3131async function onCreate ( event : CloudFormationCustomResourceEvent ) {
3232 const standards = event . ResourceProperties . standards ;
33- const standardsResponse = await throttlingBackOff ( ( ) => hub . describeStandards ( ) . promise ( ) ) ;
34- const enabledStandardsResponse = await throttlingBackOff ( ( ) => hub . getEnabledStandards ( ) . promise ( ) ) ;
33+ const standardsResponse = await describeStandards ( ) ;
34+ const enabledStandardsResponse = await getEnabledStandards ( ) ;
3535
3636 // Getting standards and disabling specific Controls for each standard
3737 for ( const standard of standards ) {
38- const standardArn = standardsResponse . Standards ?. find ( x => x . Name === standard . name ) ?. StandardsArn ;
39- const standardSubscriptionArn = enabledStandardsResponse . StandardsSubscriptions ?. find (
40- s => s . StandardsArn === standardArn ,
41- ) ?. StandardsSubscriptionArn ;
38+ const standardArn = standardsResponse ?. find ( x => x . Name === standard . name ) ?. StandardsArn ;
39+ const standardSubscriptionArn = enabledStandardsResponse ?. find ( s => s . StandardsArn === standardArn )
40+ ?. StandardsSubscriptionArn ;
4241
43- const standardControls = await throttlingBackOff ( ( ) =>
44- hub
45- . describeStandardsControls ( {
46- StandardsSubscriptionArn : standardSubscriptionArn ! ,
47- MaxResults : 100 ,
48- } )
49- . promise ( ) ,
50- ) ;
42+ const standardControls = await describeStandardsControls ( standardSubscriptionArn ) ;
5143 for ( const disableControl of standard [ 'controls-to-disable' ] ) {
52- const standardControl = standardControls . Controls ?. find ( x => x . ControlId === disableControl ) ;
44+ const standardControl = standardControls ?. find ( x => x . ControlId === disableControl ) ;
5345 if ( ! standardControl ) {
5446 console . log ( `Control "${ disableControl } " not found for Standard "${ standard . name } "` ) ;
5547 continue ;
@@ -87,16 +79,9 @@ async function onUpdate(event: CloudFormationCustomResourceUpdateEvent) {
8779 s => s . StandardsArn === standardArn ,
8880 ) ?. StandardsSubscriptionArn ;
8981
90- const standardControls = await throttlingBackOff ( ( ) =>
91- hub
92- . describeStandardsControls ( {
93- StandardsSubscriptionArn : standardSubscriptionArn ! ,
94- MaxResults : 100 ,
95- } )
96- . promise ( ) ,
97- ) ;
82+ const standardControls = await describeStandardsControls ( standardSubscriptionArn ) ;
9883 for ( const disableControl of standard [ 'controls-to-disable' ] || [ ] ) {
99- const standardControl = standardControls . Controls ?. find ( x => x . ControlId === disableControl ) ;
84+ const standardControl = standardControls ?. find ( x => x . ControlId === disableControl ) ;
10085 if ( ! standardControl ) {
10186 console . log ( `Control "${ disableControl } " not found for Standard "${ standard . name } "` ) ;
10287 continue ;
@@ -119,7 +104,7 @@ async function onUpdate(event: CloudFormationCustomResourceUpdateEvent) {
119104 c => ! standard [ 'controls-to-disable' ] ?. includes ( c ) ,
120105 ) ;
121106 for ( const enableControl of enableControls || [ ] ) {
122- const standardControl = standardControls . Controls ?. find ( x => x . ControlId === enableControl ) ;
107+ const standardControl = standardControls ?. find ( x => x . ControlId === enableControl ) ;
123108 if ( ! standardControl ) {
124109 console . log ( `Control "${ enableControl } " not found for Standard "${ standard . name } "` ) ;
125110 continue ;
@@ -140,6 +125,52 @@ async function onUpdate(event: CloudFormationCustomResourceUpdateEvent) {
140125 } ;
141126}
142127
128+ async function describeStandards ( ) {
129+ const standards = [ ] ;
130+ let token : string | undefined ;
131+ do {
132+ const response = await throttlingBackOff ( ( ) => hub . describeStandards ( ) . promise ( ) ) ;
133+ if ( response . Standards ) {
134+ standards . push ( ...response . Standards ) ;
135+ }
136+ token = response . NextToken ;
137+ } while ( token ) ;
138+
139+ return standards ;
140+ }
141+
142+ async function getEnabledStandards ( ) {
143+ const enabledStandards = [ ] ;
144+ let token : string | undefined ;
145+ do {
146+ const response = await throttlingBackOff ( ( ) => hub . getEnabledStandards ( ) . promise ( ) ) ;
147+ if ( response . StandardsSubscriptions ) {
148+ enabledStandards . push ( ...response . StandardsSubscriptions ) ;
149+ }
150+ token = response . NextToken ;
151+ } while ( token ) ;
152+
153+ return enabledStandards ;
154+ }
155+
156+ async function describeStandardsControls ( subscriptionArn : string | undefined ) {
157+ let token : string | undefined ;
158+ const standardControls : any [ ] = [ ] ;
159+ if ( ! subscriptionArn ) {
160+ return standardControls ;
161+ }
162+ do {
163+ const response = await throttlingBackOff ( ( ) =>
164+ hub . describeStandardsControls ( { StandardsSubscriptionArn : subscriptionArn , NextToken : token } ) . promise ( ) ,
165+ ) ;
166+ if ( response . Controls ) {
167+ standardControls . push ( ...response . Controls ) ;
168+ }
169+ token = response . NextToken ;
170+ } while ( token ) ;
171+ return standardControls ;
172+ }
173+
143174async function onDelete ( _ : CloudFormationCustomResourceEvent ) {
144175 console . log ( `Nothing to do for delete...` ) ;
145176}
0 commit comments