forked from caktus/aws-web-stacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets.py
504 lines (477 loc) · 16.4 KB
/
assets.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
from troposphere import (
AWS_REGION,
And,
Equals,
GetAtt,
If,
Join,
Not,
NoValue,
Output,
Ref,
Split,
iam
)
from troposphere.certificatemanager import Certificate, DomainValidationOption
from troposphere.cloudfront import (
DefaultCacheBehavior,
Distribution,
DistributionConfig,
ForwardedValues,
Origin,
S3OriginConfig,
ViewerCertificate
)
from troposphere.s3 import (
Bucket,
BucketEncryption,
CorsConfiguration,
CorsRules,
Private,
PublicAccessBlockConfiguration,
ServerSideEncryptionByDefault,
ServerSideEncryptionRule,
VersioningConfiguration
)
from . import USE_GOVCLOUD
from .common import (
arn_prefix,
cmk_arn,
use_aes256_encryption_cond,
use_cmk_arn
)
from .domain import domain_name, domain_name_alternates, no_alt_domains
from .sftp import use_sftp_condition, use_sftp_with_kms_condition
from .template import template
from .utils import ParameterWithDefaults as Parameter
assets_bucket_access_control = template.add_parameter(
Parameter(
"AssetsBucketAccessControl",
Default="PublicRead",
Description="Canned ACL for the public S3 bucket. Private is recommended; it "
"allows for objects to be make publicly readable, but prevents "
"listing of the bucket contents.",
Type="String",
AllowedValues=[
"PublicRead",
"Private",
],
ConstraintDescription="Must be PublicRead or Private.",
),
group="Static Media",
label="Assets Bucket ACL",
)
common_bucket_conf = dict(
VersioningConfiguration=VersioningConfiguration(
Status="Enabled"
),
DeletionPolicy="Retain",
CorsConfiguration=CorsConfiguration(
CorsRules=[CorsRules(
AllowedOrigins=Split(";", Join("", [
"https://", domain_name,
If(
no_alt_domains,
# if we don't have any alternate domains, return an empty string
"",
# otherwise, return the ';https://' that will be needed by the first domain
";https://",
),
# then, add all the alternate domains, joined together with ';https://'
Join(";https://", domain_name_alternates),
# now that we have a string of origins separated by ';', Split() is used to make it into a list again
])),
AllowedMethods=[
"POST",
"PUT",
"HEAD",
"GET",
],
AllowedHeaders=[
"*",
],
)],
),
)
# Create an S3 bucket that holds statics and media. Default to private to prevent
# public list permissions, but still allow objects to be made publicly readable.
assets_bucket = template.add_resource(
Bucket(
"AssetsBucket",
AccessControl=Ref(assets_bucket_access_control),
BucketEncryption=If(
use_aes256_encryption_cond,
BucketEncryption(
ServerSideEncryptionConfiguration=[
ServerSideEncryptionRule(
ServerSideEncryptionByDefault=ServerSideEncryptionByDefault(
SSEAlgorithm='AES256'
)
)
]
),
NoValue
),
**common_bucket_conf,
)
)
# Output S3 asset bucket name
template.add_output(
Output(
"AssetsBucketDomainName",
Description="Assets bucket domain name",
Value=GetAtt(assets_bucket, "DomainName"),
)
)
# Create an S3 bucket that holds user uploads or other non-public files
private_assets_bucket = template.add_resource(
Bucket(
"PrivateAssetsBucket",
AccessControl=Private,
PublicAccessBlockConfiguration=PublicAccessBlockConfiguration(
BlockPublicAcls=True,
BlockPublicPolicy=True,
IgnorePublicAcls=True,
RestrictPublicBuckets=True,
),
BucketEncryption=If(
use_aes256_encryption_cond,
BucketEncryption(
ServerSideEncryptionConfiguration=[
ServerSideEncryptionRule(
ServerSideEncryptionByDefault=ServerSideEncryptionByDefault(
SSEAlgorithm=If(use_cmk_arn, 'aws:kms', 'AES256'),
KMSMasterKeyID=If(use_cmk_arn, Ref(cmk_arn), Ref("AWS::NoValue")),
)
)
]
),
NoValue
),
**common_bucket_conf,
)
)
# Output S3 private assets bucket name
template.add_output(
Output(
"PrivateAssetsBucketDomainName",
Description="Private assets bucket domain name",
Value=GetAtt(private_assets_bucket, "DomainName"),
)
)
# Bucket for SFTP service
sftp_assets_bucket = Bucket(
"SFTPAssetsBucket",
Condition=use_sftp_condition,
AccessControl=Private,
PublicAccessBlockConfiguration=PublicAccessBlockConfiguration(
BlockPublicAcls=True,
BlockPublicPolicy=True,
IgnorePublicAcls=True,
RestrictPublicBuckets=True,
),
BucketEncryption=If(
use_aes256_encryption_cond,
BucketEncryption(
ServerSideEncryptionConfiguration=[
ServerSideEncryptionRule(
ServerSideEncryptionByDefault=ServerSideEncryptionByDefault(
SSEAlgorithm=If(use_cmk_arn, "aws:kms", "AES256"),
KMSMasterKeyID=If(
use_cmk_arn, Ref(cmk_arn), Ref("AWS::NoValue")
),
)
)
]
),
NoValue,
),
**common_bucket_conf,
)
template.add_resource(sftp_assets_bucket)
# Output SFTP asset bucket name
template.add_output(
Output(
"SFTPBucketDomainName",
Condition=use_sftp_condition,
Description="SFTP bucket domain name",
Value=GetAtt(sftp_assets_bucket, "DomainName"),
)
)
assets_management_policy_statements = [
dict(
Effect="Allow",
Action=["s3:ListBucket"],
Resource=Join("", [arn_prefix, ":s3:::", Ref(assets_bucket)]),
),
dict(
Effect="Allow",
Action=["s3:*"],
Resource=Join("", [arn_prefix, ":s3:::", Ref(assets_bucket), "/*"]),
),
dict(
Effect="Allow",
Action=["s3:ListBucket"],
Resource=Join("", [arn_prefix, ":s3:::", Ref(private_assets_bucket)]),
),
dict(
Effect="Allow",
Action=["s3:*"],
Resource=Join("", [arn_prefix, ":s3:::", Ref(private_assets_bucket), "/*"]),
),
]
assets_management_policy_statements_including_sftp_bucket = (
assets_management_policy_statements
+ [
dict(
Effect="Allow",
Action=["s3:ListBucket"],
Resource=Join("", [arn_prefix, ":s3:::", Ref(sftp_assets_bucket)]),
),
dict(
Effect="Allow",
Action=["s3:*"],
Resource=Join("", [arn_prefix, ":s3:::", Ref(sftp_assets_bucket), "/*"]),
),
]
)
# central asset management policy for use in instance roles
assets_management_policy = iam.Policy(
PolicyName="AssetsManagementPolicy",
PolicyDocument=dict(
Statement=If(
use_sftp_condition,
assets_management_policy_statements_including_sftp_bucket,
assets_management_policy_statements,
)
),
)
if not USE_GOVCLOUD:
assets_use_cloudfront = template.add_parameter(
Parameter(
"AssetsUseCloudFront",
Description="Whether or not to create a CloudFront distribution tied to the S3 assets bucket.",
Type="String",
AllowedValues=["true", "false"],
Default="true",
),
group="Static Media",
label="Enable CloudFront",
)
assets_use_cloudfront_condition = "AssetsUseCloudFrontCondition"
template.add_condition(assets_use_cloudfront_condition, Equals(Ref(assets_use_cloudfront), "true"))
assets_cloudfront_domain = template.add_parameter(
Parameter(
"AssetsCloudFrontDomain",
Description="A custom domain name (CNAME) for your CloudFront distribution, e.g., "
"\"static.example.com\".",
Type="String",
Default="",
),
group="Static Media",
label="CloudFront Custom Domain",
)
assets_custom_domain_condition = "AssetsCloudFrontDomainCondition"
template.add_condition(assets_custom_domain_condition, Not(Equals(Ref(assets_cloudfront_domain), "")))
assets_certificate_arn = template.add_parameter(
Parameter(
"AssetsCloudFrontCertArn",
Description="If (1) you specified a custom static media domain, (2) your stack is NOT in the us-east-1 "
"region, and (3) you wish to serve static media over HTTPS, you must manually create an "
"ACM certificate in the us-east-1 region and provide its ARN here.",
Type="String",
Default="",
),
group="Static Media",
label="CloudFront SSL Certificate ARN",
)
assets_certificate_arn_condition = "AssetsCloudFrontCertArnCondition"
template.add_condition(assets_certificate_arn_condition, Not(Equals(Ref(assets_certificate_arn), "")))
# Currently, you can specify only certificates that are in the US East (N. Virginia) region.
# http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distributionconfig-viewercertificate.html
assets_create_certificate_condition = "AssetsCreateCertificateCondition"
template.add_condition(
assets_create_certificate_condition,
And(
Not(Equals(Ref(assets_cloudfront_domain), "")),
Equals(Ref(AWS_REGION), "us-east-1"),
Equals(Ref(assets_certificate_arn), "")
)
)
assets_certificate = template.add_resource(
Certificate(
'AssetsCertificate',
Condition=assets_create_certificate_condition,
DomainName=Ref(assets_cloudfront_domain),
DomainValidationOptions=[
DomainValidationOption(
DomainName=Ref(assets_cloudfront_domain),
ValidationDomain=Ref(assets_cloudfront_domain),
),
],
)
)
# Create a CloudFront CDN distribution
distribution = template.add_resource(
Distribution(
'AssetsDistribution',
Condition=assets_use_cloudfront_condition,
DistributionConfig=DistributionConfig(
Aliases=If(assets_custom_domain_condition, [Ref(assets_cloudfront_domain)], Ref("AWS::NoValue")),
# use the ACM certificate we created (if any), otherwise fall back to the manually-supplied
# ARN (if any)
ViewerCertificate=If(
assets_create_certificate_condition,
ViewerCertificate(
AcmCertificateArn=Ref(assets_certificate),
SslSupportMethod='sni-only',
),
If(
assets_certificate_arn_condition,
ViewerCertificate(
AcmCertificateArn=Ref(assets_certificate_arn),
SslSupportMethod='sni-only',
),
Ref("AWS::NoValue"),
),
),
Origins=[Origin(
Id="Assets",
DomainName=GetAtt(assets_bucket, "DomainName"),
S3OriginConfig=S3OriginConfig(
OriginAccessIdentity="",
),
)],
DefaultCacheBehavior=DefaultCacheBehavior(
TargetOriginId="Assets",
ForwardedValues=ForwardedValues(
# Cache results *should* vary based on querystring (e.g., 'style.css?v=3')
QueryString=True,
# make sure headers needed by CORS policy above get through to S3
# http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html#header-caching-web-cors
Headers=[
'Origin',
'Access-Control-Request-Headers',
'Access-Control-Request-Method',
],
),
ViewerProtocolPolicy="allow-all",
),
Enabled=True
),
)
)
# Output CloudFront url
template.add_output(
Output(
"AssetsDistributionDomainName",
Description="The assets CDN domain name",
Value=GetAtt(distribution, "DomainName"),
Condition=assets_use_cloudfront_condition,
)
)
else:
distribution = None
# The scopedown policy is used to restrict a user's access to the parts of the bucket
# we don't want them to access.
common_sftp_scopedown_policy_statements = [
{
"Sid": "AllowListingOfSFTPUserFolder",
"Action": ["s3:ListBucket"],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::${transfer:HomeBucket}"],
"Condition": {
"StringLike": {
"s3:prefix": ["${transfer:UserName}/*", "${transfer:UserName}"]
}
},
},
{
"Sid": "HomeDirObjectAccess",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObjectVersion",
"s3:DeleteObject",
"s3:GetObjectVersion",
],
"Resource": [
Join("/", [GetAtt(sftp_assets_bucket, "Arn"), "${transfer:UserName}"]),
Join("/", [GetAtt(sftp_assets_bucket, "Arn"), "${transfer:UserName}/*"]),
],
},
]
sftp_kms_policy_statement = dict(
Effect="Allow",
Action=["kms:DescribeKey", "kms:GenerateDataKey", "kms:Encrypt", "kms:Decrypt"],
Resource=Ref(cmk_arn),
)
sftp_scopedown_policy = iam.ManagedPolicy(
# This is for applying when adding users to the transfer server. It's not used directly in the stack creation,
# other than adding it to IAM for later use.
"SFTPUserScopeDownPolicy",
Condition=use_sftp_condition,
PolicyDocument=dict(
Version="2012-10-17",
Statement=If(
use_sftp_with_kms_condition,
common_sftp_scopedown_policy_statements + [sftp_kms_policy_statement],
common_sftp_scopedown_policy_statements,
),
),
)
template.add_resource(sftp_scopedown_policy)
# The ROLE is applied to users to let them access the bucket in general,
# without regart to who they are.
common_sftp_user_role_statements = [
dict(
Effect="Allow",
Action=["s3:ListBucket", "s3:GetBucketLocation"],
Resource=Join("", [arn_prefix, ":s3:::", Ref(sftp_assets_bucket)]),
),
dict(
Effect="Allow",
Action=[
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject",
"s3:DeleteObjectVersion",
"s3:GetObjectVersion",
"s3:GetObjectACL",
"s3:PutObjectACL",
],
Resource=Join("", [arn_prefix, ":s3:::", Ref(sftp_assets_bucket), "/*"]),
),
]
sftp_user_role = iam.Role(
# This also is not used directly during the stack setup, but is put into IAM
# to be used later when adding users to the transfer server.
"SFTPUserRole",
template=template,
Condition=use_sftp_condition,
AssumeRolePolicyDocument=dict(
Statement=[
dict(
Effect="Allow",
Principal=dict(Service=["transfer.amazonaws.com"]),
Action=["sts:AssumeRole"],
)
]
),
Policies=[
iam.Policy(
"SFTPSUserRolePolicy",
PolicyName="SFTPSUserRolePolicy",
PolicyDocument=dict(
Version="2012-10-17",
Statement=If(
use_sftp_with_kms_condition,
common_sftp_user_role_statements + [sftp_kms_policy_statement],
common_sftp_user_role_statements,
),
),
)
],
RoleName=Join("-", [Ref("AWS::StackName"), "SFTPUserRole"]),
)