Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kms): Allow opting out of "Retain" deletion policy #1685

Merged
merged 6 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/@aws-cdk/aws-kms/lib/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ export interface EncryptionKeyProps {
* The AWS resource tags to associate with the KMS key.
*/
tags?: Tags;

/**
* Whether the encryption key should be retained when it is removed from the Stack. This is useful when one wants to
* retain access to data that was encrypted with a key that is being retired.
*
* @default true
*/
retainKey?: boolean;
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -168,7 +176,9 @@ export class EncryptionKey extends EncryptionKeyBase {
});

this.keyArn = resource.keyArn;
resource.options.deletionPolicy = DeletionPolicy.Retain;
resource.options.deletionPolicy = props.retainKey === false
? DeletionPolicy.Delete
: DeletionPolicy.Retain;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"Version": "2012-10-17"
}
},
"DeletionPolicy": "Retain"
"DeletionPolicy": "Delete"
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a good practice it is usually preferable not to modify existing tests but rather add new tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well... technically I'm only touching here because I want to stop littering the account with one-off keys that we'll never use again... 😅

"MyKeyAlias1B45D9DA": {
"Type": "AWS::KMS::Alias",
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-kms/test/integ.key-sharing.lit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class KeyStack extends cdk.Stack {

constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.key = new kms.EncryptionKey(this, 'MyKey');
this.key = new kms.EncryptionKey(this, 'MyKey', { retainKey: false });
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-kms/test/integ.key.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"Version": "2012-10-17"
}
},
"DeletionPolicy": "Retain"
"DeletionPolicy": "Delete"
},
"MyKeyAlias1B45D9DA": {
"Type": "AWS::KMS::Alias",
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-kms/test/integ.key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const app = new App();

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

const key = new EncryptionKey(stack, 'MyKey');
const key = new EncryptionKey(stack, 'MyKey', { retainKey: false });

key.addToResourcePolicy(new PolicyStatement()
.addAllResources()
Expand Down
60 changes: 60 additions & 0 deletions packages/@aws-cdk/aws-kms/test/test.key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,66 @@ export = {
test.done();
},

'default with no retention'(test: Test) {
const app = new App();
const stack = new Stack(app, 'TestStack');

new EncryptionKey(stack, 'MyKey', { retainKey: false });

expect(app.synthesizeStack(stack.name)).to(exactlyMatchTemplate({
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved
Resources: {
MyKey6AB29FA6: {
Type: "AWS::KMS::Key",
Properties: {
KeyPolicy: {
Statement: [
{
Action: [
"kms:Create*",
"kms:Describe*",
"kms:Enable*",
"kms:List*",
"kms:Put*",
"kms:Update*",
"kms:Revoke*",
"kms:Disable*",
"kms:Get*",
"kms:Delete*",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion"
],
Effect: "Allow",
Principal: {
AWS: {
"Fn::Join": [
"",
[
"arn:",
{
Ref: "AWS::Partition"
},
":iam::",
{
Ref: "AWS::AccountId"
},
":root"
]
]
}
},
Resource: "*"
}
],
Version: "2012-10-17"
}
},
DeletionPolicy: "Delete",
}
}
}));
test.done();
},

'default with some permission'(test: Test) {
const app = new App();
const stack = new Stack(app, 'Test');
Expand Down