diff --git a/envs/prod/main.tf b/envs/prod/main.tf index 4939f18..7aea36d 100644 --- a/envs/prod/main.tf +++ b/envs/prod/main.tf @@ -74,8 +74,9 @@ module "audit_logging_encryption" { name_prefix = var.name_prefix account_id = var.account_id - protect_from_destroy = true - bucket_names = local.bucket_names + protect_from_destroy = true + bucket_names = local.bucket_names + access_log_bucket_name = "aws-s3-access-logs-${var.account_id}-${var.region}-an" 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 4901fd8..c4ab1ac 100644 --- a/modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl +++ b/modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl @@ -48,6 +48,11 @@ run "apply_group1_osaka" { error_message = "The Osaka test must create two unprotected, force-destroyable S3 buckets." } + assert { + condition = length(aws_s3_bucket.access_logs_unprotected) == 1 && aws_s3_bucket.access_logs_unprotected[0].force_destroy + error_message = "The Osaka test must create one unprotected, force-destroyable S3 access log bucket." + } + 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/cloudtrail.tf b/modules/audit_logging_encryption/cloudtrail.tf index 50c3b55..37f99ee 100644 --- a/modules/audit_logging_encryption/cloudtrail.tf +++ b/modules/audit_logging_encryption/cloudtrail.tf @@ -4,7 +4,7 @@ resource "aws_cloudtrail" "management_events" { include_global_service_events = true is_multi_region_trail = var.cloudtrail_is_multi_region is_organization_trail = false - enable_log_file_validation = false + enable_log_file_validation = true enable_logging = true advanced_event_selector { diff --git a/modules/audit_logging_encryption/kms.tf b/modules/audit_logging_encryption/kms.tf index e6abb5e..ec69fb6 100644 --- a/modules/audit_logging_encryption/kms.tf +++ b/modules/audit_logging_encryption/kms.tf @@ -4,7 +4,7 @@ resource "aws_kms_key" "vault_protected" { description = "KMS for Vault" key_usage = "ENCRYPT_DECRYPT" customer_master_key_spec = "SYMMETRIC_DEFAULT" - enable_key_rotation = false + enable_key_rotation = true multi_region = false deletion_window_in_days = var.kms_deletion_window_in_days policy = local.kms_policy @@ -24,7 +24,7 @@ resource "aws_kms_key" "vault_unprotected" { description = "KMS for Vault" key_usage = "ENCRYPT_DECRYPT" customer_master_key_spec = "SYMMETRIC_DEFAULT" - enable_key_rotation = false + enable_key_rotation = true multi_region = false deletion_window_in_days = var.kms_deletion_window_in_days policy = local.kms_policy diff --git a/modules/audit_logging_encryption/locals.tf b/modules/audit_logging_encryption/locals.tf index 12cf201..e55896f 100644 --- a/modules/audit_logging_encryption/locals.tf +++ b/modules/audit_logging_encryption/locals.tf @@ -4,11 +4,18 @@ locals { flowlogs = coalesce(var.bucket_names.flowlogs, "${var.name_prefix}-flowlogs") } + access_log_bucket_name = coalesce(var.access_log_bucket_name, "${var.name_prefix}-s3-access-logs") + access_log_prefixes = { + for key in keys(local.buckets) : key => "${key}/" + } + bucket_ids = merge( { for key, bucket in aws_s3_bucket.protected : key => bucket.id }, { for key, bucket in aws_s3_bucket.unprotected : key => bucket.id }, ) + access_log_bucket_id = var.protect_from_destroy ? aws_s3_bucket.access_logs_protected[0].id : aws_s3_bucket.access_logs_unprotected[0].id + vault_kms_key_id = var.protect_from_destroy ? aws_kms_key.vault_protected[0].key_id : aws_kms_key.vault_unprotected[0].key_id vault_kms_key_arn = var.protect_from_destroy ? aws_kms_key.vault_protected[0].arn : aws_kms_key.vault_unprotected[0].arn @@ -97,14 +104,39 @@ locals { }) } + access_log_bucket_policy = jsonencode({ + Version = "2012-10-17" + Statement = [ + for key, bucket_name in local.buckets : { + Sid = "S3ServerAccessLogsPolicy${key}" + Effect = "Allow" + Principal = { Service = "logging.s3.amazonaws.com" } + Action = "s3:PutObject" + Resource = "arn:aws:s3:::${local.access_log_bucket_name}/${local.access_log_prefixes[key]}*" + Condition = { + ArnLike = { + "aws:SourceArn" = "arn:aws:s3:::${bucket_name}" + } + StringEquals = { + "aws:SourceAccount" = var.account_id + } + } + } + ] + }) + lifecycle_rules = { cloudtrail = { - id = "DeleteAfter3months" - expiration_days = 90 + id = "DeleteAfter3months" + expiration_days = 90 + noncurrent_expiration_days = 90 } flowlogs = { - id = "Housekeep" - expiration_days = 7 + id = "Housekeep" + expiration_days = 7 + noncurrent_expiration_days = 7 } } + + access_log_retention_days = 90 } diff --git a/modules/audit_logging_encryption/s3.tf b/modules/audit_logging_encryption/s3.tf index dd202bf..fca2b0e 100644 --- a/modules/audit_logging_encryption/s3.tf +++ b/modules/audit_logging_encryption/s3.tf @@ -22,7 +22,7 @@ resource "aws_s3_bucket_versioning" "this" { bucket = local.bucket_ids[each.key] versioning_configuration { - status = "Disabled" + status = "Enabled" } } @@ -82,5 +82,129 @@ resource "aws_s3_bucket_lifecycle_configuration" "this" { expiration { days = each.value.expiration_days } + + noncurrent_version_expiration { + noncurrent_days = each.value.noncurrent_expiration_days + } + } + + rule { + id = "DeleteExpiredObjectDeleteMarkers" + status = "Enabled" + + expiration { + expired_object_delete_marker = true + } + } + + depends_on = [aws_s3_bucket_versioning.this] +} + +resource "aws_s3_bucket" "access_logs_protected" { + count = var.protect_from_destroy ? 1 : 0 + + bucket = local.access_log_bucket_name + force_destroy = false + + lifecycle { + prevent_destroy = true } } + +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 +} + +resource "aws_s3_bucket_versioning" "access_logs" { + bucket = local.access_log_bucket_id + + versioning_configuration { + status = "Enabled" + } +} + +resource "aws_s3_bucket_server_side_encryption_configuration" "access_logs" { + bucket = local.access_log_bucket_id + + rule { + apply_server_side_encryption_by_default { + sse_algorithm = "AES256" + } + + bucket_key_enabled = false + blocked_encryption_types = ["SSE-C"] + } +} + +resource "aws_s3_bucket_public_access_block" "access_logs" { + bucket = local.access_log_bucket_id + + block_public_acls = true + block_public_policy = true + ignore_public_acls = true + restrict_public_buckets = true +} + +resource "aws_s3_bucket_ownership_controls" "access_logs" { + bucket = local.access_log_bucket_id + + rule { + object_ownership = "BucketOwnerEnforced" + } +} + +resource "aws_s3_bucket_policy" "access_logs" { + bucket = local.access_log_bucket_id + policy = local.access_log_bucket_policy +} + +resource "aws_s3_bucket_lifecycle_configuration" "access_logs" { + bucket = local.access_log_bucket_id + transition_default_minimum_object_size = "all_storage_classes_128K" + + rule { + id = "DeleteAfter90Days" + status = "Enabled" + + expiration { + days = local.access_log_retention_days + } + + noncurrent_version_expiration { + noncurrent_days = local.access_log_retention_days + } + } + + rule { + id = "DeleteExpiredObjectDeleteMarkers" + status = "Enabled" + + expiration { + expired_object_delete_marker = true + } + } + + depends_on = [aws_s3_bucket_versioning.access_logs] +} + +resource "aws_s3_bucket_logging" "this" { + for_each = local.buckets + + bucket = local.bucket_ids[each.key] + target_bucket = local.access_log_bucket_id + target_prefix = local.access_log_prefixes[each.key] + + target_object_key_format { + partitioned_prefix { + partition_date_source = "EventTime" + } + } + + depends_on = [ + aws_s3_bucket_ownership_controls.access_logs, + aws_s3_bucket_policy.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 c417050..74dd87a 100644 --- a/modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl +++ b/modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl @@ -45,6 +45,11 @@ run "plan_group1_osaka" { error_message = "The Osaka test must create two unprotected, force-destroyable S3 buckets." } + assert { + condition = length(aws_s3_bucket.access_logs_unprotected) == 1 && aws_s3_bucket.access_logs_unprotected[0].force_destroy + error_message = "The Osaka test must create one unprotected, force-destroyable S3 access log bucket." + } + 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 a7e0763..5210e4c 100644 --- a/modules/audit_logging_encryption/variables.tf +++ b/modules/audit_logging_encryption/variables.tf @@ -34,6 +34,12 @@ variable "bucket_names" { default = {} } +variable "access_log_bucket_name" { + description = "Optional S3 server access log destination bucket name. Omitted values are derived from name_prefix for isolated tests." + type = string + default = null +} + variable "trail_name" { description = "CloudTrail trail name." type = string diff --git a/modules/network_public/security_groups.tf b/modules/network_public/security_groups.tf index 5d16526..b7c637b 100644 --- a/modules/network_public/security_groups.tf +++ b/modules/network_public/security_groups.tf @@ -24,29 +24,19 @@ resource "aws_vpc_security_group_egress_rule" "web_all_ipv4" { region = var.region security_group_id = aws_security_group.web.id + description = "Allow all outbound traffic from the public web instance" ip_protocol = "-1" cidr_ipv4 = "0.0.0.0/0" } -# The default SG is adopted. Its two rules are managed as an absolute inline -# set and must not be mixed with standalone rule resources. +# The default SG is adopted. Its ingress and egress rules are managed as an +# absolute empty set and must not be mixed with standalone rule resources. resource "aws_default_security_group" "this" { region = var.region vpc_id = local.vpc.id - ingress { - description = null - protocol = "tcp" - from_port = 22 - to_port = 22 - cidr_blocks = ["0.0.0.0/0"] - } - - egress { - description = null - protocol = "-1" - from_port = 0 - to_port = 0 - cidr_blocks = ["0.0.0.0/0"] - } + # ingress/egress use attributes-as-blocks semantics. Explicit empty lists are + # required to revoke the rules already managed in state. + ingress = [] + egress = [] }