-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathevent-bus.ts
422 lines (371 loc) · 12.8 KB
/
event-bus.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import { Construct } from 'constructs';
import { Archive, BaseArchiveProps } from './archive';
import { CfnEventBus, CfnEventBusPolicy } from './events.generated';
import * as iam from '../../aws-iam';
import { ArnFormat, IResource, Lazy, Names, Resource, Stack, Token } from '../../core';
/**
* Interface which all EventBus based classes MUST implement
*/
export interface IEventBus extends IResource {
/**
* The physical ID of this event bus resource
*
* @attribute
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#cfn-events-eventbus-name
*/
readonly eventBusName: string;
/**
* The ARN of this event bus resource
*
* @attribute
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#Arn-fn::getatt
*/
readonly eventBusArn: string;
/**
* The JSON policy of this event bus resource
*
* @attribute
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#Policy-fn::getatt
*/
readonly eventBusPolicy: string;
/**
* The partner event source to associate with this event bus resource
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#cfn-events-eventbus-eventsourcename
*/
readonly eventSourceName?: string;
/**
* Create an EventBridge archive to send events to.
* When you create an archive, incoming events might not immediately start being sent to the archive.
* Allow a short period of time for changes to take effect.
*
* @param props Properties of the archive
*/
archive(id: string, props: BaseArchiveProps): Archive;
/**
* Grants an IAM Principal to send custom events to the eventBus
* so that they can be matched to rules.
*
* @param grantee The principal (no-op if undefined)
*/
grantPutEventsTo(grantee: iam.IGrantable): iam.Grant;
}
/**
* Properties to define an event bus
*/
export interface EventBusProps {
/**
* The name of the event bus you are creating
* Note: If 'eventSourceName' is passed in, you cannot set this
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#cfn-events-eventbus-name
* @default - automatically generated name
*/
readonly eventBusName?: string;
/**
* The partner event source to associate with this event bus resource
* Note: If 'eventBusName' is passed in, you cannot set this
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#cfn-events-eventbus-eventsourcename
* @default - no partner event source
*/
readonly eventSourceName?: string;
}
/**
* Interface with properties necessary to import a reusable EventBus
*/
export interface EventBusAttributes {
/**
* The physical ID of this event bus resource
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#cfn-events-eventbus-name
*/
readonly eventBusName: string;
/**
* The ARN of this event bus resource
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#Arn-fn::getatt
*/
readonly eventBusArn: string;
/**
* The JSON policy of this event bus resource
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#Policy-fn::getatt
*/
readonly eventBusPolicy: string;
/**
* The partner event source to associate with this event bus resource
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#cfn-events-eventbus-eventsourcename
* @default - no partner event source
*/
readonly eventSourceName?: string;
}
abstract class EventBusBase extends Resource implements IEventBus {
/**
* The physical ID of this event bus resource
*/
public abstract readonly eventBusName: string;
/**
* The ARN of the event bus, such as:
* arn:aws:events:us-east-2:123456789012:event-bus/aws.partner/PartnerName/acct1/repo1.
*/
public abstract readonly eventBusArn: string;
/**
* The policy for the event bus in JSON form.
*/
public abstract readonly eventBusPolicy: string;
/**
* The name of the partner event source
*/
public abstract readonly eventSourceName?: string;
public archive(id: string, props: BaseArchiveProps): Archive {
return new Archive(this, id, {
sourceEventBus: this,
description: props.description || `Event Archive for ${this.eventBusName} Event Bus`,
eventPattern: props.eventPattern,
retention: props.retention,
archiveName: props.archiveName,
});
}
public grantPutEventsTo(grantee: iam.IGrantable): iam.Grant {
return iam.Grant.addToPrincipal({
grantee,
actions: ['events:PutEvents'],
resourceArns: [this.eventBusArn],
});
}
}
/**
* Define an EventBridge EventBus
*
* @resource AWS::Events::EventBus
*/
export class EventBus extends EventBusBase {
/**
* Import an existing event bus resource
* @param scope Parent construct
* @param id Construct ID
* @param eventBusArn ARN of imported event bus
*/
public static fromEventBusArn(scope: Construct, id: string, eventBusArn: string): IEventBus {
const parts = Stack.of(scope).splitArn(eventBusArn, ArnFormat.SLASH_RESOURCE_NAME);
return new ImportedEventBus(scope, id, {
eventBusArn: eventBusArn,
eventBusName: parts.resourceName || '',
eventBusPolicy: '',
});
}
/**
* Import an existing event bus resource
* @param scope Parent construct
* @param id Construct ID
* @param eventBusName Name of imported event bus
*/
public static fromEventBusName(scope: Construct, id: string, eventBusName: string): IEventBus {
const eventBusArn = Stack.of(scope).formatArn({
resource: 'event-bus',
service: 'events',
resourceName: eventBusName,
});
return EventBus.fromEventBusAttributes(scope, id, {
eventBusName: eventBusName,
eventBusArn: eventBusArn,
eventBusPolicy: '',
});
}
/**
* Import an existing event bus resource
* @param scope Parent construct
* @param id Construct ID
* @param attrs Imported event bus properties
*/
public static fromEventBusAttributes(scope: Construct, id: string, attrs: EventBusAttributes): IEventBus {
return new ImportedEventBus(scope, id, attrs);
}
/**
* Permits an IAM Principal to send custom events to EventBridge
* so that they can be matched to rules.
*
* @param grantee The principal (no-op if undefined)
* @deprecated use grantAllPutEvents instead
*/
public static grantPutEvents(grantee: iam.IGrantable): iam.Grant {
// It's currently not possible to restrict PutEvents to specific resources.
// See https://docs.aws.amazon.com/eventbridge/latest/userguide/permissions-reference-eventbridge.html
return iam.Grant.addToPrincipal({
grantee,
actions: ['events:PutEvents'],
resourceArns: ['*'],
});
}
/**
* Permits an IAM Principal to send custom events to EventBridge
* so that they can be matched to rules.
*
* @param grantee The principal (no-op if undefined)
*/
public static grantAllPutEvents(grantee: iam.IGrantable): iam.Grant {
return iam.Grant.addToPrincipal({
grantee,
actions: ['events:PutEvents'],
resourceArns: ['*'],
});
}
private static eventBusProps(defaultEventBusName: string, props: EventBusProps = {}) {
const { eventBusName, eventSourceName } = props;
const eventBusNameRegex = /^[\/\.\-_A-Za-z0-9]{1,256}$/;
if (eventBusName !== undefined && eventSourceName !== undefined) {
throw new Error(
'\'eventBusName\' and \'eventSourceName\' cannot both be provided',
);
}
if (eventBusName !== undefined) {
if (!Token.isUnresolved(eventBusName)) {
if (eventBusName === 'default') {
throw new Error(
'\'eventBusName\' must not be \'default\'',
);
} else if (eventBusName.indexOf('/') > -1) {
throw new Error(
'\'eventBusName\' must not contain \'/\'',
);
} else if (!eventBusNameRegex.test(eventBusName)) {
throw new Error(
`'eventBusName' must satisfy: ${eventBusNameRegex}`,
);
}
}
return { eventBusName };
}
if (eventSourceName !== undefined) {
if (!Token.isUnresolved(eventSourceName)) {
// Ex: aws.partner/PartnerName/acct1/repo1
const eventSourceNameRegex = /^aws\.partner(\/[\.\-_A-Za-z0-9]+){2,}$/;
if (!eventSourceNameRegex.test(eventSourceName)) {
throw new Error(
`'eventSourceName' must satisfy: ${eventSourceNameRegex}`,
);
} else if (!eventBusNameRegex.test(eventSourceName)) {
throw new Error(
`'eventSourceName' must satisfy: ${eventBusNameRegex}`,
);
}
}
return { eventBusName: eventSourceName, eventSourceName };
}
return { eventBusName: defaultEventBusName };
}
/**
* The physical ID of this event bus resource
*/
public readonly eventBusName: string;
/**
* The ARN of the event bus, such as:
* arn:aws:events:us-east-2:123456789012:event-bus/aws.partner/PartnerName/acct1/repo1.
*/
public readonly eventBusArn: string;
/**
* The policy for the event bus in JSON form.
*/
public readonly eventBusPolicy: string;
/**
* The name of the partner event source
*/
public readonly eventSourceName?: string;
constructor(scope: Construct, id: string, props?: EventBusProps) {
const { eventBusName, eventSourceName } = EventBus.eventBusProps(
Lazy.string({ produce: () => Names.uniqueId(this) }),
props,
);
super(scope, id, { physicalName: eventBusName });
const eventBus = new CfnEventBus(this, 'Resource', {
name: this.physicalName,
eventSourceName,
});
this.eventBusArn = this.getResourceArnAttribute(eventBus.attrArn, {
service: 'events',
resource: 'event-bus',
resourceName: eventBus.name,
});
this.eventBusName = this.getResourceNameAttribute(eventBus.ref);
this.eventBusPolicy = eventBus.attrPolicy;
this.eventSourceName = eventBus.eventSourceName;
}
/**
* Adds a statement to the IAM resource policy associated with this event bus.
*/
public addToResourcePolicy(statement: iam.PolicyStatement): iam.AddToResourcePolicyResult {
if (statement.sid == null) {
throw new Error('Event Bus policy statements must have a sid');
}
// In order to generate new statementIDs for the change in https://github.com/aws/aws-cdk/pull/27340
const statementId = `cdk-${statement.sid}`.slice(0, 64);
statement.sid = statementId;
const policy = new EventBusPolicy(this, statementId, {
eventBus: this,
statement: statement.toJSON(),
statementId,
});
return { statementAdded: true, policyDependable: policy };
}
}
class ImportedEventBus extends EventBusBase {
public readonly eventBusArn: string;
public readonly eventBusName: string;
public readonly eventBusPolicy: string;
public readonly eventSourceName?: string;
constructor(scope: Construct, id: string, attrs: EventBusAttributes) {
const arnParts = Stack.of(scope).splitArn(attrs.eventBusArn, ArnFormat.SLASH_RESOURCE_NAME);
super(scope, id, {
account: arnParts.account,
region: arnParts.region,
});
this.eventBusArn = attrs.eventBusArn;
this.eventBusName = attrs.eventBusName;
this.eventBusPolicy = attrs.eventBusPolicy;
this.eventSourceName = attrs.eventSourceName;
}
}
/**
* Properties to associate Event Buses with a policy
*/
export interface EventBusPolicyProps {
/**
* The event bus to which the policy applies
*/
readonly eventBus: IEventBus;
/**
* An IAM Policy Statement to apply to the Event Bus
*/
readonly statement: iam.PolicyStatement;
/**
* An identifier string for the external account that
* you are granting permissions to.
*/
readonly statementId: string;
}
/**
* The policy for an Event Bus
*
* Policies define the operations that are allowed on this resource.
*
* You almost never need to define this construct directly.
*
* All AWS resources that support resource policies have a method called
* `addToResourcePolicy()`, which will automatically create a new resource
* policy if one doesn't exist yet, otherwise it will add to the existing
* policy.
*
* Prefer to use `addToResourcePolicy()` instead.
*/
export class EventBusPolicy extends Resource {
constructor(scope: Construct, id: string, props: EventBusPolicyProps) {
super(scope, id);
new CfnEventBusPolicy(this, 'Resource', {
statementId: props.statementId!,
statement: props.statement,
eventBusName: props.eventBus.eventBusName,
});
}
}