forked from bridgecrewio/terraform-aws-bridgecrew-cloudtrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kms_cloudtrail_key.tf
107 lines (85 loc) · 2.31 KB
/
kms_cloudtrail_key.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
resource "aws_kms_key" "cloudtrail_key" {
count = var.existing_kms_key_arn == null ? 1 : 0
description = "KMS for Cloudtrail, shared with Lacework and Bridgecrew"
enable_key_rotation = true
policy = data.aws_iam_policy_document.cloudtrail_key[0].json
}
resource "aws_kms_alias" "cloudtrail_key" {
count = var.existing_kms_key_arn == null ? 1 : 0
name = "alias/${local.resource_name_prefix}-CloudtrailKey"
target_key_id = aws_kms_key.cloudtrail_key[0].key_id
}
data "aws_iam_policy_document" "cloudtrail_key" {
count = var.existing_kms_key_arn == null ? 1 : 0
statement {
sid = "AccountOwner"
actions = [
"kms:*",
]
effect = "Allow"
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${local.account_id}:root"]
}
resources = ["*"]
}
statement {
sid = "CloudTrailEncrypt"
actions = [
"kms:GenerateDataKey*",
"kms:ReEncryptFrom",
]
effect = "Allow"
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
condition {
test = "StringLike"
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
values = [
"arn:aws:cloudtrail:*:${var.source_account_id != "" ? var.source_account_id : local.account_id}:trail/*"
]
}
resources = ["*"]
}
statement {
sid = "CloudTrailDescribe"
actions = [
"kms:DescribeKey",
]
effect = "Allow"
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
resources = ["*"]
}
statement {
sid = "BridgecrewDecrypt"
actions = [
"kms:Decrypt",
"kms:ReEncryptFrom",
]
effect = "Allow"
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "kms:CallerAccount"
values = [var.source_account_id != "" ? var.source_account_id : local.account_id]
}
condition {
test = "StringLike"
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
values = [
"arn:aws:cloudtrail:*:${
var.source_account_id != "" ? var.source_account_id : local.account_id
}:trail/*"
]
}
resources = ["*"]
}
}