-
Notifications
You must be signed in to change notification settings - Fork 88
/
index.js
176 lines (140 loc) · 5.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const path = require('path');
const _ = require('lodash');
const chalk = require('chalk');
const yaml = require('js-yaml');
const fs = require('fs');
class ServerlessApiCloudFrontPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.hooks = {
'before:deploy:createDeploymentArtifacts': this.createDeploymentArtifacts.bind(this),
'aws:info:displayStackOutputs': this.printSummary.bind(this),
};
}
createDeploymentArtifacts() {
const baseResources = this.serverless.service.provider.compiledCloudFormationTemplate;
const filename = path.resolve(__dirname, 'resources.yml');
const content = fs.readFileSync(filename, 'utf-8');
const resources = yaml.safeLoad(content, {
filename: filename
});
this.prepareResources(resources);
return _.merge(baseResources, resources);
}
printSummary() {
const cloudTemplate = this.serverless;
const awsInfo = _.find(this.serverless.pluginManager.getPlugins(), (plugin) => {
return plugin.constructor.name === 'AwsInfo';
});
if (!awsInfo || !awsInfo.gatheredData) {
return;
}
const outputs = awsInfo.gatheredData.outputs;
const apiDistributionDomain = _.find(outputs, (output) => {
return output.OutputKey === 'ApiDistribution';
});
if (!apiDistributionDomain || !apiDistributionDomain.OutputValue) {
return ;
}
const cnameDomain = this.getConfig('domain', '-');
this.serverless.cli.consoleLog(chalk.yellow('CloudFront domain name'));
this.serverless.cli.consoleLog(` ${apiDistributionDomain.OutputValue} (CNAME: ${cnameDomain})`);
}
prepareResources(resources) {
const distributionConfig = resources.Resources.ApiDistribution.Properties.DistributionConfig;
this.prepareLogging(distributionConfig);
this.prepareDomain(distributionConfig);
this.preparePriceClass(distributionConfig);
this.prepareOrigins(distributionConfig);
this.prepareCookies(distributionConfig);
this.prepareHeaders(distributionConfig);
this.prepareQueryString(distributionConfig);
this.prepareComment(distributionConfig);
this.prepareCertificate(distributionConfig);
this.prepareWaf(distributionConfig);
this.prepareCompress(distributionConfig);
this.prepareMinimumProtocolVersion(distributionConfig);
}
prepareLogging(distributionConfig) {
const loggingBucket = this.getConfig('logging.bucket', null);
if (loggingBucket !== null) {
distributionConfig.Logging.Bucket = loggingBucket;
distributionConfig.Logging.Prefix = this.getConfig('logging.prefix', '');
} else {
delete distributionConfig.Logging;
}
}
prepareDomain(distributionConfig) {
const domain = this.getConfig('domain', null);
if (domain !== null) {
distributionConfig.Aliases = Array.isArray(domain) ? domain : [ domain ];
} else {
delete distributionConfig.Aliases;
}
}
preparePriceClass(distributionConfig) {
const priceClass = this.getConfig('priceClass', 'PriceClass_All');
distributionConfig.PriceClass = priceClass;
}
prepareOrigins(distributionConfig) {
distributionConfig.Origins[0].OriginPath = `/${this.options.stage}`;
}
prepareCookies(distributionConfig) {
const forwardCookies = this.getConfig('cookies', 'all');
distributionConfig.DefaultCacheBehavior.ForwardedValues.Cookies.Forward = Array.isArray(forwardCookies) ? 'whitelist' : forwardCookies;
if (Array.isArray(forwardCookies)) {
distributionConfig.DefaultCacheBehavior.ForwardedValues.Cookies.WhitelistedNames = forwardCookies;
}
}
prepareHeaders(distributionConfig) {
const forwardHeaders = this.getConfig('headers', 'none');
if (Array.isArray(forwardHeaders)) {
distributionConfig.DefaultCacheBehavior.ForwardedValues.Headers = forwardHeaders;
} else {
distributionConfig.DefaultCacheBehavior.ForwardedValues.Headers = forwardHeaders === 'none' ? [] : ['*'];
}
}
prepareQueryString(distributionConfig) {
const forwardQueryString = this.getConfig('querystring', 'all');
if (Array.isArray(forwardQueryString)) {
distributionConfig.DefaultCacheBehavior.ForwardedValues.QueryString = true;
distributionConfig.DefaultCacheBehavior.ForwardedValues.QueryStringCacheKeys = forwardQueryString;
} else {
distributionConfig.DefaultCacheBehavior.ForwardedValues.QueryString = forwardQueryString === 'all' ? true : false;
}
}
prepareComment(distributionConfig) {
const name = this.serverless.getProvider('aws').naming.getApiGatewayName();
distributionConfig.Comment = `Serverless Managed ${name}`;
}
prepareCertificate(distributionConfig) {
const certificate = this.getConfig('certificate', null);
if (certificate !== null) {
distributionConfig.ViewerCertificate.AcmCertificateArn = certificate;
} else {
delete distributionConfig.ViewerCertificate;
}
}
prepareWaf(distributionConfig) {
const waf = this.getConfig('waf', null);
if (waf !== null) {
distributionConfig.WebACLId = waf;
} else {
delete distributionConfig.WebACLId;
}
}
prepareCompress(distributionConfig) {
distributionConfig.DefaultCacheBehavior.Compress = (this.getConfig('compress', false) === true) ? true : false;
}
prepareMinimumProtocolVersion(distributionConfig) {
const minimumProtocolVersion = this.getConfig('minimumProtocolVersion', undefined);
if (minimumProtocolVersion) {
distributionConfig.ViewerCertificate.MinimumProtocolVersion = minimumProtocolVersion;
}
}
getConfig(field, defaultValue) {
return _.get(this.serverless, `service.custom.apiCloudFront.${field}`, defaultValue)
}
}
module.exports = ServerlessApiCloudFrontPlugin;