Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions src/kms-key.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
module "kms_key_archive" {
source = "cloudposse/kms-key/aws"
version = "0.12.2"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for folks who don't want to kms encrypt this. could we add a variable that sets the enabled field for this module?

Suggested change
version = "0.12.2"
version = "0.12.2"
enabled = var.enable_kms_encryption

Also, you'll probably want to output the arn of the kms key


description = "KMS key for Datadog Log Archives"
deletion_window_in_days = 10
enable_key_rotation = true
policy = join("", data.aws_iam_policy_document.kms_key_archive[*].json)

context = module.this.context
}

data "aws_iam_policy_document" "kms_key_archive" {
count = local.enabled ? 1 : 0

statement {
sid = "Enable IAM User Permissions"
effect = "Allow"
principals {
type = "AWS"
identifiers = [
format("arn:${local.aws_partition}:iam::%s:root", local.aws_account_id)
]
}
actions = ["kms:*"]
resources = ["*"]
}

statement {
sid = "Allow CloudTrail to use the key"
effect = "Allow"
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
actions = [
"kms:Encrypt",
"kms:GenerateDataKey*"
]
resources = ["*"]
condition {
test = "StringLike"
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
values = [
format("arn:${local.aws_partition}:cloudtrail:*:%s:trail/*", local.aws_account_id)
]
}
}

statement {
sid = "Allow use of the key"
effect = "Allow"
principals {
type = "AWS"
identifiers = [
format("arn:${local.aws_partition}:iam::%s:role/${local.datadog_aws_role_name}", local.aws_account_id)
]
}
actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
]
resources = ["*"]
}

statement {
sid = "Allow attachment of persistent resources"
effect = "Allow"
principals {
type = "AWS"
identifiers = [
format("arn:${local.aws_partition}:iam::%s:role/${local.datadog_aws_role_name}", local.aws_account_id)
]
}
actions = [
"kms:CreateGrant",
"kms:ListGrants",
"kms:RevokeGrant"
]
resources = ["*"]
condition {
test = "Bool"
variable = "kms:GrantIsForAWSResource"
values = ["true"]
}
}
}
15 changes: 12 additions & 3 deletions src/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ module "bucket_policy" {
source = "cloudposse/iam-policy/aws"
version = "2.0.2"

iam_policy_statements = try(lookup(local.policy, "Statement"), null)
iam_policy = local.enabled ? {
statements = local.policy.Statement
} : null

context = module.this.context
}
Expand All @@ -167,6 +169,9 @@ module "archive_bucket" {
enabled = local.enabled
force_destroy = var.s3_force_destroy

kms_master_key_arn = module.kms_key_archive.key_arn
sse_algorithm = "aws:kms"

lifecycle_rules = [
{
prefix = null
Expand Down Expand Up @@ -229,6 +234,9 @@ module "cloudtrail_s3_bucket" {
enabled = local.enabled
force_destroy = var.s3_force_destroy

kms_master_key_arn = module.kms_key_archive.key_arn
sse_algorithm = "aws:kms"

source_policy_documents = data.aws_iam_policy_document.default.*.json

lifecycle_rules = [
Expand All @@ -239,8 +247,8 @@ module "cloudtrail_s3_bucket" {

abort_incomplete_multipart_upload_days = null
enable_glacier_transition = var.enable_glacier_transition
glacier_transition_days = 365
noncurrent_version_glacier_transition_days = 365
glacier_transition_days = var.glacier_transition_days
noncurrent_version_glacier_transition_days = var.glacier_transition_days
enable_deeparchive_transition = false
deeparchive_transition_days = 0
noncurrent_version_deeparchive_transition_days = 0
Expand Down Expand Up @@ -302,6 +310,7 @@ module "cloudtrail" {
enabled = local.enabled
enable_logging = true
s3_bucket_name = module.cloudtrail_s3_bucket[0].bucket_id
kms_key_arn = module.kms_key_archive.key_arn

event_selector = [
{
Expand Down
Loading