-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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(sns-subscriptions): SQS queue encrypted by AWS managed KMS key is allowed to be specified as subscription and dead-letter queue #26110
Merged
mergify
merged 3 commits into
aws:main
from
tam0ri:fix/validate-sqs-subscription-encrypted-by-managed-key
Jul 24, 2023
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,18 @@ export class SqsSubscription implements sns.ITopicSubscription { | |
} | ||
const snsServicePrincipal = new iam.ServicePrincipal('sns.amazonaws.com'); | ||
|
||
// if the queue is encrypted by AWS managed KMS key (alias/aws/sqs), | ||
// throw error message | ||
if ((this.queue.node.defaultChild as sqs.CfnQueue).kmsMasterKeyId == 'alias/aws/sqs') { | ||
throw new Error('SQS queue encrypted by AWS managed KMS key cannot be used as SNS subscription'); | ||
} | ||
|
||
// if the dead-letter queue is encrypted by AWS managed KMS key (alias/aws/sqs), | ||
// throw error message | ||
if (this.props.deadLetterQueue && (this.props.deadLetterQueue.node.defaultChild as sqs.CfnQueue).kmsMasterKeyId == 'alias/aws/sqs') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above |
||
throw new Error('SQS queue encrypted by AWS managed KMS key cannot be used as dead-letter queue'); | ||
} | ||
|
||
// add a statement to the queue resource policy which allows this topic | ||
// to send messages to the queue. | ||
const queuePolicyDependable = this.queue.addToResourcePolicy(new iam.PolicyStatement({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need to handle the case where the queue is imported
IQueue
in whichcase there will not be a default child.
I think we may want to add a new attribute to the
IQueue
, something likeencryptionType
that holds the type of encryption.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @corymhall, thank you for your review! I understand your concern.
If users import the queue via
fromQueueArn
method, CDK cannot determine whether the queue is encrypted by AWS managed KMS key or not.aws-cdk/packages/aws-cdk-lib/aws-sqs/lib/queue.ts
Lines 268 to 287 in f8a94d8
So in my understanding, we can't validate it completely in that case. I think one possible solution is skipping the validation if
encryptionMasterKey
is undefined to prevent error due to missing default child. Does it make sense?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe users can provide the key in
fromQueueAttributes
so if theencryptionType
is undefined then we don't do the validation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please let me clarify. In you assumption, what type of value
encryptionType
has? Is the same value as QueueEncryption enum?https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_sqs.QueueEncryption.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think so.