-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.tf
724 lines (608 loc) · 19.9 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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
locals {
account_id = data.aws_caller_identity.default.account_id
account_region = data.aws_region.default.name
logging_permissions = length(var.logging_source_bucket_arns) > 0 ? { create = true } : {}
malware_iam_role_name = aws_s3_bucket.default.id
policy = var.policy != null ? var.policy : null
# On/Off switches for optional resources and configuration
bucket_key_enabled = var.kms_key_arn != null ? true : false
cors_rule_enabled = var.cors_rule != null ? { create = true } : {}
logging_partitioned_prefix_enabled = try(var.logging.target_object_key_format.format_type, null) == "partitioned" ? { create = true } : {}
logging_simple_prefix_enabled = try(var.logging.target_object_key_format.format_type, null) == "simple" ? { create = true } : {}
logging_target_object_key_format_enabled = try(var.logging.target_object_key_format, null) != null ? { create = true } : {}
malware_protection_enabled = var.malware_protection.enabled ? { create = true } : {}
object_lock_enabled = var.object_lock_mode != null ? { create = true } : {}
replication_configuration_enabled = var.replication_configuration != null ? { create = true } : {}
}
################################################################################
# S3 Bucket & Bucket Policy
################################################################################
resource "aws_s3_bucket" "default" {
bucket = var.name
bucket_prefix = var.name_prefix
force_destroy = var.force_destroy
object_lock_enabled = var.object_lock_mode != null ? true : false
tags = var.tags
}
data "aws_iam_policy_document" "ssl_policy" {
statement {
sid = "AllowSSLRequestsOnly"
actions = ["s3:*"]
effect = "Deny"
resources = [
aws_s3_bucket.default.arn,
"${aws_s3_bucket.default.arn}/*"
]
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
principals {
type = "*"
identifiers = ["*"]
}
}
}
data "aws_iam_policy_document" "logging_policy" {
dynamic "statement" {
for_each = local.logging_permissions
content {
sid = "S3AccessLog"
actions = ["s3:PutObject"]
effect = "Allow"
resources = [
"${aws_s3_bucket.default.arn}/*"
]
principals {
type = "Service"
identifiers = ["logging.s3.amazonaws.com"]
}
condition {
test = "ArnLike"
variable = "aws:SourceArn"
values = var.logging_source_bucket_arns
}
}
}
}
data "aws_iam_policy_document" "malware_protection_policy" {
for_each = local.malware_protection_enabled
statement {
sid = "NoReadExceptForClean"
effect = "Deny"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = [
"s3:GetObject",
"s3:GetObjectVersion"
]
resources = [
aws_s3_bucket.default.arn,
"${aws_s3_bucket.default.arn}/*"
]
condition {
test = "StringNotEquals"
variable = "s3:ExistingObjectTag/GuardDutyMalwareScanStatus"
values = ["NO_THREATS_FOUND"]
}
condition {
test = "ForAnyValue:ArnNotEquals"
variable = "aws:PrincipalArn"
values = [
"arn:aws:iam::${local.account_id}:assumed-role/${module.s3_malware_protection_role["create"].name}/GuardDutyMalwareProtection",
module.s3_malware_protection_role["create"].arn
]
}
}
statement {
sid = "OnlyGuardDutyCanTag"
effect = "Deny"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = ["s3:PutObjectTagging"]
resources = [
aws_s3_bucket.default.arn,
"${aws_s3_bucket.default.arn}/*"
]
condition {
test = "ForAnyValue:ArnNotEquals"
variable = "aws:PrincipalArn"
values = [
"arn:aws:iam::${local.account_id}:assumed-role/${module.s3_malware_protection_role["create"].name}/GuardDutyMalwareProtection",
module.s3_malware_protection_role["create"].arn
]
}
}
}
data "aws_iam_policy_document" "combined" {
source_policy_documents = compact([
local.policy,
data.aws_iam_policy_document.ssl_policy.json,
data.aws_iam_policy_document.logging_policy.json,
try(data.aws_iam_policy_document.malware_protection_policy["create"].json, "")
])
}
resource "aws_s3_bucket_policy" "default" {
bucket = aws_s3_bucket.default.id
policy = data.aws_iam_policy_document.combined.json
}
################################################################################
# S3 Bucket Configuration
################################################################################
###
# Ownership & ACL
###
resource "aws_s3_bucket_ownership_controls" "default" {
bucket = aws_s3_bucket.default.id
rule {
object_ownership = var.object_ownership_type
}
}
resource "aws_s3_bucket_acl" "default" {
count = var.object_ownership_type == "ObjectWriter" ? 1 : 0
bucket = aws_s3_bucket.default.id
acl = var.acl
depends_on = [aws_s3_bucket_ownership_controls.default]
}
###
# CORS
###
resource "aws_s3_bucket_cors_configuration" "default" {
for_each = local.cors_rule_enabled
bucket = aws_s3_bucket.default.bucket
cors_rule {
allowed_headers = var.cors_rule.allowed_headers
allowed_methods = var.cors_rule.allowed_methods
allowed_origins = var.cors_rule.allowed_origins
expose_headers = var.cors_rule.expose_headers
max_age_seconds = var.cors_rule.max_age_seconds
}
}
###
# Inventory
###
resource "aws_s3_bucket_inventory" "default" {
for_each = var.inventory_configuration
bucket = aws_s3_bucket.default.id
enabled = each.value.enabled
included_object_versions = each.value.included_object_versions
name = each.key
optional_fields = each.value.optional_fields
destination {
bucket {
account_id = each.value.destination.account_id
bucket_arn = each.value.destination.bucket_arn
format = each.value.destination.format
prefix = each.value.destination.prefix
encryption {
dynamic "sse_kms" {
for_each = each.value.destination.encryption.encryption_type == "sse_kms" ? { create = true } : {}
content {
key_id = each.value.destination.encryption.kms_key_id
}
}
dynamic "sse_s3" {
for_each = each.value.destination.encryption.encryption_type == "sse_s3" ? { create = true } : {}
content {
}
}
}
}
}
schedule {
frequency = each.value.frequency
}
dynamic "filter" {
for_each = each.value.filter_prefix != null ? { create = true } : {}
content {
prefix = each.value.filter_prefix
}
}
}
###
# Lifecycle
###
resource "aws_s3_bucket_lifecycle_configuration" "default" {
#checkov:skip=CKV_AWS_300: Ensure S3 lifecycle configuration sets period for aborting failed uploads - consumer of the module should decide
count = length(var.lifecycle_rule) > 0 ? 1 : 0
bucket = aws_s3_bucket.default.bucket
transition_default_minimum_object_size = var.transition_default_minimum_object_size
dynamic "rule" {
for_each = var.lifecycle_rule
content {
id = rule.value.id
status = rule.value.enabled ? "Enabled" : "Disabled"
# --------------------------------------------------------------
# abort_incomplete_multipart_upload (max 1 block)
# --------------------------------------------------------------
dynamic "abort_incomplete_multipart_upload" {
for_each = rule.value.abort_incomplete_multipart_upload != null ? [rule.value.abort_incomplete_multipart_upload] : []
content {
days_after_initiation = abort_incomplete_multipart_upload.value.days_after_initiation
}
}
# ------------
# expiration (max 1 block)
# ------------
dynamic "expiration" {
for_each = rule.value.expiration != null ? [rule.value.expiration] : []
content {
date = expiration.value.date
days = expiration.value.days
expired_object_delete_marker = expiration.value.expired_object_delete_marker
}
}
# --------------------------------------------------
# filter (max 1 block)
# --------------------------------------------------
dynamic "filter" {
for_each = rule.value.filter != null ? [rule.value.filter] : []
content {
prefix = filter.value.prefix
object_size_greater_than = filter.value.object_size_greater_than
object_size_less_than = filter.value.object_size_less_than
dynamic "tag" {
for_each = filter.value.tag != null ? [filter.value.tag] : []
content {
key = tag.value.key
value = tag.value.value
}
}
dynamic "and" {
for_each = filter.value.and != null ? [filter.value.and] : []
content {
prefix = and.value.prefix
object_size_greater_than = and.value.object_size_greater_than
object_size_less_than = and.value.object_size_less_than
tags = and.value.tags
}
}
}
}
# -------------------------------
# noncurrent_version_expiration (max 1 block)
# -------------------------------
dynamic "noncurrent_version_expiration" {
for_each = rule.value.noncurrent_version_expiration != null ? [rule.value.noncurrent_version_expiration] : []
content {
newer_noncurrent_versions = noncurrent_version_expiration.value.newer_noncurrent_versions
noncurrent_days = noncurrent_version_expiration.value.noncurrent_days
}
}
# -------------------------------
# noncurrent_version_transition (1..n blocks)
# -------------------------------
dynamic "noncurrent_version_transition" {
for_each = rule.value.noncurrent_version_transition != null ? rule.value.noncurrent_version_transition : []
content {
newer_noncurrent_versions = noncurrent_version_transition.value.newer_noncurrent_versions
noncurrent_days = noncurrent_version_transition.value.noncurrent_days
storage_class = noncurrent_version_transition.value.storage_class
}
}
# -----------
# transition (1..n blocks)
# -----------
dynamic "transition" {
for_each = rule.value.transition != null ? rule.value.transition : []
content {
date = transition.value.date
days = transition.value.days
storage_class = transition.value.storage_class
}
}
}
}
}
###
# Logging
###
resource "aws_s3_bucket_logging" "default" {
count = var.logging != null ? 1 : 0
bucket = aws_s3_bucket.default.id
target_bucket = var.logging.target_bucket
target_prefix = var.logging.target_prefix
dynamic "target_object_key_format" {
for_each = local.logging_target_object_key_format_enabled
content {
dynamic "partitioned_prefix" {
for_each = local.logging_partitioned_prefix_enabled
content {
partition_date_source = var.logging.target_object_key_format.partition_date_source
}
}
dynamic "simple_prefix" {
for_each = local.logging_simple_prefix_enabled
content {}
}
}
}
lifecycle {
precondition {
condition = var.logging.target_bucket != aws_s3_bucket.default.id || var.object_lock_mode == null
error_message = "You're trying to enable server access logging and object locking on the same bucket! Object lock will prevent server access logs from written to the bucket. Either log to a different bucket or remove the object lock configuration."
}
}
}
###
# Object Lock
###
resource "aws_s3_bucket_object_lock_configuration" "default" {
for_each = local.object_lock_enabled
bucket = aws_s3_bucket.default.bucket
rule {
default_retention {
mode = var.object_lock_mode
years = var.object_lock_years
days = var.object_lock_days
}
}
lifecycle {
precondition {
condition = var.object_lock_mode == null || length(var.logging_source_bucket_arns) == 0
error_message = "You're trying to allow (other buckets) logging to this bucket and enable object locking on the same bucket! Object lock will prevent server access logs from written to the bucket. Either remove the logging source buckets configuration or remove the object lock configuration."
}
}
}
###
# Malware protection
###
resource "aws_guardduty_malware_protection_plan" "default" {
for_each = local.malware_protection_enabled
role = module.s3_malware_protection_role["create"].arn
protected_resource {
s3_bucket {
bucket_name = aws_s3_bucket.default.id
object_prefixes = var.malware_protection.object_prefixes
}
}
actions {
tagging {
status = "ENABLED"
}
}
}
data "aws_iam_policy_document" "s3_malware_protection_assume_role" {
for_each = local.malware_protection_enabled
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["malware-protection-plan.guardduty.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "aws:SourceAccount"
values = [local.account_id]
}
condition {
test = "ArnLike"
variable = "aws:SourceArn"
values = ["arn:aws:guardduty:${local.account_region}:${local.account_id}:malware-protection-plan/*"]
}
}
}
data "aws_iam_policy_document" "s3_malware_protection_policy" {
for_each = local.malware_protection_enabled
statement {
sid = "AllowManagedRuleToSendS3EventsToGuardDuty"
effect = "Allow"
actions = [
"events:PutRule",
"events:DeleteRule",
"events:PutTargets",
"events:RemoveTargets"
]
resources = [
"arn:aws:events:${local.account_region}:${local.account_id}:rule/DO-NOT-DELETE-AmazonGuardDutyMalwareProtectionS3*"
]
condition {
test = "StringEquals"
variable = "events:ManagedBy"
values = ["malware-protection-plan.guardduty.amazonaws.com"]
}
}
statement {
sid = "AllowGuardDutyToMonitorEventBridgeManagedRule"
effect = "Allow"
actions = [
"events:DescribeRule",
"events:ListTargetsByRule"
]
resources = [
"arn:aws:events:${local.account_region}:${local.account_id}:rule/DO-NOT-DELETE-AmazonGuardDutyMalwareProtectionS3*"
]
}
statement {
sid = "AllowEnableS3EventBridgeEvents"
effect = "Allow"
actions = [
"s3:PutBucketNotification",
"s3:GetBucketNotification"
]
resources = [
aws_s3_bucket.default.arn
]
condition {
test = "StringEquals"
variable = "aws:ResourceAccount"
values = [local.account_id]
}
}
statement {
sid = "AllowPostScanTag"
effect = "Allow"
actions = [
"s3:GetObjectTagging",
"s3:GetObjectVersionTagging",
"s3:PutObjectTagging",
"s3:PutObjectVersionTagging"
]
resources = [
"${aws_s3_bucket.default.arn}/*"
]
condition {
test = "StringEquals"
variable = "aws:ResourceAccount"
values = [local.account_id]
}
}
statement {
sid = "AllowPutValidationObject"
effect = "Allow"
actions = ["s3:PutObject"]
resources = [
"${aws_s3_bucket.default.arn}/malware-protection-resource-validation-object"
]
condition {
test = "StringEquals"
variable = "aws:ResourceAccount"
values = [local.account_id]
}
}
statement {
sid = "AllowCheckBucketOwnership"
effect = "Allow"
actions = ["s3:ListBucket"]
resources = [
aws_s3_bucket.default.arn
]
condition {
test = "StringEquals"
variable = "aws:ResourceAccount"
values = [local.account_id]
}
}
statement {
sid = "AllowMalwareScan"
effect = "Allow"
actions = [
"s3:GetObject",
"s3:GetObjectVersion"
]
resources = [
"${aws_s3_bucket.default.arn}/*"
]
condition {
test = "StringEquals"
variable = "aws:ResourceAccount"
values = [local.account_id]
}
}
dynamic "statement" {
for_each = var.kms_key_arn != null ? { create = true } : {}
content {
sid = "AllowDecryptForMalwareScan"
effect = "Allow"
actions = [
"kms:Decrypt",
"kms:GenerateDataKey"
]
resources = [
var.kms_key_arn
]
condition {
test = "StringLike"
variable = "kms:ViaService"
values = ["s3.${local.account_region}.amazonaws.com"]
}
}
}
}
module "s3_malware_protection_role" {
for_each = local.malware_protection_enabled
source = "schubergphilis/mcaf-role/aws"
version = "~> 0.4.0"
name = local.malware_iam_role_name
assume_policy = data.aws_iam_policy_document.s3_malware_protection_assume_role["create"].json
create_policy = true
permissions_boundary = var.malware_protection.permissions_boundary
role_policy = data.aws_iam_policy_document.s3_malware_protection_policy["create"].json
}
###
# Notification / EventBridge
###
resource "aws_s3_bucket_notification" "eventbridge" {
count = var.eventbridge_enabled ? 1 : 0
bucket = aws_s3_bucket.default.id
eventbridge = var.eventbridge_enabled
}
###
# Replication
###
resource "aws_s3_bucket_replication_configuration" "default" {
for_each = local.replication_configuration_enabled
role = var.replication_configuration.iam_role_arn
bucket = aws_s3_bucket.default.id
dynamic "rule" {
for_each = var.replication_configuration.rules
content {
id = rule.value["id"]
priority = rule.key
status = "Enabled"
delete_marker_replication {
status = "Disabled"
}
destination {
bucket = rule.value["dest_bucket"]
storage_class = rule.value["dest_storage_class"]
dynamic "encryption_configuration" {
for_each = rule.value.replica_kms_key_arn != null ? { create = true } : {}
content {
replica_kms_key_id = rule.value.replica_kms_key_arn
}
}
}
dynamic "source_selection_criteria" {
for_each = rule.value.source_selection_criteria != null ? { create = true } : {}
content {
replica_modifications {
status = rule.value.source_selection_criteria.replica_modifications ? "Enabled" : "Disabled"
}
sse_kms_encrypted_objects {
status = rule.value.source_selection_criteria.sse_kms_encrypted_objects ? "Enabled" : "Disabled"
}
}
}
filter {}
}
}
depends_on = [aws_s3_bucket_versioning.default]
}
###
# Server-Side Encryption
###
resource "aws_s3_bucket_server_side_encryption_configuration" "default" {
bucket = aws_s3_bucket.default.bucket
rule {
bucket_key_enabled = local.bucket_key_enabled
apply_server_side_encryption_by_default {
kms_master_key_id = var.kms_key_arn
sse_algorithm = var.kms_key_arn != null ? "aws:kms" : "AES256"
}
}
}
###
# Public Access Block
###
resource "aws_s3_bucket_public_access_block" "default" {
bucket = 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
}
###
# Versioning
###
resource "aws_s3_bucket_versioning" "default" {
bucket = aws_s3_bucket.default.id
versioning_configuration {
status = var.versioning ? "Enabled" : "Suspended"
}
}