forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kms): support fromLookup in KMS key to get key by alias name (aw…
…s#15652) Add method `fromLookup` in KMS key which provides the option to get a KMS key including its key id by an alias name. In some cases, aliases can't be used because access to the underlying key id is necessary. In this case, the `fromLookup` method can be used. The following packages were changed: - @aws-cdk/aws-kms: introduce new `fromLookup` method - @aws-cdk/cx-api: new KeyContextResponse - @aws-cdk/cloud-assembly-schema: new ContextProvider KEY_PROVIDER and KeyContextQuery - aws-cdk: implementation of key ContextProvider Closes aws#8822 ----- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
9044e6a
commit 831019d
Showing
16 changed files
with
481 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Properties for looking up an existing Key. | ||
*/ | ||
export interface KeyLookupOptions { | ||
/** | ||
* The alias name of the Key | ||
*/ | ||
readonly aliasName: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import * as cxschema from '@aws-cdk/cloud-assembly-schema'; | ||
import { ContextProvider, GetContextValueOptions, GetContextValueResult, Lazy, Stack } from '@aws-cdk/core'; | ||
import * as cxapi from '@aws-cdk/cx-api'; | ||
import { Construct } from 'constructs'; | ||
import { Key } from '../lib'; | ||
import '@aws-cdk/assert-internal/jest'; | ||
|
||
test('requires concrete values', () => { | ||
expect(() => { | ||
// GIVEN | ||
const stack = new Stack(); | ||
|
||
Key.fromLookup(stack, 'Key', { | ||
aliasName: Lazy.string({ produce: () => 'some-id' }), | ||
}); | ||
}).toThrow('All arguments to Key.fromLookup() must be concrete (no Tokens)'); | ||
}); | ||
|
||
test('return correct key', () => { | ||
const previous = mockKeyContextProviderWith({ | ||
keyId: '12345678-1234-1234-1234-123456789012', | ||
}, options => { | ||
expect(options.aliasName).toEqual('alias/foo'); | ||
}); | ||
|
||
const stack = new Stack(undefined, undefined, { env: { region: 'us-east-1', account: '123456789012' } }); | ||
const key = Key.fromLookup(stack, 'Key', { | ||
aliasName: 'alias/foo', | ||
}); | ||
|
||
expect(key.keyId).toEqual('12345678-1234-1234-1234-123456789012'); | ||
expect(stack.resolve(key.keyArn)).toEqual({ | ||
'Fn::Join': ['', [ | ||
'arn:', | ||
{ Ref: 'AWS::Partition' }, | ||
':kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012', | ||
]], | ||
}); | ||
|
||
restoreContextProvider(previous); | ||
}); | ||
|
||
interface MockKeyContextResponse { | ||
readonly keyId: string; | ||
} | ||
|
||
function mockKeyContextProviderWith( | ||
response: MockKeyContextResponse, | ||
paramValidator?: (options: cxschema.KeyContextQuery) => void) { | ||
const previous = ContextProvider.getValue; | ||
ContextProvider.getValue = (_scope: Construct, options: GetContextValueOptions) => { | ||
// do some basic sanity checks | ||
expect(options.provider).toEqual(cxschema.ContextProvider.KEY_PROVIDER); | ||
|
||
if (paramValidator) { | ||
paramValidator(options.props as any); | ||
} | ||
|
||
return { | ||
value: { | ||
...response, | ||
} as cxapi.KeyContextResponse, | ||
}; | ||
}; | ||
return previous; | ||
} | ||
|
||
function restoreContextProvider(previous: (scope: Construct, options: GetContextValueOptions) => GetContextValueResult): void { | ||
ContextProvider.getValue = previous; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/@aws-cdk/cloud-assembly-schema/schema/cloud-assembly.version.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"version":"13.0.0"} | ||
{"version":"14.0.0"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Properties of a discovered key | ||
*/ | ||
export interface KeyContextResponse { | ||
|
||
/** | ||
* Id of the key | ||
*/ | ||
readonly keyId: string; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.