From d228250cd7cc80d1bdf90881ca8c83b6b47899db Mon Sep 17 00:00:00 2001 From: ozwi Date: Mon, 14 Sep 2020 11:17:40 +0300 Subject: [PATCH 1/8] awsLogGroup regex schema definition --- lib/plugins/aws/provider/awsProvider.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index cc087d3ef4a..b9719d79c28 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -233,6 +233,10 @@ class AwsProvider { UpdateReplacePolicy: { type: 'string' }, Condition: { type: 'string' }, }, + awsLogGroup: { + type: 'string', + pattern: '^/aws/[a-zA-Z0-9-_.]+$', + }, }, provider: { properties: { From 0d91ae8740fd2b95c33a732e994575e92a658f37 Mon Sep 17 00:00:00 2001 From: ozwi Date: Mon, 14 Sep 2020 11:17:56 +0300 Subject: [PATCH 2/8] aws cloudwatchlog event schema --- .../package/compile/events/cloudWatchLog/index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js index 9c9eaee3a1a..ca489c3a0f9 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js @@ -13,7 +13,18 @@ class AwsCompileCloudWatchLogEvents { // TODO: Complete schema, see https://github.com/serverless/serverless/issues/8027 this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'cloudwatchLog', { - anyOf: [{ type: 'string' }, { type: 'object' }], + anyOf: [ + { $ref: '#/definitions/awsLogGroup' }, + { + type: 'object', + properties: { + logGroup: { $ref: '#/definitions/awsLogGroup' }, + filter: { type: 'string' }, + required: ['logGroup'], + additionalProperties: false, + }, + }, + ], }); } From bba26f1442242aa6fad0e1cf9cd383269d794591 Mon Sep 17 00:00:00 2001 From: ozwi Date: Mon, 14 Sep 2020 13:43:36 +0300 Subject: [PATCH 3/8] fix schema --- lib/plugins/aws/package/compile/events/cloudWatchLog/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js index ca489c3a0f9..0b5d4571c91 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js @@ -20,9 +20,9 @@ class AwsCompileCloudWatchLogEvents { properties: { logGroup: { $ref: '#/definitions/awsLogGroup' }, filter: { type: 'string' }, - required: ['logGroup'], - additionalProperties: false, }, + required: ['logGroup'], + additionalProperties: false, }, ], }); From 8924fc04e0d5f9127e0ef14763a4acdf5bf76ad3 Mon Sep 17 00:00:00 2001 From: ozwi Date: Mon, 14 Sep 2020 13:45:04 +0300 Subject: [PATCH 4/8] remove comment --- lib/plugins/aws/package/compile/events/cloudWatchLog/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js index 0b5d4571c91..de81e81263e 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js @@ -11,7 +11,6 @@ class AwsCompileCloudWatchLogEvents { 'package:compileEvents': this.compileCloudWatchLogEvents.bind(this), }; - // TODO: Complete schema, see https://github.com/serverless/serverless/issues/8027 this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'cloudwatchLog', { anyOf: [ { $ref: '#/definitions/awsLogGroup' }, From e96ea5a87ff8c55eaa6738ca5f665f1327e10046 Mon Sep 17 00:00:00 2001 From: ozwi Date: Tue, 15 Sep 2020 11:04:56 +0300 Subject: [PATCH 5/8] update awsLogGroupName regex --- lib/plugins/aws/package/compile/events/cloudWatchLog/index.js | 4 ++-- lib/plugins/aws/provider/awsProvider.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js index de81e81263e..65bbc81b013 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js @@ -13,11 +13,11 @@ class AwsCompileCloudWatchLogEvents { this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'cloudwatchLog', { anyOf: [ - { $ref: '#/definitions/awsLogGroup' }, + { $ref: '#/definitions/awsLogGroupName' }, { type: 'object', properties: { - logGroup: { $ref: '#/definitions/awsLogGroup' }, + logGroup: { $ref: '#/definitions/awsLogGroupName' }, filter: { type: 'string' }, }, required: ['logGroup'], diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index b9719d79c28..573ae785460 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -233,9 +233,9 @@ class AwsProvider { UpdateReplacePolicy: { type: 'string' }, Condition: { type: 'string' }, }, - awsLogGroup: { + awsLogGroupName: { type: 'string', - pattern: '^/aws/[a-zA-Z0-9-_.]+$', + pattern: '[.-_/#A-Za-z0-9]+', }, }, provider: { From 9304bbdb755b212776c0e9f668f3b06e9486981f Mon Sep 17 00:00:00 2001 From: Oz Weiss Date: Tue, 15 Sep 2020 13:18:42 +0300 Subject: [PATCH 6/8] fix name regex --- lib/plugins/aws/provider/awsProvider.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index 573ae785460..9662e312239 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -235,7 +235,7 @@ class AwsProvider { }, awsLogGroupName: { type: 'string', - pattern: '[.-_/#A-Za-z0-9]+', + pattern: '^[a-zA-Z0-9-_.]+$', }, }, provider: { From 04d6e4d7b889f2a63a5c246ef1063d2ac5830701 Mon Sep 17 00:00:00 2001 From: ozwi Date: Tue, 22 Sep 2020 10:23:41 +0300 Subject: [PATCH 7/8] fix aws log group name regex --- lib/plugins/aws/provider/awsProvider.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index 9662e312239..f7b25a1ccef 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -235,7 +235,7 @@ class AwsProvider { }, awsLogGroupName: { type: 'string', - pattern: '^[a-zA-Z0-9-_.]+$', + pattern: '^[/#A-Za-z0-9-_.]+$', }, }, provider: { From 8eb71690dbe39348759cc3a22962957c1773971e Mon Sep 17 00:00:00 2001 From: ozwi Date: Tue, 22 Sep 2020 10:25:41 +0300 Subject: [PATCH 8/8] remove inline validations and validations tests --- .../compile/events/cloudWatchLog/index.js | 22 ------ .../events/cloudWatchLog/index.test.js | 72 ------------------- 2 files changed, 94 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js index 65bbc81b013..63cb9a7ce4e 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.js @@ -44,22 +44,6 @@ class AwsCompileCloudWatchLogEvents { let FilterPattern; if (typeof event.cloudwatchLog === 'object') { - if (!event.cloudwatchLog.logGroup) { - const errorMessage = [ - 'Missing "logGroup" property for cloudwatchLog event ', - `in function ${functionName} Please check the docs for more info.`, - ].join(''); - throw new this.serverless.classes.Error(errorMessage); - } - - if (event.cloudwatchLog.filter && typeof event.cloudwatchLog.filter !== 'string') { - const errorMessage = [ - `"filter" property for cloudwatchLog event in function ${functionName} `, - 'should be string. Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); - } - LogGroupName = event.cloudwatchLog.logGroup.replace(/\r?\n/g, ''); FilterPattern = event.cloudwatchLog.filter ? event.cloudwatchLog.filter.replace(/\r?\n/g, '') @@ -67,12 +51,6 @@ class AwsCompileCloudWatchLogEvents { } else if (typeof event.cloudwatchLog === 'string') { LogGroupName = event.cloudwatchLog.replace(/\r?\n/g, ''); FilterPattern = ''; - } else { - const errorMessage = [ - `cloudwatchLog event of function "${functionName}" is not an object or a string`, - ' Please check the docs for more info.', - ].join(''); - throw new this.serverless.classes.Error(errorMessage); } if (logGroupNames.includes(LogGroupName)) { diff --git a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.test.js b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.test.js index 7803c51814b..83e3e3a9ecd 100644 --- a/lib/plugins/aws/package/compile/events/cloudWatchLog/index.test.js +++ b/lib/plugins/aws/package/compile/events/cloudWatchLog/index.test.js @@ -23,46 +23,6 @@ describe('AwsCompileCloudWatchLogEvents', () => { }); describe('#compileCloudWatchLogEvents()', () => { - it('should throw an error if cloudwatchLog event type is not an object or a string', () => { - awsCompileCloudWatchLogEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchLog: 42, - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchLogEvents.compileCloudWatchLogEvents()).to.throw(Error); - - awsCompileCloudWatchLogEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchLog: [42], - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchLogEvents.compileCloudWatchLogEvents()).to.throw(Error); - }); - - it('should throw an error if the "logGroup" property is not given', () => { - awsCompileCloudWatchLogEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchLog: {}, - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchLogEvents.compileCloudWatchLogEvents()).to.throw(Error); - }); - it('should create corresponding resources when cloudwatchLog events are given', () => { awsCompileCloudWatchLogEvents.serverless.service.functions = { first: { @@ -199,38 +159,6 @@ describe('AwsCompileCloudWatchLogEvents', () => { ).to.equal(''); }); - it('should throw an error if "filter" variable is not a string', () => { - awsCompileCloudWatchLogEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchLog: { - logGroup: '/aws/lambda/hello1', - filter: {}, - }, - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchLogEvents.compileCloudWatchLogEvents()).to.throw(Error); - - awsCompileCloudWatchLogEvents.serverless.service.functions = { - first: { - events: [ - { - cloudwatchLog: { - logGroup: '/aws/lambda/hello1', - filter: [], - }, - }, - ], - }, - }; - - expect(() => awsCompileCloudWatchLogEvents.compileCloudWatchLogEvents()).to.throw(Error); - }); - it('should create corresponding resources when cloudwatchLog events are given as a string', () => { awsCompileCloudWatchLogEvents.serverless.service.functions = { first: {