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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
}
},
"Resource": "*"
},
{
"Action": [
"kms:Decrypt",
"kms:GenerateDataKey*"
],
"Effect": "Allow",
"Principal": {
"Service": "s3.amazonaws.com"
},
"Resource": "*"
}
],
"Version": "2012-10-17"
Expand Down Expand Up @@ -132,12 +143,6 @@
"Type": "AWS::SNS::Topic",
"Properties": {
"DisplayName": "fooDisplayName2",
"KmsMasterKeyId": {
"Fn::GetAtt": [
"CustomKey1E6D0D07",
"Arn"
]
},
"TopicName": "fooTopic2"
}
},
Expand Down Expand Up @@ -166,8 +171,26 @@
{
"Action": "sns:Publish",
"Effect": "Allow",
"Resource": [
{
"Ref": "MyTopic288CE2107"
},
{
"Ref": "MyTopic3134CFDFB"
}
]
},
{
"Action": [
"kms:Decrypt",
"kms:GenerateDataKey*"
],
"Effect": "Allow",
"Resource": {
"Ref": "MyTopic288CE2107"
"Fn::GetAtt": [
"CustomKey1E6D0D07",
"Arn"
]
}
}
],
Expand All @@ -180,6 +203,19 @@
}
]
}
},
"MyTopic3134CFDFB": {
"Type": "AWS::SNS::Topic",
"Properties": {
"DisplayName": "fooDisplayName3",
"KmsMasterKeyId": {
"Fn::GetAtt": [
"CustomKey1E6D0D07",
"Arn"
]
},
"TopicName": "fooTopic3"
}
}
},
"Parameters": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,32 @@ class SNSInteg extends Stack {
const topic2 = new Topic(this, 'MyTopic2', {
topicName: 'fooTopic2',
displayName: 'fooDisplayName2',
masterKey: key,
});
const importedTopic = Topic.fromTopicArn(this, 'ImportedTopic', topic2.topicArn);
const importedTopic2 = Topic.fromTopicArn(this, 'ImportedTopic2', topic2.topicArn);

const publishRole = new Role(this, 'PublishRole', {
assumedBy: new ServicePrincipal('s3.amazonaws.com'),
});
importedTopic.grantPublish(publishRole);
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

topicName: 'fooTopic3',
displayName: 'fooDisplayName3',
masterKey: key,
});
const importedTopic3 = Topic.fromTopicAttributes(this, 'ImportedTopic3', {
topicArn: topic3.topicArn,
keyArn: key.keyArn,
});
importedTopic3.grantPublish(publishRole);

// Can reference KMS key after creation
topic3.masterKey!.addToResourcePolicy(new PolicyStatement({
principals: [new ServicePrincipal('s3.amazonaws.com')],
actions: ['kms:GenerateDataKey*', 'kms:Decrypt'],
resources: ['*'],
}));
}
}

Expand Down
20 changes: 19 additions & 1 deletion packages/aws-cdk-lib/aws-sns/lib/topic-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ITopicSubscription } from './subscriber';
import { Subscription } from './subscription';
import * as notifications from '../../aws-codestarnotifications';
import * as iam from '../../aws-iam';
import { IKey } from '../../aws-kms';
import { IResource, Resource, ResourceProps, Token } from '../../core';

/**
Expand All @@ -25,6 +26,17 @@ 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!

* This property applies only to server-side encryption.
*
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-server-side-encryption.html
*
* @default None
*/
readonly masterKey?: IKey;

/**
* Enables content-based deduplication for FIFO topics.
*
Expand Down Expand Up @@ -72,6 +84,8 @@ export abstract class TopicBase extends Resource implements ITopic {

public abstract readonly topicName: string;

public abstract readonly masterKey?: IKey;

public abstract readonly fifo: boolean;

public abstract readonly contentBasedDeduplication: boolean;
Expand Down Expand Up @@ -173,12 +187,16 @@ export abstract class TopicBase extends Resource implements ITopic {
* Grant topic publishing permissions to the given identity
*/
public grantPublish(grantee: iam.IGrantable) {
return iam.Grant.addToPrincipalOrResource({
const ret = iam.Grant.addToPrincipalOrResource({
grantee,
actions: ['sns:Publish'],
resourceArns: [this.topicArn],
resource: this,
});
if (this.masterKey) {
this.masterKey.grant(grantee, 'kms:Decrypt', 'kms:GenerateDataKey*');
}
return ret;
}

/**
Expand Down
Loading
Loading