Skip to content

Commit 5bdb012

Browse files
authored
feat(events): Validate events rule name (#25366)
This PR is adding validations for events rule name. Closes #25352. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 9096602 commit 5bdb012

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

packages/aws-cdk-lib/aws-events/lib/rule.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,23 @@ export class Rule extends Resource implements IRule {
295295
}
296296

297297
protected validateRule() {
298+
const errors: string[] = [];
299+
300+
const name = this.physicalName;
301+
if (name !== undefined && !Token.isUnresolved(name)) {
302+
if (name.length < 1 || name.length > 64) {
303+
errors.push(`Event rule name must be between 1 and 64 characters. Received: ${name}`);
304+
}
305+
if (!/^[\.\-_A-Za-z0-9]+$/.test(name)) {
306+
errors.push(`Event rule name ${name} can contain only letters, numbers, periods, hyphens, or underscores with no spaces.`);
307+
}
308+
}
309+
298310
if (Object.keys(this.eventPattern).length === 0 && !this.scheduleExpression) {
299-
return ['Either \'eventPattern\' or \'schedule\' must be defined'];
311+
errors.push('Either \'eventPattern\' or \'schedule\' must be defined');
300312
}
301313

302-
return [];
314+
return errors;
303315
}
304316

305317
private renderTargets() {

packages/aws-cdk-lib/aws-events/test/rule.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,38 @@ describe('rule', () => {
203203
expect(() => app.synth()).toThrow(/Either 'eventPattern' or 'schedule' must be defined/);
204204
});
205205

206+
test('fails synthesis when rule name is less than 1 chars', () => {
207+
const app = new cdk.App();
208+
const stack = new cdk.Stack(app, 'MyStack');
209+
new Rule(stack, 'Rule', {
210+
ruleName: '',
211+
schedule: Schedule.rate(cdk.Duration.minutes(10)),
212+
});
213+
expect(() => app.synth()).toThrow(/Event rule name must be between 1 and 64 characters./);
214+
});
215+
216+
test('fails synthesis when rule name is longer than 64 chars', () => {
217+
const app = new cdk.App();
218+
const stack = new cdk.Stack(app, 'MyStack');
219+
new Rule(stack, 'Rule', {
220+
ruleName: 'a'.repeat(65),
221+
schedule: Schedule.rate(cdk.Duration.minutes(10)),
222+
});
223+
expect(() => app.synth()).toThrow(/Event rule name must be between 1 and 64 characters./);
224+
});
225+
226+
test('fails synthesis when rule name contains invalid characters', () => {
227+
const app = new cdk.App();
228+
const stack = new cdk.Stack(app, 'MyStack');
229+
[' ', '\n', '\r', '[', ']', '<', '>', '$'].forEach(invalidChar => {
230+
new Rule(stack, `Rule${invalidChar}`, {
231+
ruleName: `Rule${invalidChar}`,
232+
schedule: Schedule.rate(cdk.Duration.minutes(10)),
233+
});
234+
expect(() => app.synth()).toThrow(/can contain only letters, numbers, periods, hyphens, or underscores with no spaces./);
235+
});
236+
});
237+
206238
test('addEventPattern can be used to add filters', () => {
207239
const stack = new cdk.Stack();
208240

0 commit comments

Comments
 (0)