Skip to content

Commit 43ffcff

Browse files
authored
fix(kms): Alias reference incorrectly resolves to underlying Key (#35545)
Because `IAlias` extends `IKey`, `IAlias` inherits the new `keyRef: KeyReference`. It was originally implemented by referencing the underlying key, but that's wrong: it should reference the alias itself, but using the field names it shares with `IKey`. In fact, we should have introduced a new interface like `IKeyLike` to do this job, but instead we overloaded `IKey` to behave like a hypothetical `IKeyLike`, and `IKeyRef` now inherits this double duty. Therefore, we now make the `IKeyRef` implementation of `Alias` behave like a `IKeyLikeRef`, and satisfy the contract using its own fields. Closes #35543 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 44781ef commit 43ffcff

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ abstract class AliasBase extends Resource implements IAlias {
6969
}
7070

7171
public get keyRef(): KeyReference {
72-
return this.aliasTargetKey.keyRef;
72+
// Not actually referering to the key: `IKeyRef` here is being used as a
73+
// hypothetical `IKeyLikeRef`, and we need to return the Alias values using
74+
// the Key interface.
75+
return {
76+
keyArn: this.aliasArn,
77+
keyId: this.keyId,
78+
};
7379
}
7480

7581
/**

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import * as cxapi from '../../cx-api';
2626

2727
/**
2828
* A KMS Key, either managed by this CDK app, or imported.
29+
*
30+
* This interface does double duty: it represents an actual KMS keys, but it
31+
* also represents things that can behave like KMS keys, like a key alias.
2932
*/
3033
export interface IKey extends IResource, IKeyRef {
3134
/**

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,19 @@ test('aliasArn should be a valid ARN', () => {
911911
}, stack));
912912
});
913913

914+
test('Alias keyRef should reference the Alias, not the underlying key', () => {
915+
// GIVEN
916+
const app = new App();
917+
const stack = new Stack(app, 'Test');
918+
const key = new Key(stack, 'Key');
919+
920+
// WHEN
921+
const alias = key.addAlias('alias/foo');
922+
923+
// THEN
924+
expect(alias.keyRef.keyArn).toEqual(alias.aliasArn);
925+
});
926+
914927
class AliasOutputsConstruct extends Construct {
915928
constructor(scope: Construct, id: string, key: IKey) {
916929
super(scope, id);

0 commit comments

Comments
 (0)