infra: scope HA alert publishing to configured topics
Co-Authored-By: GPT-6 Astra <noreply@openai.com>
infra/modules/region-host/alert_publish.tf+17 −0infra/modules/region-host/outputs.tf+5 −0infra/modules/region-host/tests/alert_publish.tftest.hcl+405 −0infra/modules/region-host/tests/group_iam.tftest.hcl+12 −0infra/modules/region-host/variables.tf+13 −0scripts/check-region-group-plan.py+1 −0scripts/check_region_group_plan_test.py+43 −0scripts/region_group_contract_test.py+65 −4
Intent
<task-notification> <task-id>aa77ced5b32775b0d</task-id> <tool-use-id>toolu_019xq9LjRmtBWqh6tfHrNMNh</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/5555438b-7fa2-408d-b2c4-9fa87ea89332/tasks/aa77ced5b32775b0d.output</output-file> <status>completed</status> <summary>Agent "F-A grouped review R1 (Opus)" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>**Verdict:** READY AFTER FIXES. 0 P1, 2 P2, 3 P3. No P1s. The two P2s: - **P2-1:** Shutdown's wait for the recovery workers (`activator.go:6159`) ignores the shutdown deadline and runs before any other host is stopped. One slow recovery wake can delay the stop of every host on the box. - **P2-2:** The sweep retries every latched host every 10 s with no request behind it (`haleaseloss.go:208`). That means S3 polling forever (including hosts now held by a peer, or deleted), and an app wake for every lost host after a mass loss. Review: `/home/paulca/amadan.net/carlos/platform/.claude/worktrees/region-ha-g2-fixes/.superpowers/sdd/2026-11-06-region-ha-g2-fixes/fa-review-r1.md`</result> <usage><subagent_tokens>152176</subagent_tokens><tool_uses>29</tool_uses><duration_ms>625879</duration_ms></usage> </task-notification>
diff --git a/infra/modules/region-host/alert_publish.tf b/infra/modules/region-host/alert_publish.tf| new file mode 100644 |
| index 000000000..052f7d292 |
| --- /dev/null |
| +++ b/infra/modules/region-host/alert_publish.tf |
| @@ -0,0 +1,17 @@ |
| +locals { |
| + alert_publish_topic_arns = setunion(var.alert_publish_topic_arns, toset(aws_sns_topic.alerts[*].arn)) |
| +} |
| + |
| +resource "aws_iam_role_policy" "alert_publish" { |
| + count = var.alerts_email != "" || length(var.alert_publish_topic_arns) > 0 ? 1 : 0 |
| + name = "${var.iam_name}-alert-publish" |
| + role = aws_iam_role.host.id |
| + policy = jsonencode({ |
| + Version = "2012-10-17" |
| + Statement = [{ |
| + Effect = "Allow" |
| + Action = ["sns:Publish"] |
| + Resource = sort(tolist(local.alert_publish_topic_arns)) |
| + }] |
| + }) |
| +} |
diff --git a/infra/modules/region-host/outputs.tf b/infra/modules/region-host/outputs.tf| index f5a136a36..d0aee991e 100644 |
| --- a/infra/modules/region-host/outputs.tf |
| +++ b/infra/modules/region-host/outputs.tf |
| @@ -23,6 +23,11 @@ output "role_name" { |
| value = aws_iam_role.host.name |
| } |
| +output "alerts_topic_arn" { |
| + description = "ARN of this module's alerts topic. Null when alerts_email is empty." |
| + value = one(aws_sns_topic.alerts[*].arn) |
| +} |
| + |
| output "ami_id" { |
| description = "AMI ID selected for this host or group." |
| value = var.mode == "fixed" ? data.aws_ami.debian_arm64[0].id : var.group_config.ami_id |
diff --git a/infra/modules/region-host/tests/alert_publish.tftest.hcl b/infra/modules/region-host/tests/alert_publish.tftest.hcl| new file mode 100644 |
| index 000000000..c6ca6ec16 |
| --- /dev/null |
| +++ b/infra/modules/region-host/tests/alert_publish.tftest.hcl |
| @@ -0,0 +1,405 @@ |
| +mock_provider "aws" { |
| + mock_resource "aws_launch_template" { defaults = { id = "lt-0123456789abcdef0" } } |
| + mock_resource "aws_security_group" { defaults = { id = "sg-0123456789abcdef1" } } |
| +} |
| +override_data { |
| + target = data.aws_region.current |
| + values = { region = "eu-west-1" } |
| +} |
| +override_data { |
| + target = data.aws_partition.current |
| + values = { partition = "aws", dns_suffix = "amazonaws.com" } |
| +} |
| +override_data { |
| + target = data.aws_caller_identity.current |
| + values = { account_id = "123456789012" } |
| +} |
| +override_data { |
| + target = data.aws_vpc.host |
| + values = { cidr_block = "10.0.0.0/16", cidr_block_associations = [] } |
| +} |
| +override_data { |
| + target = data.aws_subnets.group |
| + values = { ids = ["subnet-00000000000000001", "subnet-00000000000000002"] } |
| +} |
| +override_data { |
| + target = data.aws_route_table.group |
| + values = { routes = [{ cidr_block = "0.0.0.0/0", gateway_id = "igw-0123456789abcdef0", carrier_gateway_id = "", core_network_arn = "", destination_prefix_list_id = "", egress_only_gateway_id = "", instance_id = "", ipv6_cidr_block = "", local_gateway_id = "", nat_gateway_id = "", network_interface_id = "", odb_network_arn = "", transit_gateway_id = "", vpc_endpoint_id = "", vpc_peering_connection_id = "" }] } |
| +} |
| +override_data { |
| + target = data.aws_lb.group |
| + values = { |
| + vpc_id = "vpc-0123456789abcdef0" |
| + internal = false |
| + load_balancer_type = "network" |
| + ip_address_type = "ipv4" |
| + enable_cross_zone_load_balancing = true |
| + security_groups = ["sg-0123456789abcdef0"] |
| + subnet_mapping = [ |
| + { subnet_id = "subnet-00000000000000001", allocation_id = "eipalloc-00000000000000001", ipv6_address = "", outpost_id = "", private_ipv4_address = "" }, |
| + { subnet_id = "subnet-00000000000000002", allocation_id = "eipalloc-00000000000000002", ipv6_address = "", outpost_id = "", private_ipv4_address = "" } |
| + ] |
| + } |
| +} |
| +override_data { |
| + target = data.aws_lb_target_group.group_http |
| + values = { vpc_id = "vpc-0123456789abcdef0", port = 80, protocol = "TCP", target_type = "instance", load_balancer_arns = ["arn:aws:elasticloadbalancing:eu-west-1:123456789012:loadbalancer/net/test/1234"], preserve_client_ip = "true" } |
| +} |
| +override_data { |
| + target = data.aws_lb_target_group.group_https |
| + values = { vpc_id = "vpc-0123456789abcdef0", port = 443, protocol = "TCP", target_type = "instance", load_balancer_arns = ["arn:aws:elasticloadbalancing:eu-west-1:123456789012:loadbalancer/net/test/1234"], preserve_client_ip = "true" } |
| +} |
| +override_data { |
| + target = data.aws_eip.group["eu-west-1a"] |
| + values = { public_ip = "198.51.100.10" } |
| +} |
| +override_data { |
| + target = data.aws_eip.group["eu-west-1b"] |
| + values = { public_ip = "198.51.100.11" } |
| +} |
| +override_resource { |
| + target = aws_sqs_queue.terminations |
| + values = { arn = "arn:aws:sqs:eu-west-1:123456789012:carlos-dev-ie-group-terminations", id = "https://sqs.eu-west-1.amazonaws.com/123456789012/carlos-dev-ie-group-terminations" } |
| +} |
| +override_resource { |
| + target = aws_cloudwatch_event_rule.terminations |
| + values = { arn = "arn:aws:events:eu-west-1:123456789012:rule/carlos-dev-ie-group-terminations" } |
| +} |
| +override_resource { |
| + target = aws_iam_role.host |
| + values = { id = "carlos-dev-ie-group", arn = "arn:aws:iam::123456789012:role/carlos-dev-ie-group" } |
| +} |
| +variables { |
| + mode = "group" |
| + name = "carlos-dev-ie-group" |
| + iam_name = "carlos-dev-ie-group" |
| + sg_description = "test group" |
| + instance_type = "t4g.micro" |
| + root_volume_size = 20 |
| + user_data = "" |
| + deployment_bucket_arn = "arn:aws:s3:::carlos-dev-platform" |
| + data_bucket_arn = "arn:aws:s3:::carlos-dev-ie" |
| + ssm_parameter_arn = "" |
| + vpc_id = "vpc-0123456789abcdef0" |
| + box_count = 2 |
| + group_config = { |
| + deployment = "dev" |
| + region = "ie" |
| + placement_region = "ie" |
| + pools = ["default"] |
| + ami_id = "ami-0123456789abcdef0" |
| + release = "v0.100.0" |
| + configuration_kms_key_arn = "arn:aws:kms:eu-west-1:123456789012:key/00000000-0000-0000-0000-000000000001" |
| + } |
| + group_zones = { |
| + eu-west-1a = { subnet_id = "subnet-00000000000000001", allocation_id = "eipalloc-00000000000000001" } |
| + eu-west-1b = { subnet_id = "subnet-00000000000000002", allocation_id = "eipalloc-00000000000000002" } |
| + } |
| + group_ingress = { |
| + nlb_arn = "arn:aws:elasticloadbalancing:eu-west-1:123456789012:loadbalancer/net/test/1234" |
| + nlb_security_group_id = "sg-0123456789abcdef0" |
| + target_group_arns = { |
| + "80" = "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/test-80/1234" |
| + "443" = "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/test-443/1234" |
| + } |
| + public_ips = { eu-west-1a = "198.51.100.10", eu-west-1b = "198.51.100.11" } |
| + } |
| +} |
| + |
| +override_data { |
| + target = data.aws_ami.group |
| + values = { id = "ami-0123456789abcdef0", root_device_name = "/dev/xvda", root_device_type = "ebs" } |
| +} |
| +override_data { |
| + target = data.aws_ami.debian_arm64 |
| + values = { id = "ami-0123456789abcdef0" } |
| +} |
| +override_resource { |
| + target = aws_instance.host |
| + values = { id = "i-0123456789abcdef0" } |
| +} |
| +override_resource { |
| + target = aws_security_group.host |
| + values = { id = "sg-0123456789abcdef0" } |
| +} |
| +override_resource { |
| + target = aws_sns_topic.alerts |
| + values = { arn = "arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts" } |
| +} |
| + |
| +run "ha_alert_publish_scope" { |
| + command = plan |
| + variables { |
| + alerts_email = "ops@example.com" |
| + } |
| + assert { |
| + condition = length(aws_iam_role_policy.alert_publish) == 1 && output.alerts_topic_arn == "arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts" |
| + error_message = "alert policy and topic output must match the configured sources" |
| + } |
| + assert { |
| + condition = aws_iam_role_policy.alert_publish[0].role == "carlos-dev-ie-group" && aws_iam_role_policy.alert_publish[0].policy == jsonencode({ |
| + Version = "2012-10-17" |
| + Statement = [{ Effect = "Allow", Action = ["sns:Publish"], Resource = sort(["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"]) }] |
| + }) |
| + error_message = "alert publishing must grant only sns:Publish on the exact topics to the host role" |
| + } |
| +} |
| + |
| +run "ha_alert_publish_both" { |
| + command = plan |
| + variables { |
| + alerts_email = "ops@example.com" |
| + alert_publish_topic_arns = ["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts", "arn:aws:sns:eu-west-1:123456789012:explicit-group-alerts", "arn:aws:sns:eu-west-1:123456789012:explicit-group-alerts"] |
| + } |
| + assert { |
| + condition = length(aws_iam_role_policy.alert_publish) == 1 && output.alerts_topic_arn == "arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts" |
| + error_message = "alert policy and topic output must match the configured sources" |
| + } |
| + assert { |
| + condition = aws_iam_role_policy.alert_publish[0].role == "carlos-dev-ie-group" && aws_iam_role_policy.alert_publish[0].policy == jsonencode({ |
| + Version = "2012-10-17" |
| + Statement = [{ Effect = "Allow", Action = ["sns:Publish"], Resource = sort(["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts", "arn:aws:sns:eu-west-1:123456789012:explicit-group-alerts"]) }] |
| + }) |
| + error_message = "alert publishing must grant only sns:Publish on the exact topics to the host role" |
| + } |
| +} |
| + |
| +run "ha_alert_publish_group_none" { |
| + command = plan |
| + assert { |
| + condition = length(aws_iam_role_policy.alert_publish) == 0 && output.alerts_topic_arn == null |
| + error_message = "alert policy and topic output must match the configured sources" |
| + } |
| +} |
| + |
| +run "ha_alert_publish_fixed_none" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + user_data = "#!/bin/sh\n" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + iam_name = "carlos-test-fixed" |
| + } |
| + override_resource { |
| + target = aws_iam_role.host |
| + values = { id = "carlos-test-fixed", arn = "arn:aws:iam::123456789012:role/carlos-test-fixed" } |
| + } |
| + assert { |
| + condition = length(aws_iam_role_policy.alert_publish) == 0 && output.alerts_topic_arn == null |
| + error_message = "alert policy and topic output must match the configured sources" |
| + } |
| +} |
| + |
| +run "ha_alert_publish_fixed_explicit" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + user_data = "#!/bin/sh\n" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + iam_name = "carlos-test-fixed" |
| + alert_publish_topic_arns = ["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"] |
| + } |
| + override_resource { |
| + target = aws_iam_role.host |
| + values = { id = "carlos-test-fixed", arn = "arn:aws:iam::123456789012:role/carlos-test-fixed" } |
| + } |
| + assert { |
| + condition = length(aws_iam_role_policy.alert_publish) == 1 && output.alerts_topic_arn == null |
| + error_message = "alert policy and topic output must match the configured sources" |
| + } |
| + assert { |
| + condition = aws_iam_role_policy.alert_publish[0].role == "carlos-test-fixed" && aws_iam_role_policy.alert_publish[0].policy == jsonencode({ |
| + Version = "2012-10-17" |
| + Statement = [{ Effect = "Allow", Action = ["sns:Publish"], Resource = sort(["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"]) }] |
| + }) |
| + error_message = "alert publishing must grant only sns:Publish on the exact topics to the host role" |
| + } |
| +} |
| + |
| +run "ha_alert_publish_invalid_wildcard_resource" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + user_data = "#!/bin/sh\n" |
| + alert_publish_topic_arns = ["*"] |
| + } |
| + expect_failures = [var.alert_publish_topic_arns] |
| +} |
| + |
| +run "ha_alert_publish_invalid_wildcard_topic" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + user_data = "#!/bin/sh\n" |
| + alert_publish_topic_arns = ["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts*"] |
| + } |
| + expect_failures = [var.alert_publish_topic_arns] |
| +} |
| + |
| +run "ha_alert_publish_invalid_question_mark_topic" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + user_data = "#!/bin/sh\n" |
| + alert_publish_topic_arns = ["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts?"] |
| + } |
| + expect_failures = [var.alert_publish_topic_arns] |
| +} |
| + |
| +run "ha_alert_publish_invalid_subscription" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + user_data = "#!/bin/sh\n" |
| + alert_publish_topic_arns = ["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts:subscription"] |
| + } |
| + expect_failures = [var.alert_publish_topic_arns] |
| +} |
| + |
| +run "ha_alert_publish_invalid_wrong_service" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + user_data = "#!/bin/sh\n" |
| + alert_publish_topic_arns = ["arn:aws:sqs:eu-west-1:123456789012:carlos-dev-ie-group-alerts"] |
| + } |
| + expect_failures = [var.alert_publish_topic_arns] |
| +} |
| + |
| +run "ha_alert_publish_invalid_wildcard_account" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + user_data = "#!/bin/sh\n" |
| + alert_publish_topic_arns = ["arn:aws:sns:eu-west-1:*:carlos-dev-ie-group-alerts"] |
| + } |
| + expect_failures = [var.alert_publish_topic_arns] |
| +} |
| + |
| +run "ha_alert_publish_invalid_wildcard_region" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + user_data = "#!/bin/sh\n" |
| + alert_publish_topic_arns = ["arn:aws:sns:*:123456789012:carlos-dev-ie-group-alerts"] |
| + } |
| + expect_failures = [var.alert_publish_topic_arns] |
| +} |
| + |
| +run "ha_alert_publish_invalid_wildcard_partition" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + user_data = "#!/bin/sh\n" |
| + alert_publish_topic_arns = ["arn:*:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"] |
| + } |
| + expect_failures = [var.alert_publish_topic_arns] |
| +} |
| + |
| +run "ha_alert_publish_invalid_short_account" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + user_data = "#!/bin/sh\n" |
| + alert_publish_topic_arns = ["arn:aws:sns:eu-west-1:123:carlos-dev-ie-group-alerts"] |
| + } |
| + expect_failures = [var.alert_publish_topic_arns] |
| +} |
| + |
| +run "ha_alert_publish_invalid_empty_topic" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + user_data = "#!/bin/sh\n" |
| + alert_publish_topic_arns = ["arn:aws:sns:eu-west-1:123456789012:"] |
| + } |
| + expect_failures = [var.alert_publish_topic_arns] |
| +} |
| + |
| +run "ha_alert_publish_invalid_topic_path" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + user_data = "#!/bin/sh\n" |
| + alert_publish_topic_arns = ["arn:aws:sns:eu-west-1:123456789012:a/b"] |
| + } |
| + expect_failures = [var.alert_publish_topic_arns] |
| +} |
| + |
| +run "ha_alert_publish_invalid_long_topic" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + user_data = "#!/bin/sh\n" |
| + alert_publish_topic_arns = ["arn:aws:sns:eu-west-1:123456789012:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"] |
| + } |
| + expect_failures = [var.alert_publish_topic_arns] |
| +} |
| + |
| +run "ha_alert_publish_invalid_null_topic" { |
| + command = plan |
| + variables { |
| + mode = "fixed" |
| + subnet_id = "subnet-00000000000000001" |
| + group_config = null |
| + group_zones = {} |
| + group_ingress = null |
| + user_data = "#!/bin/sh\n" |
| + alert_publish_topic_arns = [null] |
| + } |
| + expect_failures = [var.alert_publish_topic_arns] |
| +} |
diff --git a/infra/modules/region-host/tests/group_iam.tftest.hcl b/infra/modules/region-host/tests/group_iam.tftest.hcl| index dcedf6161..772ebc7ec 100644 |
| --- a/infra/modules/region-host/tests/group_iam.tftest.hcl |
| +++ b/infra/modules/region-host/tests/group_iam.tftest.hcl |
| @@ -69,7 +69,12 @@ override_resource { |
| target = aws_iam_role.host |
| values = { id = "carlos-dev-ie-group", arn = "arn:aws:iam::123456789012:role/carlos-dev-ie-group" } |
| } |
| +override_resource { |
| + target = aws_sns_topic.alerts |
| + values = { arn = "arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts" } |
| +} |
| variables { |
| + alerts_email = "ops@example.com" |
| mode = "group" |
| name = "carlos-dev-ie-group" |
| iam_name = "carlos-dev-ie-group" |
| @@ -108,6 +113,13 @@ variables { |
| run "group_iam_exact_scope" { |
| command = plan |
| + assert { |
| + condition = length(aws_iam_role_policy.alert_publish) == 1 && aws_iam_role_policy.alert_publish[0].role == "carlos-dev-ie-group" && aws_iam_role_policy.alert_publish[0].policy == jsonencode({ |
| + Version = "2012-10-17" |
| + Statement = [{ Effect = "Allow", Action = ["sns:Publish"], Resource = ["arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts"] }] |
| + }) |
| + error_message = "group alerts must grant only sns:Publish on the configured topic to the group role" |
| + } |
| assert { |
| condition = toset(flatten([ |
| for statement in jsondecode(aws_iam_role_policy.host_buckets.policy).Statement : |
diff --git a/infra/modules/region-host/variables.tf b/infra/modules/region-host/variables.tf| index 63a8d8f45..4ec6a50aa 100644 |
| --- a/infra/modules/region-host/variables.tf |
| +++ b/infra/modules/region-host/variables.tf |
| @@ -212,6 +212,19 @@ variable "alerts_email" { |
| default = "" |
| } |
| +variable "alert_publish_topic_arns" { |
| + description = "Additional SNS topic ARNs the host may publish alerts to. This grant does not configure CARLOS_ALERT_SNS_TOPIC_ARN." |
| + type = set(string) |
| + default = [] |
| + nullable = false |
| + validation { |
| + condition = alltrue([for arn in var.alert_publish_topic_arns : can(regex( |
| + "^arn:aws(-[a-z]+)*:sns:[a-z]{2}(-[a-z]+)+-[0-9]+:[0-9]{12}:([A-Za-z0-9_-]{1,256}|[A-Za-z0-9_-]{1,251}\\.fifo)$", arn |
| + ))]) |
| + error_message = "alert_publish_topic_arns must contain concrete SNS topic ARNs without wildcards or subscription suffixes" |
| + } |
| +} |
| + |
| variable "sibling_staging_arns" { |
| description = "Sibling regions' data-bucket ARNs this host may STAGE residence-moves into (instance-move spec, issue #331). The grant is moves/*-scoped push-only: Put/Delete on the staging objects and a prefix-conditioned ListBucket — never instances/*, never GetObject, so residency-at-rest holds while a move's delta rounds run. Empty (the default): no cross-bucket statement at all." |
| type = list(string) |
diff --git a/scripts/check-region-group-plan.py b/scripts/check-region-group-plan.py| index 9f24d8424..3a1d5526b 100755 |
| --- a/scripts/check-region-group-plan.py |
| +++ b/scripts/check-region-group-plan.py |
| @@ -50,6 +50,7 @@ GROUP_DISPOSABLE = { |
| ('aws_iam_role_policy', 'host_buckets'), ('aws_iam_role_policy', 'host_route53'), |
| ('aws_iam_role_policy', 'host_steering'), ('aws_iam_role_policy', 'host_metrics'), |
| ('aws_iam_role_policy', 'host_ssm_parameter_scope'), |
| + ('aws_iam_role_policy', 'alert_publish'), |
| ('aws_iam_role_policy', 'host_release_key_deny'), ('aws_iam_role_policy', 'host_terminations'), |
| ('aws_iam_role_policy', 'group'), ('aws_iam_role_policy_attachment', 'host_ssm'), |
| ('aws_sns_topic', 'alerts'), ('aws_sns_topic_subscription', 'alerts_email'), |
diff --git a/scripts/check_region_group_plan_test.py b/scripts/check_region_group_plan_test.py| index 577c2c1f3..d9a167159 100644 |
| --- a/scripts/check_region_group_plan_test.py |
| +++ b/scripts/check_region_group_plan_test.py |
| @@ -25,6 +25,49 @@ def plan(*resources): |
| class GateTests(unittest.TestCase): |
| + def test_alert_publish_disposable(self): |
| + address = GROUP + 'aws_iam_role_policy.alert_publish[0]' |
| + values = {'name': 'carlos-dev-ie-group-alert-publish', 'role': 'carlos-dev-ie-group', |
| + 'policy': json.dumps({'Version': '2012-10-17', 'Statement': [{ |
| + 'Effect': 'Allow', 'Action': ['sns:Publish'], |
| + 'Resource': ['arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts']}]})} |
| + creation = self.fixture('tofu-1.12-group-create.json') |
| + creation['resource_changes'].append(resource(address, 'aws_iam_role_policy', |
| + 'alert_publish', None, values, actions=['create'])) |
| + self.gate(creation, 0) |
| + deletion = self.fixture('tofu-1.12-group-partial-destroy.json') |
| + deletion['resource_changes'].append(resource(address, 'aws_iam_role_policy', |
| + 'alert_publish', values, None, actions=['delete'])) |
| + self.gate(deletion, 0) |
| + self.gate(deletion, 0, cleanup=group_manifest(deletion, creation, [address])) |
| + for prefix in [FIXED_MODULE, 'module.host_ie_group_other.', |
| + GROUP + 'module.child.', '']: |
| + with self.subTest(prefix=prefix): |
| + born, gone = copy.deepcopy(creation), copy.deepcopy(deletion) |
| + for doc in [born, gone]: |
| + doc['resource_changes'][-1]['address'] = prefix + 'aws_iam_role_policy.alert_publish[0]' |
| + self.gate(gone, 1, cleanup=group_manifest( |
| + gone, born, [gone['resource_changes'][-1]['address']])) |
| + |
| + def test_alert_publish_cannot_modify_fixed_role(self): |
| + for prefix in [GROUP, FIXED_MODULE, 'module.other.', '']: |
| + for actions in [['create'], ['update'], ['delete']]: |
| + with self.subTest(prefix=prefix, actions=actions): |
| + document = self.fixture('tofu-1.12-group-create.json') |
| + before = {'name': 'alert-publish', 'role': 'carlos-dev-ie', 'policy': '{}'} |
| + after = dict(before, policy='{"Statement": []}') |
| + document['resource_changes'] = [resource( |
| + prefix + 'aws_iam_role_policy.alert_publish[0]', 'aws_iam_role_policy', |
| + 'alert_publish', None if actions == ['create'] else before, |
| + None if actions == ['delete'] else after, actions=actions)] |
| + self.assertIn('fixed box resources cannot be changed', self.gate(document, 1).stderr) |
| + document = self.fixture('tofu-1.12-group-create.json') |
| + document['resource_changes'] = [resource( |
| + FIXED_MODULE + 'aws_iam_role_policy.alert_publish[0]', 'aws_iam_role_policy', |
| + 'alert_publish', after={'role': 'carlos-dev-ie', 'policy': '{}'}, actions=['create'])] |
| + self.gate(document, 1, allow=[{'address': '*', 'attribute': '*', |
| + 'before': None, 'after': '*', 'purpose': 'alert grant', 'approval': 'test'}]) |
| + |
| def test_unknown_foreign_identity(self): |
| for kind, field in [('aws_ec2_instance_state', 'instance_id'), |
| ('aws_volume_attachment', 'instance_id'), |
diff --git a/scripts/region_group_contract_test.py b/scripts/region_group_contract_test.py| index 4b4a6afdc..52a0bb082 100644 |
| --- a/scripts/region_group_contract_test.py |
| +++ b/scripts/region_group_contract_test.py |
| @@ -4,6 +4,7 @@ Pass region-host's event file. The fixed snapshot was generated from the |
| Task 2 base commit using fixed_state_unchanged, with the same mock region. |
| """ |
| import ast |
| +import copy |
| import re |
| import base64 |
| import gzip |
| @@ -68,13 +69,27 @@ SSM_CORE = { |
| } |
| +def audit_alert_publish(plan, role, topics): |
| + rows = [r for r in plan['planned_values']['root_module']['resources'] |
| + if r['type'] == 'aws_iam_role_policy' and r['name'] == 'alert_publish'] |
| + assert len(rows) == bool(topics), rows |
| + if topics: |
| + assert rows[0]['address'] == 'aws_iam_role_policy.alert_publish[0]' |
| + assert rows[0]['values']['role'] == role |
| + assert json.loads(rows[0]['values']['policy']) == { |
| + 'Version': '2012-10-17', 'Statement': [ |
| + {'Effect': 'Allow', 'Action': ['sns:Publish'], 'Resource': sorted(set(topics))}]} |
| + |
| + |
| def audit_group(plan, dns): |
| rows = plan['planned_values']['root_module']['resources'] |
| resources = {r['address']: r['values'] for r in rows if r['mode'] == 'managed'} |
| policies = {r['name']: json.loads(r['values']['policy'])['Statement'] for r in rows |
| if r['type'] == 'aws_iam_role_policy'} |
| expected = {'group', 'host_buckets', 'host_metrics', 'host_terminations', |
| - 'host_ssm_parameter_scope', 'host_release_key_deny'} |
| + 'host_ssm_parameter_scope', 'host_release_key_deny', 'alert_publish'} |
| + topic = 'arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts' |
| + audit_alert_publish(plan, 'carlos-dev-ie-group', [topic]) |
| if dns: |
| expected |= {'host_route53', 'host_steering'} |
| assert set(policies) == expected, set(policies) |
| @@ -90,7 +105,7 @@ def audit_group(plan, dns): |
| statements.append({'Effect': 'Allow', 'Action': sorted(SSM_CORE), 'Resource': '*'}) |
| actions = {a for s in statements if s['Effect'] == 'Allow' for a in seq(s['Action'])} |
| expected_actions = SSM_CORE | { |
| - 'ssm:GetParameterHistory', 'ssm:GetParametersByPath', 'kms:Decrypt', |
| + 'sns:Publish', 'ssm:GetParameterHistory', 'ssm:GetParametersByPath', 'kms:Decrypt', |
| 'autoscaling:CompleteLifecycleAction', 'ec2:DescribeInstances', |
| 's3:GetObject', 's3:PutObject', 's3:DeleteObject', 's3:GetObjectVersion', |
| 's3:ListBucket', 's3:GetBucketLocation', 's3:DeleteObjectVersion', |
| @@ -108,6 +123,7 @@ def audit_group(plan, dns): |
| parameter_resources = ['arn:aws:ssm:eu-west-1:123456789012:parameter/carlos/dev/' + p |
| for p in ['ie', 'ie/*', 'shared', 'shared/*']] |
| groups = [ |
| + (['sns:Publish'], [topic]), |
| (['ssm:GetParameter', 'ssm:GetParameters', 'ssm:GetParameterHistory', 'ssm:GetParametersByPath'], parameter_resources), |
| (['kms:Decrypt'], ['arn:aws:kms:eu-west-1:123456789012:key/00000000-0000-0000-0000-000000000001']), |
| (['autoscaling:CompleteLifecycleAction'], ['arn:aws:autoscaling:eu-west-1:123456789012:autoScalingGroup:*:autoScalingGroupName/carlos-dev-ie-group']), |
| @@ -228,13 +244,29 @@ def audit_group(plan, dns): |
| def main(path): |
| seen = set() |
| + fixed_alert_plans = {} |
| + topic = 'arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts' |
| + alert_cases = { |
| + 'ha_alert_publish_scope': ('carlos-dev-ie-group', [topic]), |
| + 'ha_alert_publish_both': ('carlos-dev-ie-group', [topic, 'arn:aws:sns:eu-west-1:123456789012:explicit-group-alerts']), |
| + 'ha_alert_publish_group_none': ('carlos-dev-ie-group', []), |
| + 'ha_alert_publish_fixed_none': ('carlos-test-fixed', []), |
| + 'ha_alert_publish_fixed_explicit': ('carlos-test-fixed', [topic]), |
| + } |
| for line in Path(path).read_text().splitlines(): |
| event = json.loads(line) |
| plan = event.get('test_plan') |
| run = event.get('@testrun') |
| if plan is None: |
| continue |
| - if run == 'fixed_state_unchanged': |
| + if run in alert_cases: |
| + audit_alert_publish(plan, *alert_cases[run]) |
| + if run.startswith('ha_alert_publish_fixed_'): |
| + fixed_alert_plans[run] = {r['address']: r['change']['after'] |
| + for r in plan['resource_changes'] if r['mode'] == 'managed' |
| + and r['address'] != 'aws_iam_role_policy.alert_publish[0]'} |
| + seen.add(run) |
| + elif run == 'fixed_state_unchanged': |
| actual = {r['address'].replace('[0]', ''): r['change']['after'] for r in plan['resource_changes'] if r['mode'] == 'managed'} |
| expected = json.loads((Path(__file__).parent / 'testdata/region-group-plan/fixed-resource-contract.json').read_text()) |
| assert actual == expected, 'fixed resource attributes changed from Task 2 base' |
| @@ -265,10 +297,39 @@ def main(path): |
| elif run in {'group_iam_exact_scope', 'group_dns_duties_enabled'}: |
| audit_group(plan, run == 'group_dns_duties_enabled') |
| seen.add(run) |
| - assert seen == {'fixed_state_unchanged', 'group_iam_exact_scope', 'group_dns_duties_enabled', 'group_boot_quotes', 'real_dev_user_data'}, seen |
| + assert seen == {'fixed_state_unchanged', 'group_iam_exact_scope', 'group_dns_duties_enabled', 'group_boot_quotes', 'real_dev_user_data'} | set(alert_cases), seen |
| + assert fixed_alert_plans['ha_alert_publish_fixed_none'] == fixed_alert_plans['ha_alert_publish_fixed_explicit'], 'explicit fixed alert grant changed unrelated resources' |
| class ContractTests(unittest.TestCase): |
| + def test_alert_publish_exact_policy(self): |
| + topic = 'arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts' |
| + policy = {'Version': '2012-10-17', 'Statement': [ |
| + {'Effect': 'Allow', 'Action': ['sns:Publish'], 'Resource': [topic]}]} |
| + row = {'address': 'aws_iam_role_policy.alert_publish[0]', 'type': 'aws_iam_role_policy', |
| + 'mode': 'managed', 'name': 'alert_publish', |
| + 'values': {'role': 'carlos-dev-ie-group', 'policy': json.dumps(policy)}} |
| + plan = {'planned_values': {'root_module': {'resources': [row]}}} |
| + audit_alert_publish(plan, 'carlos-dev-ie-group', [topic]) |
| + audit_alert_publish({'planned_values': {'root_module': {'resources': []}}}, 'unused', []) |
| + mutations = { |
| + 'wildcard': lambda r: r['values'].update(policy=json.dumps(dict(policy, Statement=[ |
| + dict(policy['Statement'][0], Resource=['*'])]))), |
| + 'unrelated role': lambda r: r['values'].update(role='carlos-other'), |
| + 'subscription': lambda r: r['values'].update(policy=json.dumps(dict(policy, Statement=[ |
| + dict(policy['Statement'][0], Action=['sns:Publish', 'sns:Subscribe'])]))), |
| + 'extra statement': lambda r: r['values'].update(policy=json.dumps(dict(policy, Statement= |
| + policy['Statement'] + [{'Effect': 'Allow', 'Action': ['kms:Decrypt'], 'Resource': ['*']}]))), |
| + } |
| + for label, mutate in mutations.items(): |
| + with self.subTest(label=label): |
| + bad = copy.deepcopy(plan) |
| + mutate(bad['planned_values']['root_module']['resources'][0]) |
| + with self.assertRaises(AssertionError): |
| + audit_alert_publish(bad, 'carlos-dev-ie-group', [topic]) |
| + with self.assertRaises(AssertionError): |
| + audit_alert_publish(plan, 'carlos-dev-ie-group', []) |
| + |
| def test_ssm_core_inheritance_and_explicit_deny(self): |
| resource = 'arn:aws:ssm:eu-west-1:123456789012:parameter/carlos/release-key' |
| statements = [{'Effect': 'Allow', 'Action': sorted(SSM_CORE), 'Resource': '*'}] |