-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathqueue-base.ts
311 lines (277 loc) · 8.57 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
import autoscaling_api = require('@aws-cdk/aws-autoscaling-api');
import iam = require('@aws-cdk/aws-iam');
import kms = require('@aws-cdk/aws-kms');
import s3n = require('@aws-cdk/aws-s3-notifications');
import { IResource, Resource } from '@aws-cdk/cdk';
import { QueuePolicy } from './policy';
export interface IQueue extends IResource, s3n.IBucketNotificationDestination, autoscaling_api.ILifecycleHookTarget {
/**
* 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.IEncryptionKey;
/**
* Export a queue
*/
export(): QueueAttributes;
/**
* 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 improted (`Queue.import`), then this is a no-op.
*/
addToResourcePolicy(statement: iam.PolicyStatement): void;
/**
* Grant permissions to consume messages from a queue
*
* This will grant the following permissions:
*
* - sqs:ChangeMessageVisibility
* - sqs:ChangeMessageVisibilityBatch
* - sqs:DeleteMessage
* - sqs:ReceiveMessage
* - sqs:DeleteMessageBatch
* - 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:SendMessageBatch
* - 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.IEncryptionKey;
/**
* Controls automatic creation of policy objects.
*
* Set by subclasses.
*/
protected abstract readonly autoCreatePolicy: boolean;
private policy?: QueuePolicy;
/**
* The set of S3 bucket IDs that are allowed to send notifications to this queue.
*/
private readonly notifyingBuckets = new Set<string>();
/**
* Export a queue
*/
public abstract export(): QueueAttributes;
/**
* 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 improted (`Queue.import`), then this is a no-op.
*/
public addToResourcePolicy(statement: iam.PolicyStatement) {
if (!this.policy && this.autoCreatePolicy) {
this.policy = new QueuePolicy(this, 'Policy', { queues: [ this ] });
}
if (this.policy) {
this.policy.document.addStatement(statement);
}
}
/**
* Allows using SQS queues as destinations for bucket notifications.
* Use `bucket.onEvent(event, queue)` to subscribe.
* @param bucketArn The ARN of the notifying bucket.
* @param bucketId A unique ID for the notifying bucket.
*/
public asBucketNotificationDestination(bucketArn: string, bucketId: string): s3n.BucketNotificationDestinationProps {
if (!this.notifyingBuckets.has(bucketId)) {
this.addToResourcePolicy(new iam.PolicyStatement()
.addServicePrincipal('s3.amazonaws.com')
.addAction('sqs:SendMessage')
.addResource(this.queueArn)
.addCondition('ArnLike', { 'aws:SourceArn': bucketArn }));
// if this queue is encrypted, we need to allow S3 to read messages since that's how
// it verifies that the notification destination configuration is valid.
// by setting allowNoOp to false, we ensure that only custom keys that we can actually
// control access to can be used here as described in:
// https://docs.aws.amazon.com/AmazonS3/latest/dev/ways-to-add-notification-config-to-bucket.html
if (this.encryptionMasterKey) {
this.encryptionMasterKey.addToResourcePolicy(new iam.PolicyStatement()
.addServicePrincipal('s3.amazonaws.com')
.addAction('kms:GenerateDataKey')
.addAction('kms:Decrypt')
.addAllResources(), /* allowNoOp */ false);
}
this.notifyingBuckets.add(bucketId);
}
return {
arn: this.queueArn,
type: s3n.BucketNotificationDestinationType.Queue,
dependencies: [ this.policy! ]
};
}
/**
* Allow using SQS queues as lifecycle hook targets
*/
public asLifecycleHookTarget(lifecycleHook: autoscaling_api.ILifecycleHook): autoscaling_api.LifecycleHookTargetProps {
this.grantSendMessages(lifecycleHook.role);
return { notificationTargetArn: this.queueArn };
}
/**
* Grant permissions to consume messages from a queue
*
* This will grant the following permissions:
*
* - sqs:ChangeMessageVisibility
* - sqs:ChangeMessageVisibilityBatch
* - sqs:DeleteMessage
* - sqs:ReceiveMessage
* - sqs:DeleteMessageBatch
* - sqs:GetQueueAttributes
* - sqs:GetQueueUrl
*
* @param grantee Principal to grant consume rights to
*/
public grantConsumeMessages(grantee: iam.IGrantable) {
return this.grant(grantee,
'sqs:ReceiveMessage',
'sqs:ChangeMessageVisibility',
'sqs:ChangeMessageVisibilityBatch',
'sqs:GetQueueUrl',
'sqs:DeleteMessage',
'sqs:DeleteMessageBatch',
'sqs:GetQueueAttributes');
}
/**
* Grant access to send messages to a queue to the given identity.
*
* This will grant the following permissions:
*
* - sqs:SendMessage
* - sqs:SendMessageBatch
* - sqs:GetQueueAttributes
* - sqs:GetQueueUrl
*
* @param grantee Principal to grant send rights to
*/
public grantSendMessages(grantee: iam.IGrantable) {
return this.grant(grantee,
'sqs:SendMessage',
'sqs:SendMessageBatch',
'sqs:GetQueueAttributes',
'sqs:GetQueueUrl');
}
/**
* 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.
*/
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.
*/
readonly keyArn?: string;
}