@@ -2,7 +2,7 @@ import * as iam from '@aws-cdk/aws-iam';
2
2
import { ArnFormat , IResource , Lazy , Names , Resource , Stack , Token } from '@aws-cdk/core' ;
3
3
import { Construct } from 'constructs' ;
4
4
import { Archive , BaseArchiveProps } from './archive' ;
5
- import { CfnEventBus } from './events.generated' ;
5
+ import { CfnEventBus , CfnEventBusPolicy } from './events.generated' ;
6
6
7
7
/**
8
8
* Interface which all EventBus based classes MUST implement
@@ -309,6 +309,8 @@ export class EventBus extends EventBusBase {
309
309
*/
310
310
public readonly eventSourceName ?: string ;
311
311
312
+ private policy ?: EventBusPolicy ;
313
+
312
314
constructor ( scope : Construct , id : string , props ?: EventBusProps ) {
313
315
const { eventBusName, eventSourceName } = EventBus . eventBusProps (
314
316
Lazy . string ( { produce : ( ) => Names . uniqueId ( this ) } ) ,
@@ -332,13 +334,36 @@ export class EventBus extends EventBusBase {
332
334
this . eventBusPolicy = eventBus . attrPolicy ;
333
335
this . eventSourceName = eventBus . eventSourceName ;
334
336
}
337
+
338
+ /**
339
+ * Adds a statement to the IAM resource policy associated with this event bus.
340
+ */
341
+ public addToResourcePolicy ( statement : iam . PolicyStatement ) : iam . AddToResourcePolicyResult {
342
+ if ( statement . sid == null ) {
343
+ throw new Error ( 'Event Bus policy statements must have a sid' ) ;
344
+ }
345
+
346
+ if ( this . policy ) {
347
+ // The policy can contain only one statement
348
+ return { statementAdded : false } ;
349
+ }
350
+
351
+ this . policy = new EventBusPolicy ( this , 'Policy' , {
352
+ eventBus : this ,
353
+ statement : statement . toJSON ( ) ,
354
+ statementId : statement . sid ,
355
+ } ) ;
356
+
357
+ return { statementAdded : true , policyDependable : this . policy } ;
358
+ }
335
359
}
336
360
337
361
class ImportedEventBus extends EventBusBase {
338
362
public readonly eventBusArn : string ;
339
363
public readonly eventBusName : string ;
340
364
public readonly eventBusPolicy : string ;
341
365
public readonly eventSourceName ?: string ;
366
+
342
367
constructor ( scope : Construct , id : string , attrs : EventBusAttributes ) {
343
368
const arnParts = Stack . of ( scope ) . splitArn ( attrs . eventBusArn , ArnFormat . SLASH_RESOURCE_NAME ) ;
344
369
super ( scope , id , {
@@ -352,3 +377,50 @@ class ImportedEventBus extends EventBusBase {
352
377
this . eventSourceName = attrs . eventSourceName ;
353
378
}
354
379
}
380
+
381
+ /**
382
+ * Properties to associate Event Buses with a policy
383
+ */
384
+ export interface EventBusPolicyProps {
385
+ /**
386
+ * The event bus to which the policy applies
387
+ */
388
+ readonly eventBus : IEventBus ;
389
+
390
+ /**
391
+ * An IAM Policy Statement to apply to the Event Bus
392
+ */
393
+ readonly statement : iam . PolicyStatement ;
394
+
395
+ /**
396
+ * An identifier string for the external account that
397
+ * you are granting permissions to.
398
+ */
399
+ readonly statementId : string ;
400
+ }
401
+
402
+ /**
403
+ * The policy for an Event Bus
404
+ *
405
+ * Policies define the operations that are allowed on this resource.
406
+ *
407
+ * You almost never need to define this construct directly.
408
+ *
409
+ * All AWS resources that support resource policies have a method called
410
+ * `addToResourcePolicy()`, which will automatically create a new resource
411
+ * policy if one doesn't exist yet, otherwise it will add to the existing
412
+ * policy.
413
+ *
414
+ * Prefer to use `addToResourcePolicy()` instead.
415
+ */
416
+ export class EventBusPolicy extends Resource {
417
+ constructor ( scope : Construct , id : string , props : EventBusPolicyProps ) {
418
+ super ( scope , id ) ;
419
+
420
+ new CfnEventBusPolicy ( this , 'Resource' , {
421
+ statementId : props . statementId ! ,
422
+ statement : props . statement ,
423
+ eventBusName : props . eventBus . eventBusName ,
424
+ } ) ;
425
+ }
426
+ }
0 commit comments