-
Notifications
You must be signed in to change notification settings - Fork 4k
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): for SSE topics, add KMS permissions in grantPublish #32794
Conversation
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.
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.
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.
Thanks 👍 Left comments for minor adjustments
grantee, | ||
actions: ['sns:Publish'], | ||
resourceArns: [this.topicArn], | ||
resource: this, | ||
}); | ||
if (this.masterKey) { | ||
this.masterKey.grantEncryptDecrypt(grantee); |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't we import topic2
instead?
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.
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.
importedTopic
isn't aware there's a KMS key sograntPublish
doesn't work. - I unencrypted
topic2
, so that importing withfromTopicArn
and callinggrantPublish
does work. - That left me wanting to import an encrypted topic, so I created
topic3
and 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clarifying 👍 Your implementation makes sense as is
e0ee009
to
469a884
Compare
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.
👍
importedTopic2.grantPublish(publishRole); | ||
|
||
// Can import encrypted topic by attributes | ||
const topic3 = new Topic(this, 'MyTopic3', { |
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.
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
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious what is this code block for?
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I'll explore that tonight.
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 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.
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.
Awesome, this works as well!
41a5f94
to
b13671d
Compare
Pull request has been modified.
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.
LGTM, approving this PR as assertions has been added which is the only requirement from Security guardian. Thank you for the patience and addressing the comments quickly!
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
grantPublish
are 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#grantPublish
grantkms:Decrypt
+kms:GenerateKeyData*
grantEncryptDecrypt
(but I have no preference -- just let me know what's best)masterKey
as a property ofITopic
so 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-failed
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license