@@ -81,6 +81,11 @@ describe('Providing codePipeline parameter and prop(s) of codePipeline parameter
81
81
pipelineName : 'randomName' ,
82
82
} ) . create ( ) ) . toThrowError ( 'Cannot set \'pipelineName\' if an existing CodePipeline is given using \'codePipeline\'' ) ;
83
83
} ) ;
84
+ test ( 'Providing codePipeline parameter and enableKeyRotation parameter should throw error' , ( ) => {
85
+ expect ( ( ) => new CodePipelinePropsCheckTest ( app , 'CodePipeline' , {
86
+ enableKeyRotation : true ,
87
+ } ) . create ( ) ) . toThrowError ( 'Cannot set \'enableKeyRotation\' if an existing CodePipeline is given using \'codePipeline\'' ) ;
88
+ } ) ;
84
89
test ( 'Providing codePipeline parameter and crossAccountKeys parameter should throw error' , ( ) => {
85
90
expect ( ( ) => new CodePipelinePropsCheckTest ( app , 'CodePipeline' , {
86
91
crossAccountKeys : true ,
@@ -192,6 +197,60 @@ test('CodeBuild action role has the right AssumeRolePolicyDocument', () => {
192
197
} ) ;
193
198
} ) ;
194
199
200
+ test ( 'CodePipeline throws when key rotation is enabled without enabling cross account keys' , ( ) => {
201
+ const pipelineStack = new cdk . Stack ( app , 'PipelineStack' , { env : PIPELINE_ENV } ) ;
202
+ const repo = new ccommit . Repository ( pipelineStack , 'Repo' , {
203
+ repositoryName : 'MyRepo' ,
204
+ } ) ;
205
+ const cdkInput = cdkp . CodePipelineSource . codeCommit (
206
+ repo ,
207
+ 'main' ,
208
+ ) ;
209
+
210
+ expect ( ( ) => new CodePipeline ( pipelineStack , 'Pipeline' , {
211
+ enableKeyRotation : true ,
212
+ synth : new cdkp . ShellStep ( 'Synth' , {
213
+ input : cdkInput ,
214
+ installCommands : [ 'npm ci' ] ,
215
+ commands : [
216
+ 'npm run build' ,
217
+ 'npx cdk synth' ,
218
+ ] ,
219
+ } ) ,
220
+ } ) . buildPipeline ( ) ) . toThrowError ( 'Setting \'enableKeyRotation\' to true also requires \'crossAccountKeys\' to be enabled' ) ;
221
+ } ) ;
222
+
223
+
224
+ test ( 'CodePipeline enables key rotation on cross account keys' , ( ) => {
225
+ const pipelineStack = new cdk . Stack ( app , 'PipelineStack' , { env : PIPELINE_ENV } ) ;
226
+ const repo = new ccommit . Repository ( pipelineStack , 'Repo' , {
227
+ repositoryName : 'MyRepo' ,
228
+ } ) ;
229
+ const cdkInput = cdkp . CodePipelineSource . codeCommit (
230
+ repo ,
231
+ 'main' ,
232
+ ) ;
233
+
234
+ new CodePipeline ( pipelineStack , 'Pipeline' , {
235
+ enableKeyRotation : true ,
236
+ crossAccountKeys : true , // requirement of key rotation
237
+ synth : new cdkp . ShellStep ( 'Synth' , {
238
+ input : cdkInput ,
239
+ installCommands : [ 'npm ci' ] ,
240
+ commands : [
241
+ 'npm run build' ,
242
+ 'npx cdk synth' ,
243
+ ] ,
244
+ } ) ,
245
+ } ) ;
246
+
247
+ const template = Template . fromStack ( pipelineStack ) ;
248
+
249
+ template . hasResourceProperties ( 'AWS::KMS::Key' , {
250
+ EnableKeyRotation : true ,
251
+ } ) ;
252
+ } ) ;
253
+
195
254
test ( 'CodePipeline supports use of existing role' , ( ) => {
196
255
const pipelineStack = new cdk . Stack ( app , 'PipelineStack' , { env : PIPELINE_ENV } ) ;
197
256
const repo = new ccommit . Repository ( pipelineStack , 'Repo' , {
@@ -393,6 +452,7 @@ class ReuseStack extends cdk.Stack {
393
452
interface CodePipelineStackProps extends cdk . StackProps {
394
453
pipelineName ?: string ;
395
454
crossAccountKeys ?: boolean ;
455
+ enableKeyRotation ?: boolean ;
396
456
reuseCrossRegionSupportStacks ?: boolean ;
397
457
role ?: iam . IRole ;
398
458
}
@@ -414,21 +474,28 @@ class CodePipelinePropsCheckTest extends cdk.Stack {
414
474
if ( this . cProps . crossAccountKeys !== undefined ) {
415
475
new cdkp . CodePipeline ( this , 'CodePipeline2' , {
416
476
crossAccountKeys : this . cProps . crossAccountKeys ,
417
- codePipeline : new Pipeline ( this , 'Pipline2 ' ) ,
477
+ codePipeline : new Pipeline ( this , 'Pipeline2 ' ) ,
418
478
synth : new cdkp . ShellStep ( 'Synth' , { commands : [ 'ls' ] } ) ,
419
479
} ) . buildPipeline ( ) ;
420
480
}
421
- if ( this . cProps . reuseCrossRegionSupportStacks !== undefined ) {
481
+ if ( this . cProps . enableKeyRotation !== undefined ) {
422
482
new cdkp . CodePipeline ( this , 'CodePipeline3' , {
483
+ enableKeyRotation : this . cProps . enableKeyRotation ,
484
+ codePipeline : new Pipeline ( this , 'Pipeline3' ) ,
485
+ synth : new cdkp . ShellStep ( 'Synth' , { commands : [ 'ls' ] } ) ,
486
+ } ) . buildPipeline ( ) ;
487
+ }
488
+ if ( this . cProps . reuseCrossRegionSupportStacks !== undefined ) {
489
+ new cdkp . CodePipeline ( this , 'CodePipeline4' , {
423
490
reuseCrossRegionSupportStacks : this . cProps . reuseCrossRegionSupportStacks ,
424
- codePipeline : new Pipeline ( this , 'Pipline3 ' ) ,
491
+ codePipeline : new Pipeline ( this , 'Pipeline4 ' ) ,
425
492
synth : new cdkp . ShellStep ( 'Synth' , { commands : [ 'ls' ] } ) ,
426
493
} ) . buildPipeline ( ) ;
427
494
}
428
495
if ( this . cProps . role !== undefined ) {
429
- new cdkp . CodePipeline ( this , 'CodePipeline4 ' , {
496
+ new cdkp . CodePipeline ( this , 'CodePipeline5 ' , {
430
497
role : this . cProps . role ,
431
- codePipeline : new Pipeline ( this , 'Pipline4 ' ) ,
498
+ codePipeline : new Pipeline ( this , 'Pipeline5 ' ) ,
432
499
synth : new cdkp . ShellStep ( 'Synth' , { commands : [ 'ls' ] } ) ,
433
500
} ) . buildPipeline ( ) ;
434
501
}
0 commit comments