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

AWS: Validate rate/cron syntax before Deploy #5635

Merged
merged 3 commits into from
Dec 31, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 24 additions & 15 deletions lib/plugins/aws/package/compile/events/schedule/index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@


const _ = require('lodash'); const _ = require('lodash');


const rateSyntaxPattern =
/^rate\((?:1 (?:minute|hour|day)|(?:1\d+|[2-9]\d*) (?:minute|hour|day)s)\)$/;
const cronSyntaxPattern =
/^cron\(\S+ \S+ \S+ \S+ \S+ \S+\)$/;

class AwsCompileScheduledEvents { class AwsCompileScheduledEvents {
constructor(serverless) { constructor(serverless) {
this.serverless = serverless; this.serverless = serverless;
Expand All @@ -12,6 +17,16 @@ class AwsCompileScheduledEvents {
}; };
} }


buildValidationErrorMessage(functionName) {
return [
`"rate" property for schedule event is missing or invalid in function ${functionName}.`,
' The correct syntax is: `schedule: rate(10 minutes)`, `schedule: cron(0 12 * * ? *)`',
' OR an object with "rate" property.',
' Please check the docs for more info:',
' https://serverless.com/framework/docs/providers/aws/events/schedule/',
].join('');
}

compileScheduledEvents() { compileScheduledEvents() {
this.serverless.service.getAllFunctions().forEach((functionName) => { this.serverless.service.getAllFunctions().forEach((functionName) => {
const functionObj = this.serverless.service.getFunction(functionName); const functionObj = this.serverless.service.getFunction(functionName);
Expand All @@ -28,15 +43,9 @@ class AwsCompileScheduledEvents {
let Name; let Name;
let Description; let Description;


// TODO validate rate syntax
if (typeof event.schedule === 'object') { if (typeof event.schedule === 'object') {
if (!event.schedule.rate) { if (!this.validateScheduleSyntax(event.schedule.rate)) {
const errorMessage = [ const errorMessage = this.buildValidationErrorMessage(functionName);
`Missing "rate" property for schedule event in function ${functionName}`,
' The correct syntax is: schedule: rate(10 minutes)',
' OR an object with "rate" property.',
' Please check the docs for more info.',
].join('');
throw new this.serverless.classes throw new this.serverless.classes
.Error(errorMessage); .Error(errorMessage);
} }
Expand Down Expand Up @@ -81,16 +90,11 @@ class AwsCompileScheduledEvents {
// escape quotes to favor JSON.parse // escape quotes to favor JSON.parse
Input = Input.replace(/\"/g, '\\"'); // eslint-disable-line Input = Input.replace(/\"/g, '\\"'); // eslint-disable-line
} }
} else if (typeof event.schedule === 'string') { } else if (this.validateScheduleSyntax(event.schedule)) {
ScheduleExpression = event.schedule; ScheduleExpression = event.schedule;
State = 'ENABLED'; State = 'ENABLED';
} else { } else {
const errorMessage = [ const errorMessage = this.buildValidationErrorMessage(functionName);
`Schedule event of function ${functionName} is not an object nor a string`,
' The correct syntax is: schedule: rate(10 minutes)',
' OR an object with "rate" property.',
' Please check the docs for more info.',
].join('');
throw new this.serverless.classes throw new this.serverless.classes
.Error(errorMessage); .Error(errorMessage);
} }
Expand Down Expand Up @@ -149,6 +153,11 @@ class AwsCompileScheduledEvents {
} }
}); });
} }

validateScheduleSyntax(input) {
return typeof input === 'string' &&
(rateSyntaxPattern.test(input) || cronSyntaxPattern.test(input));
}
} }


module.exports = AwsCompileScheduledEvents; module.exports = AwsCompileScheduledEvents;
139 changes: 139 additions & 0 deletions lib/plugins/aws/package/compile/events/schedule/index.test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -53,6 +53,138 @@ describe('AwsCompileScheduledEvents', () => {
expect(() => awsCompileScheduledEvents.compileScheduledEvents()).to.throw(Error); expect(() => awsCompileScheduledEvents.compileScheduledEvents()).to.throw(Error);
}); });


describe('rate syntax validation: rate(value unit)', () => {
describe('schedule string', () => {
it('should throw an error if the value is 1 but the unit is plural', () => {
awsCompileScheduledEvents.serverless.service.functions = {
first: {
events: [
{
schedule: 'rate(1 days)',
},
],
},
};

expect(() => awsCompileScheduledEvents.compileScheduledEvents()).to.throw(Error);
});

it('should throw an error if the value is >1 but the unit is not plural', () => {
awsCompileScheduledEvents.serverless.service.functions = {
first: {
events: [
{
schedule: 'rate(5 minute)',
},
],
},
};

expect(() => awsCompileScheduledEvents.compileScheduledEvents()).to.throw(Error);
});
});

describe('schedule object', () => {
it('should throw an error if the value is 1 but the unit is plural', () => {
awsCompileScheduledEvents.serverless.service.functions = {
first: {
events: [
{
schedule: {
rate: 'rate(1 days)',
},
},
],
},
};

expect(() => awsCompileScheduledEvents.compileScheduledEvents()).to.throw(Error);
});

it('should throw an error if the value is >1 but the unit is not plural', () => {
awsCompileScheduledEvents.serverless.service.functions = {
first: {
events: [
{
schedule: {
rate: 'rate(5 minute)',
},
},
],
},
};

expect(() => awsCompileScheduledEvents.compileScheduledEvents()).to.throw(Error);
});
});
});

describe('cron syntax validation: cron(* * * * * 2018)', () => {
describe('schedule string', () => {
it('should throw an error if number of fields is less than 6', () => {
awsCompileScheduledEvents.serverless.service.functions = {
first: {
events: [
{
schedule: 'cron(0 12 * * ?)',
},
],
},
};

expect(() => awsCompileScheduledEvents.compileScheduledEvents()).to.throw(Error);
});

it('should throw an error if number of fields is greater than 6', () => {
awsCompileScheduledEvents.serverless.service.functions = {
first: {
events: [
{
schedule: 'cron(0 12 * * ? * *)',
},
],
},
};

expect(() => awsCompileScheduledEvents.compileScheduledEvents()).to.throw(Error);
});
});

describe('schedule object', () => {
it('should throw an error if number of fields is less than 6', () => {
awsCompileScheduledEvents.serverless.service.functions = {
first: {
events: [
{
schedule: {
rate: 'cron(0 12 * * ?)',
},
},
],
},
};

expect(() => awsCompileScheduledEvents.compileScheduledEvents()).to.throw(Error);
});

it('should throw an error if number of fields is greater than 6', () => {
awsCompileScheduledEvents.serverless.service.functions = {
first: {
events: [
{
schedule: {
rate: 'cron(0 12 * * ? * *)',
},
},
],
},
};

expect(() => awsCompileScheduledEvents.compileScheduledEvents()).to.throw(Error);
});
});
});

it('should create corresponding resources when schedule events are given', () => { it('should create corresponding resources when schedule events are given', () => {
awsCompileScheduledEvents.serverless.service.functions = { awsCompileScheduledEvents.serverless.service.functions = {
first: { first: {
Expand All @@ -72,6 +204,9 @@ describe('AwsCompileScheduledEvents', () => {
{ {
schedule: 'rate(10 minutes)', schedule: 'rate(10 minutes)',
}, },
{
schedule: 'cron(5,35 12 ? * 6l 2002-2005)',
},
], ],
}, },
}; };
Expand Down Expand Up @@ -99,6 +234,10 @@ describe('AwsCompileScheduledEvents', () => {
.provider.compiledCloudFormationTemplate.Resources .provider.compiledCloudFormationTemplate.Resources
.FirstLambdaPermissionEventsRuleSchedule3.Type .FirstLambdaPermissionEventsRuleSchedule3.Type
).to.equal('AWS::Lambda::Permission'); ).to.equal('AWS::Lambda::Permission');
expect(awsCompileScheduledEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.FirstLambdaPermissionEventsRuleSchedule4.Type
).to.equal('AWS::Lambda::Permission');
}); });


it('should respect enabled variable, defaulting to true', () => { it('should respect enabled variable, defaulting to true', () => {
Expand Down