tofu: ingress drain target group retention, fixed-box ELB reads, host-env key
W6 Task 8 (fixed-edge ingress drain spec §4.1, §8). - ingress module: both target groups keep an unhealthy target's connections for 180 s (target_health_state, termination off). - region-host, fixed mode: input ingress_target_group_arns; when set, the role gains elasticloadbalancing:DescribeTargetHealth and DescribeTargetGroupAttributes on "*" (the service authorization reference lists no resource type for either), and output ingress_target_groups gives the CARLOS_INGRESS_TARGET_GROUPS value for the operator's host-env, a separate later step. Group mode refuses it. - hostenv: CARLOS_INGRESS_TARGET_GROUPS allowed, validated with ingresshealth.ParseTargetGroups (shape and self-consistency in Assemble and Seed; the group config's account and region in AssembleGroup). Tests: tests/ingress_drain_read.tftest.hcl, group_iam and group_ingress runs (OpenTofu 1.13.9, mock provider); hostenv assembly and seed tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
infra/modules/region-host/ingress/main.tf+10 −0infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl+18 −0infra/modules/region-host/main.tf+23 −0infra/modules/region-host/outputs.tf+5 −0infra/modules/region-host/tests/group_iam.tftest.hcl+13 −0infra/modules/region-host/tests/ingress_drain_read.tftest.hcl+112 −0infra/modules/region-host/variables.tf+32 −0internal/hostenv/group.go+3 −0internal/hostenv/hostenv.go+41 −0internal/hostenv/hostenv_test.go+106 −0
Intent
<task-notification> <task-id>ab52d0991e8a15736</task-id> <tool-use-id>toolu_013scZbWj2S9aeWPNs1oUE5F</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/a373aecd-faa9-4b83-8736-ff726fb2d3af/tasks/ab52d0991e8a15736.output</output-file> <status>completed</status> <summary>Agent "Implement W6 Task 6 (helper+verbs)" 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>Status: DONE. Fix round 1 is committed. Git printed the same `packed-refs.lock` error during the commit (another git process was running in the repo), but HEAD and `w6-ingress-drain` both point at 5408ff60. Commits: f42b6ade (Task 6) and 5408ff60 (fix round 1). Fix: when the agent returns a report, `__ingress-drain` and the apply and rollback verbs now print two lines. The first is the existing summary. The second is the whole report as one compact JSON line, prefixed `ingress drain report: `. It includes target states over time, step times, read count, held, finished and closed counts, the close error, target groups and incarnation. When there is no report, it is still one line saying why. Tests: the new tests failed first and pass after the fix. A fully filled-in report decodes back exactly through both `__ingress-drain` and the verb's logging. Only answers that are reports get the JSON line. The same 195-test set passes (162 s), and `go vet`, gofmt and the darwin/windows builds are clean. Concerns: no new ones. The earlier ones still stand: recovery doesn't ask for the drain, a refusal after the drain leaves the box out until the agent's 3-minute exit, and Task 11 has census entries to add. I touched only `internal/host`; the other agent's uncommitted changes in `infra/` and `internal/hostenv` are untouched. Report (fix appended): /home/paulca/amadan.net/carlos/platform/.claude/worktrees/w6-build/.superpowers/sdd/2026-11-10-fixed-edge-ingress-drain/task-6-report.md</result> <usage><subagent_tokens>210410</subagent_tokens><tool_uses>14</tool_uses><duration_ms>403086</duration_ms></usage> </task-notification>
Attributed to this commit: 10.2 g CO₂e (1,300,309 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on w6-ingress-drain, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/infra/modules/region-host/ingress/main.tf b/infra/modules/region-host/ingress/main.tf| index bcbcb620d..a31951c27 100644 |
| --- a/infra/modules/region-host/ingress/main.tf |
| +++ b/infra/modules/region-host/ingress/main.tf |
| @@ -113,6 +113,16 @@ resource "aws_lb_target_group" "target" { |
| unhealthy_threshold = 2 |
| matcher = "200" |
| } |
| + # W6 ingress drain (fixed-edge ingress drain spec §8): an unhealthy target |
| + # keeps its open connections, refusing new ones, for 180 s instead of |
| + # being reset at the unhealthy mark. A box draining itself out fails its |
| + # health check on purpose and still has requests in flight; 180 s covers |
| + # the drain's 90 s cap, 10 s settle and 30 s connection wait with 50 s to |
| + # spare. The interval can be set only with termination off. |
| + target_health_state { |
| + enable_unhealthy_connection_termination = false |
| + unhealthy_draining_interval = 180 |
| + } |
| } |
| resource "aws_lb_listener" "tcp" { |
diff --git a/infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl b/infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl| index 2dd9545d5..2bb599401 100644 |
| --- a/infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl |
| +++ b/infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl |
| @@ -102,6 +102,24 @@ run "nlb_contract" { |
| } |
| } |
| +# W6 ingress drain (fixed-edge ingress drain spec §8): a target that turns |
| +# unhealthy keeps its open connections for 180 s instead of being reset, so |
| +# a box draining itself out of the load balancer finishes the requests it |
| +# has. The interval outlasts the whole drain (90 s cap, 10 s settle, 30 s |
| +# connection wait), and the drain reads both attributes back before it |
| +# trusts a withdrawal. |
| +run "unhealthy_targets_keep_their_connections" { |
| + command = plan |
| + assert { |
| + condition = alltrue([for key, tg in aws_lb_target_group.target : |
| + length(tg.target_health_state) == 1 && |
| + tg.target_health_state[0].enable_unhealthy_connection_termination == false && |
| + tg.target_health_state[0].unhealthy_draining_interval == 180 |
| + ]) && toset(keys(aws_lb_target_group.target)) == toset(["80", "443"]) |
| + error_message = "both target groups must keep an unhealthy target's connections for 180 s" |
| + } |
| +} |
| + |
| run "target_security" { |
| command = plan |
| assert { |
diff --git a/infra/modules/region-host/main.tf b/infra/modules/region-host/main.tf| index d59db290c..0f9e36518 100644 |
| --- a/infra/modules/region-host/main.tf |
| +++ b/infra/modules/region-host/main.tf |
| @@ -150,6 +150,7 @@ resource "aws_iam_role" "host" { |
| var.console_mail_from == "" && var.console_mail_region == "" && |
| var.store_bucket_prefix == "" && !var.store_scanning && |
| var.scope_ssm_parameter_reads && var.deny_release_key_reads && |
| + length(var.ingress_target_group_arns) == 0 && |
| (length(var.steering_zone_ids) == 0 || length(var.steering_record_names) > 0) && |
| can(regex("^[a-zA-Z][a-zA-Z0-9-]{0,22}$", var.name)) && |
| can(regex("^[a-zA-Z][a-zA-Z0-9-]{0,63}$", var.iam_name)) |
| @@ -312,6 +313,28 @@ resource "aws_iam_role_policy" "host_route53" { |
| }) |
| } |
| +# W6 ingress drain (fixed-edge ingress drain spec §4.1, §8), fixed boxes |
| +# attached to an NLB only: the agent reads its own target health, with no |
| +# target filter so one answer also counts the other healthy targets, and |
| +# reads each target group's unhealthy-connection attributes once before it |
| +# trusts a withdrawal. Read-only; no ELB write right (region HA §12.2). |
| +# Resource is "*" because the service authorization reference lists no |
| +# resource type for either action: neither takes a target group ARN. |
| +# Group mode never gets it (the role's precondition refuses the input). |
| +resource "aws_iam_role_policy" "host_ingress_drain" { |
| + count = var.mode == "fixed" && length(var.ingress_target_group_arns) > 0 ? 1 : 0 |
| + name = "${var.iam_name}-ingress-drain" |
| + role = aws_iam_role.host.id |
| + policy = jsonencode({ |
| + Version = "2012-10-17" |
| + Statement = [{ |
| + Effect = "Allow" |
| + Action = ["elasticloadbalancing:DescribeTargetGroupAttributes", "elasticloadbalancing:DescribeTargetHealth"] |
| + Resource = "*" |
| + }] |
| + }) |
| +} |
| + |
| # DNS steering (internal/steering/route53.go, run as a housekeeper duty on |
| # whichever box holds the lease): latency A records per armed edge, each bound |
| # to a Route53 health check on that edge's EIP. Every action here is one the |
diff --git a/infra/modules/region-host/outputs.tf b/infra/modules/region-host/outputs.tf| index 66e9b4f19..e2186891d 100644 |
| --- a/infra/modules/region-host/outputs.tf |
| +++ b/infra/modules/region-host/outputs.tf |
| @@ -43,6 +43,11 @@ output "vpc_cidr" { |
| value = data.aws_vpc.host.cidr_block |
| } |
| +output "ingress_target_groups" { |
| + description = "The value for a fixed box's CARLOS_INGRESS_TARGET_GROUPS: ingress_target_group_arns, comma-separated. Add it to the box's host-env only after this module's role and the target group attributes are applied and checked with aws elbv2 describe-target-group-attributes. Null when ingress_target_group_arns is empty, and in group mode." |
| + value = var.mode == "fixed" && length(var.ingress_target_group_arns) > 0 ? join(",", var.ingress_target_group_arns) : null |
| +} |
| + |
| output "termination_queue_url" { |
| description = "The queue that receives this region's EC2 terminated events." |
| value = aws_sqs_queue.terminations.id |
diff --git a/infra/modules/region-host/tests/group_iam.tftest.hcl b/infra/modules/region-host/tests/group_iam.tftest.hcl| index 772ebc7ec..ddb44d488 100644 |
| --- a/infra/modules/region-host/tests/group_iam.tftest.hcl |
| +++ b/infra/modules/region-host/tests/group_iam.tftest.hcl |
| @@ -162,6 +162,8 @@ run "group_iam_exact_scope" { |
| length(aws_iam_role_policy.host_store_scan) == 0 && |
| length(aws_iam_role_policy.host_steering) == 0 && |
| length(aws_iam_role_policy.host_route53) == 0 && |
| + length(aws_iam_role_policy.host_ingress_drain) == 0 && |
| + output.ingress_target_groups == null && |
| jsondecode(aws_iam_role_policy.host_terminations.policy).Statement[0].Resource == aws_sqs_queue.terminations.arn && |
| toset(jsondecode(aws_iam_role_policy.host_terminations.policy).Statement[0].Action) == toset(["sqs:ReceiveMessage", "sqs:DeleteMessage", "sqs:GetQueueAttributes"]) && |
| jsondecode(aws_sqs_queue_policy.terminations.policy).Statement[1].Effect == "Deny" && |
| @@ -186,6 +188,17 @@ run "group_no_control_writes" { |
| } |
| } |
| +# Group members leave the load balancer through the region HA drain only |
| +# (fixed-edge ingress drain spec §4.1): the ingress drain's read right and |
| +# its host.env value are for fixed boxes, so group mode refuses the input. |
| +run "group_refuses_ingress_target_groups" { |
| + command = plan |
| + variables { |
| + ingress_target_group_arns = ["arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/test-80/0123456789abcdef"] |
| + } |
| + expect_failures = [aws_iam_role.host] |
| +} |
| + |
| run "group_dns_duties_enabled" { |
| command = plan |
| variables { |
diff --git a/infra/modules/region-host/tests/ingress_drain_read.tftest.hcl b/infra/modules/region-host/tests/ingress_drain_read.tftest.hcl| new file mode 100644 |
| index 000000000..ac4fd50e6 |
| --- /dev/null |
| +++ b/infra/modules/region-host/tests/ingress_drain_read.tftest.hcl |
| @@ -0,0 +1,112 @@ |
| +# Plan-time tests for the W6 ingress drain's read right on a fixed box |
| +# (fixed-edge ingress drain spec §8 "Box role, fixed boxes only", §4.1). |
| +# mock_provider means no AWS account and no credentials: these assert what |
| +# the module WOULD create. Group mode refusing the input is in |
| +# group_iam.tftest.hcl, beside the group overrides it needs. |
| +mock_provider "aws" {} |
| + |
| +override_data { |
| + target = data.aws_vpcs.default |
| + values = { ids = ["vpc-0aaaaaaaaaaaaaaaa"] } |
| +} |
| + |
| +override_data { |
| + target = data.aws_caller_identity.current |
| + values = { account_id = "123456789012" } |
| +} |
| + |
| +# The termination queue's ARN feeds an EventBridge target, which the provider |
| +# validates as an ARN; the mock's random string is not one. |
| +override_resource { |
| + target = aws_sqs_queue.terminations |
| + values = { arn = "arn:aws:sqs:eu-west-1:123456789012:carlos-test-host-terminations" } |
| +} |
| + |
| +variables { |
| + name = "carlos-test-ie" |
| + sg_description = "carlos test host" |
| + iam_name = "carlos-test-host" |
| + instance_type = "t4g.small" |
| + root_volume_size = 20 |
| + user_data = "#!/bin/sh\n" |
| + deployment_bucket_arn = "arn:aws:s3:::carlos-test-platform" |
| + data_bucket_arn = "arn:aws:s3:::carlos-test-ie" |
| + ssm_parameter_arn = "arn:aws:ssm:eu-west-1:123456789012:parameter/carlos/*" |
| +} |
| + |
| +# A fixed box not attached to a load balancer: no read right and no value |
| +# for host.env, so its drain stays not_armed. |
| +run "off_by_default" { |
| + command = plan |
| + assert { |
| + condition = length(aws_iam_role_policy.host_ingress_drain) == 0 |
| + error_message = "no ingress target groups must create no ingress drain policy" |
| + } |
| + assert { |
| + condition = output.ingress_target_groups == null |
| + error_message = "no ingress target groups must give no CARLOS_INGRESS_TARGET_GROUPS value" |
| + } |
| + assert { |
| + condition = alltrue([for s in jsondecode(aws_iam_role_policy.host_buckets.policy).Statement : |
| + length([for a in flatten([s.Action]) : a if startswith(a, "elasticloadbalancing:")]) == 0 |
| + ]) |
| + error_message = "the bucket and inventory policy must carry no load balancer action" |
| + } |
| +} |
| + |
| +run "attached_box_gets_the_read_statement" { |
| + command = plan |
| + variables { |
| + ingress_target_group_arns = [ |
| + "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/shop-80/0123456789abcdef", |
| + "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/shop-443/fedcba9876543210", |
| + ] |
| + } |
| + assert { |
| + condition = length(aws_iam_role_policy.host_ingress_drain) == 1 && aws_iam_role_policy.host_ingress_drain[0].name == "carlos-test-host-ingress-drain" |
| + error_message = "attached target groups must create one policy named <iam_name>-ingress-drain" |
| + } |
| + # Exactly one statement, the two read actions, on "*": the service |
| + # authorization reference lists no resource type for either action, so |
| + # neither takes a target group ARN. No write action of any kind. |
| + assert { |
| + condition = jsondecode(aws_iam_role_policy.host_ingress_drain[0].policy) == { |
| + Version = "2012-10-17" |
| + Statement = [{ |
| + Effect = "Allow" |
| + Action = ["elasticloadbalancing:DescribeTargetGroupAttributes", "elasticloadbalancing:DescribeTargetHealth"] |
| + Resource = "*" |
| + }] |
| + } |
| + error_message = "the ingress drain policy must be the two ELB reads on * and nothing else" |
| + } |
| + assert { |
| + condition = output.ingress_target_groups == "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/shop-80/0123456789abcdef,arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/shop-443/fedcba9876543210" |
| + error_message = "the CARLOS_INGRESS_TARGET_GROUPS value must be the ARNs, comma-separated, in order" |
| + } |
| + assert { |
| + condition = alltrue([for s in jsondecode(aws_iam_role_policy.host_buckets.policy).Statement : |
| + length([for a in flatten([s.Action]) : a if startswith(a, "elasticloadbalancing:")]) == 0 |
| + ]) |
| + error_message = "the read right must not move into the bucket and inventory policy" |
| + } |
| +} |
| + |
| +run "not_a_target_group_arn_is_refused" { |
| + command = plan |
| + variables { |
| + ingress_target_group_arns = ["arn:aws:elasticloadbalancing:eu-west-1:123456789012:loadbalancer/net/shop/0123456789abcdef"] |
| + } |
| + expect_failures = [var.ingress_target_group_arns] |
| +} |
| + |
| +run "a_repeated_arn_is_refused" { |
| + command = plan |
| + variables { |
| + ingress_target_group_arns = [ |
| + "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/shop-80/0123456789abcdef", |
| + "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/shop-80/0123456789abcdef", |
| + ] |
| + } |
| + expect_failures = [var.ingress_target_group_arns] |
| +} |
diff --git a/infra/modules/region-host/variables.tf b/infra/modules/region-host/variables.tf| index ad57700c7..fd0bff04e 100644 |
| --- a/infra/modules/region-host/variables.tf |
| +++ b/infra/modules/region-host/variables.tf |
| @@ -539,3 +539,35 @@ variable "managed_fleet_user_path" { |
| error_message = "managed_fleet_user_path overlaps store_user_path. Neither may contain the other, so object storage users and fleet users never share a grant." |
| } |
| } |
| + |
| +variable "ingress_target_group_arns" { |
| + description = <<-EOT |
| + Fixed mode only: the target groups this box is registered in, when an |
| + operator's tofu attaches it to a network load balancer |
| + (aws_lb_target_group_attachment). Set, the host role gains |
| + elasticloadbalancing:DescribeTargetHealth and |
| + DescribeTargetGroupAttributes, the reads the W6 ingress drain makes when |
| + the box is stopped on purpose (fixed-edge ingress drain spec §4.1, §8), |
| + and output ingress_target_groups gives the value for the box's |
| + CARLOS_INGRESS_TARGET_GROUPS. |
| + |
| + The host.env key is a separate step, on purpose. Apply the target group |
| + attributes and this role first, check them with |
| + `aws elbv2 describe-target-group-attributes`, and only then add the key. |
| + Until the key is there the drain is not_armed; with the key and the |
| + wrong attributes the drain reports unverified, never withdrawn. |
| + |
| + Empty (the default): no load balancer read and no value, which is every |
| + fixed box not behind an NLB. Group mode refuses it: group members leave |
| + the load balancer through the region HA drain. |
| + EOT |
| + type = list(string) |
| + default = [] |
| + nullable = false |
| + validation { |
| + condition = alltrue([for arn in var.ingress_target_group_arns : |
| + can(regex("^arn:aws(-[a-z]+)*:elasticloadbalancing:[a-z0-9-]+:[0-9]{12}:targetgroup/[A-Za-z0-9-]{1,32}/[0-9a-f]{16}$", arn)) |
| + ]) && length(distinct(var.ingress_target_group_arns)) == length(var.ingress_target_group_arns) |
| + error_message = "ingress_target_group_arns must be distinct Elastic Load Balancing target group ARNs (arn:aws:elasticloadbalancing:<region>:<account>:targetgroup/<name>/<id>)." |
| + } |
| +} |
diff --git a/internal/hostenv/group.go b/internal/hostenv/group.go| index b596c8d45..bcec60eb7 100644 |
| --- a/internal/hostenv/group.go |
| +++ b/internal/hostenv/group.go |
| @@ -135,6 +135,9 @@ func AssembleGroup(shared, regional map[string]string, c GroupConfig, queue stri |
| if err := validateDemoAPIOrigins(env); err != nil { |
| return nil, err |
| } |
| + if err := validateIngressTargetGroups(env, c.Account, c.AWSRegion); err != nil { |
| + return nil, err |
| + } |
| if _, err := Render(env); err != nil { |
| return nil, err |
| } |
diff --git a/internal/hostenv/hostenv.go b/internal/hostenv/hostenv.go| index 3f9a121cb..7eba8d39b 100644 |
| --- a/internal/hostenv/hostenv.go |
| +++ b/internal/hostenv/hostenv.go |
| @@ -21,6 +21,7 @@ import ( |
| "fmt" |
| "github.com/carlosframework/platform/internal/controlname" |
| "github.com/carlosframework/platform/internal/demo" |
| + "github.com/carlosframework/platform/internal/ingresshealth" |
| "regexp" |
| "slices" |
| "sort" |
| @@ -93,6 +94,7 @@ var Optional = []string{ |
| "CARLOS_HIBERNATE_PREFIX", |
| "CARLOS_HOST_CLAIM_RETIRED_LOCATIONS", |
| "CARLOS_HOST_METRICS", |
| + "CARLOS_INGRESS_TARGET_GROUPS", |
| "CARLOS_LAYOUT_DEFAULT", |
| "CARLOS_MOVES_PHASE1", |
| "CARLOS_MOVE_READY_DELTA_BYTES", |
| @@ -229,6 +231,9 @@ func Assemble(shared, regional map[string]string) (map[string]string, error) { |
| if err := validateDemoAPIOrigins(env); err != nil { |
| return nil, err |
| } |
| + if err := validateIngressTargetGroups(env, "", ""); err != nil { |
| + return nil, err |
| + } |
| return env, nil |
| } |
| @@ -243,6 +248,39 @@ func validateDemoAPIOrigins(env map[string]string) error { |
| return nil |
| } |
| +// ingressTargetGroups is the W6 ingress drain's list of the target groups |
| +// a fixed box is attached to (fixed-edge ingress drain spec §4.1). |
| +const ingressTargetGroups = "CARLOS_INGRESS_TARGET_GROUPS" |
| + |
| +// validateIngressTargetGroups refuses a CARLOS_INGRESS_TARGET_GROUPS the |
| +// drain could not read, with the drain's own parser, so a bad value fails |
| +// here at write or seed rather than as not_armed at a box's next stop. An |
| +// empty or absent value is no drain and passes. account and region are |
| +// the box's when the caller knows them (a group box's config); when it |
| +// does not (Assemble and Seed run without the box's identity), the first |
| +// entry's account and region stand in, so the check is the value's shape |
| +// and that every entry names the same account and region. The drain |
| +// checks them against the box itself when it is asked to run. The error |
| +// is ErrValue and names the variable only, like every refusal here. |
| +func validateIngressTargetGroups(env map[string]string, account, region string) error { |
| + value := strings.TrimSpace(env[ingressTargetGroups]) |
| + if value == "" { |
| + return nil |
| + } |
| + if account == "" || region == "" { |
| + first, _, _ := strings.Cut(value, ",") |
| + parts := strings.Split(strings.TrimSpace(first), ":") |
| + if len(parts) != 6 { |
| + return fmt.Errorf("%w: %s", ErrValue, ingressTargetGroups) |
| + } |
| + region, account = parts[3], parts[4] |
| + } |
| + if _, err := ingresshealth.ParseTargetGroups(value, account, region); err != nil { |
| + return fmt.Errorf("%w: %s", ErrValue, ingressTargetGroups) |
| + } |
| + return nil |
| +} |
| + |
| // Missing lists the Required variables env has no value for, sorted. |
| func Missing(env map[string]string) []string { |
| var out []string |
| @@ -422,6 +460,9 @@ func Seed(ctx context.Context, p Params, deployment, region string, env map[stri |
| if _, err := Render(env); err != nil { |
| return Seeded{}, err |
| } |
| + if err := validateIngressTargetGroups(env, "", ""); err != nil { |
| + return Seeded{}, err |
| + } |
| shared, regional := Split(env) |
| delete(regional, BoxReference) |
| existing, err := p.ByPath(ctx, sharedPrefix) |
diff --git a/internal/hostenv/hostenv_test.go b/internal/hostenv/hostenv_test.go| index e5a23bdad..46a4079cf 100644 |
| --- a/internal/hostenv/hostenv_test.go |
| +++ b/internal/hostenv/hostenv_test.go |
| @@ -523,3 +523,109 @@ func TestDemoAPIOriginPair(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// The W6 ingress drain's target groups (fixed-edge ingress drain spec |
| +// §4.1): a fixed box attached to an NLB names them in |
| +// CARLOS_INGRESS_TARGET_GROUPS. Seed and Fetch carry the value through; |
| +// a value that is not a list of target group ARNs is refused by write and |
| +// by seed alike, never carried to a box. |
| +const ( |
| + ingressTG80 = "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/shop-80/0123456789abcdef" |
| + ingressTG443 = "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/shop-443/fedcba9876543210" |
| +) |
| + |
| +func TestIngressTargetGroupsHostEnv(t *testing.T) { |
| + const key = "CARLOS_INGRESS_TARGET_GROUPS" |
| + if !Allowed(key) { |
| + t.Fatalf("%s is not in the host.env allowlist", key) |
| + } |
| + for _, value := range []string{"", ingressTG80, ingressTG80 + "," + ingressTG443} { |
| + env := today() |
| + if value != "" { |
| + env[key] = value |
| + } |
| + p := &fakeParams{vals: map[string]string{}} |
| + if _, err := Seed(t.Context(), p, "shop", "ie", env); err != nil { |
| + t.Fatalf("seed %q: %v", value, err) |
| + } |
| + if value != "" && p.vals["/carlos/shop/ie/"+key] != value { |
| + t.Fatalf("seed %q put it %v", value, p.vals) |
| + } |
| + body, err := Fetch(t.Context(), p, "shop", "ie") |
| + if err != nil { |
| + t.Fatalf("fetch %q: %v", value, err) |
| + } |
| + got, err := Parse(body) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if got[key] != value { |
| + t.Errorf("round trip of %q gave %q", value, got[key]) |
| + } |
| + } |
| +} |
| + |
| +func TestIngressTargetGroupsRefusedWhenNotTargetGroupARNs(t *testing.T) { |
| + const key = "CARLOS_INGRESS_TARGET_GROUPS" |
| + otherAccount := strings.Replace(ingressTG443, "123456789012", "210987654321", 1) |
| + otherRegion := strings.Replace(ingressTG443, "eu-west-1", "eu-west-2", 1) |
| + for name, value := range map[string]string{ |
| + "notAnARN": "shop-80", |
| + "notELB": "arn:aws:sns:eu-west-1:123456789012:carlos-alerts", |
| + "loadBalancer": "arn:aws:elasticloadbalancing:eu-west-1:123456789012:loadbalancer/net/shop/0123456789abcdef", |
| + "emptyEntry": ingressTG80 + ",", |
| + "repeated": ingressTG80 + "," + ingressTG80, |
| + "twoAccounts": ingressTG80 + "," + otherAccount, |
| + "twoRegions": ingressTG80 + "," + otherRegion, |
| + "onlyASeparator": ",", |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + env := today() |
| + env[key] = value |
| + p := &fakeParams{vals: map[string]string{}} |
| + if _, err := Seed(t.Context(), p, "shop", "ie", env); !errors.Is(err, ErrValue) { |
| + t.Fatalf("seed: %v, want ErrValue", err) |
| + } |
| + if len(p.sets) != 0 { |
| + t.Fatalf("seed wrote %v before refusing", p.sets) |
| + } |
| + shared, regional := Split(env) |
| + if got, err := Assemble(shared, regional); !errors.Is(err, ErrValue) || got != nil { |
| + t.Fatalf("assemble: %v, want ErrValue", err) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// A group box's host.env knows its account and AWS region (GroupConfig), |
| +// so the names are checked against them as the drain would. Group members |
| +// never arm the ingress drain (spec §4.1), but a value seeded from a fixed |
| +// box's file must still be one the drain could read. |
| +func TestIngressTargetGroupsInAGroupBoxHostEnv(t *testing.T) { |
| + const key = "CARLOS_INGRESS_TARGET_GROUPS" |
| + for name, c := range map[string]struct { |
| + value string |
| + valid bool |
| + }{ |
| + "absent": {"", true}, |
| + "own": {ingressTG80 + "," + ingressTG443, true}, |
| + "otherAccount": {strings.Replace(ingressTG80, "123456789012", "210987654321", 1), false}, |
| + "otherRegion": {strings.Replace(ingressTG80, "eu-west-1", "eu-west-2", 1), false}, |
| + "notTargetGrps": {"shop-80", false}, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + gc, env := groupEnvFixture() |
| + if c.value != "" { |
| + env[key] = c.value |
| + } |
| + got, err := AssembleGroup(nil, env, gc, "queue") |
| + if c.valid { |
| + if err != nil || got[key] != c.value { |
| + t.Fatalf("valid %q: %v", c.value, err) |
| + } |
| + } else if !errors.Is(err, ErrValue) || got != nil { |
| + t.Fatalf("invalid %q: %v, want ErrValue", c.value, err) |
| + } |
| + }) |
| + } |
| +} |