Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(events): Validate events rule name #25366

Merged
merged 2 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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