-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
174 lines (135 loc) · 4.88 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
const parseInputOrigins = require('./parseInputOrigins')
const getDefaultCacheBehavior = require('./getDefaultCacheBehavior')
const createOriginAccessIdentity = require('./createOriginAccessIdentity')
const grantCloudFrontBucketAccess = require('./grantCloudFrontBucketAccess')
const servePrivateContentEnabled = (inputs) =>
inputs.origins.some((origin) => {
return origin && origin.private === true
})
const updateBucketsPolicies = async (s3, origins, s3CanonicalUserId) => {
// update bucket policies with cloudfront access
const bucketNames = origins.Items.filter((origin) => origin.S3OriginConfig).map(
(origin) => origin.Id
)
return Promise.all(
bucketNames.map((bucketName) => grantCloudFrontBucketAccess(s3, bucketName, s3CanonicalUserId))
)
}
const createCloudFrontDistribution = async (cf, s3, inputs) => {
const params = {
DistributionConfig: {
CallerReference: String(Date.now()),
Comment: inputs.comment,
Aliases: {
Quantity: 0,
Items: []
},
Origins: {
Quantity: 0,
Items: []
},
PriceClass: 'PriceClass_All',
Enabled: inputs.enabled,
HttpVersion: 'http2'
}
}
const distributionConfig = params.DistributionConfig
let originAccessIdentityId
let s3CanonicalUserId
if (servePrivateContentEnabled(inputs)) {
;({ originAccessIdentityId, s3CanonicalUserId } = await createOriginAccessIdentity(cf))
}
const { Origins, CacheBehaviors } = parseInputOrigins(inputs.origins, { originAccessIdentityId })
if (s3CanonicalUserId) {
await updateBucketsPolicies(s3, Origins, s3CanonicalUserId)
}
distributionConfig.Origins = Origins
// set first origin declared as the default cache behavior
distributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior(
Origins.Items[0].Id,
inputs.defaults
)
if (CacheBehaviors) {
distributionConfig.CacheBehaviors = CacheBehaviors
}
const res = await cf.createDistribution(params).promise()
return {
id: res.Distribution.Id,
arn: res.Distribution.ARN,
url: `https://${res.Distribution.DomainName}`
}
}
const updateCloudFrontDistribution = async (cf, s3, distributionId, inputs) => {
// Update logic is a bit weird...
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFront.html#updateDistribution-property
// 1. we gotta get the config first...
// todo what if id does not exist?
const params = await cf.getDistributionConfig({ Id: distributionId }).promise()
// 2. then add this property
params.IfMatch = params.ETag
// 3. then delete this property
delete params.ETag
// 4. then set this property
params.Id = distributionId
// 5. then make our changes
params.DistributionConfig.Enabled = inputs.enabled
params.DistributionConfig.Comment = inputs.comment
let s3CanonicalUserId
let originAccessIdentityId
if (servePrivateContentEnabled(inputs)) {
// presumably it's ok to call create origin access identity again
// aws api returns cached copy of what was previously created
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFront.html#createCloudFrontOriginAccessIdentity-property
;({ originAccessIdentityId, s3CanonicalUserId } = await createOriginAccessIdentity(cf))
}
const { Origins, CacheBehaviors } = parseInputOrigins(inputs.origins, { originAccessIdentityId })
if (s3CanonicalUserId) {
await updateBucketsPolicies(s3, Origins, s3CanonicalUserId)
}
params.DistributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior(
Origins.Items[0].Id,
inputs.defaults
)
params.DistributionConfig.Origins = Origins
if (CacheBehaviors) {
params.DistributionConfig.CacheBehaviors = CacheBehaviors
}
// 6. then finally update!
const res = await cf.updateDistribution(params).promise()
return {
id: res.Distribution.Id,
arn: res.Distribution.ARN,
url: `https://${res.Distribution.DomainName}`
}
}
const disableCloudFrontDistribution = async (cf, distributionId) => {
const params = await cf.getDistributionConfig({ Id: distributionId }).promise()
params.IfMatch = params.ETag
delete params.ETag
params.Id = distributionId
params.DistributionConfig.Enabled = false
const res = await cf.updateDistribution(params).promise()
return {
id: res.Distribution.Id,
arn: res.Distribution.ARN,
url: `https://${res.Distribution.DomainName}`
}
}
const deleteCloudFrontDistribution = async (cf, distributionId) => {
try {
const res = await cf.getDistributionConfig({ Id: distributionId }).promise()
const params = { Id: distributionId, IfMatch: res.ETag }
await cf.deleteDistribution(params).promise()
} catch (e) {
if (e.code === 'DistributionNotDisabled') {
await disableCloudFrontDistribution(cf, distributionId)
} else {
throw e
}
}
}
module.exports = {
createCloudFrontDistribution,
updateCloudFrontDistribution,
deleteCloudFrontDistribution
}