Skip to content

Commit

Permalink
Fix KMS and S3 policies
Browse files Browse the repository at this point in the history
* `jsonencode`s the s3 log bucket source arns
* Correctly formats `service-allow-decrypt.json.tpl`
* Uses the key arn rather than alias for cloudtrail
* Replaces the key arn with a wildcard, as ARNs can't contain wildcards
* Corrects the logs service name
* Adds the aws account ID to the logs bucket, so that it is globally
  unique
  • Loading branch information
Stretch96 committed Oct 27, 2023
1 parent 6a0d102 commit 3d8125a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions cloudtrail.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ resource "aws_kms_key" "cloudtrail_cloudwatch_logs" {
${templatefile("${path.root}/policies/kms-key-policy-statements/service-describe-key.json.tpl",
{
services = jsonencode(["cloudtrail.amazonaws.com"])
key_arn = "arn:aws:kms:${local.aws_region}:${local.aws_account_id}:key/*"
key_arn = "*"
source_arn = "arn:aws:cloudtrail:${local.aws_region}:${local.aws_account_id}:trail/${local.project_name}"
}
)},
Expand All @@ -55,7 +55,7 @@ resource "aws_cloudwatch_log_group" "cloudtrail" {

name = "${local.project_name}-cloudtrail"
retention_in_days = local.cloudtrail_log_retention
kms_key_id = local.cloudtrail_kms_encryption ? aws_kms_alias.cloudtrail_cloudwatch_logs[0].name : null
kms_key_id = local.cloudtrail_kms_encryption ? aws_kms_key.cloudtrail_cloudwatch_logs[0].arn : null
skip_destroy = true
}

Expand Down Expand Up @@ -99,7 +99,7 @@ resource "aws_cloudtrail" "cloudtrail" {
cloud_watch_logs_role_arn = aws_iam_role.cloudtrail_cloudwatch_logs[0].arn
cloud_watch_logs_group_arn = "${aws_cloudwatch_log_group.cloudtrail[0].arn}:*"
enable_log_file_validation = true
kms_key_id = local.cloudtrail_kms_encryption ? aws_kms_alias.cloudtrail_cloudwatch_logs[0].name : null
kms_key_id = local.cloudtrail_kms_encryption ? aws_kms_key.cloudtrail_cloudwatch_logs[0].arn : null

depends_on = [
aws_s3_bucket_policy.cloudtrail
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Effect": "Allow",
"Principal": {
"Service": "logs.region.amazonaws.com"
"Service": "logs.amazonaws.com"
},
"Action": [
"kms:Encrypt*",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Effect": "Allow",
"Principal": {
Service": ${services}
"Service": ${services}
},
"Action": "kms:Decrypt",
"Resource": "*"
Expand Down
4 changes: 2 additions & 2 deletions s3-logs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
resource "aws_s3_bucket" "logs" {
count = local.enable_logs_bucket ? 1 : 0

bucket = "${local.project_name}-logs"
bucket = "${local.aws_account_id}-${local.aws_region}-${local.project_name}-logs"
}

resource "aws_s3_bucket_policy" "logs" {
Expand All @@ -18,7 +18,7 @@ resource "aws_s3_bucket_policy" "logs" {
${templatefile("${path.root}/policies/s3-bucket-policy-statements/enforce-tls.json.tpl", { bucket_arn = aws_s3_bucket.logs[0].arn })},
${templatefile("${path.root}/policies/s3-bucket-policy-statements/log-delivery-access.json.tpl", {
log_bucket_arn = aws_s3_bucket.logs[0].arn
source_bucket_arns = local.logs_bucket_source_arns
source_bucket_arns = jsonencode(local.logs_bucket_source_arns)
account_id = local.aws_account_id
})}
]
Expand Down

0 comments on commit 3d8125a

Please sign in to comment.