Skip to content

Commit

Permalink
Merge branch 'master' into fix-logical-version-id-for-layers
Browse files Browse the repository at this point in the history
  • Loading branch information
pwithams committed Sep 22, 2020
2 parents eee300e + feece9a commit 76bf822
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 388 deletions.
31 changes: 26 additions & 5 deletions lib/classes/ConfigSchemaHandler/index.js
Expand Up @@ -23,12 +23,35 @@ 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 normalizeUserConfig = object => {
if (normalizedObjectsMap.has(object)) return normalizedObjectsMap.get(object);
if (Array.isArray(object)) {
const normalizedObject = [];
normalizedObjectsMap.set(object, normalizedObject);
for (const value of object) {
normalizedObject.push(_.isObject(value) ? normalizeUserConfig(value) : 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;
};

class ConfigSchemaHandler {
constructor(serverless) {
this.serverless = serverless;
this.schema = _.cloneDeep(schema);

deepFreeze(this.schema.properties.service);
// TODO: Switch back to deepFreeze(this.schema.properties.service) once awsKmsKeyArn property is removed, see https://github.com/serverless/serverless/issues/8261
Object.freeze(this.schema.properties.service.name);
deepFreeze(this.schema.properties.plugins);
deepFreeze(this.schema.properties.package);
Object.freeze(this.schema.properties.layers);
Expand Down Expand Up @@ -77,11 +100,9 @@ class ConfigSchemaHandler {
normalizeSchemaObject(this.schema, this.schema);
const validate = ajv.compile(this.schema);

validate(userConfig);
validate(normalizeUserConfig(userConfig));
if (validate.errors) {
const messages = normalizeAjvErrors(validate.errors, userConfig, this.schema).map(
err => err.message
);
const messages = normalizeAjvErrors(validate.errors).map(err => err.message);
this.handleErrorMessages(messages);
}
}
Expand Down
7 changes: 6 additions & 1 deletion lib/configSchema.js
Expand Up @@ -9,7 +9,7 @@ const schema = {
type: 'object',
properties: {
name: { pattern: '^[a-zA-Z][0-9a-zA-Z-]+$' },
awsKmsKeyArn: { pattern: '^arn:(aws[a-zA-Z-]*)?:kms:[a-z0-9-]+-\\d+:\\d{12}:[^\\s]+$' },
awsKmsKeyArn: { $ref: '#/definitions/awsKmsArn' },
},
additionalProperties: false,
},
Expand Down Expand Up @@ -125,6 +125,11 @@ const schema = {
additionalProperties: false,
required: ['provider', 'service'],
definitions: {
// TODO: awsKmsArn definition to be moved to lib/plugins/aws/provider/awsProvider.js once service.awsKmsKeyArn moved to provider.awsKmsKeyArn, see https://github.com/serverless/serverless/issues/8261
// TODO: awsKmsArn to include #/definitions/awsCfFunction instead of type: object as one of the possible definition, see https://github.com/serverless/serverless/issues/8261
awsKmsArn: {
anyOf: [{ type: 'object' }, { type: 'string', pattern: '^arn:aws[a-z-]*:kms' }],
},
errorCode: {
type: 'string',
pattern: '^[A-Z0-9_]+$',
Expand Down
3 changes: 1 addition & 2 deletions lib/configSchema.test.js
Expand Up @@ -21,8 +21,7 @@ describe('#configSchema', () => {
},
{
isValid: false,
errorMessage:
'should match pattern "^arn:(aws[a-zA-Z-]*)?:kms:[a-z0-9-]+-\\d+:\\d{12}:[^\\s]+$',
errorMessage: 'should match pattern "^arn:aws[a-z-]*:kms',
description: 'service awsKmsKeyArn',
mutation: {
service: {
Expand Down
8 changes: 5 additions & 3 deletions lib/plugins/aws/package/compile/events/sns/index.js
Expand Up @@ -118,9 +118,11 @@ class AwsCompileSNSEvents {
targetUrl = {
'Fn::Join': [
'',
`https://sqs.${deRegion}.`,
{ Ref: 'AWS::URLSuffix' },
`/${deAccount}/${deQueueName}`,
[
`https://sqs.${deRegion}.`,
{ Ref: 'AWS::URLSuffix' },
`/${deAccount}/${deQueueName}`,
],
],
};
} else if (deadLetterTargetRef) {
Expand Down
17 changes: 17 additions & 0 deletions lib/plugins/aws/package/compile/events/sns/index.test.js
Expand Up @@ -591,6 +591,23 @@ describe('AwsCompileSNSEvents', () => {
awsCompileSNSEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources
.Topic1ToFirstDLQPolicy.Type
).to.equal('AWS::SQS::QueuePolicy');
expect(
awsCompileSNSEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources
.Topic1ToFirstDLQPolicy.Properties.Queues
).to.deep.equal([
{
'Fn::Join': [
'',
[
'https://sqs.us-east-1.',
{
Ref: 'AWS::URLSuffix',
},
'/11111111111/myDLQ',
],
],
},
]);
});

it('should link topic to corresponding dlq when redrivePolicy is defined with resource ref', () => {
Expand Down

0 comments on commit 76bf822

Please sign in to comment.