From ff9bbb0ff4b33b142da01e7d194e33e1edfa718f Mon Sep 17 00:00:00 2001 From: Piotr Grzesik Date: Mon, 25 Oct 2021 15:59:44 +0200 Subject: [PATCH] feat(AWS CloudFront): Remove support for deprecated `behavior` props BREAKING CHANGE: Support for `MinTTL`, `MaxTTL`, `DefaultTTL` and `ForwardedValues` on `cloudfront.behavior` has been removed. --- .../aws/package/compile/events/cloudFront.js | 82 +------------- .../package/compile/events/cloudFront.test.js | 104 +----------------- 2 files changed, 2 insertions(+), 184 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/cloudFront.js b/lib/plugins/aws/package/compile/events/cloudFront.js index c78d5ba9d..78ddee1f7 100644 --- a/lib/plugins/aws/package/compile/events/cloudFront.js +++ b/lib/plugins/aws/package/compile/events/cloudFront.js @@ -124,37 +124,6 @@ class AwsCompileCloudFrontEvents { }, ], }, - ForwardedValues: { - type: 'object', - properties: { - Cookies: { - anyOf: [ - { - type: 'object', - properties: { - Forward: { enum: ['all', 'none'] }, - }, - additionalProperties: false, - required: ['Forward'], - }, - { - type: 'object', - properties: { - Forward: { const: 'whitelist' }, - WhitelistedNames: { type: 'array', items: { type: 'string' } }, - }, - additionalProperties: false, - required: ['Forward', 'WhitelistedNames'], - }, - ], - }, - Headers: { type: 'array', items: { type: 'string' } }, - QueryString: { type: 'boolean' }, - QueryStringCacheKeys: { type: 'array', items: { type: 'string' } }, - }, - additionalProperties: false, - required: ['QueryString'], - }, CachePolicyId: { type: 'string' }, Compress: { type: 'boolean' }, FieldLevelEncryptionId: { type: 'string' }, @@ -205,32 +174,6 @@ class AwsCompileCloudFrontEvents { }); this.hooks = { - 'initialize': () => { - if ( - this.serverless.service.provider.name === 'aws' && - Object.values(this.serverless.service.functions).some(({ events }) => - events.some(({ cloudFront: eventObject }) => { - const behaviorConfig = eventObject && eventObject.behavior; - if (!behaviorConfig) return false; - return ( - behaviorConfig.ForwardedValues || - behaviorConfig.MinTTL !== undefined || - behaviorConfig.MaxTTL !== undefined || - behaviorConfig.DefaultTTL !== undefined - ); - }) - ) - ) { - this.serverless._logDeprecation( - 'CLOUDFRONT_CACHE_BEHAVIOR_FORWARDED_VALUES_AND_TTL', - 'Cloudfront has deprecated the use of the ForwardedValues, MinTTL, MaxTTL' + - 'and DefaultTTL field to configure cache behavior.' + - 'Please use "provider.cloudfront.cachePolicies" to define Cache Policies' + - 'and reference it here with "cachePolicy.name" property.' + - 'You can also reference existing policies with "cachePolicy.id".' - ); - } - }, 'package:initialize': this.validate.bind(this), 'before:package:compileFunctions': this.prepareFunctions.bind(this), 'package:compileEvents': () => { @@ -440,32 +383,9 @@ class AwsCompileCloudFrontEvents { }; let shouldAssignCachePolicy = true; if (event.cloudFront.behavior) { - if ( - event.cloudFront.behavior.ForwardedValues || - event.cloudFront.behavior.MinTTL !== undefined || - event.cloudFront.behavior.MaxTTL !== undefined || - event.cloudFront.behavior.DefaultTTL !== undefined - ) { - behavior.ForwardedValues = { QueryString: false }; - shouldAssignCachePolicy = false; - } Object.assign(behavior, event.cloudFront.behavior); } - if ( - (event.cloudFront.cachePolicy || - (event.cloudFront.behavior && event.cloudFront.behavior.CachePolicyId)) && - !shouldAssignCachePolicy - ) { - throw new ServerlessError( - 'Both cachePolicy and deprecated fields ForwardedValues, MinTTL, MaxTTL' + - 'and DefaultTTL found in function ${functionObj.name} configuration.' + - 'Specifying a cachePolicy override those deprecated parameters.' + - 'Please remove one of the cache behavior definition.', - 'CACHE_POLICY_ID_AND_DEPRECATED_FIELDS_USED' - ); - } - if (event.cloudFront.behavior && event.cloudFront.behavior.CachePolicyId) { Object.assign(behavior, { CachePolicyId: event.cloudFront.behavior.CachePolicyId, @@ -492,7 +412,7 @@ class AwsCompileCloudFrontEvents { shouldAssignCachePolicy = false; } - // Assigning default cache policy only if neither cache policy reference nor any of the ForwardedValues, MinTTL, MaxTTL, and DefaultTTL deprecated fields is defined. + // Assigning default cache policy only if cache policy reference is not defined. if (shouldAssignCachePolicy) { // Assigning default Managed-CachingOptimized Cache Policy. // See details at https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-cache-policies.html#managed-cache-policies-list diff --git a/test/unit/lib/plugins/aws/package/compile/events/cloudFront.test.js b/test/unit/lib/plugins/aws/package/compile/events/cloudFront.test.js index 902ed1f03..13efad790 100644 --- a/test/unit/lib/plugins/aws/package/compile/events/cloudFront.test.js +++ b/test/unit/lib/plugins/aws/package/compile/events/cloudFront.test.js @@ -1418,71 +1418,6 @@ describe('AwsCompileCloudFrontEvents', () => { ).to.not.have.any.keys('CacheBehaviors'); }); - it('should create behavior with all values given as an object', () => { - awsCompileCloudFrontEvents.serverless.service.functions = { - first: { - name: 'first', - events: [ - { - cloudFront: { - eventType: 'viewer-request', - origin: 's3://bucketname.s3.amazonaws.com/files', - behavior: { - ForwardedValues: { - QueryString: true, - Headers: ['*'], - }, - ViewerProtocolPolicy: 'https-only', - AllowedMethods: ['GET', 'HEAD', 'OPTIONS'], - CachedMethods: ['GET', 'HEAD', 'OPTIONS'], - }, - }, - }, - ], - }, - }; - - awsCompileCloudFrontEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources = - { - FirstLambdaFunction: { - Type: 'AWS::Lambda::Function', - Properties: { - FunctionName: 'first', - }, - }, - FirstLambdaVersion: { - Type: 'AWS::Lambda::Version', - Properties: { - FunctionName: { Ref: 'FirstLambdaFunction' }, - }, - }, - }; - - awsCompileCloudFrontEvents.compileCloudFrontEvents(); - - expect( - awsCompileCloudFrontEvents.serverless.service.provider.compiledCloudFormationTemplate - .Resources.CloudFrontDistribution.Properties.DistributionConfig.DefaultCacheBehavior - ).to.eql({ - TargetOriginId: 's3/bucketname.s3.amazonaws.com/files', - ViewerProtocolPolicy: 'https-only', - ForwardedValues: { - QueryString: true, - Headers: ['*'], - }, - AllowedMethods: ['GET', 'HEAD', 'OPTIONS'], - CachedMethods: ['GET', 'HEAD', 'OPTIONS'], - LambdaFunctionAssociations: [ - { - EventType: 'viewer-request', - LambdaFunctionARN: { - Ref: 'FirstLambdaVersion', - }, - }, - ], - }); - }); - it('should throw if more than one behavior with the same PathPattern', () => { awsCompileCloudFrontEvents.serverless.service.functions = { first: { @@ -1749,43 +1684,6 @@ describe('test/unit/lib/plugins/aws/package/compile/events/cloudFront.test.js', ).to.eventually.be.rejected.and.have.property('code', 'UNRECOGNIZED_CLOUDFRONT_CACHE_POLICY'); }); - it('Should throw if lambda config includes both deprecated behavior values and cache policy reference', () => { - const cachePolicyId = '08627262-05a9-4f76-9ded-b50ca2e3a84f'; - return expect( - runServerless({ - fixture: 'function', - command: 'package', - configExt: { - disabledDeprecations: ['CLOUDFRONT_CACHE_BEHAVIOR_FORWARDED_VALUES_AND_TTL'], - functions: { - basic: { - handler: 'myLambdaAtEdge.handler', - events: [ - { - cloudFront: { - origin: 's3://bucketname.s3.amazonaws.com/files', - eventType: 'viewer-response', - behavior: { - ForwardedValues: { - QueryString: true, - }, - }, - cachePolicy: { - id: cachePolicyId, - }, - }, - }, - ], - }, - }, - }, - }) - ).to.eventually.be.rejected.and.have.property( - 'code', - 'CACHE_POLICY_ID_AND_DEPRECATED_FIELDS_USED' - ); - }); - it('Should not throw if lambda config includes AllowedMethods or CachedMethods behavior values and cache policy reference', async () => { const cachePolicyId = '08627262-05a9-4f76-9ded-b50ca2e3a84f'; await runServerless({ @@ -2150,7 +2048,7 @@ describe('test/unit/lib/plugins/aws/package/compile/events/cloudFront.test.js', }); }); - it('Should attach a default cache policy when none are provided, and no deprecated behavior values are used', () => { + it('Should attach a default cache policy when none are provided', () => { // Default Managed-CachingOptimized Cache Policy id const defaultCachePolicyId = '658327ea-f89d-4fab-a63d-7e88639e58f6'; expect(getAssociatedCacheBehavior('noPolicy').CachePolicyId).to.eq(defaultCachePolicyId);