Skip to content

Commit 6126413

Browse files
chore(kms): prefer new aliasArn to keyArn for getting arn of an alias (#28197)
**Motivation:** The current implementation of `keyArn` within the AWS CDK AWS KMS module returns the Key ARN for a key and an alias, which causes confusion for users expecting the Alias ARN. This PR aims to alleviate this confusion by providing clearer access to the Alias ARN. **Changes:** Introducing a new attribute `aliasArn` that mirrors the value from `keyArn` specifically for aliases to explicitly retrieve the Alias ARN. ```typescript /** * The ARN of the alias. * * @Attribute * @deprecated use `aliasArn` instead */ public get keyArn(): string { return Stack.of(this).formatArn({ service: 'kms', // aliasName already contains the '/' resource: this.aliasName, }); } /** * The ARN of the alias. * * @Attribute */ public get aliasArn(): string { return this.keyArn; } ``` **Query:** Should we deprecate the existing `keyArn` and mirror it in `aliasArn` or change the logic within `keyArn` to `aliasArn` and use the `keyArn` as the mirror? > Your feedback on the preferred approach would be greatly appreciated! Closes #28105. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 46f3a00 commit 6126413

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

packages/aws-cdk-lib/aws-kms/lib/alias.ts

+19
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ abstract class AliasBase extends Resource implements IAlias {
5959

6060
public abstract readonly aliasTargetKey: IKey;
6161

62+
/**
63+
* The ARN of the alias.
64+
*
65+
* @attribute
66+
* @deprecated use `aliasArn` instead
67+
*/
6268
public get keyArn(): string {
6369
return Stack.of(this).formatArn({
6470
service: 'kms',
@@ -67,6 +73,19 @@ abstract class AliasBase extends Resource implements IAlias {
6773
});
6874
}
6975

76+
/**
77+
* The ARN of the alias.
78+
*
79+
* @attribute
80+
*/
81+
public get aliasArn(): string {
82+
return Stack.of(this).formatArn({
83+
service: 'kms',
84+
// aliasName already contains the '/'
85+
resource: this.aliasName,
86+
});
87+
}
88+
7089
public get keyId(): string {
7190
return this.aliasName;
7291
}

packages/aws-cdk-lib/aws-kms/test/alias.test.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Construct } from 'constructs';
22
import { Template } from '../../assertions';
33
import * as iam from '../../aws-iam';
44
import { ArnPrincipal, PolicyStatement } from '../../aws-iam';
5-
import { App, Aws, CfnOutput, Stack } from '../../core';
5+
import { App, Arn, Aws, CfnOutput, Stack } from '../../core';
66
import { KMS_ALIAS_NAME_REF } from '../../cx-api';
77
import { Alias } from '../lib/alias';
88
import { IKey, Key } from '../lib/key';
@@ -357,6 +357,30 @@ test('does not add alias if starts with token', () => {
357357
});
358358
});
359359

360+
test('aliasArn and keyArn from alias should match', () => {
361+
const app = new App();
362+
const stack = new Stack(app, 'Test');
363+
const key = new Key(stack, 'Key');
364+
365+
const alias = new Alias(stack, 'Alias', { targetKey: key, aliasName: 'alias/foo' });
366+
367+
expect(alias.aliasArn).toEqual(alias.keyArn);
368+
});
369+
370+
test('aliasArn should be a valid ARN', () => {
371+
const app = new App();
372+
const stack = new Stack(app, 'Test');
373+
const key = new Key(stack, 'Key');
374+
375+
const alias = new Alias(stack, 'Alias', { targetKey: key, aliasName: 'alias/foo' });
376+
377+
expect(alias.aliasArn).toEqual(Arn.format({
378+
service: 'kms',
379+
// aliasName already contains the '/'
380+
resource: alias.aliasName,
381+
}, stack));
382+
});
383+
360384
class AliasOutputsConstruct extends Construct {
361385
constructor(scope: Construct, id: string, key: IKey) {
362386
super(scope, id);

0 commit comments

Comments
 (0)