@@ -83,25 +83,12 @@ export const handler = async (input: ScheduledEvent) => {
8383 return 'INVALID_REQUEST' ;
8484 }
8585
86- // describe policy
87- const policyResponse = await organizations . describePolicy ( policyId ) ;
88- const policy = policyResponse . Policy ;
89- if ( ! policy ) {
90- console . error ( `Invalid PolicyId provided ${ policyId } ` ) ;
91- return false ;
92- }
93-
94- if ( ! isServiceControlPolicy ( policy ) ) {
95- console . log ( 'The policy is NOT of type SERVICE_CONTROL_POLICY; No operation required' ) ;
96- return 'NO_OPERATION_REQUIRED' ;
97- }
98-
99- if ( isControlTowerSCP ( policy ) ) {
100- console . log ( 'Policy Changes Performed by Control Tower; No operation required' ) ;
86+ if ( await isControlTowerSCP ( policyId ) ) {
87+ console . log ( 'Policy Changes Performed by Control Tower, No operation required' ) ;
10188 return 'NO_OPERATION_REQUIRED' ;
10289 }
10390 const eventName = requestDetail . eventName ;
104- if ( ! [ 'DeletePolicy' , 'AttachPolicy' ] . includes ( eventName ) && ! isAcceleratorScp ( policy , scpNames ) ) {
91+ if ( ! [ 'DeletePolicy' , 'AttachPolicy' ] . includes ( eventName ) && ! ( await isAcceleratorScp ( policyId , scpNames ) ) ) {
10592 console . log ( `SCP ${ policyId } is not managed by Accelerator` ) ;
10693 return 'SUCCESS' ;
10794 }
@@ -168,15 +155,15 @@ export const handler = async (input: ScheduledEvent) => {
168155 ) ;
169156 console . log ( `SCP Names for Target are :: ${ acclScpNames } ` ) ;
170157 if ( eventName === 'AttachPolicy' ) {
171- if ( isAcceleratorScp ( policy , acclScpNames ) ) {
158+ if ( await isAcceleratorScp ( policyId , acclScpNames ) ) {
172159 console . log ( 'Accelerator Managed policy is attached' ) ;
173160 return 'IGNORE' ;
174161 }
175162 // Detach target from policy
176163 console . log ( `Detaching target "${ targetId } " from policy "${ policyId } "` ) ;
177164 await organizations . detachPolicy ( policyId , targetId ) ;
178165 } else {
179- if ( ! isAcceleratorScp ( policy , acclScpNames ) ) {
166+ if ( ! ( await isAcceleratorScp ( policyId , acclScpNames ) ) ) {
180167 console . log ( 'Non Accelerator Managed policy is detached' ) ;
181168 return 'IGNORE' ;
182169 }
@@ -249,44 +236,35 @@ export const handler = async (input: ScheduledEvent) => {
249236 return 'SUCCESS' ;
250237} ;
251238
252- function isAcceleratorScp ( policy : any , scpNames : string [ ] ) : boolean {
239+ async function isAcceleratorScp ( policyId : string , scpNames : string [ ] ) : Promise < boolean > {
240+ const policyResponse = await organizations . describePolicy ( policyId ) ;
241+ const policy = policyResponse . Policy ;
242+ if ( ! policy ) {
243+ console . error ( `Invalid PolicyId provided ${ policyId } ` ) ;
244+ return false ;
245+ }
253246 const policyName = policy . PolicySummary ?. Name ;
254247 if ( ! policyName ) {
255- console . error ( `isAcceleratorScp - Invalid policy name` ) ;
256248 return false ;
257249 }
258- if ( policyName !== FULL_AWS_ACCESS_POLICY_NAME && ! scpNames . includes ( policyName ! ) ) {
250+ if ( policyName !== FULL_AWS_ACCESS_POLICY_NAME && ! scpNames . includes ( policy . PolicySummary ?. Name ! ) ) {
259251 console . error ( `Policy is not handled through Accelerator` ) ;
260252 return false ;
261253 }
262254 return true ;
263255}
264256
265- function isControlTowerSCP ( policy : any ) : boolean {
257+ async function isControlTowerSCP ( policyId : string ) : Promise < boolean > {
258+ const policyResponse = await organizations . describePolicy ( policyId ) ;
259+ const policy = policyResponse . Policy ;
260+
266261 const policyName = policy ?. PolicySummary ?. Name ;
267262 if ( policyName ?. startsWith ( 'aws-guardrails-' ) ) {
268263 return true ;
269264 }
270265 return false ;
271266}
272267
273- /**
274- * Checks if the policy type is SERVICE_CONTROL_POLICY.
275- * @see https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/organizations.html#Organizations.Client.describe_policy
276- */
277- function isServiceControlPolicy ( policy : any ) : boolean {
278- const policyType : string = policy ?. PolicySummary ?. Type ;
279- if ( ! policyType ) {
280- console . error ( `isServiceControlPolicy - Invalid policy type` ) ;
281- return false ;
282- }
283- console . log ( `isServiceControlPolicy - Policy type : ${ policyType } ` ) ;
284- if ( policyType === 'SERVICE_CONTROL_POLICY' ) {
285- return true ;
286- }
287- return false ;
288- }
289-
290268async function loadAccountsAndOrganizationsFromConfig (
291269 config : AcceleratorConfig ,
292270) : Promise < { organizationalUnits : OrganizationalUnit [ ] ; accounts : ConfigurationAccount [ ] } > {
0 commit comments