From d52e65383d3516e9da312a960dc5a8bf0dbb8939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6=E8=94=B5=E4=B8=80=E5=BC=98?= Date: Sat, 14 Nov 2026 16:33:14 +0900 Subject: [PATCH] Prepare group 1 Osaka Terraform tests --- .../.terraform.lock.hcl | 26 ++++++++ .../apply-tests/osaka_apply.tftest.hcl | 65 +++++++++++++++++++ .../audit_logging_encryption/cloudtrail.tf | 2 +- modules/audit_logging_encryption/kms.tf | 2 + modules/audit_logging_encryption/s3.tf | 2 +- .../tests/osaka_plan.tftest.hcl | 62 ++++++++++++++++++ modules/audit_logging_encryption/variables.tf | 23 +++++++ 7 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 modules/audit_logging_encryption/.terraform.lock.hcl create mode 100644 modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl create mode 100644 modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl diff --git a/modules/audit_logging_encryption/.terraform.lock.hcl b/modules/audit_logging_encryption/.terraform.lock.hcl new file mode 100644 index 0000000..4c68232 --- /dev/null +++ b/modules/audit_logging_encryption/.terraform.lock.hcl @@ -0,0 +1,26 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "6.73.0" + constraints = "~> 6.73" + hashes = [ + "h1:87t8o/A0y2RE3PtCLx/BiASSrDrKSETYTF9jRvKc7aw=", + "zh:0f7403264fbfbcad526a5ddb9553be6ab12dba14e5901d0c2ccabc38d9ccf9a0", + "zh:26be4e8afc94ef29c98b8219dc49b19ce21a04d3f2b4e5be6116e9526ece0355", + "zh:2ba661a4a782fba053c411ce4d828dc148d7ed3998d073a28370a559400b1578", + "zh:2c422d0edd37bf7470c84f1d1f76487f30948768878204a65a18189bf878467a", + "zh:49f9d1486d48033d298977f7c288f8bbb22bb246f00926d1cb8b0bb98985451c", + "zh:4f946cf11667e1f3bce53fd5ad92eccf47fafaa1bbaa68c742173db61773a1da", + "zh:70beb5e13ddcdbeecb2a78be2b75d09a0fc6ce7e69b574c84a90924fd4262406", + "zh:8c5b974104ff9a887b29fdb2700cdbb5e6fd2eceef1cc3b28f523beda4fd8dfb", + "zh:9252068667bc6528ee6e83e837f3c2f901756136a33442455d61c6fa34e1160a", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:9d086b792598b43e87502d47c4a3f388465b3a21a6a6b2783e48c89dee1f0543", + "zh:b5e6e77a43c1655285d99145b2cf0cc2da7a6c6ea3531d000cf8cca2c717693b", + "zh:b7ebe893e5ec76b362b61ffb28c67b40d890e0f59f5f2f8829765b16ee3e53b0", + "zh:c8c1e186839ad3cba37b427181af1e62eaf06c6daa5dca29105e1981dfaeb4c2", + "zh:e988377e7aea51e1fefe0b52c78bc725a11327051e20a71f81069191ee077d2d", + "zh:ee9188027919459a48bf9bac8f8f1dd172f5be198b88c2c47789b6daa8bb8bcb", + ] +} diff --git a/modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl b/modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl new file mode 100644 index 0000000..4901fd8 --- /dev/null +++ b/modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl @@ -0,0 +1,65 @@ +# This file is intentionally outside the default tests/ directory. +# Run it only after per-run human approval with -test-directory=apply-tests. + +provider "aws" { + alias = "osaka" + region = "ap-northeast-3" +} + +provider "aws" { + alias = "billing" + region = "us-east-1" +} + +variables { + region = "ap-northeast-3" + name_prefix = "tftest-g1-7f3a" + + protect_from_destroy = false + force_destroy_unprotected_buckets = true + + trail_name = "tftest-g1-7f3a-management-events" + cloudtrail_is_multi_region = false + + kms_alias_name = "alias/tftest-g1-7f3a-vault" + kms_deletion_window_in_days = 7 + + log_groups = { + vpc_flowlogs = { + name = "/tftest/g1/7f3a/flowlogs" + retention_in_days = 1 + } + } + + billing_alarm_name = "tftest-g1-7f3a-billing" + billing_alarm_action_arns = [] +} + +run "apply_group1_osaka" { + command = apply + + providers = { + aws = aws.osaka + aws.us_east_1 = aws.billing + } + + assert { + condition = length(aws_s3_bucket.unprotected) == 2 && alltrue([for bucket in aws_s3_bucket.unprotected : bucket.force_destroy]) + error_message = "The Osaka test must create two unprotected, force-destroyable S3 buckets." + } + + assert { + condition = !aws_cloudtrail.management_events.is_multi_region_trail + error_message = "The Osaka test CloudTrail trail must be single-Region." + } + + assert { + condition = aws_kms_key.vault_unprotected[0].deletion_window_in_days == 7 + error_message = "The Osaka test KMS key must use the minimum seven-day deletion window." + } + + assert { + condition = try(length(aws_cloudwatch_metric_alarm.billing.alarm_actions), 0) == 0 + error_message = "The Osaka test billing alarm must not notify production targets." + } +} diff --git a/modules/audit_logging_encryption/cloudtrail.tf b/modules/audit_logging_encryption/cloudtrail.tf index 873bacc..50c3b55 100644 --- a/modules/audit_logging_encryption/cloudtrail.tf +++ b/modules/audit_logging_encryption/cloudtrail.tf @@ -2,7 +2,7 @@ resource "aws_cloudtrail" "management_events" { name = var.trail_name s3_bucket_name = local.bucket_ids["cloudtrail"] include_global_service_events = true - is_multi_region_trail = true + is_multi_region_trail = var.cloudtrail_is_multi_region is_organization_trail = false enable_log_file_validation = false enable_logging = true diff --git a/modules/audit_logging_encryption/kms.tf b/modules/audit_logging_encryption/kms.tf index 973c477..e6abb5e 100644 --- a/modules/audit_logging_encryption/kms.tf +++ b/modules/audit_logging_encryption/kms.tf @@ -6,6 +6,7 @@ resource "aws_kms_key" "vault_protected" { customer_master_key_spec = "SYMMETRIC_DEFAULT" enable_key_rotation = false multi_region = false + deletion_window_in_days = var.kms_deletion_window_in_days policy = local.kms_policy lifecycle { @@ -25,6 +26,7 @@ resource "aws_kms_key" "vault_unprotected" { customer_master_key_spec = "SYMMETRIC_DEFAULT" enable_key_rotation = false multi_region = false + deletion_window_in_days = var.kms_deletion_window_in_days policy = local.kms_policy lifecycle { diff --git a/modules/audit_logging_encryption/s3.tf b/modules/audit_logging_encryption/s3.tf index d919a1a..dd202bf 100644 --- a/modules/audit_logging_encryption/s3.tf +++ b/modules/audit_logging_encryption/s3.tf @@ -13,7 +13,7 @@ resource "aws_s3_bucket" "unprotected" { for_each = var.protect_from_destroy ? {} : local.buckets bucket = each.value - force_destroy = false + force_destroy = var.force_destroy_unprotected_buckets } resource "aws_s3_bucket_versioning" "this" { diff --git a/modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl b/modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl new file mode 100644 index 0000000..c417050 --- /dev/null +++ b/modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl @@ -0,0 +1,62 @@ +provider "aws" { + alias = "osaka" + region = "ap-northeast-3" +} + +provider "aws" { + alias = "billing" + region = "us-east-1" +} + +variables { + region = "ap-northeast-3" + name_prefix = "tftest-g1-7f3a" + + protect_from_destroy = false + force_destroy_unprotected_buckets = true + + trail_name = "tftest-g1-7f3a-management-events" + cloudtrail_is_multi_region = false + + kms_alias_name = "alias/tftest-g1-7f3a-vault" + kms_deletion_window_in_days = 7 + + log_groups = { + vpc_flowlogs = { + name = "/tftest/g1/7f3a/flowlogs" + retention_in_days = 1 + } + } + + billing_alarm_name = "tftest-g1-7f3a-billing" + billing_alarm_action_arns = [] +} + +run "plan_group1_osaka" { + command = plan + + providers = { + aws = aws.osaka + aws.us_east_1 = aws.billing + } + + assert { + condition = length(aws_s3_bucket.unprotected) == 2 && alltrue([for bucket in aws_s3_bucket.unprotected : bucket.force_destroy]) + error_message = "The Osaka test must create two unprotected, force-destroyable S3 buckets." + } + + assert { + condition = !aws_cloudtrail.management_events.is_multi_region_trail + error_message = "The Osaka test CloudTrail trail must be single-Region." + } + + assert { + condition = aws_kms_key.vault_unprotected[0].deletion_window_in_days == 7 + error_message = "The Osaka test KMS key must use the minimum seven-day deletion window." + } + + assert { + condition = try(length(aws_cloudwatch_metric_alarm.billing.alarm_actions), 0) == 0 + error_message = "The Osaka test billing alarm must not notify production targets." + } +} diff --git a/modules/audit_logging_encryption/variables.tf b/modules/audit_logging_encryption/variables.tf index e5eb771..a7e0763 100644 --- a/modules/audit_logging_encryption/variables.tf +++ b/modules/audit_logging_encryption/variables.tf @@ -19,6 +19,12 @@ variable "protect_from_destroy" { default = true } +variable "force_destroy_unprotected_buckets" { + description = "Allow Terraform to delete objects while destroying unprotected test buckets. Protected production buckets always keep force_destroy=false." + type = bool + default = false +} + variable "bucket_names" { description = "Optional existing bucket names. Omitted values are derived from name_prefix for isolated tests." type = object({ @@ -33,11 +39,28 @@ variable "trail_name" { type = string } +variable "cloudtrail_is_multi_region" { + description = "Whether CloudTrail records events from all AWS Regions. Production keeps this true; isolated regional tests set it false." + type = bool + default = true +} + variable "kms_alias_name" { description = "Alias of the customer managed Vault key." type = string } +variable "kms_deletion_window_in_days" { + description = "Waiting period before permanent KMS key deletion. Isolated tests use the minimum seven days." + type = number + default = 30 + + validation { + condition = var.kms_deletion_window_in_days >= 7 && var.kms_deletion_window_in_days <= 30 + error_message = "kms_deletion_window_in_days must be between 7 and 30." + } +} + variable "log_groups" { description = "CloudWatch log groups and their retention in days. Null means never expire." type = map(object({