-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
topic.ts
354 lines (310 loc) · 10.3 KB
/
topic.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import { Construct } from 'constructs';
import { CfnTopic } from './sns.generated';
import { ITopic, TopicBase } from './topic-base';
import { IRole } from '../../aws-iam';
import { IKey } from '../../aws-kms';
import { ArnFormat, Lazy, Names, Stack, Token } from '../../core';
/**
* Properties for a new SNS topic
*/
export interface TopicProps {
/**
* A developer-defined string that can be used to identify this SNS topic.
*
* The display name must be maximum 100 characters long, including hyphens (-),
* underscores (_), spaces, and tabs.
*
* @default None
*/
readonly displayName?: string;
/**
* A name for the topic.
*
* If you don't specify a name, AWS CloudFormation generates a unique
* physical ID and uses that ID for the topic name. For more information,
* see Name Type.
*
* @default Generated name
*/
readonly topicName?: string;
/**
* A KMS Key, either managed by this CDK app, or imported.
*
* @default None
*/
readonly masterKey?: IKey;
/**
* Enables content-based deduplication for FIFO topics.
*
* @default None
*/
readonly contentBasedDeduplication?: boolean;
/**
* Set to true to create a FIFO topic.
*
* @default None
*/
readonly fifo?: boolean;
/**
* The list of delivery status logging configurations for the topic.
*
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-topic-attributes.html.
*
* @default None
*/
readonly loggingConfigs?: LoggingConfig[];
/**
* The number of days Amazon SNS retains messages.
*
* It can only be set for FIFO topics.
*
* @see https://docs.aws.amazon.com/sns/latest/dg/fifo-message-archiving-replay.html
*
* @default - do not archive messages
*/
readonly messageRetentionPeriodInDays?: number;
/**
* Adds a statement to enforce encryption of data in transit when publishing to the topic.
*
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-security-best-practices.html#enforce-encryption-data-in-transit.
*
* @default false
*/
readonly enforceSSL?: boolean;
/**
* The signature version corresponds to the hashing algorithm used while creating the signature of the notifications,
* subscription confirmations, or unsubscribe confirmation messages sent by Amazon SNS.
*
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-verify-signature-of-message.html.
*
* @default 1
*/
readonly signatureVersion?: string;
/**
* Tracing mode of an Amazon SNS topic.
*
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-active-tracing.html
*
* @default TracingConfig.PASS_THROUGH
*/
readonly tracingConfig?: TracingConfig;
}
/**
* A logging configuration for delivery status of messages sent from SNS topic to subscribed endpoints.
*
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-topic-attributes.html.
*/
export interface LoggingConfig {
/**
* Indicates one of the supported protocols for the SNS topic.
*/
readonly protocol: LoggingProtocol;
/**
* The IAM role to be used when logging failed message deliveries in Amazon CloudWatch.
*
* @default None
*/
readonly failureFeedbackRole?: IRole;
/**
* The IAM role to be used when logging successful message deliveries in Amazon CloudWatch.
*
* @default None
*/
readonly successFeedbackRole?: IRole;
/**
* The percentage of successful message deliveries to be logged in Amazon CloudWatch.
*
* Valid values are integer between 0-100
*
* @default None
*/
readonly successFeedbackSampleRate?: number;
}
/**
* The type of supported protocol for delivery status logging.
*/
export enum LoggingProtocol {
/**
* HTTP
*/
HTTP = 'http/s',
/**
* Amazon Simple Queue Service
*/
SQS = 'sqs',
/**
* AWS Lambda
*/
LAMBDA = 'lambda',
/**
* Amazon Kinesis Data Firehose
*/
FIREHOSE = 'firehose',
/**
* Platform application endpoint
*/
APPLICATION = 'application',
}
/**
* The tracing mode of an Amazon SNS topic
*/
export enum TracingConfig {
/**
* The mode that topic passes trace headers received from the Amazon SNS publisher to its subscription.
*/
PASS_THROUGH = 'PassThrough',
/**
* The mode that Amazon SNS vend X-Ray segment data to topic owner account if the sampled flag in the tracing header is true.
*/
ACTIVE = 'Active',
}
/**
* Represents an SNS topic defined outside of this stack.
*/
export interface TopicAttributes {
/**
* The ARN of the SNS topic.
*/
readonly topicArn: string;
/**
* Whether content-based deduplication is enabled.
* Only applicable for FIFO topics.
*
* @default false
*/
readonly contentBasedDeduplication?: boolean;
}
/**
* A new SNS topic
*/
export class Topic extends TopicBase {
/**
* Import an existing SNS topic provided an ARN
*
* @param scope The parent creating construct
* @param id The construct's name
* @param topicArn topic ARN (i.e. arn:aws:sns:us-east-2:444455556666:MyTopic)
*/
public static fromTopicArn(scope: Construct, id: string, topicArn: string): ITopic {
return Topic.fromTopicAttributes(scope, id, { topicArn });
};
/**
* Import an existing SNS topic provided a topic attributes
*
* @param scope The parent creating construct
* @param id The construct's name
* @param attrs the attributes of the topic to import
*/
public static fromTopicAttributes(scope: Construct, id: string, attrs: TopicAttributes): ITopic {
const topicName = Stack.of(scope).splitArn(attrs.topicArn, ArnFormat.NO_RESOURCE_NAME).resource;
const fifo = topicName.endsWith('.fifo');
if (attrs.contentBasedDeduplication && !fifo) {
throw new Error('Cannot import topic; contentBasedDeduplication is only available for FIFO SNS topics.');
}
class Import extends TopicBase {
public readonly topicArn = attrs.topicArn;
public readonly topicName = topicName;
public readonly fifo = fifo;
public readonly contentBasedDeduplication = attrs.contentBasedDeduplication || false;
protected autoCreatePolicy: boolean = false;
}
return new Import(scope, id, {
environmentFromArn: attrs.topicArn,
});
}
public readonly topicArn: string;
public readonly topicName: string;
public readonly contentBasedDeduplication: boolean;
public readonly fifo: boolean;
protected readonly autoCreatePolicy: boolean = true;
private readonly loggingConfigs: LoggingConfig[] = [];
constructor(scope: Construct, id: string, props: TopicProps = {}) {
super(scope, id, {
physicalName: props.topicName,
});
this.enforceSSL = props.enforceSSL;
if (props.contentBasedDeduplication && !props.fifo) {
throw new Error('Content based deduplication can only be enabled for FIFO SNS topics.');
}
if (props.messageRetentionPeriodInDays && !props.fifo) {
throw new Error('`messageRetentionPeriodInDays` is only valid for FIFO SNS topics.');
}
if (
props.messageRetentionPeriodInDays !== undefined
&& !Token.isUnresolved(props.messageRetentionPeriodInDays)
&& (!Number.isInteger(props.messageRetentionPeriodInDays) || props.messageRetentionPeriodInDays > 365 || props.messageRetentionPeriodInDays < 1)
) {
throw new Error('`messageRetentionPeriodInDays` must be an integer between 1 and 365');
}
if (props.loggingConfigs) {
props.loggingConfigs.forEach(c => this.addLoggingConfig(c));
}
let cfnTopicName: string;
if (props.fifo && props.topicName && !props.topicName.endsWith('.fifo')) {
cfnTopicName = this.physicalName + '.fifo';
} else if (props.fifo && !props.topicName) {
// Max length allowed by CloudFormation is 256, we subtract 5 to allow for ".fifo" suffix
const prefixName = Names.uniqueResourceName(this, {
maxLength: 256 - 5,
separator: '-',
});
cfnTopicName = `${prefixName}.fifo`;
} else {
cfnTopicName = this.physicalName;
}
if (
props.signatureVersion &&
!Token.isUnresolved(props.signatureVersion) &&
props.signatureVersion !== '1' &&
props.signatureVersion !== '2'
) {
throw new Error(`signatureVersion must be "1" or "2", received: "${props.signatureVersion}"`);
}
if (props.displayName && !Token.isUnresolved(props.displayName) && props.displayName.length > 100) {
throw new Error(`displayName must be less than or equal to 100 characters, got ${props.displayName.length}`);
}
const resource = new CfnTopic(this, 'Resource', {
archivePolicy: props.messageRetentionPeriodInDays ? {
MessageRetentionPeriod: props.messageRetentionPeriodInDays,
} : undefined,
displayName: props.displayName,
topicName: cfnTopicName,
kmsMasterKeyId: props.masterKey && props.masterKey.keyArn,
contentBasedDeduplication: props.contentBasedDeduplication,
fifoTopic: props.fifo,
signatureVersion: props.signatureVersion,
deliveryStatusLogging: Lazy.any({ produce: () => this.renderLoggingConfigs() }, { omitEmptyArray: true }),
tracingConfig: props.tracingConfig,
});
this.topicArn = this.getResourceArnAttribute(resource.ref, {
service: 'sns',
resource: this.physicalName,
});
this.topicName = this.getResourceNameAttribute(resource.attrTopicName);
this.fifo = props.fifo || false;
this.contentBasedDeduplication = props.contentBasedDeduplication || false;
}
private renderLoggingConfigs(): CfnTopic.LoggingConfigProperty[] {
return this.loggingConfigs.map(renderLoggingConfig);
function renderLoggingConfig(spec: LoggingConfig): CfnTopic.LoggingConfigProperty {
if (spec.successFeedbackSampleRate !== undefined) {
const rate = spec.successFeedbackSampleRate;
if (!Number.isInteger(rate) || rate < 0 || rate > 100) {
throw new Error('Success feedback sample rate must be an integer between 0 and 100');
}
}
return {
protocol: spec.protocol,
failureFeedbackRoleArn: spec.failureFeedbackRole?.roleArn,
successFeedbackRoleArn: spec.successFeedbackRole?.roleArn,
successFeedbackSampleRate: spec.successFeedbackSampleRate?.toString(),
};
}
}
/**
* Adds a delivery status logging configuration to the topic.
*/
public addLoggingConfig(config: LoggingConfig) {
this.loggingConfigs.push(config);
}
}