Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
fix(events-targets): remove circular dependency when adding an SQS Qu…
Browse files Browse the repository at this point in the history
…eue encrypted with KMS as a target of an AWS Events Rule

fixes aws#11158
  • Loading branch information
madeline-k committed May 11, 2021
1 parent 256fd4c commit 302872a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 16 deletions.
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 = {};
if (this.queue.encryptionMasterKey == null) {
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();

0 comments on commit 302872a

Please sign in to comment.