Skip to content

Commit 7706302

Browse files
authored
feat(kms): Allow opting out of "Retain" deletion policy (#1685)
Gives the user control over whether the key should be retained or scheduled for deletion when it is removed from the stack (or the stack is deleted). This is convenient in particular for integration tests, to avoid accumulating garbage over successive runs.
1 parent 46236d9 commit 7706302

File tree

6 files changed

+26
-6
lines changed

6 files changed

+26
-6
lines changed

packages/@aws-cdk/aws-kms/lib/key.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ export interface EncryptionKeyProps {
106106
* administer the key will be created.
107107
*/
108108
policy?: PolicyDocument;
109+
110+
/**
111+
* Whether the encryption key should be retained when it is removed from the Stack. This is useful when one wants to
112+
* retain access to data that was encrypted with a key that is being retired.
113+
*
114+
* @default true
115+
*/
116+
retain?: boolean;
109117
}
110118

111119
/**
@@ -155,7 +163,9 @@ export class EncryptionKey extends EncryptionKeyBase {
155163
});
156164

157165
this.keyArn = resource.keyArn;
158-
resource.options.deletionPolicy = DeletionPolicy.Retain;
166+
resource.options.deletionPolicy = props.retain === false
167+
? DeletionPolicy.Delete
168+
: DeletionPolicy.Retain;
159169
}
160170

161171
/**

packages/@aws-cdk/aws-kms/test/integ.key-sharing.lit.expected.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"Version": "2012-10-17"
4747
}
4848
},
49-
"DeletionPolicy": "Retain"
49+
"DeletionPolicy": "Delete"
5050
},
5151
"MyKeyAlias1B45D9DA": {
5252
"Type": "AWS::KMS::Alias",

packages/@aws-cdk/aws-kms/test/integ.key-sharing.lit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class KeyStack extends cdk.Stack {
1313

1414
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
1515
super(scope, id, props);
16-
this.key = new kms.EncryptionKey(this, 'MyKey');
16+
this.key = new kms.EncryptionKey(this, 'MyKey', { retain: false });
1717
}
1818
}
1919

packages/@aws-cdk/aws-kms/test/integ.key.expected.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"Version": "2012-10-17"
5656
}
5757
},
58-
"DeletionPolicy": "Retain"
58+
"DeletionPolicy": "Delete"
5959
},
6060
"MyKeyAlias1B45D9DA": {
6161
"Type": "AWS::KMS::Alias",

packages/@aws-cdk/aws-kms/test/integ.key.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const app = new App();
66

77
const stack = new Stack(app, `aws-cdk-kms-1`);
88

9-
const key = new EncryptionKey(stack, 'MyKey');
9+
const key = new EncryptionKey(stack, 'MyKey', { retain: false });
1010

1111
key.addToResourcePolicy(new PolicyStatement()
1212
.addAllResources()

packages/@aws-cdk/aws-kms/test/test.key.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { exactlyMatchTemplate, expect } from '@aws-cdk/assert';
1+
import { exactlyMatchTemplate, expect, haveResource, ResourcePart } from '@aws-cdk/assert';
22
import { PolicyDocument, PolicyStatement } from '@aws-cdk/aws-iam';
33
import { App, Stack, Tag } from '@aws-cdk/cdk';
44
import { Test } from 'nodeunit';
@@ -64,6 +64,16 @@ export = {
6464
test.done();
6565
},
6666

67+
'default with no retention'(test: Test) {
68+
const app = new App();
69+
const stack = new Stack(app, 'TestStack');
70+
71+
new EncryptionKey(stack, 'MyKey', { retain: false });
72+
73+
expect(app.synthesizeStack(stack.name)).to(haveResource('AWS::KMS::Key', { DeletionPolicy: "Delete" }, ResourcePart.CompleteDefinition));
74+
test.done();
75+
},
76+
6777
'default with some permission'(test: Test) {
6878
const app = new App();
6979
const stack = new Stack(app, 'Test');

0 commit comments

Comments
 (0)