Skip to content

Commit

Permalink
feat(aws-kms): allow tagging kms keys (#1485)
Browse files Browse the repository at this point in the history
Small change to enable tagging KMS keys.
  • Loading branch information
rsmogura authored and Elad Ben-Israel committed Jan 7, 2019
1 parent 82ec0ff commit f43b4d4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
17 changes: 15 additions & 2 deletions packages/@aws-cdk/aws-kms/lib/key.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PolicyDocument, PolicyStatement } from '@aws-cdk/aws-iam';
import { Construct, DeletionPolicy, IConstruct, Output, resolve } from '@aws-cdk/cdk';
import { Construct, DeletionPolicy, IConstruct, Output, resolve, TagManager, Tags } from '@aws-cdk/cdk';
import { EncryptionKeyAlias } from './alias';
import { CfnKey } from './kms.generated';

Expand Down Expand Up @@ -106,6 +106,11 @@ export interface EncryptionKeyProps {
* administer the key will be created.
*/
policy?: PolicyDocument;

/**
* The AWS resource tags to associate with the KMS key.
*/
tags?: Tags;
}

/**
Expand Down Expand Up @@ -134,6 +139,11 @@ export class EncryptionKey extends EncryptionKeyBase {
return new ImportedEncryptionKey(scope, id, props);
}

/**
* Manage tags for this construct and children
*/
public readonly tags: TagManager;

public readonly keyArn: string;
protected readonly policy?: PolicyDocument;

Expand All @@ -147,11 +157,14 @@ export class EncryptionKey extends EncryptionKeyBase {
this.allowAccountToAdmin();
}

this.tags = new TagManager(this, { initialTags: props.tags });

const resource = new CfnKey(this, 'Resource', {
description: props.description,
enableKeyRotation: props.enableKeyRotation,
enabled: props.enabled,
keyPolicy: this.policy
keyPolicy: this.policy,
tags: this.tags
});

this.keyArn = resource.keyArn;
Expand Down
23 changes: 21 additions & 2 deletions packages/@aws-cdk/aws-kms/test/test.key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ export = {

const key = new EncryptionKey(stack, 'MyKey', {
enableKeyRotation: true,
enabled: false
enabled: false,
tags: {
tag1: 'value1',
tag2: 'value2',
tag3: ''
}
});
const p = new PolicyStatement().addAllResources().addAction('kms:encrypt');
p.addAwsPrincipal('arn');
Expand Down Expand Up @@ -204,7 +209,21 @@ export = {
}
],
Version: "2012-10-17"
}
},
Tags: [
{
Key: "tag1",
Value: "value1"
},
{
Key: "tag2",
Value: "value2"
},
{
Key: "tag3",
Value: ""
}
]
},
DeletionPolicy: "Retain"
}
Expand Down

0 comments on commit f43b4d4

Please sign in to comment.