@@ -5,32 +5,24 @@ import { createName } from '@aws-accelerator/cdk-accelerator/src/core/accelerato
55import * as awsConfig from '@aws-cdk/aws-config' ;
66import { Account , getAccountId } from '../../utils/accounts' ;
77import { StackOutput } from '@aws-accelerator/common-outputs/src/stack-output' ;
8- import { LogBucketOutput } from '../defaults/outputs' ;
8+ import { LogBucketOutput , AccountBucketOutputFinder } from '../defaults/outputs' ;
99
1010export interface CreateRuleProps {
1111 acceleratorExecutionRoleName : string ;
12- centralBucketName : string ;
13- centralAccountId : string ;
1412 config : c . AcceleratorConfig ;
1513 accountStacks : AccountStacks ;
1614 accounts : Account [ ] ;
1715 outputs : StackOutput [ ] ;
16+ defaultRegion : string ;
1817}
1918
2019export async function createRule ( props : CreateRuleProps ) {
21- const {
22- acceleratorExecutionRoleName,
23- config,
24- centralAccountId,
25- centralBucketName,
26- accountStacks,
27- accounts,
28- outputs,
29- } = props ;
20+ const { acceleratorExecutionRoleName, config, accountStacks, accounts, outputs, defaultRegion } = props ;
3021 const awsConfigConf = config [ 'global-options' ] [ 'aws-config' ] ;
3122 if ( ! awsConfigConf ) {
3223 return ;
3324 }
25+
3426 const configRules = awsConfigConf [ 'managed-rules' ] . rules ;
3527 const configRuleDefaults = awsConfigConf [ 'managed-rules' ] . defaults ;
3628
@@ -82,6 +74,8 @@ export async function createRule(props: CreateRuleProps) {
8274 ruleParams : awsConfigRule . parameters ,
8375 config,
8476 outputs,
77+ accountKey,
78+ defaultRegion,
8579 } ) ;
8680 const configRule = new awsConfig . ManagedRule ( accountStack , `ConfigRule-${ ruleName } ` , {
8781 identifier : ruleName ,
@@ -126,8 +120,12 @@ export async function createRule(props: CreateRuleProps) {
126120 accounts ,
127121 ssmDocInOu . account ,
128122 ) } :document/${ remediationActionName } `;
123+ } else if ( config [ 'global-options' ] [ 'default-ssm-documents' ] . includes ( remediationAction ) ) {
124+ targetId = remediationAction ;
129125 } else {
130- console . warn ( `No Remediation is Created in account "${ accountKey } " and region "${ region } "` ) ;
126+ console . warn (
127+ `No Remediation "${ remediationAction } "is Created in account "${ accountKey } " and region "${ region } "` ,
128+ ) ;
131129 continue ;
132130 }
133131 }
@@ -147,6 +145,8 @@ export async function createRule(props: CreateRuleProps) {
147145 remediationParams : awsConfigRule [ 'remediation-params' ] ,
148146 roleName : acceleratorExecutionRoleName ,
149147 config,
148+ accountKey,
149+ defaultRegion,
150150 } ) ;
151151
152152 new awsConfig . CfnRemediationConfiguration ( accountStack , `ConfigRuleRemediation-${ ruleName } ` , {
@@ -181,19 +181,18 @@ export function getRemediationParameters(params: {
181181 roleName : string ;
182182 outputs : StackOutput [ ] ;
183183 config : c . AcceleratorConfig ;
184+ accountKey : string ;
185+ defaultRegion : string ;
184186} ) : RemediationParameters {
185187 const reutrnParams : RemediationParameters = { } ;
186- const { outputs, remediationParams, roleName, config } = params ;
187- if ( ! remediationParams . AutomationAssumeRole ) {
188- reutrnParams . AutomationAssumeRole = {
189- StaticValue : {
190- Values : [ `arn:aws:iam::${ cdk . Aws . ACCOUNT_ID } :role/${ roleName } ` ] ,
191- } ,
192- } ;
193- }
188+ const { outputs, remediationParams, roleName, config, accountKey, defaultRegion } = params ;
189+ reutrnParams . AutomationAssumeRole = {
190+ StaticValue : {
191+ Values : [ `arn:aws:iam::${ cdk . Aws . ACCOUNT_ID } :role/${ remediationParams . AutomationAssumeRole || roleName } ` ] ,
192+ } ,
193+ } ;
194194
195195 Object . keys ( remediationParams ) . map ( key => {
196- console . log ( remediationParams [ key ] , remediationParams [ key ] . startsWith ( '${SEA::' ) ) ;
197196 if ( remediationParams [ key ] === 'RESOURCE_ID' ) {
198197 reutrnParams [ key ] = {
199198 ResourceValue : {
@@ -212,11 +211,19 @@ export function getRemediationParameters(params: {
212211 const replaceKey = remediationParams [ key ] . match ( '{SEA::(.*)}' ) ?. [ 1 ] ! ;
213212 reutrnParams [ key ] = {
214213 StaticValue : {
215- Values : [ getParameterValue ( replaceKey , outputs , config ) ] ,
214+ Values : [
215+ getParameterValue ( {
216+ paramKey : replaceKey ,
217+ outputs,
218+ config,
219+ accountKey,
220+ defaultRegion,
221+ } ) ,
222+ ] ,
216223 } ,
217224 } ;
218225 } else {
219- reutrnParams . AutomationAssumeRole = {
226+ reutrnParams [ key ] = {
220227 StaticValue : {
221228 Values : [ remediationParams [ key ] ] ,
222229 } ,
@@ -232,23 +239,46 @@ export function getConfigRuleParameters(params: {
232239 ruleParams : { [ key : string ] : string } ;
233240 outputs : StackOutput [ ] ;
234241 config : c . AcceleratorConfig ;
242+ accountKey : string ;
243+ defaultRegion : string ;
235244} ) : { [ key : string ] : string } {
236- const { config, outputs, ruleParams } = params ;
245+ const { config, outputs, ruleParams, accountKey , defaultRegion } = params ;
237246 Object . keys ( ruleParams ) . map ( key => {
238247 if ( ruleParams [ key ] . startsWith ( '${SEA::' ) ) {
239248 const replaceKey = ruleParams [ key ] . match ( '{SEA::(.*)}' ) ?. [ 1 ] ! ;
240- ruleParams [ key ] = getParameterValue ( replaceKey , outputs , config ) ;
249+ ruleParams [ key ] = getParameterValue ( {
250+ paramKey : replaceKey ,
251+ outputs,
252+ config,
253+ accountKey,
254+ defaultRegion,
255+ } ) ;
241256 }
242257 } ) ;
243258 return ruleParams ;
244259}
245260
246- export function getParameterValue ( input : string , outputs : StackOutput [ ] , config : c . AcceleratorConfig ) : string {
247- if ( input === 'LogArchiveAesBucket' ) {
261+ export function getParameterValue ( props : {
262+ paramKey : string ;
263+ outputs : StackOutput [ ] ;
264+ config : c . AcceleratorConfig ;
265+ accountKey : string ;
266+ defaultRegion : string ;
267+ } ) : string {
268+ const { accountKey, config, outputs, paramKey, defaultRegion } = props ;
269+ if ( paramKey === 'LogArchiveAesBucket' ) {
248270 return LogBucketOutput . getBucketDetails ( {
249271 config,
250272 outputs,
251273 } ) . name ;
252274 }
275+ if ( paramKey === 'S3BucketEncryptionKey' ) {
276+ const accountBucket = AccountBucketOutputFinder . tryFindOne ( {
277+ outputs,
278+ accountKey,
279+ region : defaultRegion ,
280+ } ) ;
281+ return `arn:aws:kms:${ cdk . Aws . REGION } :${ cdk . Aws . ACCOUNT_ID } :alias/${ accountBucket ?. encryptionKeyName } ` ;
282+ }
253283 return '' ;
254284}
0 commit comments