Skip to content

Commit f32c0c2

Browse files
Enable CMK KMS
1 parent 1995c9e commit f32c0c2

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

src/kms-key.tf

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
module "kms_key_archive" {
2+
source = "cloudposse/kms-key/aws"
3+
version = "0.12.2"
4+
5+
description = "KMS key for Datadog Log Archives"
6+
deletion_window_in_days = 10
7+
enable_key_rotation = true
8+
policy = join("", data.aws_iam_policy_document.kms_key_archive[*].json)
9+
10+
context = module.this.context
11+
}
12+
13+
data "aws_iam_policy_document" "kms_key_archive" {
14+
count = local.enabled ? 1 : 0
15+
16+
statement {
17+
sid = "Enable IAM User Permissions"
18+
effect = "Allow"
19+
principals {
20+
type = "AWS"
21+
identifiers = [
22+
format("arn:${local.aws_partition}:iam::%s:root", local.aws_account_id)
23+
]
24+
}
25+
actions = ["kms:*"]
26+
resources = ["*"]
27+
}
28+
29+
statement {
30+
sid = "Allow CloudTrail to use the key"
31+
effect = "Allow"
32+
principals {
33+
type = "Service"
34+
identifiers = ["cloudtrail.amazonaws.com"]
35+
}
36+
actions = [
37+
"kms:Encrypt",
38+
"kms:GenerateDataKey*"
39+
]
40+
resources = ["*"]
41+
condition {
42+
test = "StringLike"
43+
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
44+
values = [
45+
format("arn:${local.aws_partition}:cloudtrail:*:%s:trail/*", local.aws_account_id)
46+
]
47+
}
48+
}
49+
50+
statement {
51+
sid = "Allow use of the key"
52+
effect = "Allow"
53+
principals {
54+
type = "AWS"
55+
identifiers = [
56+
format("arn:${local.aws_partition}:iam::%s:role/${local.datadog_aws_role_name}", local.aws_account_id)
57+
]
58+
}
59+
actions = [
60+
"kms:Encrypt",
61+
"kms:Decrypt",
62+
"kms:ReEncrypt*",
63+
"kms:GenerateDataKey*",
64+
"kms:DescribeKey"
65+
]
66+
resources = ["*"]
67+
}
68+
69+
statement {
70+
sid = "Allow attachment of persistent resources"
71+
effect = "Allow"
72+
principals {
73+
type = "AWS"
74+
identifiers = [
75+
format("arn:${local.aws_partition}:iam::%s:role/${local.datadog_aws_role_name}", local.aws_account_id)
76+
]
77+
}
78+
actions = [
79+
"kms:CreateGrant",
80+
"kms:ListGrants",
81+
"kms:RevokeGrant"
82+
]
83+
resources = ["*"]
84+
condition {
85+
test = "Bool"
86+
variable = "kms:GrantIsForAWSResource"
87+
values = ["true"]
88+
}
89+
}
90+
}

src/main.tf

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ module "archive_bucket" {
169169
enabled = local.enabled
170170
force_destroy = var.s3_force_destroy
171171

172+
kms_master_key_arn = module.kms_key_archive.key_arn
173+
sse_algorithm = "aws:kms"
174+
172175
lifecycle_rules = [
173176
{
174177
prefix = null
@@ -220,7 +223,7 @@ module "archive_bucket" {
220223

221224
module "cloudtrail_s3_bucket" {
222225
source = "cloudposse/s3-bucket/aws"
223-
version = "3.1.2"
226+
version = "4.10.0"
224227

225228
depends_on = [data.aws_iam_policy_document.default]
226229

@@ -231,6 +234,9 @@ module "cloudtrail_s3_bucket" {
231234
enabled = local.enabled
232235
force_destroy = var.s3_force_destroy
233236

237+
kms_master_key_arn = module.kms_key_archive.key_arn
238+
sse_algorithm = "aws:kms"
239+
234240
source_policy_documents = data.aws_iam_policy_document.default.*.json
235241

236242
lifecycle_rules = [
@@ -241,8 +247,8 @@ module "cloudtrail_s3_bucket" {
241247

242248
abort_incomplete_multipart_upload_days = null
243249
enable_glacier_transition = var.enable_glacier_transition
244-
glacier_transition_days = 365
245-
noncurrent_version_glacier_transition_days = 365
250+
glacier_transition_days = var.glacier_transition_days
251+
noncurrent_version_glacier_transition_days = var.glacier_transition_days
246252
enable_deeparchive_transition = false
247253
deeparchive_transition_days = 0
248254
noncurrent_version_deeparchive_transition_days = 0
@@ -304,6 +310,7 @@ module "cloudtrail" {
304310
enabled = local.enabled
305311
enable_logging = true
306312
s3_bucket_name = module.cloudtrail_s3_bucket[0].bucket_id
313+
kms_key_arn = module.kms_key_archive.key_arn
307314

308315
event_selector = [
309316
{

0 commit comments

Comments
 (0)