forked from terraform-aws-modules/terraform-aws-kms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
471 lines (388 loc) · 13.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
data "aws_partition" "current" {}
data "aws_caller_identity" "current" {}
################################################################################
# Key
################################################################################
resource "aws_kms_key" "this" {
count = var.create && !var.create_external && !var.create_replica && !var.create_replica_external ? 1 : 0
bypass_policy_lockout_safety_check = var.bypass_policy_lockout_safety_check
customer_master_key_spec = var.customer_master_key_spec
deletion_window_in_days = var.deletion_window_in_days
description = var.description
enable_key_rotation = var.enable_key_rotation
is_enabled = var.is_enabled
key_usage = var.key_usage
multi_region = var.multi_region
policy = coalesce(var.policy, data.aws_iam_policy_document.this[0].json)
tags = var.tags
}
################################################################################
# External Key
################################################################################
resource "aws_kms_external_key" "this" {
count = var.create && var.create_external && !var.create_replica && !var.create_replica_external ? 1 : 0
bypass_policy_lockout_safety_check = var.bypass_policy_lockout_safety_check
deletion_window_in_days = var.deletion_window_in_days
description = var.description
enabled = var.is_enabled
key_material_base64 = var.key_material_base64
multi_region = var.multi_region
policy = coalesce(var.policy, data.aws_iam_policy_document.this[0].json)
valid_to = var.valid_to
tags = var.tags
}
################################################################################
# Replica Key
################################################################################
resource "aws_kms_replica_key" "this" {
count = var.create && var.create_replica && !var.create_external && !var.create_replica_external ? 1 : 0
bypass_policy_lockout_safety_check = var.bypass_policy_lockout_safety_check
deletion_window_in_days = var.deletion_window_in_days
description = var.description
primary_key_arn = var.primary_key_arn
enabled = var.is_enabled
policy = coalesce(var.policy, data.aws_iam_policy_document.this[0].json)
tags = var.tags
}
################################################################################
# Replica External Key
################################################################################
resource "aws_kms_replica_external_key" "this" {
count = var.create && !var.create_replica && !var.create_external && var.create_replica_external ? 1 : 0
bypass_policy_lockout_safety_check = var.bypass_policy_lockout_safety_check
deletion_window_in_days = var.deletion_window_in_days
description = var.description
enabled = var.is_enabled
key_material_base64 = var.key_material_base64
policy = coalesce(var.policy, data.aws_iam_policy_document.this[0].json)
primary_key_arn = var.primary_external_key_arn
valid_to = var.valid_to
tags = var.tags
}
################################################################################
# Policy
################################################################################
data "aws_iam_policy_document" "this" {
count = var.create ? 1 : 0
source_policy_documents = var.source_policy_documents
override_policy_documents = var.override_policy_documents
# Default policy - account wide access to all key operations
dynamic "statement" {
for_each = var.enable_default_policy ? [1] : []
content {
sid = "Default"
actions = ["kms:*"]
resources = ["*"]
principals {
type = "AWS"
identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"]
}
}
}
# Key owner - all key operations
dynamic "statement" {
for_each = length(var.key_owners) > 0 ? [1] : []
content {
sid = "KeyOwner"
actions = ["kms:*"]
resources = ["*"]
principals {
type = "AWS"
identifiers = var.key_owners
}
}
}
# Key administrators - https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-default-allow-administrators
dynamic "statement" {
for_each = length(var.key_administrators) > 0 ? [1] : []
content {
sid = "KeyAdministration"
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",
]
resources = ["*"]
principals {
type = "AWS"
identifiers = var.key_administrators
}
}
}
# Key users - https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-default-allow-users
dynamic "statement" {
for_each = length(var.key_users) > 0 ? [1] : []
content {
sid = "KeyUsage"
actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey",
]
resources = ["*"]
principals {
type = "AWS"
identifiers = var.key_users
}
}
}
# Key service users - https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-service-integration
dynamic "statement" {
for_each = length(var.key_service_users) > 0 ? [1] : []
content {
sid = "KeyServiceUsage"
actions = [
"kms:CreateGrant",
"kms:ListGrants",
"kms:RevokeGrant",
]
resources = ["*"]
principals {
type = "AWS"
identifiers = var.key_service_users
}
condition {
test = "Bool"
variable = "kms:GrantIsForAWSResource"
values = [true]
}
}
}
# Key service roles for autoscaling - https://docs.aws.amazon.com/autoscaling/ec2/userguide/key-policy-requirements-EBS-encryption.html#policy-example-cmk-access
dynamic "statement" {
for_each = length(var.key_service_roles_for_autoscaling) > 0 ? [1] : []
content {
sid = "KeyServiceRolesASG"
actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey",
]
resources = ["*"]
principals {
type = "AWS"
identifiers = var.key_service_roles_for_autoscaling
}
}
}
dynamic "statement" {
for_each = length(var.key_service_roles_for_autoscaling) > 0 ? [1] : []
content {
sid = "KeyServiceRolesASGPersistentVol"
actions = [
"kms:CreateGrant"
]
resources = ["*"]
principals {
type = "AWS"
identifiers = var.key_service_roles_for_autoscaling
}
condition {
test = "Bool"
variable = "kms:GrantIsForAWSResource"
values = [true]
}
}
}
# Key cryptographic operations - https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-users-crypto
dynamic "statement" {
for_each = length(var.key_symmetric_encryption_users) > 0 ? [1] : []
content {
sid = "KeySymmetricEncryption"
actions = [
"kms:Decrypt",
"kms:DescribeKey",
"kms:Encrypt",
"kms:GenerateDataKey*",
"kms:ReEncrypt*",
]
resources = ["*"]
principals {
type = "AWS"
identifiers = var.key_symmetric_encryption_users
}
}
}
dynamic "statement" {
for_each = length(var.key_hmac_users) > 0 ? [1] : []
content {
sid = "KeyHMAC"
actions = [
"kms:DescribeKey",
"kms:GenerateMac",
"kms:VerifyMac",
]
resources = ["*"]
principals {
type = "AWS"
identifiers = var.key_hmac_users
}
}
}
dynamic "statement" {
for_each = length(var.key_asymmetric_public_encryption_users) > 0 ? [1] : []
content {
sid = "KeyAsymmetricPublicEncryption"
actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:DescribeKey",
"kms:GetPublicKey",
]
resources = ["*"]
principals {
type = "AWS"
identifiers = var.key_asymmetric_public_encryption_users
}
}
}
dynamic "statement" {
for_each = length(var.key_asymmetric_sign_verify_users) > 0 ? [1] : []
content {
sid = "KeyAsymmetricSignVerify"
actions = [
"kms:DescribeKey",
"kms:GetPublicKey",
"kms:Sign",
"kms:Verify",
]
resources = ["*"]
principals {
type = "AWS"
identifiers = var.key_asymmetric_sign_verify_users
}
}
}
# https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/access-control-managing-permissions.html#KMS-key-policy-for-DNSSEC
dynamic "statement" {
for_each = var.enable_route53_dnssec ? [1] : []
content {
sid = "Route53DnssecService"
actions = [
"kms:DescribeKey",
"kms:GetPublicKey",
"kms:Sign",
]
resources = ["*"]
principals {
type = "Service"
identifiers = ["dnssec-route53.${data.aws_partition.current.dns_suffix}"]
}
}
}
# https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/access-control-managing-permissions.html#KMS-key-policy-for-DNSSEC
dynamic "statement" {
for_each = var.enable_route53_dnssec ? [1] : []
content {
sid = "Route53DnssecGrant"
actions = ["kms:CreateGrant"]
resources = ["*"]
principals {
type = "Service"
identifiers = ["dnssec-route53.${data.aws_partition.current.dns_suffix}"]
}
condition {
test = "Bool"
variable = "kms:GrantIsForAWSResource"
values = ["true"]
}
dynamic "condition" {
for_each = var.route53_dnssec_sources
content {
test = "StringEquals"
variable = "aws:SourceAccount"
values = try(condition.value.account_ids, [data.aws_caller_identity.current.account_id])
}
}
dynamic "condition" {
for_each = var.route53_dnssec_sources
content {
test = "ArnLike"
variable = "aws:SourceArn"
values = [try(condition.value.hosted_zone_arn, "arn:${data.aws_partition.current.partition}:route53:::hostedzone/*")]
}
}
}
}
dynamic "statement" {
for_each = var.key_statements
content {
sid = try(statement.value.sid, null)
actions = try(statement.value.actions, null)
not_actions = try(statement.value.not_actions, null)
effect = try(statement.value.effect, null)
resources = try(statement.value.resources, null)
not_resources = try(statement.value.not_resources, null)
dynamic "principals" {
for_each = try(statement.value.principals, [])
content {
type = principals.value.type
identifiers = principals.value.identifiers
}
}
dynamic "not_principals" {
for_each = try(statement.value.not_principals, [])
content {
type = not_principals.value.type
identifiers = not_principals.value.identifiers
}
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
values = condition.value.values
variable = condition.value.variable
}
}
}
}
}
################################################################################
# Alias
################################################################################
locals {
aliases = { for k, v in toset(var.aliases) : k => { name = v } }
}
resource "aws_kms_alias" "this" {
for_each = { for k, v in merge(local.aliases, var.computed_aliases) : k => v if var.create }
name = var.aliases_use_name_prefix ? null : "alias/${each.value.name}"
name_prefix = var.aliases_use_name_prefix ? "alias/${each.value.name}-" : null
target_key_id = try(aws_kms_key.this[0].key_id, aws_kms_external_key.this[0].id, aws_kms_replica_key.this[0].key_id, aws_kms_replica_external_key.this[0].key_id)
}
################################################################################
# Grant
################################################################################
resource "aws_kms_grant" "this" {
for_each = { for k, v in var.grants : k => v if var.create }
name = try(each.value.name, each.key)
key_id = try(aws_kms_key.this[0].key_id, aws_kms_external_key.this[0].id, aws_kms_replica_key.this[0].key_id, aws_kms_replica_external_key.this[0].key_id)
grantee_principal = each.value.grantee_principal
operations = each.value.operations
dynamic "constraints" {
for_each = length(lookup(each.value, "constraints", {})) == 0 ? [] : [each.value.constraints]
content {
encryption_context_equals = try(constraints.value.encryption_context_equals, null)
encryption_context_subset = try(constraints.value.encryption_context_subset, null)
}
}
retiring_principal = try(each.value.retiring_principal, null)
grant_creation_tokens = try(each.value.grant_creation_tokens, null)
retire_on_delete = try(each.value.retire_on_delete, null)
}