fix(sns): for SSE topics, add KMS permissions in grantPublish#32794
fix(sns): for SSE topics, add KMS permissions in grantPublish#32794mergify[bot] merged 2 commits intoaws:mainfrom
Conversation
aws-cdk-automation
left a comment
There was a problem hiding this comment.
The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.
A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.
a45469a to
24f6412
Compare
✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.
lpizzinidev
left a comment
There was a problem hiding this comment.
Thanks 👍 Left comments for minor adjustments
| resource: this, | ||
| }); | ||
| if (this.masterKey) { | ||
| this.masterKey.grantEncryptDecrypt(grantee); |
There was a problem hiding this comment.
| this.masterKey.grantEncryptDecrypt(grantee); | |
| this.masterKey.grant(grantee, 'kms:Decrypt', 'kms:GenerateDataKey*') |
We should be fine without kms:Encrypt and kms:ReEncrypt.
There was a problem hiding this comment.
Sounds good, thanks!
| importedTopic2.grantPublish(publishRole); | ||
|
|
||
| // Can import encrypted topic by attributes | ||
| const topic3 = new Topic(this, 'MyTopic3', { |
There was a problem hiding this comment.
Couldn't we import topic2 instead?
There was a problem hiding this comment.
Sorry, not sure I understand the suggestion, do you mean topic? I can try to provide some of my thinking here:
- The existing test is showing a non-functional example.
importedTopicisn't aware there's a KMS key sograntPublishdoesn't work. - I unencrypted
topic2, so that importing withfromTopicArnand callinggrantPublishdoes work. - That left me wanting to import an encrypted topic, so I created
topic3and usedfromTopicAttributes, testing the same basic flow.
topic is encrypted and could be used for importing... it's just created up higher, and it felt like we'd moved on from it flow-wise.
I'm not totally sure what the norm is here, so your guidance is appreciated!
There was a problem hiding this comment.
Thanks for clarifying 👍 Your implementation makes sense as is
e0ee009 to
469a884
Compare
| importedTopic2.grantPublish(publishRole); | ||
|
|
||
| // Can import encrypted topic by attributes | ||
| const topic3 = new Topic(this, 'MyTopic3', { |
There was a problem hiding this comment.
Thanks for clarifying 👍 Your implementation makes sense as is
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #32794 +/- ##
=======================================
Coverage 80.83% 80.83%
=======================================
Files 236 236
Lines 14253 14253
Branches 2490 2490
=======================================
Hits 11522 11522
Misses 2446 2446
Partials 285 285
Flags with carried forward coverage won't be shown. Click here to find out more.
|
Dismissing as this would require security review.
|
I think the changes look good to me. I'll bring it up to the internal security team for review next week. |
469a884 to
41a5f94
Compare
GavinZZ
left a comment
There was a problem hiding this comment.
An update on the security review: please wait patiently, I've raised this PR to the security engineer and pending an response from him.
There was a problem hiding this comment.
Setting this PR to changes requested to meet the security engineer's requirement to add assertion tests. @lightningboltemoji let me know if the comments make sense to you.
| topic3.masterKey!.addToResourcePolicy(new PolicyStatement({ | ||
| principals: [new ServicePrincipal('s3.amazonaws.com')], | ||
| actions: ['kms:GenerateDataKey*', 'kms:Decrypt'], | ||
| resources: ['*'], | ||
| })); |
There was a problem hiding this comment.
Curious what is this code block for?
There was a problem hiding this comment.
What I meant by:
// Can reference KMS key after creation
is that referring to the topic's KMS key after topic creation is new functionality. This is testing that masterKey refers to the key we passed in, and that we're able to make changes to it.
There was a problem hiding this comment.
I switched this to a more generic unit test
| topicArn: topic3.topicArn, | ||
| keyArn: key.keyArn, | ||
| }); | ||
| importedTopic3.grantPublish(publishRole); |
There was a problem hiding this comment.
One of the security engineer's feedback is to test functionality instead of simply deployability. Can you use assertions to test publishing a message to the SNS topic to see if it will be successful? https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md#assertions
There was a problem hiding this comment.
OK. I'll explore that tonight.
There was a problem hiding this comment.
I added an integration test, but I had to use assertions.invokeFunction to accomplish it... I first tried to implement it along the lines of:
const publish = testCase.assertions.awsApiCall('sns', 'Publish', {
TopicArn: ..., Message: ...
});
but I couldn't find a way to combine this with grantPublish (i.e. we can't access the role associated with this custom resource).
Let me know what you think.
41a5f94 to
b13671d
Compare
Pull request has been modified.
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
|
Comments on closed issues and PRs are hard for our team to see. |
Issue # (if applicable)
Fixes #18387, #31012, #24848
Pre-requisite for #16271, #29511
Reason for this change
For SNS topics with SSE enabled, the grants added by
grantPublishare insufficient, since they don't include any KMS actions.The SNS docs discuss what's required to publish to an encrypted topic here (
sns:Publish,kms:Decrypt,kms:GenerateKeyData*).Description of changes
I used the SQS queue implementation as a reference, since it's configured similarly, etc.
Topic#grantPublishgrantkms:Decrypt+kms:GenerateKeyData*grantEncryptDecrypt(but I have no preference -- just let me know what's best)masterKeyas a property ofITopicso callers can access it after creationDescribe any new or updated permissions being added
(Discussed above)
Description of how you validated changes
yarn integ test/aws-sns/test/integ.sns.js --update-on-failedChecklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license