Skip to content

Commit 07f017b

Browse files
authored
fix(sqs): do not emit grants to the AWS-managed encryption key (#3169)
Grants on the `alias/aws/sqs` KMS key alias are not necessary since the key will implicitly allow for it's intended usage to be fulfilled (as opposed to how you have to manage grants yourself when using a user-managed key instead). This removes the statement that was generated using an invalid resource entry. Fixes #2794
1 parent fac7c61 commit 07f017b

File tree

5 files changed

+194
-19
lines changed

5 files changed

+194
-19
lines changed

packages/@aws-cdk/aws-s3-notifications/test/queue.test.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,3 @@ test('if the queue is encrypted with a custom kms key, the key resource policy i
140140
Description: "Created by Queue"
141141
});
142142
});
143-
144-
test('fails if trying to subscribe to a queue with managed kms encryption', () => {
145-
const stack = new Stack();
146-
const queue = new sqs.Queue(stack, 'Queue', { encryption: sqs.QueueEncryption.KMS_MANAGED });
147-
const bucket = new s3.Bucket(stack, 'Bucket');
148-
expect(() => {
149-
bucket.addObjectRemovedNotification(new notif.SqsDestination(queue));
150-
}).toThrow('Unable to add statement to IAM resource policy for KMS key: "alias/aws/sqs"');
151-
});

packages/@aws-cdk/aws-sqs/lib/queue.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,7 @@ export class Queue extends QueueBase {
277277
}
278278

279279
if (encryption === QueueEncryption.KMS_MANAGED) {
280-
const masterKey = kms.Key.fromKeyArn(this, 'Key', 'alias/aws/sqs');
281-
282280
return {
283-
encryptionMasterKey: masterKey,
284281
encryptionProps: {
285282
kmsMasterKeyId: 'alias/aws/sqs',
286283
kmsDataKeyReusePeriodSeconds: props.dataKeyReuse && props.dataKeyReuse.toSeconds()

packages/@aws-cdk/aws-sqs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
},
6363
"license": "Apache-2.0",
6464
"devDependencies": {
65+
"@aws-cdk/aws-kms": "^1.3.0",
6566
"@aws-cdk/assert": "^1.3.0",
6667
"@aws-cdk/aws-s3": "^1.3.0",
6768
"aws-sdk": "^2.438.0",
@@ -93,4 +94,4 @@
9394
]
9495
},
9596
"stability": "stable"
96-
}
97+
}

packages/@aws-cdk/aws-sqs/test/integ.sqs.expected.json

Lines changed: 176 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"Queue4A7E3555": {
77
"Type": "AWS::SQS::Queue",
88
"Properties": {
9+
"KmsMasterKeyId": "alias/aws/sqs",
910
"RedrivePolicy": {
1011
"deadLetterTargetArn": {
1112
"Fn::GetAtt": [
@@ -17,10 +18,184 @@
1718
}
1819
}
1920
},
21+
"EncryptionKey1B843E66": {
22+
"Type": "AWS::KMS::Key",
23+
"Properties": {
24+
"KeyPolicy": {
25+
"Statement": [
26+
{
27+
"Action": [
28+
"kms:Create*",
29+
"kms:Describe*",
30+
"kms:Enable*",
31+
"kms:List*",
32+
"kms:Put*",
33+
"kms:Update*",
34+
"kms:Revoke*",
35+
"kms:Disable*",
36+
"kms:Get*",
37+
"kms:Delete*",
38+
"kms:ScheduleKeyDeletion",
39+
"kms:CancelKeyDeletion",
40+
"kms:GenerateDataKey"
41+
],
42+
"Effect": "Allow",
43+
"Principal": {
44+
"AWS": {
45+
"Fn::Join": [
46+
"",
47+
[
48+
"arn:",
49+
{
50+
"Ref": "AWS::Partition"
51+
},
52+
":iam::",
53+
{
54+
"Ref": "AWS::AccountId"
55+
},
56+
":root"
57+
]
58+
]
59+
}
60+
},
61+
"Resource": "*"
62+
},
63+
{
64+
"Action": "kms:Decrypt",
65+
"Effect": "Allow",
66+
"Principal": {
67+
"AWS": {
68+
"Fn::GetAtt": [
69+
"Role1ABCC5F0",
70+
"Arn"
71+
]
72+
}
73+
},
74+
"Resource": "*"
75+
}
76+
],
77+
"Version": "2012-10-17"
78+
}
79+
},
80+
"UpdateReplacePolicy": "Delete",
81+
"DeletionPolicy": "Delete"
82+
},
2083
"FifoQueueE5FF7273": {
2184
"Type": "AWS::SQS::Queue",
2285
"Properties": {
23-
"FifoQueue": true
86+
"FifoQueue": true,
87+
"KmsMasterKeyId": {
88+
"Fn::GetAtt": [
89+
"EncryptionKey1B843E66",
90+
"Arn"
91+
]
92+
}
93+
}
94+
},
95+
"Role1ABCC5F0": {
96+
"Type": "AWS::IAM::Role",
97+
"Properties": {
98+
"AssumeRolePolicyDocument": {
99+
"Statement": [
100+
{
101+
"Action": "sts:AssumeRole",
102+
"Effect": "Allow",
103+
"Principal": {
104+
"AWS": {
105+
"Fn::Join": [
106+
"",
107+
[
108+
"arn:",
109+
{
110+
"Ref": "AWS::Partition"
111+
},
112+
":iam::",
113+
{
114+
"Ref": "AWS::AccountId"
115+
},
116+
":root"
117+
]
118+
]
119+
}
120+
}
121+
}
122+
],
123+
"Version": "2012-10-17"
124+
}
125+
}
126+
},
127+
"RoleDefaultPolicy5FFB7DAB": {
128+
"Type": "AWS::IAM::Policy",
129+
"Properties": {
130+
"PolicyDocument": {
131+
"Statement": [
132+
{
133+
"Action": [
134+
"sqs:ReceiveMessage",
135+
"sqs:ChangeMessageVisibility",
136+
"sqs:GetQueueUrl",
137+
"sqs:DeleteMessage",
138+
"sqs:GetQueueAttributes"
139+
],
140+
"Effect": "Allow",
141+
"Resource": {
142+
"Fn::GetAtt": [
143+
"DeadLetterQueue9F481546",
144+
"Arn"
145+
]
146+
}
147+
},
148+
{
149+
"Action": [
150+
"sqs:ReceiveMessage",
151+
"sqs:ChangeMessageVisibility",
152+
"sqs:GetQueueUrl",
153+
"sqs:DeleteMessage",
154+
"sqs:GetQueueAttributes"
155+
],
156+
"Effect": "Allow",
157+
"Resource": {
158+
"Fn::GetAtt": [
159+
"Queue4A7E3555",
160+
"Arn"
161+
]
162+
}
163+
},
164+
{
165+
"Action": [
166+
"sqs:ReceiveMessage",
167+
"sqs:ChangeMessageVisibility",
168+
"sqs:GetQueueUrl",
169+
"sqs:DeleteMessage",
170+
"sqs:GetQueueAttributes"
171+
],
172+
"Effect": "Allow",
173+
"Resource": {
174+
"Fn::GetAtt": [
175+
"FifoQueueE5FF7273",
176+
"Arn"
177+
]
178+
}
179+
},
180+
{
181+
"Action": "kms:Decrypt",
182+
"Effect": "Allow",
183+
"Resource": {
184+
"Fn::GetAtt": [
185+
"EncryptionKey1B843E66",
186+
"Arn"
187+
]
188+
}
189+
}
190+
],
191+
"Version": "2012-10-17"
192+
},
193+
"PolicyName": "RoleDefaultPolicy5FFB7DAB",
194+
"Roles": [
195+
{
196+
"Ref": "Role1ABCC5F0"
197+
}
198+
]
24199
}
25200
}
26201
},
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
1-
import { App, CfnOutput, Stack } from '@aws-cdk/core';
2-
import { Queue } from '../lib';
1+
import { AccountRootPrincipal, Role } from '@aws-cdk/aws-iam';
2+
import { Key } from '@aws-cdk/aws-kms';
3+
import { App, CfnOutput, RemovalPolicy, Stack } from '@aws-cdk/core';
4+
import { Queue, QueueEncryption } from '../lib';
35

46
const app = new App();
57

68
const stack = new Stack(app, 'aws-cdk-sqs');
79

810
const dlq = new Queue(stack, 'DeadLetterQueue');
911
const queue = new Queue(stack, 'Queue', {
10-
deadLetterQueue: { queue: dlq, maxReceiveCount: 5 }
12+
deadLetterQueue: { queue: dlq, maxReceiveCount: 5 },
13+
encryption: QueueEncryption.KMS_MANAGED,
14+
});
15+
const fifo = new Queue(stack, 'FifoQueue', {
16+
fifo: true,
17+
encryptionMasterKey: new Key(stack, 'EncryptionKey', { removalPolicy: RemovalPolicy.DESTROY })
1118
});
1219

13-
new Queue(stack, 'FifoQueue', {
14-
fifo: true
20+
const role = new Role(stack, 'Role', {
21+
assumedBy: new AccountRootPrincipal(),
1522
});
1623

24+
dlq.grantConsumeMessages(role);
25+
queue.grantConsumeMessages(role);
26+
fifo.grantConsumeMessages(role);
27+
1728
new CfnOutput(stack, 'QueueUrl', { value: queue.queueUrl });
1829

1930
app.synth();

0 commit comments

Comments
 (0)