-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkms_encription.tf
212 lines (177 loc) · 5.34 KB
/
kms_encription.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
data "aws_caller_identity" "primary" {
count = var.create_kms_key ? 1 : 0
provider = aws.primary
}
data "aws_caller_identity" "secondary" {
count = (var.create_kms_key && var.bucket_replication_enabled) ? 1 : 0
provider = aws.primary
}
data "aws_iam_policy_document" "primary" {
count = var.create_kms_key ? 1 : 0
statement { #Allow access for Root User
sid = "Allow access for Root User"
effect = "Allow"
resources = [aws_kms_key.primary[0].arn]
actions = ["kms:*"]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${data.aws_caller_identity.primary[0].account_id}:root"]
}
}
statement { #Allow access for Key Administrator
sid = "Allow access for Key Administrator"
effect = "Allow"
resources = [aws_kms_key.primary[0].arn]
actions = [
"kms:Create*",
"kms:Describe*",
"kms:Enable*",
"kms:List*",
"kms:Put*",
"kms:Update*",
"kms:Revoke*",
"kms:Disable*",
"kms:Get*",
"kms:Delete*",
"kms:TagResource",
"kms:UntagResource",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion",
]
principals {
type = "AWS"
identifiers = [data.aws_caller_identity.primary[0].arn]
}
}
dynamic "statement" {
for_each = aws_sns_topic.topic
content {
sid = "Allow access for Key User (SNS Service Principal)"
effect = "Allow"
resources = [aws_kms_key.primary[0].arn]
actions = [
"kms:GenerateDataKey*",
"kms:Decrypt",
]
principals {
type = "Service"
identifiers = ["sns.amazonaws.com"]
}
}
}
dynamic "statement" {
for_each = var.notifications_sns ? [1] : []
content {
sid = "Allow access for Key User (S3 Service Principal)"
effect = "Allow"
resources = [aws_kms_key.primary[0].arn]
actions = [
"kms:GenerateDataKey*",
"kms:Decrypt",
]
principals {
type = "Service"
identifiers = ["s3.amazonaws.com"]
}
}
}
}
data "aws_iam_policy_document" "secondary" {
count = (var.create_kms_key && var.bucket_replication_enabled) ? 1 : 0
statement { #Allow access for Root User
sid = "Allow access for Root User"
effect = "Allow"
resources = [aws_kms_replica_key.secondary[0].arn]
actions = ["kms:*"]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${data.aws_caller_identity.secondary[0].account_id}:root"]
}
}
statement { #Allow access for Key Administrator
sid = "Allow access for Key Administrator"
effect = "Allow"
resources = [aws_kms_replica_key.secondary[0].arn]
actions = [
"kms:Create*",
"kms:Describe*",
"kms:Enable*",
"kms:List*",
"kms:Put*",
"kms:Update*",
"kms:Revoke*",
"kms:Disable*",
"kms:Get*",
"kms:Delete*",
"kms:TagResource",
"kms:UntagResource",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion",
]
principals {
type = "AWS"
identifiers = [data.aws_caller_identity.secondary[0].arn]
}
}
dynamic "statement" {
for_each = var.bucket_replication_enabled == true ? [1] : []
content {
sid = "Allow access for replication role"
effect = "Allow"
resources = ["*"]
actions = [
"kms:GenerateDataKey",
"kms:Encrypt"
]
principals {
type = "AWS"
identifiers = [aws_iam_role.bucket_replication[0].arn]
}
}
}
dynamic "statement" {
for_each = var.notifications_sns ? [1] : []
content {
sid = "Allow access for Key User (S3 Service Principal)"
effect = "Allow"
resources = [aws_kms_key.primary[0].arn]
actions = [
"kms:GenerateDataKey*",
"kms:Decrypt",
]
principals {
type = "Service"
identifiers = ["s3.amazonaws.com"]
}
}
}
}
resource "aws_kms_key" "primary" {
# checkov:skip=CKV2_AWS_64:Since we use the attribute 'count' to create an aws_kms_key, checkov has a known issue that results in
## an error even though we are using the correct configurations. (Ref https://github.com/bridgecrewio/checkov/issues/3847)
count = var.create_kms_key ? 1 : 0
provider = aws.primary
description = "${aws_s3_bucket.default.bucket}-key"
deletion_window_in_days = var.kms_key_deletion_windows
enable_key_rotation = var.kms_key_rotation
multi_region = var.bucket_replication_enabled ? "true" : "false"
}
resource "aws_kms_replica_key" "secondary" {
count = (var.create_kms_key && var.bucket_replication_enabled) ? 1 : 0
provider = aws.secondary
description = "Multi-Region replica key"
deletion_window_in_days = var.kms_key_deletion_windows
primary_key_arn = aws_kms_key.primary[0].arn
}
resource "aws_kms_key_policy" "primary" {
count = var.create_kms_key ? 1 : 0
provider = aws.primary
key_id = aws_kms_key.primary[0].id
policy = data.aws_iam_policy_document.primary[0].json
}
resource "aws_kms_key_policy" "secondary" {
count = (var.create_kms_key && var.bucket_replication_enabled) ? 1 : 0
provider = aws.secondary
key_id = aws_kms_replica_key.secondary[0].id
policy = data.aws_iam_policy_document.secondary[0].json
}