Skip to content

Commit

Permalink
feat(events): Validate events rule name (#25366)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
pyto86pri authored May 1, 2023
1 parent 9096602 commit 5bdb012
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/aws-cdk-lib/aws-events/lib/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,23 @@ export class Rule extends Resource implements IRule {
}

protected validateRule() {
const errors: string[] = [];

const name = this.physicalName;
if (name !== undefined && !Token.isUnresolved(name)) {
if (name.length < 1 || name.length > 64) {
errors.push(`Event rule name must be between 1 and 64 characters. Received: ${name}`);
}
if (!/^[\.\-_A-Za-z0-9]+$/.test(name)) {
errors.push(`Event rule name ${name} can contain only letters, numbers, periods, hyphens, or underscores with no spaces.`);
}
}

if (Object.keys(this.eventPattern).length === 0 && !this.scheduleExpression) {
return ['Either \'eventPattern\' or \'schedule\' must be defined'];
errors.push('Either \'eventPattern\' or \'schedule\' must be defined');
}

return [];
return errors;
}

private renderTargets() {
Expand Down
32 changes: 32 additions & 0 deletions packages/aws-cdk-lib/aws-events/test/rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,38 @@ describe('rule', () => {
expect(() => app.synth()).toThrow(/Either 'eventPattern' or 'schedule' must be defined/);
});

test('fails synthesis when rule name is less than 1 chars', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
new Rule(stack, 'Rule', {
ruleName: '',
schedule: Schedule.rate(cdk.Duration.minutes(10)),
});
expect(() => app.synth()).toThrow(/Event rule name must be between 1 and 64 characters./);
});

test('fails synthesis when rule name is longer than 64 chars', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
new Rule(stack, 'Rule', {
ruleName: 'a'.repeat(65),
schedule: Schedule.rate(cdk.Duration.minutes(10)),
});
expect(() => app.synth()).toThrow(/Event rule name must be between 1 and 64 characters./);
});

test('fails synthesis when rule name contains invalid characters', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
[' ', '\n', '\r', '[', ']', '<', '>', '$'].forEach(invalidChar => {
new Rule(stack, `Rule${invalidChar}`, {
ruleName: `Rule${invalidChar}`,
schedule: Schedule.rate(cdk.Duration.minutes(10)),
});
expect(() => app.synth()).toThrow(/can contain only letters, numbers, periods, hyphens, or underscores with no spaces./);
});
});

test('addEventPattern can be used to add filters', () => {
const stack = new cdk.Stack();

Expand Down

0 comments on commit 5bdb012

Please sign in to comment.