-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.tf
128 lines (110 loc) · 3.71 KB
/
data.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
data "aws_caller_identity" "current" {}
data "aws_iam_policy_document" "merged" {
count = local.conditions.merge_policies ? local.total_buckets : 0
source_policy_documents = [
data.aws_iam_policy_document.unmerged[count.index].json,
var.bucket.policy
]
}
data "aws_iam_policy_document" "unmerged" {
#checkov:skip=CKV_AWS_111: "Ensure IAM policies does not allow write access without constraints"
#checkov:skip=CKV_AWS_108: "Ensure IAM policies does not allow data exfiltration"
# TODO: fix CHK_AWS_108
count = local.total_buckets
statement {
sid = "EnableActions"
effect = "Allow"
actions = [ "s3:*" ]
resources = count.index == 0 ? (
local.source_bucket_arns
) : [
local.destination_bucket_arns[count.index - 1],
local.destination_bucket_path_arns[count.index - 1]
]
principals {
type = "AWS"
identifiers = [
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
]
}
}
statement {
sid = "DenyActions"
effect = "Deny"
actions = [
"s3:DeleteBucket",
"s3:PutBucketAcl",
"s3:PutObjectAcl",
"s3:PutObjectVersionAcl"
]
resources = count.index == 0 ? (
local.source_bucket_arns
) : [
local.destination_bucket_arns[count.index - 1],
local.destination_bucket_path_arns[count.index - 1]
]
principals {
type = "AWS"
identifiers = [ "*" ]
}
}
}
data "aws_iam_policy_document" "replication" {
statement {
effect = "Allow"
actions = [
"s3:GetReplicationConfiguration",
"s3:ListBucket"
]
resources = local.source_bucket_arns
condition {
test = "StringEquals"
variable = "aws:SourceAccount"
values = [ data.aws_caller_identity.current.account_id ]
}
}
statement {
effect = "Allow"
actions = [
"s3:GetObjectVersionForReplication",
"s3:GetObjectVersionAcl",
"s3:GetObjectVersionTagging"
]
resources = local.source_bucket_arns
condition {
test = "StringEquals"
variable = "aws:SourceAccount"
values = [ data.aws_caller_identity.current.account_id ]
}
}
statement {
effect = "Allow"
actions = [
"s3:ReplicateObject",
"s3:ReplicateDelete",
"s3:ReplicateTags"
]
resources = local.destination_bucket_arns
condition {
test = "StringEquals"
variable = "aws:SourceAccount"
values = [ data.aws_caller_identity.current.account_id ]
}
}
}
data "aws_iam_policy_document" "notification" {
statement {
effect = "Allow"
actions = [ "sns:Publish"]
resources = [ local.event_notification_arn ]
condition {
test = "ArnLike"
variable = "aws:SourceArn"
values = [ aws_s3_bucket.this[0].arn ]
}
principals {
type = "*"
identifiers = [ "*" ]
}
}
}