@@ -74,7 +74,7 @@ export const handler = async (input: PolicyChangeEvent) => {
7474 return 'INVALID_REQUEST' ;
7575 }
7676 const eventName = requestDetail . eventName ;
77- if ( eventName !== 'DeletePolicy' && ! ( await isAcceleratorScp ( policyId , scpNames ) ) ) {
77+ if ( ! [ 'DeletePolicy' , 'AttachPolicy' ] . includes ( eventName ) && ! ( await isAcceleratorScp ( policyId , scpNames ) ) ) {
7878 console . log ( `SCP ${ policyId } is not managed by Accelerator` ) ;
7979 return 'SUCCESS' ;
8080 }
@@ -87,7 +87,7 @@ export const handler = async (input: PolicyChangeEvent) => {
8787 organizationAdminRole,
8888 } ) ;
8989 const { organizationalUnits, accounts } = await loadAccountsAndOrganizationsFromConfig ( config ) ;
90- if ( eventName === 'DetachPolicy' ) {
90+ if ( eventName === 'DetachPolicy' || eventName === 'AttachPolicy' ) {
9191 const { targetId } = requestDetail . requestParameters ;
9292 if ( ! targetId ) {
9393 console . warn ( `Missing required parameters, Ignoring` ) ;
@@ -98,20 +98,60 @@ export const handler = async (input: PolicyChangeEvent) => {
9898 const destinationOrg = await organizations . getOrganizationalUnitWithPath ( targetId ) ;
9999 const destinationRootOrg = destinationOrg . Name ! ;
100100 if ( ignoredOus . includes ( destinationRootOrg ) ) {
101- console . log ( `DetachPolicy is on ignored-ou from ROOT, no need to reattach` ) ;
101+ console . log ( `${ eventName } is on ignored-ou from ROOT, no need to reattach` ) ;
102102 return 'IGNORE' ;
103103 }
104104 } else {
105105 const accountObject = accounts . find ( acc => acc . accountId === targetId ) ;
106106 if ( ignoredOus . includes ( accountObject ?. organizationalUnit ! ) ) {
107- console . log ( `DetachPolicy is on account in ignored-ous from ROOT, no need to reattach` ) ;
107+ console . log ( `${ eventName } is on account in ignored-ous from ROOT, no need to reattach` ) ;
108108 return 'IGNORE' ;
109109 }
110110 }
111111 }
112- // ReAttach target to policy
113- console . log ( `Reattaching target "${ targetId } " to policy "${ policyId } "` ) ;
114- await organizations . attachPolicy ( policyId , targetId ) ;
112+ const targetScpNames : string [ ] = [ ] ;
113+ if ( targetId . startsWith ( 'ou-' ) ) {
114+ const destinationOrg = await organizations . getOrganizationalUnitWithPath ( targetId ) ;
115+ const destinationRootOrg = destinationOrg . Name ! ;
116+ const targetOuConfig = config . getOrganizationalUnits ( ) . find ( ( [ ouKey , _ ] ) => ouKey === destinationRootOrg ) ?. [ 1 ] ;
117+ targetScpNames . push ( ...( targetOuConfig ?. scps || [ ] ) ) ;
118+ } else {
119+ const accountObject = accounts . find ( acc => acc . accountId === targetId ) ;
120+ if ( ! accountObject ) {
121+ console . log ( 'Account is not in Configuration' ) ;
122+ return 'IGNORE' ;
123+ }
124+ const accountConfig = config . getAccountByKey ( accountObject . accountKey ) ;
125+ const targetOuConfig = config . getOrganizationalUnits ( ) . find ( ( [ ouKey , _ ] ) => ouKey === accountConfig . ou ) ?. [ 1 ] ;
126+ targetScpNames . push ( ...( targetOuConfig ?. scps || [ ] ) ) ;
127+ if ( accountConfig . scps ) {
128+ targetScpNames . push ( ...accountConfig . scps ) ;
129+ }
130+ }
131+ const acclScpNames = targetScpNames . map ( scp =>
132+ ServiceControlPolicy . policyNameToAcceleratorPolicyName ( {
133+ acceleratorPrefix,
134+ policyName : scp ,
135+ } ) ,
136+ ) ;
137+ console . log ( `SCP Names for Target are :: ${ acclScpNames } ` ) ;
138+ if ( eventName === 'AttachPolicy' ) {
139+ if ( await isAcceleratorScp ( policyId , acclScpNames ) ) {
140+ console . log ( 'Accelerator Managed policy is attached' ) ;
141+ return 'IGNORE' ;
142+ }
143+ // Detach target from policy
144+ console . log ( `Detaching target "${ targetId } " from policy "${ policyId } "` ) ;
145+ await organizations . detachPolicy ( policyId , targetId ) ;
146+ } else {
147+ if ( ! ( await isAcceleratorScp ( policyId , acclScpNames ) ) ) {
148+ console . log ( 'Non Accelerator Managed policy is detached' ) ;
149+ return 'IGNORE' ;
150+ }
151+ // ReAttach target to policy
152+ console . log ( `Reattaching target "${ targetId } " to policy "${ policyId } "` ) ;
153+ await organizations . attachPolicy ( policyId , targetId ) ;
154+ }
115155 } else if ( eventName === 'UpdatePolicy' || eventName === 'DeletePolicy' ) {
116156 console . log ( `${ eventName } , changing back to original config from config` ) ;
117157
@@ -137,11 +177,12 @@ export const handler = async (input: PolicyChangeEvent) => {
137177 const acceleratorOuIds = organizationalUnits . map ( ou => ou . ouId ) ;
138178 const acceleratorAccountIds = accounts . map ( a => a . accountId ! ) ;
139179 const acceleratorTargetIds = [ ...rootIds , ...acceleratorOuIds , ...acceleratorAccountIds ] ;
180+ const acceleratorTargetOuIds = [ ...rootIds , ...acceleratorOuIds ] ;
140181
141182 // Detach non-Accelerator policies from Accelerator accounts
142183 await scps . detachPoliciesFromTargets ( {
143184 policyNamesToKeep : acceleratorPolicyNames ,
144- policyTargetIdsToInclude : acceleratorTargetIds ,
185+ policyTargetIdsToInclude : acceleratorTargetOuIds ,
145186 } ) ;
146187
147188 await scps . attachFullAwsAccessPolicyToTargets ( {
@@ -155,6 +196,20 @@ export const handler = async (input: PolicyChangeEvent) => {
155196 acceleratorOus : config . getOrganizationalUnits ( ) ,
156197 acceleratorPrefix,
157198 } ) ;
199+
200+ await scps . attachOrDetachPoliciesToAccounts ( {
201+ existingPolicies,
202+ configurationAccounts : accounts . map ( acc => ( {
203+ key : acc . accountKey ,
204+ id : acc . accountId ! ,
205+ arn : '' ,
206+ name : acc . accountName ,
207+ ou : acc . organizationalUnit ,
208+ email : acc . emailAddress ,
209+ } ) ) ,
210+ accountConfigs : config . getAccountConfigs ( ) ,
211+ acceleratorPrefix,
212+ } ) ;
158213 }
159214 return 'SUCCESS' ;
160215} ;
0 commit comments