From 8943693c33359749d6685d867c01151cfd8000cf Mon Sep 17 00:00:00 2001 From: Oz Weiss Date: Tue, 22 Sep 2020 11:03:15 +0300 Subject: [PATCH] feat(Config Schema): Schema for AWS `cloudfront` event (#8250) --- .../compile/events/cloudFront/index.js | 97 ++++++++++++++++++- .../compile/events/cloudFront/index.test.js | 26 ----- 2 files changed, 92 insertions(+), 31 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/cloudFront/index.js b/lib/plugins/aws/package/compile/events/cloudFront/index.js index 0c4e244212d..78bf860b002 100644 --- a/lib/plugins/aws/package/compile/events/cloudFront/index.js +++ b/lib/plugins/aws/package/compile/events/cloudFront/index.js @@ -27,9 +27,100 @@ class AwsCompileCloudFrontEvents { 'before:remove:remove': this.logRemoveReminder.bind(this), }; - // TODO: Complete schema, see https://github.com/serverless/serverless/issues/8025 + const originObjectSchema = { + type: 'object', + properties: { + ConnectionAttempts: { type: 'integer', miminum: 1, maximum: 3 }, + ConnectionTimeout: { type: 'integer', miminum: 1, maximum: 10 }, + CustomOriginConfig: { + type: 'object', + properties: { + HTTPPort: { type: 'integer', miminum: 0, maximum: 65535 }, + HTTPSPort: { type: 'integer', miminum: 0, maximum: 65535 }, + OriginKeepaliveTimeout: { type: 'integer', miminum: 1, maximum: 60 }, + OriginProtocolPolicy: { + enum: ['http-only', 'match-viewer', 'https-only'], + }, + OriginReadTimeout: { type: 'integer', miminum: 1, maximum: 60 }, + OriginSSLProtocols: { + type: 'array', + items: { enum: ['SSLv3', 'TLSv1', 'TLSv1.1', 'TLSv1.2'] }, + }, + }, + additionalProperties: false, + required: ['OriginProtocolPolicy'], + }, + DomainName: { type: 'string' }, + OriginCustomHeaders: { + type: 'array', + items: { + type: 'object', + properties: { + HeaderName: { type: 'string' }, + HeaderValue: { type: 'string' }, + }, + additionalProperties: false, + required: ['HeaderName', 'HeaderValue'], + }, + }, + OriginPath: { type: 'string' }, + S3OriginConfig: { + type: 'object', + properties: { + OriginAccessIdentity: { + type: 'string', + pattern: '^origin-access-identity/cloudfront/.+', + }, + }, + additionalProperties: false, + }, + }, + additionalProperties: false, + required: ['DomainName', 'Id'], + oneOf: [{ required: ['CustomOriginConfig'] }, { required: ['S3OriginConfig'] }], + }; + + const behaviorObjectSchema = { + type: 'object', + properties: { + AllowedMethods: { + oneOf: [ + { enum: ['GET', 'HEAD'] }, + { enum: ['GET', 'HEAD', 'OPTIONS'] }, + { enum: ['GET', 'HEAD', 'OPTIONS', 'PUT', 'PATCH', 'POST', 'DELETE'] }, + ], + }, + CachedMethods: { + oneOf: [{ enum: ['GET', 'HEAD'] }, { enum: ['GET', 'HEAD', 'OPTIONS'] }], + }, + CachePolicyId: { type: 'string' }, + Compress: { type: 'boolean' }, + FieldLevelEncryptionId: { type: 'string' }, + OriginRequestPolicyId: { type: 'string' }, + SmoothStreaming: { type: 'boolean' }, + TrustedSigners: { type: 'array', items: { type: 'string' } }, + ViewerProtocolPolicy: { + enum: ['allow-all', 'redirect-to-https', 'https-only'], + }, + }, + additionalProperties: false, + }; + this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'cloudFront', { type: 'object', + properties: { + behavior: behaviorObjectSchema, + eventType: { + enum: ['viewer-request', 'origin-request', 'origin-response', 'viewer-response'], + }, + isDefaultOrigin: { type: 'boolean' }, + includeBody: { type: 'boolean' }, + origin: { + oneOf: [{ type: 'string', format: 'uri' }, originObjectSchema], + }, + pathPattern: { type: 'string', pattern: '^([A-Za-z0-9_.*$/~"\'@:+-]|&)+$' }, + }, + additionalProperties: false, }); } @@ -156,10 +247,6 @@ class AwsCompileCloudFrontEvents { if (functionObj.events) { functionObj.events.forEach(event => { if (event.cloudFront) { - if (!_.isObject(event.cloudFront)) { - throw new Error('cloudFront event has to be an object'); - } - const lambdaFunctionLogicalId = Object.keys(Resources).find( key => Resources[key].Type === 'AWS::Lambda::Function' && diff --git a/lib/plugins/aws/package/compile/events/cloudFront/index.test.js b/lib/plugins/aws/package/compile/events/cloudFront/index.test.js index e9044929e50..b231ebf5765 100644 --- a/lib/plugins/aws/package/compile/events/cloudFront/index.test.js +++ b/lib/plugins/aws/package/compile/events/cloudFront/index.test.js @@ -351,32 +351,6 @@ describe('AwsCompileCloudFrontEvents', () => { }); describe('#compileCloudFrontEvents()', () => { - it('should throw an error if cloudFront event type is not an object', () => { - awsCompileCloudFrontEvents.serverless.service.functions = { - first: { - events: [ - { - cloudFront: 42, - }, - ], - }, - }; - - expect(() => awsCompileCloudFrontEvents.compileCloudFrontEvents()).to.throw(Error); - - awsCompileCloudFrontEvents.serverless.service.functions = { - first: { - events: [ - { - cloudFront: 'some', - }, - ], - }, - }; - - expect(() => awsCompileCloudFrontEvents.compileCloudFrontEvents()).to.throw(Error); - }); - it('should throw an error if the region is not us-east-1', () => { options.region = 'eu-central-1'; awsCompileCloudFrontEvents.serverless.service.functions = {