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

Merged
merged 2 commits into from
Feb 26, 2025

Conversation

lightningboltemoji
Copy link
Contributor

@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
Contributor 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
Contributor 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

@lightningboltemoji lightningboltemoji force-pushed the sns-kms branch 2 times, most recently from e0ee009 to 469a884 Compare January 11, 2025 02:26
@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 80.83%. Comparing base (bc82f57) to head (41a5f94).
Report is 17 commits behind head on main.

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           
Flag Coverage Δ
suite.unit 80.83% <ø> (ø)

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

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

GavinZZ
GavinZZ previously approved these changes Feb 5, 2025
@GavinZZ GavinZZ dismissed their stale review February 5, 2025 18:49

Dismissing as this would require security review.

@GavinZZ
Copy link
Contributor

GavinZZ commented Feb 5, 2025

I think the changes look good to me. I'll bring it up to the internal security team for review next week.

@GavinZZ GavinZZ self-assigned this Feb 5, 2025
Copy link
Contributor

@GavinZZ GavinZZ left a 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.

Copy link
Contributor

@GavinZZ GavinZZ left a 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.

Comment on lines 86 to 90
topic3.masterKey!.addToResourcePolicy(new PolicyStatement({
principals: [new ServicePrincipal('s3.amazonaws.com')],
actions: ['kms:GenerateDataKey*', 'kms:Decrypt'],
resources: ['*'],
}));
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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);
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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!

@mergify mergify bot dismissed GavinZZ’s stale review February 26, 2025 07:40

Pull request has been modified.

Copy link
Contributor

@GavinZZ GavinZZ left a 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!

Copy link
Contributor

mergify bot commented Feb 26, 2025

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-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

Copy link
Contributor

mergify bot commented Feb 26, 2025

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).

@mergify mergify bot merged commit f1c0926 into aws:main Feb 26, 2025
10 checks passed
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 26, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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
5 participants