/
queue-base.ts
343 lines (303 loc) · 9.15 KB
/
queue-base.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
import { Construct } from 'constructs';
import { QueuePolicy } from './policy';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import { IResource, Resource, ResourceProps } from '../../core';
/**
* Represents an SQS queue
*/
export interface IQueue extends IResource {
/**
* The ARN of this queue
* @attribute
*/
readonly queueArn: string;
/**
* The URL of this queue
* @attribute
*/
readonly queueUrl: string;
/**
* The name of this queue
* @attribute
*/
readonly queueName: string;
/**
* If this queue is server-side encrypted, this is the KMS encryption key.
*/
readonly encryptionMasterKey?: kms.IKey;
/**
* Whether this queue is an Amazon SQS FIFO queue. If false, this is a standard queue.
*/
readonly fifo: boolean;
/**
* Whether the contents of the queue are encrypted, and by what type of key.
*/
readonly encryptionType?: QueueEncryption;
/**
* Adds a statement to the IAM resource policy associated with this queue.
*
* If this queue was created in this stack (`new Queue`), a queue policy
* will be automatically created upon the first call to `addToPolicy`. If
* the queue is imported (`Queue.import`), then this is a no-op.
*/
addToResourcePolicy(statement: iam.PolicyStatement): iam.AddToResourcePolicyResult;
/**
* Grant permissions to consume messages from a queue
*
* This will grant the following permissions:
*
* - sqs:ChangeMessageVisibility
* - sqs:DeleteMessage
* - sqs:ReceiveMessage
* - sqs:GetQueueAttributes
* - sqs:GetQueueUrl
*
* @param grantee Principal to grant consume rights to
*/
grantConsumeMessages(grantee: iam.IGrantable): iam.Grant;
/**
* Grant access to send messages to a queue to the given identity.
*
* This will grant the following permissions:
*
* - sqs:SendMessage
* - sqs:GetQueueAttributes
* - sqs:GetQueueUrl
*
* @param grantee Principal to grant send rights to
*/
grantSendMessages(grantee: iam.IGrantable): iam.Grant;
/**
* Grant an IAM principal permissions to purge all messages from the queue.
*
* This will grant the following permissions:
*
* - sqs:PurgeQueue
* - sqs:GetQueueAttributes
* - sqs:GetQueueUrl
*
* @param grantee Principal to grant send rights to
*/
grantPurge(grantee: iam.IGrantable): iam.Grant;
/**
* Grant the actions defined in queueActions to the identity Principal given
* on this SQS queue resource.
*
* @param grantee Principal to grant right to
* @param queueActions The actions to grant
*/
grant(grantee: iam.IGrantable, ...queueActions: string[]): iam.Grant;
}
/**
* Reference to a new or existing Amazon SQS queue
*/
export abstract class QueueBase extends Resource implements IQueue {
/**
* The ARN of this queue
*/
public abstract readonly queueArn: string;
/**
* The URL of this queue
*/
public abstract readonly queueUrl: string;
/**
* The name of this queue
*/
public abstract readonly queueName: string;
/**
* If this queue is server-side encrypted, this is the KMS encryption key.
*/
public abstract readonly encryptionMasterKey?: kms.IKey;
/**
* Whether this queue is an Amazon SQS FIFO queue. If false, this is a standard queue.
*/
public abstract readonly fifo: boolean;
/**
* Whether the contents of the queue are encrypted, and by what type of key.
*/
public abstract readonly encryptionType?: QueueEncryption;
/**
* Controls automatic creation of policy objects.
*
* Set by subclasses.
*/
protected abstract readonly autoCreatePolicy: boolean;
private policy?: QueuePolicy;
constructor(scope: Construct, id: string, props: ResourceProps = {}) {
super(scope, id, props);
this.node.addValidation({ validate: () => this.policy?.document.validateForResourcePolicy() ?? [] });
}
/**
* Adds a statement to the IAM resource policy associated with this queue.
*
* If this queue was created in this stack (`new Queue`), a queue policy
* will be automatically created upon the first call to `addToPolicy`. If
* the queue is imported (`Queue.import`), then this is a no-op.
*/
public addToResourcePolicy(statement: iam.PolicyStatement): iam.AddToResourcePolicyResult {
if (!this.policy && this.autoCreatePolicy) {
this.policy = new QueuePolicy(this, 'Policy', { queues: [this] });
}
if (this.policy) {
this.policy.document.addStatements(statement);
return { statementAdded: true, policyDependable: this.policy };
}
return { statementAdded: false };
}
/**
* Grant permissions to consume messages from a queue
*
* This will grant the following permissions:
*
* - sqs:ChangeMessageVisibility
* - sqs:DeleteMessage
* - sqs:ReceiveMessage
* - sqs:GetQueueAttributes
* - sqs:GetQueueUrl
*
* If encryption is used, permission to use the key to decrypt the contents of the queue will also be granted to the same principal.
*
* This will grant the following KMS permissions:
*
* - kms:Decrypt
*
* @param grantee Principal to grant consume rights to
*/
public grantConsumeMessages(grantee: iam.IGrantable) {
const ret = this.grant(grantee,
'sqs:ReceiveMessage',
'sqs:ChangeMessageVisibility',
'sqs:GetQueueUrl',
'sqs:DeleteMessage',
'sqs:GetQueueAttributes');
if (this.encryptionMasterKey) {
this.encryptionMasterKey.grantDecrypt(grantee);
}
return ret;
}
/**
* Grant access to send messages to a queue to the given identity.
*
* This will grant the following permissions:
*
* - sqs:SendMessage
* - sqs:GetQueueAttributes
* - sqs:GetQueueUrl
*
* If encryption is used, permission to use the key to encrypt/decrypt the contents of the queue will also be granted to the same principal.
*
* This will grant the following KMS permissions:
*
* - kms:Decrypt
* - kms:Encrypt
* - kms:ReEncrypt*
* - kms:GenerateDataKey*
*
* @param grantee Principal to grant send rights to
*/
public grantSendMessages(grantee: iam.IGrantable) {
const ret = this.grant(grantee,
'sqs:SendMessage',
'sqs:GetQueueAttributes',
'sqs:GetQueueUrl');
if (this.encryptionMasterKey) {
// kms:Decrypt necessary to execute grantsendMessages to an SSE enabled SQS queue
this.encryptionMasterKey.grantEncryptDecrypt(grantee);
}
return ret;
}
/**
* Grant an IAM principal permissions to purge all messages from the queue.
*
* This will grant the following permissions:
*
* - sqs:PurgeQueue
* - sqs:GetQueueAttributes
* - sqs:GetQueueUrl
*
* @param grantee Principal to grant send rights to
*/
public grantPurge(grantee: iam.IGrantable) {
return this.grant(grantee,
'sqs:PurgeQueue',
'sqs:GetQueueAttributes',
'sqs:GetQueueUrl');
}
/**
* Grant the actions defined in queueActions to the identity Principal given
* on this SQS queue resource.
*
* @param grantee Principal to grant right to
* @param actions The actions to grant
*/
public grant(grantee: iam.IGrantable, ...actions: string[]) {
return iam.Grant.addToPrincipalOrResource({
grantee,
actions,
resourceArns: [this.queueArn],
resource: this,
});
}
}
/**
* Reference to a queue
*/
export interface QueueAttributes {
/**
* The ARN of the queue.
*/
readonly queueArn: string;
/**
* The URL of the queue.
* @see https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-queue-message-identifiers.html#sqs-general-identifiers
*
* @default - 'https://sqs.<region-endpoint>/<account-ID>/<queue-name>'
*/
readonly queueUrl?: string;
/**
* The name of the queue.
* @default if queue name is not specified, the name will be derived from the queue ARN
*/
readonly queueName?: string;
/**
* KMS encryption key, if this queue is server-side encrypted by a KMS key.
*
* @default - None
*/
readonly keyArn?: string;
/**
* Whether this queue is an Amazon SQS FIFO queue. If false, this is a standard queue.
*
* In case of a FIFO queue which is imported from a token, this value has to be explicitly set to true.
*
* @default - if fifo is not specified, the property will be determined based on the queue name (not possible for FIFO queues imported from a token)
*/
readonly fifo?: boolean;
}
/**
* What kind of encryption to apply to this queue
*/
export enum QueueEncryption {
/**
* Messages in the queue are not encrypted
*/
UNENCRYPTED = 'NONE',
/**
* Server-side KMS encryption with a KMS key managed by SQS.
*/
KMS_MANAGED = 'KMS_MANAGED',
/**
* Server-side encryption with a KMS key managed by the user.
*
* If `encryptionKey` is specified, this key will be used, otherwise, one will be defined.
*/
KMS = 'KMS',
/**
* Server-side encryption key managed by SQS (SSE-SQS).
*
* To learn more about SSE-SQS on Amazon SQS, please visit the
* [Amazon SQS documentation](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-server-side-encryption.html).
*/
SQS_MANAGED = 'SQS_MANAGED',
}