Skip to content

Commit 0083256

Browse files
authored
fix(events-targets): policy restricts access to the same account as the Queue, not the Rule (#22766)
When restricting access to encrypted queues, we should use the account of the Rule, instead of the account of the Queue itself. ---- ### All Submissions: * [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent ec32b5b commit 0083256

File tree

3 files changed

+29
-31
lines changed

3 files changed

+29
-31
lines changed

packages/@aws-cdk/aws-events-targets/lib/sqs.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as events from '@aws-cdk/aws-events';
22
import * as iam from '@aws-cdk/aws-iam';
33
import * as sqs from '@aws-cdk/aws-sqs';
4-
import { Aws, FeatureFlags } from '@aws-cdk/core';
4+
import { FeatureFlags } from '@aws-cdk/core';
55
import * as cxapi from '@aws-cdk/cx-api';
66
import { addToDeadLetterQueueResourcePolicy, TargetBaseProps, bindBaseTargetConfig } from './util';
77

@@ -62,9 +62,9 @@ export class SqsQueue implements events.IRuleTarget {
6262
ArnEquals: { 'aws:SourceArn': rule.ruleArn },
6363
};
6464
} else if (restrictToSameAccount) {
65-
// Aadd only the account id as a condition, to avoid circular dependency. See issue #11158.
65+
// Add only the account id as a condition, to avoid circular dependency. See issue #11158.
6666
conditions = {
67-
StringEquals: { 'aws:SourceAccount': Aws.ACCOUNT_ID },
67+
StringEquals: { 'aws:SourceAccount': rule.env.account },
6868
};
6969
}
7070

packages/@aws-cdk/aws-events-targets/test/sqs/sqs.test.ts

+25-27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Template } from '@aws-cdk/assertions';
1+
import { Match, Template } from '@aws-cdk/assertions';
22
import * as events from '@aws-cdk/aws-events';
33
import * as kms from '@aws-cdk/aws-kms';
44
import * as sqs from '@aws-cdk/aws-sqs';
5-
import { Duration, Stack } from '@aws-cdk/core';
5+
import { App, Duration, Stack } from '@aws-cdk/core';
66
import * as cxapi from '@aws-cdk/cx-api';
77
import * as targets from '../../lib';
88

@@ -144,24 +144,38 @@ test('multiple uses of a queue as a target results in multi policy statement bec
144144
});
145145

146146
test('Encrypted queues result in a policy statement with aws:sourceAccount condition when the feature flag is on', () => {
147+
const app = new App();
147148
// GIVEN
148-
const stack = new Stack();
149-
stack.node.setContext(cxapi.EVENTS_TARGET_QUEUE_SAME_ACCOUNT, true);
150-
const queue = new sqs.Queue(stack, 'MyQueue', {
151-
encryptionMasterKey: kms.Key.fromKeyArn(stack, 'key', 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'),
149+
const ruleStack = new Stack(app, 'ruleStack', {
150+
env: {
151+
account: '111111111111',
152+
region: 'us-east-1',
153+
},
152154
});
155+
ruleStack.node.setContext(cxapi.EVENTS_TARGET_QUEUE_SAME_ACCOUNT, true);
153156

154-
const rule = new events.Rule(stack, 'MyRule', {
157+
const rule = new events.Rule(ruleStack, 'MyRule', {
155158
schedule: events.Schedule.rate(Duration.hours(1)),
156159
});
157160

161+
const queueStack = new Stack(app, 'queueStack', {
162+
env: {
163+
account: '222222222222',
164+
region: 'us-east-1',
165+
},
166+
});
167+
const queue = new sqs.Queue(queueStack, 'MyQueue', {
168+
encryptionMasterKey: kms.Key.fromKeyArn(queueStack, 'key', 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'),
169+
});
170+
171+
158172
// WHEN
159173
rule.addTarget(new targets.SqsQueue(queue));
160174

161175
// THEN
162-
Template.fromStack(stack).hasResourceProperties('AWS::SQS::QueuePolicy', {
176+
Template.fromStack(queueStack).hasResourceProperties('AWS::SQS::QueuePolicy', {
163177
PolicyDocument: {
164-
Statement: [
178+
Statement: Match.arrayWith([
165179
{
166180
Action: [
167181
'sqs:SendMessage',
@@ -170,7 +184,7 @@ test('Encrypted queues result in a policy statement with aws:sourceAccount condi
170184
],
171185
Condition: {
172186
StringEquals: {
173-
'aws:SourceAccount': { Ref: 'AWS::AccountId' },
187+
'aws:SourceAccount': '111111111111',
174188
},
175189
},
176190
Effect: 'Allow',
@@ -182,27 +196,11 @@ test('Encrypted queues result in a policy statement with aws:sourceAccount condi
182196
],
183197
},
184198
},
185-
],
199+
]),
186200
Version: '2012-10-17',
187201
},
188202
Queues: [{ Ref: 'MyQueueE6CA6235' }],
189203
});
190-
191-
Template.fromStack(stack).hasResourceProperties('AWS::Events::Rule', {
192-
ScheduleExpression: 'rate(1 hour)',
193-
State: 'ENABLED',
194-
Targets: [
195-
{
196-
Arn: {
197-
'Fn::GetAtt': [
198-
'MyQueueE6CA6235',
199-
'Arn',
200-
],
201-
},
202-
Id: 'Target0',
203-
},
204-
],
205-
});
206204
});
207205

208206
test('Encrypted queues result in a permissive policy statement when the feature flag is off', () => {

packages/@aws-cdk/cx-api/lib/features.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ export const ENABLE_PARTITION_LITERALS = '@aws-cdk/core:enablePartitionLiterals'
354354

355355
/**
356356
* This flag applies to SQS Queues that are used as the target of event Rules. When enabled, only principals
357-
* from the same account as the Queue can send messages. If a queue is unencrypted, this restriction will
357+
* from the same account as the Rule can send messages. If a queue is unencrypted, this restriction will
358358
* always apply, regardless of the value of this flag.
359359
*/
360360
export const EVENTS_TARGET_QUEUE_SAME_ACCOUNT = '@aws-cdk/aws-events:eventsTargetQueueSameAccount';

0 commit comments

Comments
 (0)