Skip to content
Open
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
27 changes: 25 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class ServerlessFullstackPlugin {
this.prepareComment(distributionConfig);
this.prepareCertificate(distributionConfig);
this.prepareWaf(distributionConfig);
this.prepareSinglePageApp(resources.Resources);
this.prepareSiteAccess(resources.Resources);
this.prepareS3(resources.Resources);
this.prepareMinimumProtocolVersion(distributionConfig);
this.prepareCompressWebContent(distributionConfig);
Expand Down Expand Up @@ -424,9 +424,11 @@ class ServerlessFullstackPlugin {
}
}

prepareSinglePageApp(resources) {
prepareSiteAccess(resources) {
const distributionConfig = resources.ApiDistribution.Properties.DistributionConfig;
const isSinglePageApp = this.getConfig('singlePageApp', false);
const isS3Private = this.getConfig('restrictS3Access', false);

if (isSinglePageApp) {
this.serverless.cli.log(`Configuring distribution for single page web app...`);
const indexDocument = this.getConfig('indexDocument', 'index.html')
Expand All @@ -445,13 +447,34 @@ class ServerlessFullstackPlugin {
return statement.Sid !== 'AllowPublicRead';
});

for (let origin of distributionConfig.Origins) {
if (origin.Id === 'WebApp') {
delete origin.CustomOriginConfig;
}
}
Comment on lines +450 to +454

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refactor to remove duplicated code

} else if (isS3Private) {
this.serverless.cli.log(`Configuring distribution to be the only access point...`);

const restrictedCustomErrorResponses = this.getConfig('restrictS3ErrorResponses', []);
const errorResponseTemplate = distributionConfig.CustomErrorResponses[0];
distributionConfig.CustomErrorResponses = _.map(restrictedCustomErrorResponses, (customResponse) => {
return { ...errorResponseTemplate, ...customResponse};
})

// Remove direct public read access to bucket, all requests must go through CloudFront
const statements = resources.WebAppS3BucketPolicy.Properties.PolicyDocument.Statement;
resources.WebAppS3BucketPolicy.Properties.PolicyDocument.Statement = _.filter(statements, (statement) => {
return statement.Sid !== 'AllowPublicRead';
});

for (let origin of distributionConfig.Origins) {
if (origin.Id === 'WebApp') {
delete origin.CustomOriginConfig;
}
}
} else {
delete distributionConfig.CustomErrorResponses;

delete resources.S3OriginAccessIdentity;

// Remove API access to S3 bucket since all content will be served through http
Expand Down