Skip to content

Commit

Permalink
fix(kms): append aliasName only after first (#3659)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmussy authored and mergify[bot] committed Aug 14, 2019
1 parent d0beab1 commit 77671ad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
16 changes: 14 additions & 2 deletions packages/@aws-cdk/aws-kms/lib/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,23 @@ abstract class KeyBase extends Resource implements IKey {
*/
protected abstract readonly policy?: PolicyDocument;

/**
* Collection of aliases added to the key
*
* Tracked to determine whether or not the aliasName should be added to the end of its ID
*/
private readonly aliases: Alias[] = [];

/**
* Defines a new alias for the key.
*/
public addAlias(alias: string): Alias {
return new Alias(this, `Alias${alias}`, { aliasName: alias, targetKey: this });
public addAlias(aliasName: string): Alias {
const aliasId = this.aliases.length > 0 ? `Alias${aliasName}` : 'Alias';

const alias = new Alias(this, aliasId, { aliasName, targetKey: this });
this.aliases.push(alias);

return alias;
}

/**
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 @@ -59,7 +59,7 @@
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"MyKeyAliasaliasbarD0D50DB8": {
"MyKeyAlias1B45D9DA": {
"Type": "AWS::KMS::Alias",
"Properties": {
"AliasName": "alias/bar",
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-kms/test/test.key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export = {
DeletionPolicy: "Retain",
UpdateReplacePolicy: "Retain",
},
MyKeyAliasaliasxoo9464EB1C: {
MyKeyAlias1B45D9DA: {
Type: "AWS::KMS::Alias",
Properties: {
AliasName: "alias/xoo",
Expand Down Expand Up @@ -396,7 +396,7 @@ export = {
DeletionPolicy: "Retain",
UpdateReplacePolicy: "Retain",
},
MyKeyAliasaliasalias1668D31D7: {
MyKeyAlias1B45D9DA: {
Type: "AWS::KMS::Alias",
Properties: {
AliasName: "alias/alias1",
Expand Down Expand Up @@ -484,7 +484,7 @@ export = {

expect(stack2).toMatch({
Resources: {
MyKeyImportedAliasaliashelloD41FEB6C: {
MyKeyImportedAliasB1C5269F: {
Type: "AWS::KMS::Alias",
Properties: {
AliasName: "alias/hello",
Expand Down

0 comments on commit 77671ad

Please sign in to comment.