From 782f101a9458d0461683fab1fd84cc887e9d5909 Mon Sep 17 00:00:00 2001 From: Laurence Warne Date: Fri, 23 Aug 2024 13:22:46 +0100 Subject: [PATCH 1/2] fix(scheduler): validate schedule name length --- packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts | 5 ++++- .../aws-scheduler-alpha/test/schedule.test.ts | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts b/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts index 52e22ac5e0495..e1756b813be15 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts +++ b/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts @@ -1,4 +1,4 @@ -import { Duration, IResource, Resource } from 'aws-cdk-lib'; +import { Duration, IResource, Resource, Token } from 'aws-cdk-lib'; import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch'; import * as kms from 'aws-cdk-lib/aws-kms'; import { CfnSchedule } from 'aws-cdk-lib/aws-scheduler'; @@ -324,6 +324,9 @@ export class Schedule extends Resource implements ISchedule { const flexibleTimeWindow = props.timeWindow ?? TimeWindow.off(); this.validateTimeFrame(props.start, props.end); + if (props.scheduleName && !Token.isUnresolved(props.scheduleName) && props.scheduleName.length > 64) { + throw new Error(`schedule name '${props.scheduleName}' exceeds 64 characters in length`); + } const resource = new CfnSchedule(this, 'Resource', { name: this.physicalName, diff --git a/packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts b/packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts index a99fcf88aef3f..3121c418d6ce9 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts +++ b/packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts @@ -214,5 +214,16 @@ describe('Schedule', () => { }); }).toThrow('The provided duration must be between 1 minute and 1440 minutes, got 0'); }); + + test('throw error when schedule name exceeds 64', () => { + const name = 'an-extremely-unnecessarily-long-name-exceeding-64-characters-in-length'; + expect(() => { + new Schedule(stack, 'TestSchedule', { + schedule: expr, + target: new SomeLambdaTarget(func, role), + scheduleName: name, + }); + }).toThrow(`schedule name '${name}' exceeds 64 characters in length`); + }); }); }); From a59e40f9f5532cdf34e588107ad2bdfd321f4c9d Mon Sep 17 00:00:00 2001 From: Laurence Warne Date: Fri, 30 Aug 2024 13:26:19 +0100 Subject: [PATCH 2/2] Update some wording --- packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts | 2 +- packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts b/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts index e1756b813be15..0ff5d143b42df 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts +++ b/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts @@ -325,7 +325,7 @@ export class Schedule extends Resource implements ISchedule { this.validateTimeFrame(props.start, props.end); if (props.scheduleName && !Token.isUnresolved(props.scheduleName) && props.scheduleName.length > 64) { - throw new Error(`schedule name '${props.scheduleName}' exceeds 64 characters in length`); + throw new Error(`scheduleName cannot be longer than 64 characters, got: ${props.scheduleName.length}`); } const resource = new CfnSchedule(this, 'Resource', { diff --git a/packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts b/packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts index 3121c418d6ce9..9b2ec94e37ebc 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts +++ b/packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts @@ -215,7 +215,7 @@ describe('Schedule', () => { }).toThrow('The provided duration must be between 1 minute and 1440 minutes, got 0'); }); - test('throw error when schedule name exceeds 64', () => { + test('throw error when scheduleName exceeds 64 characters', () => { const name = 'an-extremely-unnecessarily-long-name-exceeding-64-characters-in-length'; expect(() => { new Schedule(stack, 'TestSchedule', { @@ -223,7 +223,7 @@ describe('Schedule', () => { target: new SomeLambdaTarget(func, role), scheduleName: name, }); - }).toThrow(`schedule name '${name}' exceeds 64 characters in length`); + }).toThrow(`scheduleName cannot be longer than 64 characters, got: ${name.length}`); }); }); });