From 57e540432c1446f2233a9b0c0f4caba4e9e155d9 Mon Sep 17 00:00:00 2001 From: Sam Sussman Date: Tue, 9 Mar 2021 03:46:08 -0600 Subject: [PATCH] fix(events): imported EventBus does not correctly register source account (#13481) Parses the event bus arn to give the account and region to the underlying Resource. fixes #13469 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/aws-events/lib/event-bus.ts | 6 +- .../aws-events/test/test.event-bus.ts | 61 ++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-events/lib/event-bus.ts b/packages/@aws-cdk/aws-events/lib/event-bus.ts index cd0c7f913cbf6..ebbddfe2bc397 100644 --- a/packages/@aws-cdk/aws-events/lib/event-bus.ts +++ b/packages/@aws-cdk/aws-events/lib/event-bus.ts @@ -282,7 +282,11 @@ class ImportedEventBus extends EventBusBase { public readonly eventBusPolicy: string; public readonly eventSourceName?: string; constructor(scope: Construct, id: string, attrs: EventBusAttributes) { - super(scope, id); + const arnParts = Stack.of(scope).parseArn(attrs.eventBusArn); + super(scope, id, { + account: arnParts.account, + region: arnParts.region, + }); this.eventBusArn = attrs.eventBusArn; this.eventBusName = attrs.eventBusName; diff --git a/packages/@aws-cdk/aws-events/test/test.event-bus.ts b/packages/@aws-cdk/aws-events/test/test.event-bus.ts index 2e8434e147bb1..2156b39e06e8d 100644 --- a/packages/@aws-cdk/aws-events/test/test.event-bus.ts +++ b/packages/@aws-cdk/aws-events/test/test.event-bus.ts @@ -1,6 +1,6 @@ import { expect, haveResource } from '@aws-cdk/assert'; import * as iam from '@aws-cdk/aws-iam'; -import { Aws, CfnResource, Stack } from '@aws-cdk/core'; +import { Aws, CfnResource, Stack, Arn } from '@aws-cdk/core'; import { Test } from 'nodeunit'; import { EventBus } from '../lib'; @@ -55,6 +55,65 @@ export = { test.done(); }, + 'imported event bus'(test: Test) { + const stack = new Stack(); + + const eventBus = new EventBus(stack, 'Bus'); + + const importEB = EventBus.fromEventBusArn(stack, 'ImportBus', eventBus.eventBusArn); + + // WHEN + new CfnResource(stack, 'Res', { + type: 'Test::Resource', + properties: { + EventBusArn1: eventBus.eventBusArn, + EventBusArn2: importEB.eventBusArn, + }, + }); + + expect(stack).to(haveResource('Test::Resource', { + EventBusArn1: { 'Fn::GetAtt': ['BusEA82B648', 'Arn'] }, + EventBusArn2: { 'Fn::GetAtt': ['BusEA82B648', 'Arn'] }, + })); + + test.done(); + }, + + 'same account imported event bus has right resource env'(test: Test) { + const stack = new Stack(); + + const eventBus = new EventBus(stack, 'Bus'); + + const importEB = EventBus.fromEventBusArn(stack, 'ImportBus', eventBus.eventBusArn); + + // WHEN + test.deepEqual(stack.resolve(importEB.env.account), { 'Fn::Select': [4, { 'Fn::Split': [':', { 'Fn::GetAtt': ['BusEA82B648', 'Arn'] }] }] }); + test.deepEqual(stack.resolve(importEB.env.region), { 'Fn::Select': [3, { 'Fn::Split': [':', { 'Fn::GetAtt': ['BusEA82B648', 'Arn'] }] }] }); + + test.done(); + }, + + 'cross account imported event bus has right resource env'(test: Test) { + const stack = new Stack(); + + const arnParts = { + resource: 'bus', + service: 'events', + account: 'myAccount', + region: 'us-west-1', + }; + + const arn = Arn.format(arnParts, stack); + + const importEB = EventBus.fromEventBusArn(stack, 'ImportBus', arn); + + // WHEN + test.deepEqual(importEB.env.account, arnParts.account); + test.deepEqual(importEB.env.region, arnParts.region); + + test.done(); + }, + 'can get bus name'(test: Test) { // GIVEN const stack = new Stack();