Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(events-targets): circular dependency when adding a KMS-encrypted SQS queue #14638

Merged
merged 3 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/@aws-cdk/aws-events-targets/lib/sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ export class SqsQueue implements events.IRuleTarget {
* @see https://docs.aws.amazon.com/eventbridge/latest/userguide/resource-based-policies-eventbridge.html#sqs-permissions
*/
public bind(rule: events.IRule, _id?: string): events.RuleTargetConfig {
// deduplicated automatically
this.queue.grantSendMessages(new iam.ServicePrincipal('events.amazonaws.com',
{
// Only add the rule as a condition if the queue is not encrypted, to avoid circular dependency. See issue #11158.
var servicePrincipalOpts:iam.ServicePrincipalOpts = {};
madeline-k marked this conversation as resolved.
Show resolved Hide resolved
if (this.queue.encryptionMasterKey == null) {
madeline-k marked this conversation as resolved.
Show resolved Hide resolved
servicePrincipalOpts = {
conditions: {
ArnEquals: { 'aws:SourceArn': rule.ruleArn },
},
}),
);
};
}

// deduplicated automatically
this.queue.grantSendMessages(new iam.ServicePrincipal('events.amazonaws.com', servicePrincipalOpts));

return {
arn: this.queue.queueArn,
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-events-targets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-kinesis": "0.0.0",
"@aws-cdk/aws-kinesisfirehose": "0.0.0",
"@aws-cdk/aws-kms": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-logs": "0.0.0",
"@aws-cdk/aws-sns": "0.0.0",
Expand All @@ -114,6 +115,7 @@
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-kinesis": "0.0.0",
"@aws-cdk/aws-kinesisfirehose": "0.0.0",
"@aws-cdk/aws-kms": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-logs": "0.0.0",
"@aws-cdk/aws-sns": "0.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
{
"Resources": {
"MyKey6AB29FA6": {
"Type": "AWS::KMS::Key",
"Properties": {
"KeyPolicy": {
"Statement": [
{
"Action": "kms:*",
"Effect": "Allow",
"Principal": {
"AWS": {
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::",
{
"Ref": "AWS::AccountId"
},
":root"
]
]
}
},
"Resource": "*"
},
{
"Action": [
"kms:Decrypt",
"kms:Encrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*"
],
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
},
"Resource": "*"
}
],
"Version": "2012-10-17"
}
},
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
},
"MyRuleA44AB831": {
"Type": "AWS::Events::Rule",
"Properties": {
Expand All @@ -20,6 +68,14 @@
},
"MyQueueE6CA6235": {
"Type": "AWS::SQS::Queue",
"Properties": {
"KmsMasterKeyId": {
"Fn::GetAtt": [
"MyKey6AB29FA6",
"Arn"
]
}
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
Expand All @@ -34,16 +90,6 @@
"sqs:GetQueueAttributes",
"sqs:GetQueueUrl"
],
"Condition": {
"ArnEquals": {
"aws:SourceArn": {
"Fn::GetAtt": [
"MyRuleA44AB831",
"Arn"
]
}
}
},
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as events from '@aws-cdk/aws-events';
import * as kms from '@aws-cdk/aws-kms';
import * as sqs from '@aws-cdk/aws-sqs';
import * as cdk from '@aws-cdk/core';
import * as targets from '../../lib';
Expand All @@ -12,11 +13,17 @@ const app = new cdk.App();

const stack = new cdk.Stack(app, 'aws-cdk-sqs-event-target');

const key = new kms.Key(stack, 'MyKey');

const event = new events.Rule(stack, 'MyRule', {
schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
});

const queue = new sqs.Queue(stack, 'MyQueue');
const queue = new sqs.Queue(stack, 'MyQueue', {
encryption: sqs.QueueEncryption.KMS,
encryptionMasterKey: key,
});

event.addTarget(new targets.SqsQueue(queue));

app.synth();