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

Support for custom CORS file #103

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ class Client {
return Promise.resolve();
}
this.serverless.cli.log(`Configuring CORS for bucket...`);
return configure.configureCorsForBucket(this.aws, bucketName);
const bucketCorsFile = this.serverless.service.custom.client.bucketCorsFile;
const customCors =
bucketCorsFile && JSON.parse(fs.readFileSync(bucketCorsFile));

return configure.configureCorsForBucket(this.aws, bucketName, customCors);
})
.then(() => {
this.serverless.cli.log(`Uploading client files to bucket...`);
Expand Down
6 changes: 4 additions & 2 deletions lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ function configurePolicyForBucket(aws, bucketName, customPolicy) {
* @param {Object} aws - AWS class
* @param {string} bucketName - Name of bucket to be configured
*/
function configureCorsForBucket(aws, bucketName) {
function configureCorsForBucket(aws, bucketName, customCors) {
const cors = customCors || require('./resources/CORSPolicy')

const params = {
Bucket: bucketName,
CORSConfiguration: require('./resources/CORSPolicy')
CORSConfiguration: cors
};

return aws.request('S3', 'putBucketCors', params);
Expand Down
11 changes: 11 additions & 0 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ function validateClient(serverless, options) {
);
}
}

// check that custom bucket CORS is valid json
if (options.bucketCorsFile) {
try {
JSON.parse(fs.readFileSync(options.bucketCorsFile));
} catch (e) {
validationErrors.push(
`Failed to read and/or parse specified CORS policy. Make sure it is valid JSON.`
);
}
}

// check website configuration options

Expand Down