Skip to content

Commit 8e19dd6

Browse files
authored
feat(kms): implement .grant methods for Alias.fromAliasName (under feature flag) (#34237)
This uses the kms:ResourceAliases condition to add statements allowing the methods to the principal of the grant. ### Issue # Closes #22697 ### Reason for this change When using KMS keys cross stacks, it's sometimes difficult to pass KMS key IDs as they are only generated after deployment, however KMS key aliases could be passed instead and most services accept them. Some constructs that accept a KMS Alias, use .grant methods to add permissions to the role that interacts with the KMS key. Before this PR, the .grant methods of Alias.fromAliasName were not implemented. ### Description of changes This PR implements .grant methods for aliases imported via kms.Alias.fromAliasName so when passed to another a L2 construct that uses .grant methods, the required permissions are added to the role. The added statements will contain a kms:ResourceAliases condition that only allows access to the particular KMS key alias name. Couple of examples where this is useful: - Codepipeline construct accepts a S3 bucket for storing artifacts. If this S3 bucket was imported with the KMS key alias being imported as well via kms.Alias.fromAliasName, then this change results in the necessary permission being automatically added to the Codepipeline roles. - When .grant methods of imported SNS topics or SQS queues with imported KMS key alias for publishers are used, the necessary permission are added to the publisher roles. ### Describe any new or updated permissions being added .grant methods of Alias.fromAliasName now results in new statements with kms:ResourceAliases condition for that alias. ### Description of how you validated changes Added unit tests and integration tests: - `packages/@aws-cdk-testing/framework-integ/test/aws-kms/test/integ.alias-from-alias-name.ts` - `packages/aws-cdk-lib/aws-kms/test/alias.test.ts` Deployed my personal project which used Pipelines constructs with imported bucket and kms key from alias and now my pipeline role has correct permissions. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 9abd4eb commit 8e19dd6

16 files changed

+940
-15
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-kms/test/integ.alias-from-alias-name.js.snapshot/aws-cdk-kms.assets.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"Resources": {
3+
"Role1ABCC5F0": {
4+
"Type": "AWS::IAM::Role",
5+
"Properties": {
6+
"AssumeRolePolicyDocument": {
7+
"Statement": [
8+
{
9+
"Action": "sts:AssumeRole",
10+
"Effect": "Allow",
11+
"Principal": {
12+
"Service": "lambda.amazonaws.com"
13+
}
14+
}
15+
],
16+
"Version": "2012-10-17"
17+
}
18+
}
19+
},
20+
"RoleDefaultPolicy5FFB7DAB": {
21+
"Type": "AWS::IAM::Policy",
22+
"Properties": {
23+
"PolicyDocument": {
24+
"Statement": [
25+
{
26+
"Action": [
27+
"kms:Decrypt",
28+
"kms:Encrypt",
29+
"kms:GenerateDataKey*",
30+
"kms:GenerateMac",
31+
"kms:ReEncrypt*",
32+
"kms:Sign",
33+
"kms:Verify",
34+
"kms:VerifyMac"
35+
],
36+
"Condition": {
37+
"ForAnyValue:StringEquals": {
38+
"kms:ResourceAliases": "alias/MyKey"
39+
}
40+
},
41+
"Effect": "Allow",
42+
"Resource": {
43+
"Fn::Join": [
44+
"",
45+
[
46+
"arn:",
47+
{
48+
"Ref": "AWS::Partition"
49+
},
50+
":kms:",
51+
{
52+
"Ref": "AWS::Region"
53+
},
54+
":",
55+
{
56+
"Ref": "AWS::AccountId"
57+
},
58+
":key/*"
59+
]
60+
]
61+
}
62+
}
63+
],
64+
"Version": "2012-10-17"
65+
},
66+
"PolicyName": "RoleDefaultPolicy5FFB7DAB",
67+
"Roles": [
68+
{
69+
"Ref": "Role1ABCC5F0"
70+
}
71+
]
72+
}
73+
}
74+
},
75+
"Parameters": {
76+
"BootstrapVersion": {
77+
"Type": "AWS::SSM::Parameter::Value<String>",
78+
"Default": "/cdk-bootstrap/hnb659fds/version",
79+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
80+
}
81+
},
82+
"Rules": {
83+
"CheckBootstrapVersion": {
84+
"Assertions": [
85+
{
86+
"Assert": {
87+
"Fn::Not": [
88+
{
89+
"Fn::Contains": [
90+
[
91+
"1",
92+
"2",
93+
"3",
94+
"4",
95+
"5"
96+
],
97+
{
98+
"Ref": "BootstrapVersion"
99+
}
100+
]
101+
}
102+
]
103+
},
104+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
105+
}
106+
]
107+
}
108+
}
109+
}

packages/@aws-cdk-testing/framework-integ/test/aws-kms/test/integ.alias-from-alias-name.js.snapshot/cdk.out

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-kms/test/integ.alias-from-alias-name.js.snapshot/integ.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-kms/test/integ.alias-from-alias-name.js.snapshot/kmsaliasfromaliasnameDefaultTestDeployAssertFD733AC7.assets.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-kms/test/integ.alias-from-alias-name.js.snapshot/kmsaliasfromaliasnameDefaultTestDeployAssertFD733AC7.template.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)