Skip to content

Commit

Permalink
feat(Config Schema): Schema for AWS provider properties (#8297)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericbarthelet committed Oct 1, 2020
1 parent dd9a011 commit 38c2047
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 364 deletions.
2 changes: 1 addition & 1 deletion lib/plugins/aws/deploy/lib/createStack.js
Expand Up @@ -11,7 +11,7 @@ module.exports = {
let stackTags = { STAGE: this.provider.getStage() };

// Merge additional stack tags
if (typeof this.serverless.service.provider.stackTags === 'object') {
if (this.serverless.service.provider.stackTags) {
const customKeys = Object.keys(this.serverless.service.provider.stackTags);
const collisions = Object.keys(stackTags).filter(defaultKey =>
customKeys.some(key => defaultKey.toLowerCase() === key.toLowerCase())
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/aws/lib/updateStack.js
Expand Up @@ -17,7 +17,7 @@ module.exports = {
const templateUrl = `https://${s3Endpoint}/${this.bucketName}/${this.serverless.service.package.artifactDirectoryName}/${compiledTemplateFileName}`;

// Merge additional stack tags
if (typeof this.serverless.service.provider.stackTags === 'object') {
if (this.serverless.service.provider.stackTags) {
const customKeys = Object.keys(this.serverless.service.provider.stackTags);
const collisions = Object.keys(stackTags).filter(defaultKey =>
customKeys.some(key => defaultKey.toLowerCase() === key.toLowerCase())
Expand Down Expand Up @@ -74,7 +74,7 @@ module.exports = {
let stackTags = { STAGE: this.provider.getStage() };

// Merge additional stack tags
if (typeof this.serverless.service.provider.stackTags === 'object') {
if (this.serverless.service.provider.stackTags) {
const customKeys = Object.keys(this.serverless.service.provider.stackTags);
const collisions = Object.keys(stackTags).filter(defaultKey =>
customKeys.some(key => defaultKey.toLowerCase() === key.toLowerCase())
Expand Down
45 changes: 0 additions & 45 deletions lib/plugins/aws/lib/validateS3BucketName.js

This file was deleted.

114 changes: 0 additions & 114 deletions lib/plugins/aws/lib/validateS3BucketName.test.js

This file was deleted.

38 changes: 16 additions & 22 deletions lib/plugins/aws/package/lib/generateCoreTemplate.js
Expand Up @@ -4,12 +4,8 @@ const BbPromise = require('bluebird');
const path = require('path');
const _ = require('lodash');

const validateS3BucketName = require('../../lib/validateS3BucketName');

module.exports = {
generateCoreTemplate() {
Object.assign(this, validateS3BucketName);

this.serverless.service.provider.compiledCloudFormationTemplate = this.serverless.utils.readFileSync(
path.join(
this.serverless.config.serverlessPath,
Expand Down Expand Up @@ -47,7 +43,7 @@ module.exports = {
}

// enable S3 block public access for deployment bucket
if (deploymentBucketObject.blockPublicAccess === true) {
if (deploymentBucketObject.blockPublicAccess) {
Object.assign(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources[
deploymentBucketLogicalId
Expand Down Expand Up @@ -76,23 +72,21 @@ module.exports = {
}

if (bucketName) {
return BbPromise.bind(this)
.then(() => this.validateS3BucketName(bucketName))
.then(() => {
if (isS3TransferAccelerationEnabled) {
const warningMessage =
'Warning: S3 Transfer Acceleration will not be enabled on deploymentBucket.';
this.serverless.cli.log(warningMessage);
}
this.bucketName = bucketName;
this.serverless.service.package.deploymentBucket = bucketName;
this.serverless.service.provider.compiledCloudFormationTemplate.Outputs.ServerlessDeploymentBucketName.Value = bucketName;

delete this.serverless.service.provider.compiledCloudFormationTemplate.Resources
.ServerlessDeploymentBucket;
delete this.serverless.service.provider.compiledCloudFormationTemplate.Resources
.ServerlessDeploymentBucketPolicy;
});
return BbPromise.bind(this).then(() => {
if (isS3TransferAccelerationEnabled) {
const warningMessage =
'Warning: S3 Transfer Acceleration will not be enabled on deploymentBucket.';
this.serverless.cli.log(warningMessage);
}
this.bucketName = bucketName;
this.serverless.service.package.deploymentBucket = bucketName;
this.serverless.service.provider.compiledCloudFormationTemplate.Outputs.ServerlessDeploymentBucketName.Value = bucketName;

delete this.serverless.service.provider.compiledCloudFormationTemplate.Resources
.ServerlessDeploymentBucket;
delete this.serverless.service.provider.compiledCloudFormationTemplate.Resources
.ServerlessDeploymentBucketPolicy;
});
}

if (isS3TransferAccelerationEnabled && isS3TransferAccelerationSupported) {
Expand Down
54 changes: 0 additions & 54 deletions lib/plugins/aws/package/lib/mergeIamTemplates.js
Expand Up @@ -6,12 +6,6 @@ const path = require('path');

module.exports = {
mergeIamTemplates() {
this.validateStatements(this.serverless.service.provider.iamRoleStatements);
this.validateManagedPolicies(this.serverless.service.provider.iamManagedPolicies);
return this.merge();
},

merge() {
// resolve early if no functions are provided
if (!this.serverless.service.getAllFunctions().length) {
return BbPromise.resolve();
Expand Down Expand Up @@ -188,52 +182,4 @@ module.exports = {
}
resource.ManagedPolicyArns = resource.ManagedPolicyArns.concat(managedPolicies);
},

validateStatements(statements) {
// Verify that iamRoleStatements (if present) is an array of { Effect: ...,
// Action: ..., Resource: ... } objects.
if (!statements) {
return;
}
let violationsFound;
if (!Array.isArray(statements)) {
violationsFound = 'it is not an array';
} else {
const descriptions = statements.map((statement, i) => {
const missing = [
['Effect'],
['Action', 'NotAction'],
['Resource', 'NotResource'],
].filter(props => props.every(prop => !statement[prop]));
return missing.length === 0
? null
: `statement ${i} is missing the following properties: ${missing
.map(m => m.join(' / '))
.join(', ')}`;
});
const flawed = descriptions.filter(curr => curr);
if (flawed.length) {
violationsFound = flawed.join('; ');
}
}

if (violationsFound) {
const errorMessage = [
'iamRoleStatements should be an array of objects,',
' where each object has Effect, Action / NotAction, Resource / NotResource fields.',
` Specifically, ${violationsFound}`,
].join('');
throw new this.serverless.classes.Error(errorMessage);
}
},

validateManagedPolicies(iamManagedPolicies) {
// Verify that iamManagedPolicies (if present) is an array
if (!iamManagedPolicies) {
return;
}
if (!Array.isArray(iamManagedPolicies)) {
throw new this.serverless.classes.Error('iamManagedPolicies should be an array of arns');
}
},
};

0 comments on commit 38c2047

Please sign in to comment.