-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkms.tf
101 lines (86 loc) · 2.24 KB
/
kms.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
data "aws_iam_policy_document" "key" {
statement {
actions = ["kms:*"]
effect = "Allow"
resources = ["*"]
principals {
type = "AWS"
identifiers = ["arn:${local.partition}:iam::${local.account_id}:root"]
}
}
statement {
actions = ["kms:GenerateDataKey*"]
effect = "Allow"
resources = ["*"]
condition {
test = "StringLike"
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
values = local.kms_key_encrypt_resources
}
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
}
statement {
actions = [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*",
]
effect = "Allow"
resources = ["*"]
principals {
type = "Service"
identifiers = ["logs.${var.region}.amazonaws.com"]
}
}
statement {
actions = ["kms:Describe*"]
effect = "Allow"
resources = ["*"]
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "key_roles" {
statement {
actions = ["kms:Decrypt"]
effect = "Allow"
resources = ["*"]
sid = "AllowRolesToAccess"
principals {
type = "AWS"
identifiers = local.allow_kms_decrypt
}
}
}
data "aws_iam_policy_document" "key_empty" {
}
locals {
json_w_decrypt = [data.aws_iam_policy_document.key.json, data.aws_iam_policy_document.key_roles.json]
json_wo_decrypt = [data.aws_iam_policy_document.key.json, data.aws_iam_policy_document.key_empty.json]
}
data "aws_iam_policy_document" "key_merged_policy" {
source_policy_documents = length(local.allow_kms_decrypt) > 0 ? local.json_w_decrypt : local.json_wo_decrypt
}
resource "aws_kms_key" "this" {
deletion_window_in_days = 7
description = "CloudTrail Encryption Key"
enable_key_rotation = true
policy = data.aws_iam_policy_document.key_merged_policy.json
tags = merge(
{
"Name" = "cloudtrail-key"
},
var.tags
)
}
resource "aws_kms_alias" "this" {
name = "alias/cloudtrail_key"
target_key_id = aws_kms_key.this.id
}