From 7ec829fd0a51e48fdc5d431f23e3a393d6180145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6=E8=94=B5=E4=B8=80=E5=BC=98?= Date: Sun, 15 Nov 2026 00:52:52 +0900 Subject: [PATCH] Use account regional namespace for access logs --- envs/prod/main.tf | 7 ++++--- .../apply-tests/osaka_apply.tftest.hcl | 5 +++++ modules/audit_logging_encryption/s3.tf | 10 ++++++---- .../tests/osaka_plan.tftest.hcl | 5 +++++ modules/audit_logging_encryption/variables.tf | 11 +++++++++++ 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/envs/prod/main.tf b/envs/prod/main.tf index 7aea36d..2c772e2 100644 --- a/envs/prod/main.tf +++ b/envs/prod/main.tf @@ -74,9 +74,10 @@ module "audit_logging_encryption" { name_prefix = var.name_prefix account_id = var.account_id - protect_from_destroy = true - bucket_names = local.bucket_names - access_log_bucket_name = "aws-s3-access-logs-${var.account_id}-${var.region}-an" + protect_from_destroy = true + bucket_names = local.bucket_names + access_log_bucket_name = "aws-s3-access-logs-${var.account_id}-${var.region}-an" + access_log_bucket_namespace = "account-regional" trail_name = "management-events" kms_alias_name = "alias/vault-kms" diff --git a/modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl b/modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl index c4ab1ac..283ead5 100644 --- a/modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl +++ b/modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl @@ -53,6 +53,11 @@ run "apply_group1_osaka" { error_message = "The Osaka test must create one unprotected, force-destroyable S3 access log bucket." } + assert { + condition = aws_s3_bucket.access_logs_unprotected[0].bucket_namespace == "global" + error_message = "The Osaka test must keep the S3 access log bucket in the default global namespace." + } + assert { condition = !aws_cloudtrail.management_events.is_multi_region_trail error_message = "The Osaka test CloudTrail trail must be single-Region." diff --git a/modules/audit_logging_encryption/s3.tf b/modules/audit_logging_encryption/s3.tf index fca2b0e..511530d 100644 --- a/modules/audit_logging_encryption/s3.tf +++ b/modules/audit_logging_encryption/s3.tf @@ -103,8 +103,9 @@ resource "aws_s3_bucket_lifecycle_configuration" "this" { resource "aws_s3_bucket" "access_logs_protected" { count = var.protect_from_destroy ? 1 : 0 - bucket = local.access_log_bucket_name - force_destroy = false + bucket = local.access_log_bucket_name + bucket_namespace = var.access_log_bucket_namespace + force_destroy = false lifecycle { prevent_destroy = true @@ -114,8 +115,9 @@ resource "aws_s3_bucket" "access_logs_protected" { resource "aws_s3_bucket" "access_logs_unprotected" { count = var.protect_from_destroy ? 0 : 1 - bucket = local.access_log_bucket_name - force_destroy = var.force_destroy_unprotected_buckets + bucket = local.access_log_bucket_name + bucket_namespace = var.access_log_bucket_namespace + force_destroy = var.force_destroy_unprotected_buckets } resource "aws_s3_bucket_versioning" "access_logs" { diff --git a/modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl b/modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl index 74dd87a..488e24d 100644 --- a/modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl +++ b/modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl @@ -50,6 +50,11 @@ run "plan_group1_osaka" { error_message = "The Osaka test must create one unprotected, force-destroyable S3 access log bucket." } + assert { + condition = aws_s3_bucket.access_logs_unprotected[0].bucket_namespace == "global" + error_message = "The Osaka test must keep the S3 access log bucket in the default global namespace." + } + assert { condition = !aws_cloudtrail.management_events.is_multi_region_trail error_message = "The Osaka test CloudTrail trail must be single-Region." diff --git a/modules/audit_logging_encryption/variables.tf b/modules/audit_logging_encryption/variables.tf index 5210e4c..263f554 100644 --- a/modules/audit_logging_encryption/variables.tf +++ b/modules/audit_logging_encryption/variables.tf @@ -40,6 +40,17 @@ variable "access_log_bucket_name" { default = null } +variable "access_log_bucket_namespace" { + description = "Namespace for the S3 server access log destination bucket. Isolated tests use the global namespace by default; production can opt in to account-regional." + type = string + default = "global" + + validation { + condition = contains(["account-regional", "global"], var.access_log_bucket_namespace) + error_message = "access_log_bucket_namespace must be account-regional or global." + } +} + variable "trail_name" { description = "CloudTrail trail name." type = string