-
-
Notifications
You must be signed in to change notification settings - Fork 839
/
main.tf
412 lines (342 loc) · 14 KB
/
main.tf
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
locals {
enabled = module.this.enabled
bucket_name = var.bucket_name != null && var.bucket_name != "" ? var.bucket_name : module.this.id
replication_enabled = local.enabled && var.s3_replication_enabled
bucket_arn = "arn:${data.aws_partition.current.partition}:s3:::${join("", aws_s3_bucket.default.*.id)}"
# Deprecate `replication_rules` in favor of `s3_replication_rules` to keep all the replication related
# inputs grouped under s3_replica[tion]
s3_replication_rules = var.replication_rules == null ? var.s3_replication_rules : var.replication_rules
public_access_block_enabled = var.block_public_acls || var.block_public_policy || var.ignore_public_acls || var.restrict_public_buckets
}
resource "aws_s3_bucket" "default" {
#bridgecrew:skip=BC_AWS_S3_13:Skipping `Enable S3 Bucket Logging` because we do not have good defaults
#bridgecrew:skip=CKV_AWS_52:Skipping `Ensure S3 bucket has MFA delete enabled` due to issue in terraform (https://github.com/hashicorp/terraform-provider-aws/issues/629).
#bridgecrew:skip=BC_AWS_S3_16:Skipping `Ensure S3 bucket versioning is enabled` because dynamic blocks are not supported by checkov
#bridgecrew:skip=BC_AWS_S3_14:Skipping `Ensure all data stored in the S3 bucket is securely encrypted at rest` because variables are not understood
#bridgecrew:skip=BC_AWS_GENERAL_56:Skipping `Ensure that S3 buckets are encrypted with KMS by default` because we do not have good defaults
count = local.enabled ? 1 : 0
bucket = local.bucket_name
acl = try(length(var.grants), 0) == 0 ? var.acl : null
force_destroy = var.force_destroy
tags = module.this.tags
acceleration_status = var.transfer_acceleration_enabled ? "Enabled" : null
dynamic "versioning" {
for_each = var.versioning_enabled ? [true] : []
content {
enabled = true
}
}
dynamic "lifecycle_rule" {
for_each = var.lifecycle_rules
content {
enabled = lifecycle_rule.value.enabled
prefix = lifecycle_rule.value.prefix
tags = lifecycle_rule.value.tags
abort_incomplete_multipart_upload_days = lifecycle_rule.value.abort_incomplete_multipart_upload_days
dynamic "noncurrent_version_expiration" {
for_each = lifecycle_rule.value.enable_noncurrent_version_expiration ? [1] : []
content {
days = lifecycle_rule.value.noncurrent_version_expiration_days
}
}
dynamic "noncurrent_version_transition" {
for_each = lifecycle_rule.value.enable_glacier_transition ? [1] : []
content {
days = lifecycle_rule.value.noncurrent_version_glacier_transition_days
storage_class = "GLACIER"
}
}
dynamic "noncurrent_version_transition" {
for_each = lifecycle_rule.value.enable_deeparchive_transition ? [1] : []
content {
days = lifecycle_rule.value.noncurrent_version_deeparchive_transition_days
storage_class = "DEEP_ARCHIVE"
}
}
dynamic "transition" {
for_each = lifecycle_rule.value.enable_glacier_transition ? [1] : []
content {
days = lifecycle_rule.value.glacier_transition_days
storage_class = "GLACIER"
}
}
dynamic "transition" {
for_each = lifecycle_rule.value.enable_deeparchive_transition ? [1] : []
content {
days = lifecycle_rule.value.deeparchive_transition_days
storage_class = "DEEP_ARCHIVE"
}
}
dynamic "transition" {
for_each = lifecycle_rule.value.enable_standard_ia_transition ? [1] : []
content {
days = lifecycle_rule.value.standard_transition_days
storage_class = "STANDARD_IA"
}
}
dynamic "expiration" {
for_each = lifecycle_rule.value.enable_current_object_expiration ? [1] : []
content {
days = lifecycle_rule.value.expiration_days
}
}
}
}
dynamic "logging" {
for_each = var.logging == null ? [] : [1]
content {
target_bucket = var.logging["bucket_name"]
target_prefix = var.logging["prefix"]
}
}
# https://docs.aws.amazon.com/AmazonS3/latest/dev/bucket-encryption.html
# https://www.terraform.io/docs/providers/aws/r/s3_bucket.html#enable-default-server-side-encryption
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = var.sse_algorithm
kms_master_key_id = var.kms_master_key_arn
}
}
}
dynamic "website" {
for_each = var.website_inputs == null ? [] : var.website_inputs
content {
index_document = website.value.index_document
error_document = website.value.error_document
redirect_all_requests_to = website.value.redirect_all_requests_to
routing_rules = website.value.routing_rules
}
}
dynamic "cors_rule" {
for_each = var.cors_rule_inputs == null ? [] : var.cors_rule_inputs
content {
allowed_headers = cors_rule.value.allowed_headers
allowed_methods = cors_rule.value.allowed_methods
allowed_origins = cors_rule.value.allowed_origins
expose_headers = cors_rule.value.expose_headers
max_age_seconds = cors_rule.value.max_age_seconds
}
}
dynamic "grant" {
for_each = try(length(var.grants), 0) == 0 || try(length(var.acl), 0) > 0 ? [] : var.grants
content {
id = grant.value.id
type = grant.value.type
permissions = grant.value.permissions
uri = grant.value.uri
}
}
dynamic "replication_configuration" {
for_each = local.replication_enabled ? [1] : []
content {
role = aws_iam_role.replication[0].arn
dynamic "rules" {
for_each = local.s3_replication_rules == null ? [] : local.s3_replication_rules
content {
id = rules.value.id
priority = try(rules.value.priority, 0)
# `prefix` at this level is a V1 feature, replaced in V2 with the filter block.
# `prefix` conflicts with `filter`, and for multiple destinations, a filter block
# is required even if it empty, so we always implement `prefix` as a filter.
# OBSOLETE: prefix = try(rules.value.prefix, null)
status = try(rules.value.status, null)
destination {
# Prefer newer system of specifying bucket in rule, but maintain backward compatibility with
# s3_replica_bucket_arn to specify single destination for all rules
bucket = try(length(rules.value.destination_bucket), 0) > 0 ? rules.value.destination_bucket : var.s3_replica_bucket_arn
storage_class = try(rules.value.destination.storage_class, "STANDARD")
replica_kms_key_id = try(rules.value.destination.replica_kms_key_id, null)
account_id = try(rules.value.destination.account_id, null)
dynamic "access_control_translation" {
for_each = try(rules.value.destination.access_control_translation.owner, null) == null ? [] : [rules.value.destination.access_control_translation.owner]
content {
owner = access_control_translation.value
}
}
}
dynamic "source_selection_criteria" {
for_each = try(rules.value.source_selection_criteria.sse_kms_encrypted_objects.enabled, null) == null ? [] : [rules.value.source_selection_criteria.sse_kms_encrypted_objects.enabled]
content {
sse_kms_encrypted_objects {
enabled = source_selection_criteria.value
}
}
}
# Replication to multiple destination buckets requires that priority is specified in the rules object.
# If the corresponding rule requires no filter, an empty configuration block filter {} must be specified.
# See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket
dynamic "filter" {
for_each = try(rules.value.filter, null) == null ? [{ prefix = null, tags = {} }] : [rules.value.filter]
content {
prefix = try(filter.value.prefix, try(rules.value.prefix, null))
tags = try(filter.value.tags, {})
}
}
}
}
}
}
dynamic "object_lock_configuration" {
for_each = var.object_lock_configuration != null ? [1] : []
content {
object_lock_enabled = "Enabled"
rule {
default_retention {
mode = var.object_lock_configuration.mode
days = var.object_lock_configuration.days
years = var.object_lock_configuration.years
}
}
}
}
}
module "s3_user" {
source = "cloudposse/iam-s3-user/aws"
version = "0.15.7"
enabled = local.enabled && var.user_enabled
s3_actions = var.allowed_bucket_actions
s3_resources = ["${join("", aws_s3_bucket.default.*.arn)}/*", join("", aws_s3_bucket.default.*.arn)]
context = module.this.context
}
data "aws_partition" "current" {}
data "aws_iam_policy_document" "bucket_policy" {
count = local.enabled ? 1 : 0
dynamic "statement" {
for_each = var.allow_encrypted_uploads_only ? [1] : []
content {
sid = "DenyIncorrectEncryptionHeader"
effect = "Deny"
actions = ["s3:PutObject"]
resources = ["${local.bucket_arn}/*"]
principals {
identifiers = ["*"]
type = "*"
}
condition {
test = "StringNotEquals"
values = [var.sse_algorithm]
variable = "s3:x-amz-server-side-encryption"
}
}
}
dynamic "statement" {
for_each = var.allow_encrypted_uploads_only ? [1] : []
content {
sid = "DenyUnEncryptedObjectUploads"
effect = "Deny"
actions = ["s3:PutObject"]
resources = ["${local.bucket_arn}/*"]
principals {
identifiers = ["*"]
type = "*"
}
condition {
test = "Null"
values = ["true"]
variable = "s3:x-amz-server-side-encryption"
}
}
}
dynamic "statement" {
for_each = var.allow_ssl_requests_only ? [1] : []
content {
sid = "ForceSSLOnlyAccess"
effect = "Deny"
actions = ["s3:*"]
resources = [local.bucket_arn, "${local.bucket_arn}/*"]
principals {
identifiers = ["*"]
type = "*"
}
condition {
test = "Bool"
values = ["false"]
variable = "aws:SecureTransport"
}
}
}
dynamic "statement" {
for_each = length(var.s3_replication_source_roles) > 0 ? [1] : []
content {
sid = "CrossAccountReplicationObjects"
actions = [
"s3:ReplicateObject",
"s3:ReplicateDelete",
"s3:ReplicateTags",
"s3:GetObjectVersionTagging",
"s3:ObjectOwnerOverrideToBucketOwner"
]
resources = ["${local.bucket_arn}/*"]
principals {
type = "AWS"
identifiers = var.s3_replication_source_roles
}
}
}
dynamic "statement" {
for_each = length(var.s3_replication_source_roles) > 0 ? [1] : []
content {
sid = "CrossAccountReplicationBucket"
actions = ["s3:List*", "s3:GetBucketVersioning", "s3:PutBucketVersioning"]
resources = [local.bucket_arn]
principals {
type = "AWS"
identifiers = var.s3_replication_source_roles
}
}
}
dynamic "statement" {
for_each = keys(var.privileged_principal_arns)
content {
sid = "AllowPrivilegedPrincipal[${statement.key}]" # add indices to Sid
actions = var.privileged_principal_actions
resources = distinct(flatten([
"arn:${data.aws_partition.current.partition}:s3:::${join("", aws_s3_bucket.default.*.id)}",
formatlist("arn:${data.aws_partition.current.partition}:s3:::${join("", aws_s3_bucket.default.*.id)}/%s*", var.privileged_principal_arns[statement.value]),
]))
principals {
type = "AWS"
identifiers = [statement.value]
}
}
}
}
data "aws_iam_policy_document" "aggregated_policy" {
count = local.enabled ? 1 : 0
source_json = var.policy
override_json = join("", data.aws_iam_policy_document.bucket_policy.*.json)
}
resource "aws_s3_bucket_policy" "default" {
count = local.enabled && (var.allow_ssl_requests_only || var.allow_encrypted_uploads_only || length(var.s3_replication_source_roles) > 0 || length(var.privileged_principal_arns) > 0 || var.policy != "") ? 1 : 0
bucket = join("", aws_s3_bucket.default.*.id)
policy = join("", data.aws_iam_policy_document.aggregated_policy.*.json)
depends_on = [aws_s3_bucket_public_access_block.default]
}
# Refer to the terraform documentation on s3_bucket_public_access_block at
# https://www.terraform.io/docs/providers/aws/r/s3_bucket_public_access_block.html
# for the nuances of the blocking options
resource "aws_s3_bucket_public_access_block" "default" {
count = module.this.enabled && local.public_access_block_enabled ? 1 : 0
bucket = join("", aws_s3_bucket.default.*.id)
block_public_acls = var.block_public_acls
block_public_policy = var.block_public_policy
ignore_public_acls = var.ignore_public_acls
restrict_public_buckets = var.restrict_public_buckets
}
# Per https://docs.aws.amazon.com/AmazonS3/latest/userguide/about-object-ownership.html
resource "aws_s3_bucket_ownership_controls" "default" {
count = local.enabled ? 1 : 0
bucket = join("", aws_s3_bucket.default.*.id)
rule {
object_ownership = var.s3_object_ownership
}
depends_on = [time_sleep.wait_for_aws_s3_bucket_settings]
}
# Workaround S3 eventual consistency for settings objects
resource "time_sleep" "wait_for_aws_s3_bucket_settings" {
count = local.enabled ? 1 : 0
depends_on = [aws_s3_bucket_public_access_block.default, aws_s3_bucket_policy.default]
create_duration = "30s"
destroy_duration = "30s"
}