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(sns): for SSE topics, add KMS permissions in grantPublish #32794

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lightningboltemoji
Copy link

@lightningboltemoji lightningboltemoji commented Jan 8, 2025

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.

  • Have Topic#grantPublish grant kms:Decrypt + kms:GenerateKeyData*
    • This is least-privilege, but slightly inconsistent with SQS queues, which need these same actions and use grantEncryptDecrypt (but I have no preference -- just let me know what's best)
  • Exposes masterKey as a property of ITopic so callers can access it after creation
    • Enables this, for example, and in general makes it consistent with SQS queues

Describe any new or updated permissions being added

(Discussed above)

Description of how you validated changes

  • Unit/integration tests
    • 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

@aws-cdk-automation aws-cdk-automation requested a review from a team January 8, 2025 07:11
@github-actions github-actions bot added beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 labels Jan 8, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a 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.

@github-actions github-actions bot added bug This issue is a bug. effort/small Small work item – less than a day of effort and removed feature-request A feature should be added or improved. effort/medium Medium work item – several days of effort labels Jan 8, 2025
@lightningboltemoji lightningboltemoji force-pushed the sns-kms branch 4 times, most recently from a45469a to 24f6412 Compare January 9, 2025 06:52
@aws-cdk-automation aws-cdk-automation dismissed their stale review January 9, 2025 06:54

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@lightningboltemoji lightningboltemoji marked this pull request as ready for review January 9, 2025 06:57
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jan 9, 2025
Copy link
Contributor

@lpizzinidev lpizzinidev left a 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.masterKey.grantEncryptDecrypt(grantee);
this.masterKey.grant(grantee, 'kms:Decrypt', 'kms:GenerateDataKey*')

We should be fine without kms:Encrypt and kms:ReEncrypt.

Copy link
Author

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', {
Copy link
Contributor

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?

Copy link
Author

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 so grantPublish doesn't work.
  • I unencrypted topic2, so that importing with fromTopicArn and calling grantPublish does work.
  • That left me wanting to import an encrypted topic, so I created topic3 and used fromTopicAttributes, 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!

Copy link
Contributor

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

@@ -25,6 +26,13 @@ export interface ITopic extends IResource, notifications.INotificationRuleTarget
*/
readonly topicName: string;

/**
* A KMS Key, either managed by this CDK app, or imported.
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*
*
* This property applies only to server-side encryption.
*
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-server-side-encryption.html

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, thanks!

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 469a884
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jan 11, 2025
Copy link
Contributor

@lpizzinidev lpizzinidev left a 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', {
Copy link
Contributor

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

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jan 12, 2025
@moelasmar moelasmar added the needs-security-review Related to feature or issues that needs security review label Jan 12, 2025
Copy link

codecov bot commented Jan 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.38%. Comparing base (49eb9d3) to head (469a884).
Report is 13 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #32794   +/-   ##
=======================================
  Coverage   81.38%   81.38%           
=======================================
  Files         222      222           
  Lines       13695    13695           
  Branches     2412     2412           
=======================================
  Hits        11145    11145           
  Misses       2271     2271           
  Partials      279      279           
Flag Coverage Δ
suite.unit 81.38% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
packages/aws-cdk 80.69% <ø> (ø)
packages/aws-cdk-lib/core 82.09% <ø> (ø)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. effort/small Small work item – less than a day of effort needs-security-review Related to feature or issues that needs security review p2 pr/needs-maintainer-review This PR needs a review from a Core Team Member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-sns): grantPublish should also grant permission to decrypt master key
4 participants