From 8f4d4d75df7adf994f97de633de3da2fc32c7eee Mon Sep 17 00:00:00 2001 From: Pahud Hsieh Date: Thu, 6 Jun 2024 19:49:29 -0400 Subject: [PATCH] chore(stepfunction-tasks): eventbridge aws. event source prefix check is more strict than it should be (#30237) 1. fix the event source validation 2. move the validation from renderEntries() to validateEntries() 3. add unit tests ### Issue # (if applicable) Closes #30191 ### Reason for this change ### Description of changes ### Description of how you validated changes ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../lib/eventbridge/put-events.ts | 20 ++++++++-------- .../test/eventbridge/put-events.test.ts | 24 +++++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/packages/aws-cdk-lib/aws-stepfunctions-tasks/lib/eventbridge/put-events.ts b/packages/aws-cdk-lib/aws-stepfunctions-tasks/lib/eventbridge/put-events.ts index cb0f08bb9a311..dfb8694d801c6 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions-tasks/lib/eventbridge/put-events.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions-tasks/lib/eventbridge/put-events.ts @@ -132,17 +132,14 @@ export class EventBridgePutEvents extends sfn.TaskStateBase { } private renderEntries(): Object[] { + // we should have validated all entries in validateEntries() return this.props.entries.map(entry => { - if (entry.source?.startsWith('aws')) { - throw new Error('Event source cannot start with "aws."'); - } else { - return { - Detail: entry.detail?.value, - DetailType: entry.detailType, - EventBusName: entry.eventBus?.eventBusArn, - Source: entry.source, - }; - } + return { + Detail: entry.detail?.value, + DetailType: entry.detailType, + EventBusName: entry.eventBus?.eventBusArn, + Source: entry.source, + }; }); } @@ -150,5 +147,8 @@ export class EventBridgePutEvents extends sfn.TaskStateBase { if (this.props.entries.length <= 0) { throw new Error('Value for property `entries` must be a non-empty array.'); } + if (this.props.entries.some(e => e.source.startsWith('aws.'))) { + throw new Error('Event source cannot start with "aws."'); + } } } \ No newline at end of file diff --git a/packages/aws-cdk-lib/aws-stepfunctions-tasks/test/eventbridge/put-events.test.ts b/packages/aws-cdk-lib/aws-stepfunctions-tasks/test/eventbridge/put-events.test.ts index 67e98c798d32e..5a4a63221b504 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions-tasks/test/eventbridge/put-events.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions-tasks/test/eventbridge/put-events.test.ts @@ -161,6 +161,30 @@ describe('Put Events', () => { }).toThrowError('Unsupported service integration pattern'); }); + test('event source cannot start with "aws."', () => { + expect(() => { + new EventBridgePutEvents(stack, 'PutEvents', { + entries: [{ + detail: sfn.TaskInput.fromText('MyDetail'), + detailType: 'MyDetailType', + source: 'aws.source', + }], + }); + }).toThrow(/Event source cannot start with "aws."/); + }); + + test('event source can start with "aws" without trailing dot', () => { + expect(() => { + new EventBridgePutEvents(stack, 'PutEvents', { + entries: [{ + detail: sfn.TaskInput.fromText('MyDetail'), + detailType: 'MyDetailType', + source: 'awssource', + }], + }); + }).not.toThrow(/Event source cannot start with "aws."/); + }); + test('provided EventBus', () => { // GIVEN const eventBus = new events.EventBus(stack, 'EventBus');