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

Coerce configuration input #8319

Merged
merged 5 commits into from Oct 1, 2020
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
31 changes: 15 additions & 16 deletions lib/classes/ConfigSchemaHandler/index.js
Expand Up @@ -25,24 +25,21 @@ const normalizeSchemaObject = (object, instanceSchema) => {

// Normalizer is introduced to workaround https://github.com/ajv-validator/ajv/issues/1287
// normalizedObjectsMap allows to handle circular structures without issues
const normalizedObjectsMap = new WeakMap();
const normalizedObjectsSet = new WeakSet();
const normalizeUserConfig = object => {
if (normalizedObjectsMap.has(object)) return normalizedObjectsMap.get(object);
if (normalizedObjectsSet.has(object)) return object;
normalizedObjectsSet.add(object);
if (Array.isArray(object)) {
const normalizedObject = [];
normalizedObjectsMap.set(object, normalizedObject);
for (const value of object) {
normalizedObject.push(_.isObject(value) ? normalizeUserConfig(value) : value);
if (_.isObject(value)) normalizeUserConfig(value);
}
} else {
for (const [key, value] of Object.entries(object)) {
if (value == null) delete object[key];
else if (_.isObject(value)) normalizeUserConfig(value);
}
return normalizedObject;
}
const normalizedObject = Object.create(null);
normalizedObjectsMap.set(object, normalizedObject);
for (const [key, value] of Object.entries(object)) {
if (value == null) continue;
normalizedObject[key] = _.isObject(value) ? normalizeUserConfig(value) : value;
}
return normalizedObject;
return object;
};

class ConfigSchemaHandler {
Expand Down Expand Up @@ -94,7 +91,7 @@ class ConfigSchemaHandler {
this.relaxProviderSchema();
}

const ajv = new Ajv({ allErrors: true, verbose: true });
const ajv = new Ajv({ allErrors: true, coerceTypes: 'array', verbose: true });
require('ajv-keywords')(ajv, 'regexp');
// Workaround https://github.com/ajv-validator/ajv/issues/1255
normalizeSchemaObject(this.schema, this.schema);
Expand Down Expand Up @@ -210,13 +207,15 @@ class ConfigSchemaHandler {
}

relaxProviderSchema() {
// provider
this.schema.properties.provider.additionalProperties = true;

// functions[]
this.schema.properties.functions.patternProperties[
FUNCTION_NAME_PATTERN
].additionalProperties = true;

// Do not report errors regarding unsupported function events as
// their schemas are not defined.
// functions[].events[]
if (
Array.isArray(
this.schema.properties.functions.patternProperties[FUNCTION_NAME_PATTERN].properties.events
Expand Down
1 change: 0 additions & 1 deletion lib/configSchema.js
Expand Up @@ -17,7 +17,6 @@ const schema = {
disabledDeprecations: {
anyOf: [
{ const: '*' },
{ $ref: '#/definitions/errorCode' },
{
type: 'array',
items: { $ref: '#/definitions/errorCode' },
Expand Down
16 changes: 7 additions & 9 deletions lib/plugins/aws/package/compile/events/alb/index.js
Expand Up @@ -7,10 +7,8 @@ const compileTargetGroups = require('./lib/targetGroups');
const compileListenerRules = require('./lib/listenerRules');
const compilePermissions = require('./lib/permissions');

function arrayOrSingleSchema(schema) {
return {
oneOf: [schema, { type: 'array', items: schema }],
};
function defineArray(schema) {
return { type: 'array', items: schema };
}

class AwsCompileAlbEvents {
Expand All @@ -37,7 +35,7 @@ class AwsCompileAlbEvents {
this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'alb', {
type: 'object',
properties: {
authorizer: arrayOrSingleSchema({ type: 'string' }),
authorizer: defineArray({ type: 'string' }),
conditions: {
type: 'object',
properties: {
Expand All @@ -50,19 +48,19 @@ class AwsCompileAlbEvents {
additionalProperties: false,
required: ['name', 'values'],
},
host: arrayOrSingleSchema({
host: defineArray({
type: 'string',
pattern: '^[A-Za-z0-9*?.-]+$',
maxLength: 128,
}),
ip: arrayOrSingleSchema({
ip: defineArray({
oneOf: [
{ type: 'string', format: 'ipv4' },
{ type: 'string', format: 'ipv6' },
],
}),
method: arrayOrSingleSchema({ type: 'string', pattern: '^[A-Z_-]+$', maxLength: 40 }),
path: arrayOrSingleSchema({
method: defineArray({ type: 'string', pattern: '^[A-Z_-]+$', maxLength: 40 }),
path: defineArray({
type: 'string',
pattern: '^([A-Za-z0-9*?_.$/~"\'@:+-]|&)+$',
maxLength: 128,
Expand Down
4 changes: 1 addition & 3 deletions lib/plugins/aws/package/compile/events/httpApi/index.js
Expand Up @@ -68,9 +68,7 @@ class HttpApiEvents {
anyOf: [{ type: 'string' }, { $ref: '#/definitions/awsCfFunction' }],
},
name: { type: 'string' },
scopes: {
oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
scopes: { type: 'array', items: { type: 'string' } },
},
oneOf: [{ required: ['id'] }, { required: ['name'] }],
additionalProperties: false,
Expand Down
53 changes: 12 additions & 41 deletions lib/plugins/aws/provider/awsProvider.js
Expand Up @@ -282,9 +282,7 @@ class AwsProvider {
required: ['Fn::Sub'],
additionalProperties: false,
},
awsIamPolicyAction: {
anyOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
awsIamPolicyAction: { type: 'array', items: { type: 'string' } },
awsIamPolicyPrincipal: {
anyOf: [
{ const: '*' },
Expand All @@ -294,30 +292,19 @@ class AwsProvider {
AWS: {
anyOf: [
{ const: '*' },
{ $ref: '#/definitions/awsArn' },
{ type: 'array', items: { $ref: '#/definitions/awsArn' } },
],
},
Federated: {
anyOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
Service: {
anyOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
CanonicalUser: {
anyOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
Federated: { type: 'array', items: { type: 'string' } },
Service: { type: 'array', items: { type: 'string' } },
CanonicalUser: { type: 'array', items: { type: 'string' } },
},
additionalProperties: false,
},
],
},
awsIamPolicyResource: {
anyOf: [
{ const: '*' },
{ $ref: '#/definitions/awsArn' },
{ type: 'array', items: { $ref: '#/definitions/awsArn' } },
],
anyOf: [{ const: '*' }, { type: 'array', items: { $ref: '#/definitions/awsArn' } }],
},
// Definition of Statement taken from https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_grammar.html#policies-grammar-bnf
awsIamPolicyStatements: {
Expand Down Expand Up @@ -405,9 +392,7 @@ class AwsProvider {
pattern: '^[/#A-Za-z0-9-_.]+$',
},
awsResourceCondition: { type: 'string' },
awsResourceDependsOn: {
oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
awsResourceDependsOn: { type: 'array', items: { type: 'string' } },
awsResourceProperties: {
Properties: { type: 'object' },
CreationPolicy: { type: 'object' },
Expand Down Expand Up @@ -489,14 +474,8 @@ class AwsProvider {
identitySource: { $ref: '#/definitions/awsCfInstruction' },
issuerUrl: { $ref: '#/definitions/awsCfInstruction' },
audience: {
oneOf: [
{ $ref: '#/definitions/awsCfInstruction' },
{
type: 'array',
items: { $ref: '#/definitions/awsCfInstruction' },
additionalItems: false,
},
],
type: 'array',
items: { $ref: '#/definitions/awsCfInstruction' },
},
},
required: ['identitySource', 'issuerUrl', 'audience'],
Expand All @@ -510,18 +489,10 @@ class AwsProvider {
type: 'object',
properties: {
allowCredentials: { type: 'boolean' },
allowedHeaders: {
oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
allowedMethods: {
oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
allowedOrigins: {
oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
exposedResponseHeaders: {
oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
},
allowedHeaders: { type: 'array', items: { type: 'string' } },
allowedMethods: { type: 'array', items: { type: 'string' } },
allowedOrigins: { type: 'array', items: { type: 'string' } },
exposedResponseHeaders: { type: 'array', items: { type: 'string' } },
maxAge: { type: 'integer', minimum: 0 },
},
additionalProperties: false,
Expand Down