Skip to content

Commit dffedca

Browse files
authored
feat(sns): add signature version prop (#29543)
Closes #29539. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent fe4bc1d commit dffedca

File tree

10 files changed

+110
-11
lines changed

10 files changed

+110
-11
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-sns/test/integ.sns.js.snapshot/SNSInteg.assets.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-sns/test/integ.sns.js.snapshot/SNSInteg.template.json

+8
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@
110110
}
111111
]
112112
}
113+
},
114+
"MyTopicSignatureVersionEDDB6A3B": {
115+
"Type": "AWS::SNS::Topic",
116+
"Properties": {
117+
"DisplayName": "fooDisplayNameSignatureVersion",
118+
"SignatureVersion": "2",
119+
"TopicName": "fooTopicSignatureVersion"
120+
}
113121
}
114122
},
115123
"Parameters": {

packages/@aws-cdk-testing/framework-integ/test/aws-sns/test/integ.sns.js.snapshot/cdk.out

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-sns/test/integ.sns.js.snapshot/integ.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-sns/test/integ.sns.js.snapshot/manifest.json

+8-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-sns/test/integ.sns.js.snapshot/tree.json

+26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-sns/test/integ.sns.ts

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ class SNSInteg extends Stack {
4040
successFeedbackRole: feedbackRole,
4141
successFeedbackSampleRate: 50,
4242
});
43+
44+
new Topic(this, 'MyTopicSignatureVersion', {
45+
topicName: 'fooTopicSignatureVersion',
46+
displayName: 'fooDisplayNameSignatureVersion',
47+
signatureVersion: '2',
48+
});
4349
}
4450
}
4551

packages/aws-cdk-lib/aws-sns/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ const topic = new sns.Topic(this, 'Topic', {
1919
});
2020
```
2121

22+
Add an SNS Topic to your stack with a specified signature version, which corresponds
23+
to the hashing algorithm used while creating the signature of the notifications,
24+
subscription confirmations, or unsubscribe confirmation messages sent by Amazon SNS.
25+
26+
The default signature version is `1` (`SHA1`).
27+
SNS also supports signature version `2` (`SHA256`).
28+
29+
```ts
30+
const topic = new sns.Topic(this, 'Topic', {
31+
signatureVersion: '2',
32+
});
33+
```
34+
2235
Note that FIFO topics require a topic name to be provided. The required `.fifo` suffix will be automatically generated and added to the topic name if it is not explicitly provided.
2336

2437
## Subscriptions

packages/aws-cdk-lib/aws-sns/lib/topic.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export interface TopicProps {
5151
/**
5252
* The list of delivery status logging configurations for the topic.
5353
*
54-
* For more information, see https://docs.aws.amazon.com/sns/latest/dg/sns-topic-attributes.html.
54+
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-topic-attributes.html.
5555
*
5656
* @default None
5757
*/
@@ -71,17 +71,27 @@ export interface TopicProps {
7171
/**
7272
* Adds a statement to enforce encryption of data in transit when publishing to the topic.
7373
*
74-
* For more information, see https://docs.aws.amazon.com/sns/latest/dg/sns-security-best-practices.html#enforce-encryption-data-in-transit.
74+
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-security-best-practices.html#enforce-encryption-data-in-transit.
7575
*
7676
* @default false
7777
*/
7878
readonly enforceSSL?: boolean;
79+
80+
/**
81+
* The signature version corresponds to the hashing algorithm used while creating the signature of the notifications,
82+
* subscription confirmations, or unsubscribe confirmation messages sent by Amazon SNS.
83+
*
84+
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-verify-signature-of-message.html.
85+
*
86+
* @default 1
87+
*/
88+
readonly signatureVersion?: string;
7989
}
8090

8191
/**
8292
* A logging configuration for delivery status of messages sent from SNS topic to subscribed endpoints.
8393
*
84-
* For more information, see https://docs.aws.amazon.com/sns/latest/dg/sns-topic-attributes.html.
94+
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-topic-attributes.html.
8595
*/
8696
export interface LoggingConfig {
8797
/**
@@ -207,7 +217,7 @@ export class Topic extends TopicBase {
207217
if (props.fifo && props.topicName && !props.topicName.endsWith('.fifo')) {
208218
cfnTopicName = this.physicalName + '.fifo';
209219
} else if (props.fifo && !props.topicName) {
210-
// Max lenght allowed by CloudFormation is 256, we subtract 5 to allow for ".fifo" suffix
220+
// Max length allowed by CloudFormation is 256, we subtract 5 to allow for ".fifo" suffix
211221
const prefixName = Names.uniqueResourceName(this, {
212222
maxLength: 256 - 5,
213223
separator: '-',
@@ -217,6 +227,15 @@ export class Topic extends TopicBase {
217227
cfnTopicName = this.physicalName;
218228
}
219229

230+
if (
231+
props.signatureVersion &&
232+
!Token.isUnresolved(props.signatureVersion) &&
233+
props.signatureVersion !== '1' &&
234+
props.signatureVersion !== '2'
235+
) {
236+
throw new Error(`signatureVersion must be "1" or "2", received: "${props.signatureVersion}"`);
237+
}
238+
220239
const resource = new CfnTopic(this, 'Resource', {
221240
archivePolicy: props.messageRetentionPeriodInDays ? {
222241
MessageRetentionPeriod: props.messageRetentionPeriodInDays,
@@ -226,6 +245,7 @@ export class Topic extends TopicBase {
226245
kmsMasterKeyId: props.masterKey && props.masterKey.keyArn,
227246
contentBasedDeduplication: props.contentBasedDeduplication,
228247
fifoTopic: props.fifo,
248+
signatureVersion: props.signatureVersion,
229249
deliveryStatusLogging: Lazy.any({ produce: () => this.renderLoggingConfigs() }, { omitEmptyArray: true }),
230250
});
231251

packages/aws-cdk-lib/aws-sns/test/sns.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,26 @@ describe('Topic', () => {
156156
})).toThrow(/Content based deduplication can only be enabled for FIFO SNS topics./);
157157

158158
});
159+
160+
test('specify signatureVersion', () => {
161+
const stack = new cdk.Stack();
162+
163+
new sns.Topic(stack, 'MyTopic', {
164+
signatureVersion: '2',
165+
});
166+
167+
Template.fromStack(stack).hasResourceProperties('AWS::SNS::Topic', {
168+
'SignatureVersion': '2',
169+
});
170+
});
171+
172+
test('throw with incorrect signatureVersion', () => {
173+
const stack = new cdk.Stack();
174+
175+
expect(() => new sns.Topic(stack, 'MyTopic', {
176+
signatureVersion: '3',
177+
})).toThrow(/signatureVersion must be "1" or "2", received: "3"/);
178+
});
159179
});
160180

161181
test('can add a policy to the topic', () => {

0 commit comments

Comments
 (0)