From 5b77e271fd3cef2aa76ddb6fc23373b356f1cfbd Mon Sep 17 00:00:00 2001 From: Keeton Hodgson Date: Mon, 13 Jun 2022 19:45:57 -0600 Subject: [PATCH] fix(events): eventSourceName does not accept tokens (#20719) Addresses #20718 Adds a check to see if the value is a token before running the validation regex on `eventSourceName`. Simple fix following a similar fix in #10772 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *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 | 22 ++++++++++--------- .../aws-events/test/event-bus.test.ts | 10 +++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/@aws-cdk/aws-events/lib/event-bus.ts b/packages/@aws-cdk/aws-events/lib/event-bus.ts index daeb6060c11a5..ee953f5622634 100644 --- a/packages/@aws-cdk/aws-events/lib/event-bus.ts +++ b/packages/@aws-cdk/aws-events/lib/event-bus.ts @@ -269,16 +269,18 @@ export class EventBus extends EventBusBase { } if (eventSourceName !== undefined) { - // 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}`, - ); + 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 }; } diff --git a/packages/@aws-cdk/aws-events/test/event-bus.test.ts b/packages/@aws-cdk/aws-events/test/event-bus.test.ts index 34406986c608e..11b58d028c773 100644 --- a/packages/@aws-cdk/aws-events/test/event-bus.test.ts +++ b/packages/@aws-cdk/aws-events/test/event-bus.test.ts @@ -254,6 +254,16 @@ describe('event bus', () => { }).toThrow(/'eventSourceName' must satisfy: \/\^aws/); }); + test('does not throw if eventSourceName is a token', () => { + // GIVEN + const stack = new Stack(); + + // WHEN / THEN + expect(() => new EventBus(stack, 'EventBus', { + eventSourceName: Aws.STACK_NAME, + })).not.toThrow(); + }); + test('event bus source name cannot be empty string', () => { // GIVEN const stack = new Stack();