Skip to content

Commit

Permalink
Fix schema validation of CloudFront distribution for AllowedMethods, …
Browse files Browse the repository at this point in the history
…CachedMethods and ForwardedValues
  • Loading branch information
jede committed Sep 30, 2020
1 parent e990c09 commit 8e65905
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
40 changes: 36 additions & 4 deletions lib/plugins/aws/package/compile/events/cloudFront/index.js
Expand Up @@ -85,13 +85,45 @@ class AwsCompileCloudFrontEvents {
properties: {
AllowedMethods: {
oneOf: [
{ enum: ['GET', 'HEAD'] },
{ enum: ['GET', 'HEAD', 'OPTIONS'] },
{ enum: ['GET', 'HEAD', 'OPTIONS', 'PUT', 'PATCH', 'POST', 'DELETE'] },
{
uniqueItems: true,
minItems: 2,
items: { enum: ['GET', 'HEAD'] },
},
{
uniqueItems: true,
minItems: 3,
items: { enum: ['GET', 'HEAD', 'OPTIONS'] },
},
{
uniqueItems: true,
minItems: 7,
items: { enum: ['GET', 'HEAD', 'OPTIONS', 'PUT', 'PATCH', 'POST', 'DELETE'] },
},
],
},
CachedMethods: {
oneOf: [{ enum: ['GET', 'HEAD'] }, { enum: ['GET', 'HEAD', 'OPTIONS'] }],
oneOf: [
{
uniqueItems: true,
minItems: 2,
items: { enum: ['GET', 'HEAD'] },
},
{
uniqueItems: true,
minItems: 3,
items: { enum: ['GET', 'HEAD', 'OPTIONS'] },
},
],
},
ForwardedValues: {
Cookies: {
Forward: { type: 'string' },
WhitelistedNames: { items: { type: 'string' } },
},
Headers: { items: { type: 'string' } },
QueryString: { type: 'boolean' },
QueryStringCacheKeys: { items: { type: 'string' } },
},
CachePolicyId: { type: 'string' },
Compress: { type: 'boolean' },
Expand Down
33 changes: 32 additions & 1 deletion lib/plugins/aws/package/compile/events/cloudFront/index.test.js
Expand Up @@ -1662,14 +1662,45 @@ describe('AwsCompileCloudFrontEvents', () => {
});
});

describe('#AwsCompileCloudFrontEvents() Remove VPC & Env Vars', () => {
describe('#AwsCompileCloudFrontEvents() package', () => {
const fixtureName = 'functionCloudFront';
const edgeFunctionName = 'foo';
const otherFunctionName = 'bar';
function extendAndPackage(extension) {
return runServerless({ fixture: fixtureName, configExt: extension, cliArgs: ['package'] });
}

it('Should validate configuration schema', () => {
return extendAndPackage({
functions: {
[edgeFunctionName]: {
handler: 'myLambdaAtEdge.handler',
events: [
{
cloudFront: {
origin: 's3://bucketname.s3.amazonaws.com/files',
eventType: 'viewer-response',
behavior: {
ViewerProtocolPolicy: 'https-only',
ForwardedValues: {
QueryString: true,
},
AllowedMethods: ['GET', 'HEAD'],
CachedMethods: ['GET', 'HEAD', 'OPTIONS'],
},
},
},
],
},
},
}).then(({ cfTemplate }) => {
const behavior =
cfTemplate.Resources.CloudFrontDistribution.Properties.DistributionConfig
.DefaultCacheBehavior;
expect(behavior).to.contain.keys('AllowedMethods', 'CachedMethods', 'ForwardedValues');
});
});

it('Should ignore Environment variables if provided in function properties', () => {
return extendAndPackage({
functions: { [edgeFunctionName]: { environment: { HELLO: 'WORLD' } } },
Expand Down

0 comments on commit 8e65905

Please sign in to comment.