Skip to content

Commit

Permalink
fix(sns): for SSE topics, add KMS permissions in grantPublish
Browse files Browse the repository at this point in the history
  • Loading branch information
lightningboltemoji committed Jan 11, 2025
1 parent 49eb9d3 commit 469a884
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 30 deletions.

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', {
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.
*
* 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

0 comments on commit 469a884

Please sign in to comment.