| index c6d59abaf..09027c9ef 100644 |
| --- a/docs/superpowers/specs/2026-11-12-edge-proxy-protocol-design.md |
| +++ b/docs/superpowers/specs/2026-11-12-edge-proxy-protocol-design.md |
| @@ -1,6 +1,6 @@ |
| # PROXY protocol v2 on the region HA load balancers |
| |
| -Design, 2026-11-12, **rev 1** (draft for Astra round 1). Agent manager 8a3d302 asked for it on 2026-11-12 with Paul's approval: client IP preservation off and PROXY protocol v2 on, on both target groups of each region NLB, before G6 opens the NLBs to customers. Design only; the build is a separate GO. Review budget: at most three Astra rounds (AGENTS.md stopping rules). |
| +Design, 2026-11-12, **rev 2** (Astra round 1 applied, §12). Agent manager 8a3d302 asked for it on 2026-11-12 with Paul's approval: client IP preservation off and PROXY protocol v2 on, on both target groups of each region NLB, before G6 opens the NLBs to customers. Design only; the build is a separate GO. Review budget: at most three Astra rounds (AGENTS.md stopping rules); round 1 found one P1 (fault family F1, §12), so round 2 follows. |
| |
| Line citations are to `origin/main` at ae1b8cca. AWS facts quote "Edit target group attributes for your Network Load Balancer" (docs.aws.amazon.com/elasticloadbalancing/latest/network/edit-target-group-attributes.html, read 2026-11-12) unless another page is named. |
| |
| @@ -24,18 +24,20 @@ Today on both NLBs (read 2026-11-12, `~/.cache/ppv2/aws/`): |
| |
| ## 2. What changes, in one paragraph |
| |
| -Each target group stops preserving the client address and sends a PROXY protocol v2 header instead. The target then sees every NLB connection coming from one of the NLB's own private addresses, on a source port the NLB chose, so two client connections can never share a 4-tuple at the target. The edge learns the client address from the header. It reads a header only on connections whose socket peer is one of its region NLB's private addresses, listed in a new host-env key; it rewrites the connection's remote address to the header's source before net/http sees it, so every reader of `r.RemoteAddr` downstream (rate limits, protection, geo, records, analytics, `X-Forwarded-For`) sees the client exactly as today, with no change of its own. A header from any other peer is consumed and ignored. The cutover flips one attribute on one target group per step, in an order where every target handles every new connection correctly at every moment, and each step reverses on its own. |
| +Each target group stops preserving the client address and sends a PROXY protocol v2 header instead. The target then sees every NLB connection coming from one of the NLB's own private addresses, on a source port the NLB chose, so two client connections can never share a 4-tuple at the target. The edge learns the client address from the header. It reads a header as identity only on connections whose socket peer is one of its region NLB's private addresses, listed in a new host-env key; it rewrites the connection's remote address to the header's source before net/http sees it, so every reader of `r.RemoteAddr` downstream (rate limits, protection, geo, records, analytics, `X-Forwarded-For`) sees the client exactly as today. A header from any other peer is consumed and ignored. A trusted connection without a client address may only answer the health check. The cutover flips one attribute on one target group per step, through states where every target handles every new connection correctly, each step proven converged on every NLB node before the next, and the one unsafe state (preservation off with PROXY protocol off) is refused by the step tooling and detected by a forgery probe if it happens anyway. |
| |
| -## 3. Guarantee |
| +## 3. Guarantee and invariant |
| + |
| +**The trust invariant.** A node address grants trust to the first header on a connection only because the NLB writes that header. That holds only while the target group has PROXY protocol on. With preservation off and PROXY protocol off (S3, §6.2) the node address still arrives as the peer, the NLB writes nothing, and a client's own header would be read as the NLB's: any client could choose its address, and that address reaches limits and the signed `X-Forwarded-For` (`homemap.go:1684-1718`, `forwardproof.go:44-51`). So **S3 is a security state, not a degraded one**: the cutover never enters it (§6.2), the step tooling refuses any change that could (§6.3), and the forgery probe (§6.4) detects it within seconds if anything else causes it, with an incident procedure (§6.5). |
| |
| **Fixed:** with preservation off, two client connections through the NLB never share a 4-tuple at a target: the NLB allocates the source address and port toward each target, so the documented collision cannot happen. Hairpin connections through the NLB work too (AWS: "you can also prevent this type of connection error by disabling client IP preservation"), which removes the A21 limit (region HA spec 2026-10-26 §12.4). |
| |
| -**Kept:** every request that reaches an edge through the NLB carries the same client address in `r.RemoteAddr` as it does today, for every TG state the cutover passes through (§6.2). Requests that do not come through the NLB (fixed boxes' own EIPs, cross-region backhaul to a member's public address, hop-signed forwards between boxes, the door, hairpin, tunnels) are unchanged byte for byte, except that the edge on an armed box reads their first bytes before net/http does (§4.3). |
| +**Kept:** every request that reaches an edge through the NLB carries the same client address in `r.RemoteAddr` as it does today, in every state the cutover passes through (§6.2). Requests that do not come through the NLB (fixed boxes' own EIPs, cross-region backhaul to a member's public address, hop-signed forwards between boxes, the door, hairpin, tunnels) are unchanged, except that on an armed box the edge reads their first bytes before net/http does (§4.3). |
| |
| -**Zero downtime is the target and is measured** (§10), as in W6. AWS states that preservation changes "take effect only for new TCP connections"; it states nothing for the PROXY protocol attribute, so the design does not depend on it (§6.2 covers both old and new behaviour for every connection), and the dev witness records it. |
| +**Zero downtime is the target and is measured** (§10), as in W6. AWS states that preservation changes "take effect only for new TCP connections" and states nothing for PROXY protocol. The design assumes both apply only to new connections, because a header can only be the first bytes of a connection; the dev witness tests the assumption with connections held open across every step (§10 row 3) before the flagship relies on it. |
| |
| **Not covered:** |
| -- A target running an edge older than the release that carries this (call it vN) while its TG has PROXY protocol on. It answers the header with 400 or a TLS error, fails its health checks and leaves the target group. That is one lost target, not an outage, unless every target is old, when the NLB fails open to all of them. Refused by procedure, not code: §7.3. |
| +- A target running an edge older than the release that carries this (vN) while its TG has PROXY protocol on. It answers the header with 400 or a TLS error, fails its health checks and leaves the target group: one lost target, not an outage, unless every target is old, when the NLB fails open to all of them. Refused by procedure: §7.3. |
| - Real client addresses inside the NLB's own TCP layer (NLB logs, flow logs): unchanged, off. |
| |
| ## 4. Edge parsing |
| @@ -54,48 +56,51 @@ The wrapper goes outermost, after `ConfigureGroupHairpin` (`edge.go:5303-5310`), |
| |
| A connection is **trusted** when its socket peer address (the kernel's, before any rewrite) is in the list. Nothing else makes it trusted: not the destination, not a header, not the VPC CIDR. The VPC CIDR is not enough because target security groups admit 443 from the VPC (`infra/modules/region-host/group.tf:150-156`) and tenant code runs on boxes in that VPC; a tenant could otherwise send a header from a box's private address and choose its client address, which reaches other tenants' apps as `X-Forwarded-For`. VPC source/destination checks stop a box sending from an NLB node's address. |
| |
| -Where the addresses come from: tofu reads the NLB's network interfaces (`description = "ELB net/<name>/<id>"`) and outputs their private addresses (§8.1). They are fixed for the NLB's life and change only if a zone is added to the NLB; §4.5's private-source counter catches a list that is out of date. |
| +Trust is also conditional on the NLB writing the first header (§3): the list says who may speak, the target group attribute says that it does. The edge cannot check the second half, so §6 owns it. |
| + |
| +Where the addresses come from: tofu reads the NLB's network interfaces and outputs their private addresses (§8.1). They are fixed for the NLB's life and change only if a zone is added to the NLB; §4.5's `ignored_private` counter catches a list that is out of date. |
| |
| ### 4.3 Reading the header |
| |
| -The wrapper's `Accept` returns at once; nothing is read on the accept goroutine. Reading happens on the connection's own goroutine, at the first of `Read` or `RemoteAddr`. net/http calls `RemoteAddr` first thing in `conn.serve` (`$GOROOT/src/net/http/server.go:1908`, Go 1.26.10), before any deadline or read of its own, and `tls.Conn.RemoteAddr` delegates to the wrapped connection (`crypto/tls/conn.go:136`), so the address is settled before net/http or TLS read a byte. `ConnContext` hooks (hairpin, `connDrain.serve` at `internal/edge/hadrainconns.go:152-165`) and `ConnState` hooks run on the accept goroutine and must not call `RemoteAddr`; a test pins that the accept loop never blocks on a silent trusted connection. |
| +The wrapper's `Accept` returns at once; nothing is read on the accept goroutine. Each wrapped connection owns a parse state: pending, then exactly one of `proxied` (header source), `no-client` (LOCAL, UNSPEC or no header on a trusted connection), `plain` (untrusted), or failed (closed). The state resolves once, on the connection's own goroutine, at the first of `Read` or `RemoteAddr`. |
| + |
| +net/http calls `RemoteAddr` first thing in `conn.serve` (`$GOROOT/src/net/http/server.go:1908`, Go 1.26.10), before any deadline or read of its own, and `tls.Conn.RemoteAddr` delegates to the wrapped connection (`crypto/tls/conn.go:136`). So the state is resolved before net/http or TLS read a byte, and before any handler runs. `ConnContext` runs earlier, on the accept goroutine (`server.go:3474-3476`): it stores a pointer to the connection's parse state in the context and reads nothing. Handlers read the resolved state through that pointer; a handler that finds it pending is a bug, and a test pins that it cannot happen through `Serve` and `ServeTLS` (HTTP/1.1 and HTTP/2). `ConnState` hooks and the existing `ConnContext` hooks (hairpin, `connDrain.serve` at `internal/edge/hadrainconns.go:152-165`) must not call `RemoteAddr`; a test pins that a silent trusted connection does not delay the accept of the next one. |
| |
| Untrusted connection: `RemoteAddr` answers the socket peer at once. On the first `Read`, the wrapper reads up to 12 bytes, stopping at the first byte that differs from the PPv2 signature (`\r\n\r\n\0\r\nQUIT\n`). HTTP and TLS both differ at byte 0, so this is one read. A mismatch replays the bytes read to the caller. A full signature means a header: §4.4. |
| |
| Trusted connection: the first `RemoteAddr` or `Read` reads the same way, under a read deadline of 10 s from accept (the listeners' `ReadHeaderTimeout`, `internal/edge/ingressdrain.go:92-93`), then clears that deadline. The design does not depend on whether the NLB sends the header at connect or with the client's first bytes ("prepends a proxy protocol header to the TCP data"). A timeout closes the connection. |
| |
| +`Close` never waits for the parse: it closes the socket without taking the parse lock, which fails a pending read at once. So `Server.Shutdown` (which closes `StateNew` connections older than 5 s, `server.go:3227-3230`) and W6's `CloseIngress` (`ingressdrain.go:121`) end a connection still waiting for its header as they end one waiting for request headers today. Tests cover a silent and a partial header through both. |
| + |
| v1 (text) headers are never parsed: the NLB sends only v2. A client that sends `PROXY TCP4 ...` gets today's 400. |
| |
| ### 4.4 What each header does |
| |
| -| Peer | First bytes | Result | |
| +| Peer | First bytes | Parse state and result | |
| |---|---|---| |
| -| trusted | PPv2, command PROXY, family INET or INET6, a usable source (below) | remote address = header source address and port | |
| -| trusted | PPv2, command LOCAL, or PROXY with family UNSPEC | remote address = socket peer, request marked **client unknown** (§4.6). AWS health checks carry this: "the client connection information is not sent" | |
| -| trusted | no PPv2 signature | remote address = socket peer, **client unknown**. Health checks before the flip, and §6.2's states S0 and S1 never produce this for data; it exists for a rollback done out of order | |
| +| trusted | PPv2, command PROXY, family INET or INET6, a usable source (below) | `proxied`: remote address = header source address and port | |
| +| trusted | PPv2, command LOCAL, or PROXY with family UNSPEC | `no-client`: remote address = socket peer; health check only (§4.6). AWS health checks carry this: "the client connection information is not sent" | |
| +| trusted | no PPv2 signature | `no-client`, as above. Before step A (§6.2) this is every health check; after it, only S3 produces it | |
| | trusted | PPv2, malformed or over the limits | connection closed | |
| -| untrusted | PPv2, well formed | header consumed and ignored; remote address = socket peer. Needed in S1 (§6.2), where the peer is the client itself | |
| +| untrusted | PPv2, well formed | `plain`: header consumed and ignored; remote address = socket peer. Needed in S1 (§6.2), where the peer is the client itself | |
| | untrusted | PPv2, malformed | connection closed | |
| -| untrusted | anything else | unchanged | |
| +| untrusted | anything else | `plain`, unchanged | |
| |
| -Limits and checks: version nibble 2; command LOCAL or PROXY; family and protocol INET/STREAM, INET6/STREAM or UNSPEC; the declared length at least the address block (12 bytes for INET, 36 for INET6) and at most 512 bytes in total, TLVs included. TLVs are skipped unread (AWS sends `PP2_TYPE_AWS` 0xEA only for PrivateLink). A usable source is a unicast address that is not loopback, unspecified, link-local or multicast; anything else closes the connection (a real NLB cannot send it). Exactly one header is consumed. AWS: the NLB "does not discard or overwrite ... any incoming proxy protocol headers sent by the client", so a client's own header after the NLB's reaches net/http as payload and fails as today. |
| +Limits and checks: version nibble 2; command LOCAL or PROXY; family and protocol INET/STREAM, INET6/STREAM or UNSPEC; the declared length at least the address block (12 bytes for INET, 36 for INET6) and at most 512 bytes in total, TLVs included. TLVs are skipped unread (AWS sends `PP2_TYPE_AWS` 0xEA only for PrivateLink). A usable source is a unicast address that is not loopback, unspecified, link-local or multicast; anything else closes the connection (a real NLB cannot send it). Exactly one header is consumed. AWS: the NLB "does not discard or overwrite ... any incoming proxy protocol headers sent by the client", so in S1 and S2 a client's own header after the NLB's reaches net/http as payload and fails as today. The forgery probe (§6.4) relies on this. |
| |
| The header's destination address and port are not used. |
| |
| ### 4.5 Counters |
| |
| -Per listener, since start: `proxied` (trusted, PROXY), `local` (trusted, LOCAL or UNSPEC), `absent` (trusted, no header), `ignored` (untrusted header consumed), `ignored_private` (untrusted header from a private address: almost certainly an NLB node missing from the key), `malformed`, `timeout`. One journal line at start (`edge: proxy protocol: armed on :80 and :443, trusting 172.31.29.110,172.31.11.189`), and one throttled line every 5 minutes when a counter moved, naming them. `ignored_private` above zero also logs once at warning level with the peer address. The cutover reads these lines over SSM (§6.3); no new metric or alarm in the edge. |
| +Per listener and, for trusted connections, per node address, since start: `proxied`, `no_client`, `no_client_refused` (§4.6), `ignored` (untrusted header consumed), `ignored_private` (untrusted header from a private address: almost certainly an NLB node missing from the key), `malformed`, `timeout`. One journal line at start (`edge: proxy protocol: armed on :80 and :443, trusting 172.31.29.110,172.31.11.189`), and one throttled line every minute while armed, naming every counter. `no_client_refused` and `ignored_private` above zero also log once a minute at warning level with the peer address. The cutover reads these lines over SSM (§6.4); no new metric or alarm in the edge. |
| + |
| +### 4.6 A trusted connection without a client address answers the health check only |
| |
| -### 4.6 Client unknown |
| +A `no-client` connection has an NLB node as its socket peer and no client to key limits on, and outside the health check it exists only in S3 (§4.4). So a request on it is served only when it is a `GET` or `HEAD` of `/.carlos/lb-health` on `:80`, the path `lbHealthAnswer` already answers first (`internal/edge/edge.go:3795-3799`, `internal/edge/lbhealth.go:34-48`). Every other request on such a connection, on either listener, is answered `503` with `Connection: close`, before any routing, rate limit, analytics or forwarding, and counted `no_client_refused`. The check is one wrapper around each public server's handler that reads the parse state (§4.3). |
| |
| -A trusted connection without a client address (LOCAL, UNSPEC, no header) has the NLB node as its socket peer. Keying limits on it would put every client behind that node in one bucket. So `clientIP(r)` (`internal/edge/analytics.go:100-115`) answers "" for such a request, from a context mark set by the wrapper's `ConnContext`, and each reader already handles "no address": |
| -- `ratelimit.ClientKey("")` is not ok, so the rate limit (`internal/edge/ratelimit.go:133-136`), the probe-burst cooling and note (`internal/edge/guard.go:161`, `:316`) and backhaul fair share (`edge.go:4561`) skip, as they do for a request with no parseable address; |
| -- geo answers off (`internal/edge/geoipheaders.go:41`, `net.ParseIP("")` is nil); |
| -- request records and analytics carry no client (`reqrecords.go:46`, `analytics.go:154`); |
| -- the gate's attempt limiter (`internal/gate/gate.go:260-268`) keys on `r.RemoteAddr`, the NLB node: stricter, never looser, and this case is transitional only. |
| -`r.RemoteAddr` itself stays the NLB node's address, so `X-Forwarded-For` to the app says what the edge saw. |
| +So no request ever reaches a client-address reader without a client address: `clientIP(r)` (`internal/edge/analytics.go:100-115`) always answers a real client, nothing is forwarded with a node address in `X-Forwarded-For`, and analytics never sees an empty address (`internal/analytics/meter.go:68`, `:191` would otherwise hash it into one shared visitor). |
| |
| -This fails open on per-client limits only for connections from the NLB's own addresses, which no client can originate. |
| +In S3, ordinary clients (no header of their own) are refused loudly by this rule. A client that sends its own header is still believed; only §6 prevents that. |
| |
| ## 5. Client IP everywhere it is used |
| |
| @@ -108,8 +113,8 @@ Every reader derives the client from `r.RemoteAddr`, directly or through `client |
| | Backhaul fair share | `edge.go:4554-4561` | client | |
| | Geo headers | `geoipheaders.go:41` | client; door, hop and tunnel branches unchanged | |
| | Request records | `reqrecords.go:46` | client | |
| -| Analytics visitor hash | `analytics.go:100-115`, `:154` | client | |
| -| `X-Forwarded-For` and its proof to apps and holders | `homemap.go:1684-1718` (`SetXForwarded` reads `r.RemoteAddr`), `forwardproof.go:44` | client, signed as today | |
| +| Analytics visitor hash and pace | `analytics.go:100-115`, `:154`; `internal/analytics/meter.go:191` | client | |
| +| `X-Forwarded-For` and its proof to apps and holders | `homemap.go:1684-1718` (`SetXForwarded` reads `r.RemoteAddr`), `forwardproof.go:44-51` | client, signed as today | |
| | Gate attempt limiter | `gate.go:260-268` | client | |
| | Journal lines naming a peer | `burst_notice.go:603`, `hadrain.go:282`, net/http's TLS handshake error (`server.go:1953`) | client | |
| | Asset catch record | `assetcatch.go:676` | client | |
| @@ -117,74 +122,110 @@ Every reader derives the client from `r.RemoteAddr`, directly or through `client |
| | Door, devbox front, tunnel hub log | `door.go:126`, `devboxfront.go:44`, `tunnel.go:329` | not on a wrapped path, or not via the NLB | |
| | Console readers (`authcli.go:574`, `cliplace.go`, `demo_api_origin.go:38`, `stepup.go:499`) | console on annie | annie is not an NLB target; they read what the local edge forwards | |
| |
| -Hop-verified requests between boxes keep taking the client from the signed `X-Forwarded-For` (`analytics.go:101-108`); the sending edge now fills it from the header-derived address. |
| +Hop-verified requests between boxes keep taking the client from the signed `X-Forwarded-For` (`analytics.go:101-108`); the sending edge now fills it from the header-derived address, and never from a `no-client` connection (§4.6). |
| |
| ## 6. Cutover |
| |
| ### 6.1 Prerequisites, per NLB |
| |
| -1. vN (the release carrying §4 and §8.1) runs on **every** target in both TGs: fixed boxes rolled, members replaced through the group roll onto a launch template naming vN. |
| -2. `CARLOS_INGRESS_PROXY_SOURCES` equals the module output (§8.1) and is in effect on every target: in the region's host-env parameters before the member roll (members write host.env only at launch, `hostenv.go:144-150`), and in each fixed box's host.env on its card, as W6 delivered `CARLOS_INGRESS_TARGET_GROUPS`. Each target's journal shows the §4.5 armed line with exactly the module's addresses. |
| -3. The group module is at vN, whose precondition accepts the new states (§8.2). A group plan with the old module would refuse once the TGs change. |
| +In this order: |
| +1. **vN on every target in both TGs.** Fixed boxes rolled. Members replaced through the group roll onto a launch template naming vN. |
| +2. **The bootstrap object at vN or later.** Members run the separately seeded bootstrap binary (`group-init.sh.tftpl:200-215`) for `host-env write` (`:332`), and it refuses a key outside its compiled allowlist (`internal/hostenv/group.go:112`). So the region's `boot/bootstrap-object` and `boot/bootstrap-sha256` name a vN build, checked from SSM and the object's hash, **before** the key is published. |
| +3. **The key.** `CARLOS_INGRESS_PROXY_SOURCES` equal to the module output (§8.1): in the region's host-env parameters, then a group roll so every member is launched with it (members write host.env only at launch, `hostenv.go:144-150`), and in each fixed box's host.env with a drained restart, as W6 delivered `CARLOS_INGRESS_TARGET_GROUPS`. Each target's journal shows the §4.5 armed line with exactly the module's addresses. |
| +4. **The group module at vN**, whose precondition accepts the new states (§8.2). A group plan with the old module would refuse once the TGs change. |
| + |
| +Arming changes nothing on the wire: until step A80 no connection carries a header, and trusted peers are only health checks, which §4.6 serves. |
| |
| -Arming changes nothing on the wire: until step 1 below, no connection carries a header and trusted peers are only health checks, which get client unknown on a path (`lbHealthAnswer`, `internal/edge/lbhealth.go:34-48`) that reads no client address. |
| +**Unwinding, in reverse:** both TGs back in S0 first; then the key removed from the parameters and fixed boxes; only then may the bootstrap object or any target go below vN. A bootstrap below vN with the key still published stops every new member at boot. |
| |
| ### 6.2 States and steps |
| |
| -Two attributes per TG, four combinations. The order below visits only S0, S1 and S2; S3 is refused. |
| +Two attributes per TG, four combinations. The steps visit only S0, S1 and S2. |
| |
| -| State | preserve | PPv2 | Socket peer at target | Header | Edge result | |
| +| State | preserve | PPv2 | Socket peer at target | First header | Edge result | |
| |---|---|---|---|---|---| |
| -| S0 today | on | off | client | none | untrusted, unchanged | |
| +| S0 today | on | off | client | none, or the client's | untrusted; a client header ignored | |
| | S1 | on | on | client | NLB's, source = client | untrusted, header ignored, peer = client: same address | |
| | S2 target | off | on | NLB node | NLB's | trusted, header source = client | |
| -| S3 refused | off | off | NLB node | none | trusted, client unknown: limits off for everyone | |
| +| S3 **forbidden** | off | off | NLB node | none, or the client's | trusted: a client header is believed (§3) | |
| |
| -In S1 the edge must ignore, not trust, the header, because the peer is the client and is not in the list; §4.4 does that. Within one attribute change, nodes and connections can be on either side for a while (new connections only, per AWS for preservation; assumed for PPv2). Each step changes one attribute on one TG, so every new connection is in one of two adjacent states, and every target handles both. |
| +In S1 the edge must ignore, not trust, the header, because the peer is the client and is not in the list; §4.4 does that. While one attribute propagates, nodes can disagree, so new connections are in one of the two states on either side of the step; every edge handles both of each allowed pair. The only pairs that touch S3 are S0 to S2 and S2 to S0 in one move, and §6.3 refuses both. |
| |
| -Steps, per NLB, each a targeted tofu apply of one resource attribute (0 add / 1 change / 0 destroy): |
| -1. **A80:** PPv2 on, `:80` TG. Watch 5 min. |
| -2. **A443:** PPv2 on, `:443` TG. Watch 10 min. Both TGs in S1. |
| -3. **B80:** preservation off, `:80` TG. Watch 5 min. |
| -4. **B443:** preservation off, `:443` TG. Watch 30 min. Both TGs in S2. |
| +Steps, per NLB, each a targeted tofu apply of one attribute on one TG (0 add / 1 change / 0 destroy): |
| +1. **A80:** PPv2 on, `:80` TG (S0 to S1). |
| +2. **A443:** PPv2 on, `:443` TG. |
| +3. **B80:** preservation off, `:80` TG (S1 to S2). |
| +4. **B443:** preservation off, `:443` TG. |
| |
| -`:80` goes first in each pair because it carries only ACME challenges, redirects and health checks. |
| +`:80` goes first in each pair because it carries only ACME challenges, redirects and health checks. Each step starts only when the previous step's convergence evidence (§6.4) is in. |
| |
| -**Watches after each step** (abort rule: any one fails twice in a row, roll back that step): |
| -- every target healthy in both TGs (`describe-target-health` every 10 s); |
| -- the probe (§10) shows no error and no connect over 900 ms that its reference checks do not explain; |
| -- §4.5 counters on every target: `malformed`, `timeout`, `ignored_private` stay 0; after A80/A443, `ignored` rises (S1); after B80/B443, `proxied` rises and `absent` stays 0 on data traffic (health checks show as `local`); |
| -- the echo check (§10) shows the probe's public address, never 172.31.x. |
| +### 6.3 The step tool refuses any move that could pass through S3 |
| |
| -### 6.3 Rollback |
| +A card script in each root (`scripts/ppv2-step.sh <tg-port> <attribute> <value>`) is the only way the attributes change: |
| +- it reads the TG's live attributes (`describe-target-group-attributes`) and refuses unless the move is one of S0 to S1, S1 to S2, S2 to S1, S1 to S0 for that TG; |
| +- it runs `tofu plan -target` and refuses unless the plan changes exactly that one attribute on that one TG; |
| +- it refuses unless the previous step's convergence record (§6.4) exists and passed; |
| +- then it applies, and starts the next convergence check. |
| |
| -Reverse order, one attribute per apply: B443, B80, A443, A80. Each reverse step lands in a state the previous paragraph already covers. Two reversals in one apply could pass through S3 for a while on some nodes; that is refused on the card, and §4.6 bounds it (limits off for those connections, nothing refused) if it happens anyway. |
| +The module also refuses S3 as a declared state (§8.1), so an ordinary plan cannot request it. Direct `aws elbv2 modify-target-group-attributes` is allowed in only one direction and only in an abort or incident: preservation **on** (S2 to S1, or S3 to S0), which can never create S3. Tofu is reconciled afterwards with a plan that shows no change. |
| + |
| +### 6.4 Convergence evidence and the forgery probe |
| + |
| +The probe runs in region (§10), with one process per NLB address and port, each binding source ports from its own range so the probe itself never sends one source port to both addresses. Each process sends, every 2 s, two requests on fresh connections: |
| +- **plain:** a normal request to the echo app, which returns the `X-Forwarded-For` it received; |
| +- **marker:** a PPv2 header claiming source 192.0.2.1 (TEST-NET-1), then the same request. |
| + |
| +What each state answers, per NLB address and port: |
| + |
| +| State on that node | plain | marker | |
| +|---|---|---| |
| +| S0 | 200, probe's address | 200, probe's address (client header ignored, §4.4) | |
| +| S1 | 200, probe's address | fails: the NLB's header is first, the marker is payload (400 or TLS error) | |
| +| S2 | 200, probe's address | fails, the same way | |
| +| S3 | `503`, refused (§4.6) | **200 with 192.0.2.1: forgery** | |
| |
| -In an abort, the operator may run the same single-attribute change with `aws elbv2 modify-target-group-attributes` (seconds) and reconcile tofu afterwards with a plan that shows no change. Open question 3. |
| +Convergence of a step, per TG port, needs all of: |
| +- after A: the marker fails on **both** NLB addresses for 30 consecutive attempts (60 s); plain stays 200 with the probe's address; |
| +- after B: the same, plus every target's counters show `proxied` rising from **both** node addresses and `no_client_refused` at 0; |
| +- after a reverse A (S1 to S0): the marker answers 200 with the probe's address on both addresses for 30 consecutive attempts; |
| +- all the time: every target healthy in both TGs (`describe-target-health` every 10 s), and `malformed`, `timeout`, `ignored_private` at 0. |
| + |
| +The script writes the record (times, counts, per-address results) that the next step requires. |
| + |
| +**Any marker answer echoing 192.0.2.1, or any `no_client_refused`, is S3 on some node: §6.5 at once.** The probe runs for the whole window, not only between steps. |
| + |
| +### 6.5 Rollback and the S3 incident |
| + |
| +Rollback is the steps in reverse, one per apply, each with its convergence evidence: B443, B80, A443, A80. Each lands in a state already covered. |
| + |
| +S3 incident (forgery seen, or `no_client_refused` rising): |
| +1. Preservation **on** for the affected TG with the direct command (§6.3). S3 becomes S0 and S2 becomes S1 for new connections; neither trusts a client header. |
| +2. Close every connection that could have been opened in S3, because preservation changes apply only to new connections: drained restarts of the fixed targets (W6) and a group roll of the members, one target at a time. |
| +3. Stop the window and report to 8a3d302 with the probe and counter records. |
| |
| Edge rollback: an edge older than vN may run on a target only while both its TGs have PPv2 off. The roll card's rollback section says so, and the group re-pin card checks the TG attributes before naming a launch template older than vN. |
| |
| -### 6.4 Order across NLBs |
| +### 6.6 Order across NLBs |
| |
| -Dev IE first, in a dev #421 window after vN is on dev. Flagship US after the dev witness passes every row, in a flagship window with the lock posted on #421 (region HA live window rules), before G6 opens the US NLB to customers. Not in the same window as a release roll or a group roll. |
| +Dev IE first, in a dev #421 window after vN is on dev. Flagship US after the dev witness passes every row, in a flagship window with the lock posted on #421 (region HA live window rules), before G6 opens the US NLB to customers. Not in the same window as a release roll or a group roll, except the group roll that §6.1 or §6.5 calls for. |
| |
| ## 7. Interactions |
| |
| ### 7.1 Fixed boxes in the target groups and W6 |
| |
| -leo, sophie and adi carry NLB traffic and their own EIP traffic on the same listeners. NLB connections come from the node addresses (trusted); EIP connections come from clients (untrusted, unchanged). W6's drain reads TG health and fails `/.carlos/lb-health` on purpose (`lbhealth.go:34-48`); health checks reach that handler with or without a LOCAL header, so the drain is unchanged. The W6 witness rows (drained restarts with 0 errors) rerun in S2 (§10). |
| +leo, sophie and adi carry NLB traffic and their own EIP traffic on the same listeners. NLB connections come from the node addresses (trusted); EIP connections come from clients (untrusted, unchanged). W6's drain reads TG health and fails `/.carlos/lb-health` on purpose (`lbhealth.go:34-48`); health checks reach that handler as `no-client` or, before step A, without a header, so the drain is unchanged. The W6 witness rows (drained restarts with 0 errors) rerun in S2 (§10). |
| |
| ### 7.2 Health checks |
| |
| -Both TGs check `:80` `/.carlos/lb-health`. After A80 or A443, that TG's checks carry a LOCAL header, from trusted peers. After A80 and before A443, `:80` sees both kinds at once, per connection. Admission gating (`lbhealth.go:38-46`) is unchanged. |
| +Both TGs check `:80` `/.carlos/lb-health`. After A80 or A443, that TG's checks carry a LOCAL header from trusted peers. Between A80 and A443, `:80` sees both kinds at once, per connection. Admission gating (`lbhealth.go:38-46`) is unchanged. |
| |
| ### 7.3 Mixed versions |
| |
| -Before A80, any mix of versions and keys is fine. From A80 on, every target in that TG must be armed. Members launched while PPv2 is on must come from a launch template naming vN or later with the key in the region's parameters; a member that cannot parse fails ELB health, so the group's ELB health check replaces it with another from the same template, a loop the HA capacity alarms already report. That is why §6.3 puts the check on the re-pin card rather than relying on the loop. |
| +Before A80, any mix of versions and keys is fine. From A80 on, every target in that TG must be armed. Members launched while PPv2 is on must come from a launch template naming vN or later, with a vN bootstrap object and the key in the region's parameters (§6.1). A member that cannot parse fails ELB health and the group replaces it from the same template, a loop the HA capacity alarms report; the re-pin card's check (§6.5) prevents it. |
| |
| ### 7.4 Hairpin (A21) |
| |
| -Members redirect their own traffic to the NLB addresses to the local edge (§12.4, `hairpin.go`). That stays: it is correct in every state, and it saves a round trip. With preservation off, hairpin through the NLB would work, so the redirect becomes optional; retiring it is a separate simplification, not part of this change. |
| +Members redirect their own traffic to the NLB addresses to the local edge (§12.4 of the HA spec, `hairpin.go`). That stays: it is correct in every state, and it saves a round trip. With preservation off, hairpin through the NLB would work, so the redirect becomes optional; retiring it is a separate simplification, not part of this change. |
| |
| ### 7.5 Capacity |
| |
| @@ -192,16 +233,15 @@ AWS: with preservation off, about 55,000 connections a minute to each target IP |
| |
| ### 7.6 Security groups |
| |
| -Unchanged. Targets already admit 80 and 443 from the NLB's security group (`group.tf:134-143`); the NLB's egress already covers the VPC CIDRs on both ports (`ingress/main.tf:51-62`). Health checks already come from the node addresses. |
| +Unchanged. Targets already admit 80 and 443 from the NLB's security group (`group.tf:134-143`); the NLB's egress already covers the VPC CIDRs on both ports (`ingress/main.tf:51-63`). Health checks already come from the node addresses. |
| |
| ## 8. Tofu |
| |
| ### 8.1 This repo, `infra/modules/region-host/ingress` (rides vN) |
| |
| -- `aws_lb_target_group.target` (`ingress/main.tf:97-126`): `preserve_client_ip = var.preserve_client_ip` (default `true`) and `proxy_protocol_v2 = var.proxy_protocol_v2` (default `false`), each a map by port (`{ "80" = ..., "443" = ... }`) so a root can flip one TG at a time. Defaults keep today's state, so re-pinning to vN plans 0/0/0. A validation refuses any port with preservation off and PPv2 off (S3). |
| -- Output `proxy_protocol_sources`: the node private addresses, sorted, from `data "aws_network_interfaces"` filtered on `description = "ELB ${aws_lb.nlb.arn_suffix}"`, with a precondition that the count equals the number of zones. |
| +- `aws_lb_target_group.target` (`ingress/main.tf:97-126`): `preserve_client_ip` and `proxy_protocol_v2` from two map variables keyed by port (`{ "80" = ..., "443" = ... }`), defaults `true` and `false`, so a root can flip one TG at a time and re-pinning to vN plans no TG change. A validation refuses any port with preservation off and PPv2 off (S3). |
| +- Output `proxy_protocol_sources`: the node private addresses, sorted. One singular `data "aws_network_interface"` per zone in `var.zones` (keys known at plan time), filtered on `description = "ELB ${aws_lb.nlb.arn_suffix}"` and that zone's `subnet-id`, each yielding exactly one interface and its `private_ip`. (The plural `aws_network_interfaces` returns only IDs.) |
| - Alarm on `PortAllocationErrorCount` Sum > 0 over 5 minutes, beside the module's existing alarms (`ingress/alarms.tf`), on the module's topic. |
| -- The dev root (pinfra `dev/region-ha-ingress.tf`) and flagship root (carloku-infrastructure `region-ha-us.tf`) use this module; both get the four step values as root locals. |
| |
| ### 8.2 This repo, `infra/modules/region-host/group.tf` (rides vN) |
| |
| @@ -209,40 +249,56 @@ The existing-target-group precondition (`group.tf:182-187`) requires `preserve_c |
| |
| ### 8.3 Roots |
| |
| -- **dev, platform-infrastructure:** re-pin `dev/region-ha-ingress.tf` and `dev/region-ha-group.tf` to vN (planned 0/0/0 apart from the new alarm, 1/0/0); write `CARLOS_INGRESS_PROXY_SOURCES` into `/carlos/dev/ie/` and into leo's and sophie's host.env on the roll card; then the four steps, one targeted apply each. |
| -- **flagship, carloku-infrastructure:** push vN to the GitHub mirror, bump `?ref=` on `region-ha-us.tf` and `region-ha-us-group.tf`, same sequence with adi. |
| +- **dev, platform-infrastructure:** re-pin `dev/region-ha-ingress.tf` and `dev/region-ha-group.tf` to vN (no TG change; one new alarm); bootstrap object to vN; `CARLOS_INGRESS_PROXY_SOURCES` into `/carlos/dev/ie/` and into leo's and sophie's host.env; group roll; then the four steps through `scripts/ppv2-step.sh`. |
| +- **flagship, carloku-infrastructure:** push vN to the GitHub mirror, bump `?ref=` on `region-ha-us.tf` and `region-ha-us-group.tf`, the same sequence with adi. |
| |
| -First-of-kind applies need 8a3d302's GO; later NLBs' identical steps fall under the standing approval for counted applies when the plan matches the card exactly. |
| +First-of-kind applies need 8a3d302's GO; later NLBs' identical steps fall under the standing approval for counted applies when the plan and the step script's record match the card exactly. |
| |
| ## 9. Tests |
| |
| -- `internal/proxyproto` (new, about 200 lines, no dependency): table tests for every row of §4.4 and every limit; byte-at-a-time and split reads; a 12-byte prefix that diverges at each position; timeout; exactly one header consumed; a v1 header passed through. |
| -- Listener: a real `http.Server` behind the wrapper, plain and TLS (HTTP/1.1 and HTTP/2), with a trusted and an untrusted dialler (loopback aliases or a test hook supplying the peer), asserting `r.RemoteAddr`; a silent trusted connection does not delay a second connection's accept; `ConnContext` and `ConnState` hooks never block. |
| -- Production wiring (house rule, production-wired witnesses): the listener stack built the way `Serve` builds it, `publicServers` plus `ConfigureGroupHairpin` plus the wrapper, with a header-carrying request reaching the rate limiter and `X-Forwarded-For` with the header's address, and a hairpin connection keeping its normalisation. |
| -- Client unknown: a LOCAL request skips the rate limit and burst, gets no geo, and forwards `X-Forwarded-For` with the node address. |
| -- hostenv: the key's validation (private IPv4, 1-16, no duplicates). |
| -- Tofu: `ingress` tests for the defaults (S0), each step map, the S3 refusal and the output; `group.tf` test for the precondition in all four states. |
| +- `internal/proxyproto` (new, about 250 lines, no dependency): table tests for every row of §4.4 and every limit; byte-at-a-time and split reads; a 12-byte prefix that diverges at each position; timeout; exactly one header consumed, a second left as payload; a v1 header passed through; `Close` during a pending read returns at once. |
| +- Listener: a real `http.Server` behind the wrapper, plain and TLS (HTTP/1.1 and HTTP/2), with a trusted and an untrusted dialler (a test hook supplying the peer), asserting `r.RemoteAddr` and the parse state seen by the handler; a silent trusted connection does not delay a second connection's accept; `ConnContext` and `ConnState` hooks never block; `Shutdown` and `CloseIngress` end silent and partial-header connections within their bounds. |
| +- Production wiring (house rule, production-wired witnesses): the listener stack built the way `Serve` builds it (`publicServers`, `ConfigureGroupHairpin`, the wrapper, the §4.6 handler wrapper), with a header-carrying request reaching the rate limiter and `X-Forwarded-For` with the header's address, a hairpin connection keeping its normalisation, a `no-client` health check answered and a `no-client` request for an app refused with 503 before routing. |
| +- hostenv: the key's validation (private IPv4, 1-16, no duplicates), in both `Assemble` and the group `write` path. |
| +- Tofu: `ingress` tests for the defaults (S0), each per-port step, the S3 refusal and the output with one interface per zone; `group.tf` test for the precondition in all four states. |
| +- The step script: a dry-run mode tested against recorded `describe-target-group-attributes` outputs for every allowed and refused move. |
| - `go vet` and cross-builds (darwin, windows) for the new package. |
| |
| ## 10. Witness |
| |
| -**Probe:** in-region, as the drain experiment recommends: a throwaway t4g.nano (or the root runner) in the NLB's VPC public subnet, dialling one NLB public address at 3 requests a second, recording source port and connect time per request, with docs-t1 reference checks if run from there instead. **Echo:** an app on the HA pool that returns the `X-Forwarded-For` it received (the plan names it; a 20-line app if none exists). |
| +**Probe:** in region, as the drain experiment recommends: a throwaway t4g.nano (or the root runner) in the NLB's VPC public subnet, running §6.4's processes on both NLB addresses and both ports. **Echo:** an app on the HA pool that returns the `X-Forwarded-For` it received (the plan names it; a 20-line app if none exists). |
| |
| Dev rows, all in one window: |
| -1. Prerequisites (§6.1) shown from command output: versions, armed lines, key equals module output. |
| -2. Each step A80 to B443 with its watches; record when `ignored` and `proxied` start moving relative to the apply, which measures propagation. |
| -3. In S2: the echo shows the probe's public address; a rate-limit burst from the probe is limited and a second address is not. |
| -4. In S2: 4 W6 drained restarts (2 leo, 2 sophie) under the probe, 0 errors. |
| -5. In S2: one group roll replacing a member, under the probe, 0 errors, the new member armed. |
| -6. In S2: from a member, `curl` an NLB public address with the hairpin redirect on (unchanged path) and one request through the NLB from a host outside the hairpin set, both 200. |
| -7. **Collision row:** from one host, 200 pairs of connections with the same source port to both NLB addresses (the lost-post repro), in S0 and then S2. In S2, target-side `ss` shows the pairs as distinct 4-tuples from the node addresses. S0 is expected to pass too (the repro never failed); the row shows the mechanism, not a before and after. |
| -8. Rollback drill: B443 to A80 in reverse with the watches, then forward again. |
| - |
| -Flagship rows: 1, 2, 3 (echo only), 4 (2 adi restarts), 7 in S2 only. |
| +1. Prerequisites (§6.1) shown from command output: versions, bootstrap object hash, armed lines, key equal to the module output. |
| +2. Each step A80 to B443 through the step script, with its convergence record; record how long after each apply the marker starts failing on each address, which measures propagation. |
| +3. **Held connections across steps.** Before each step, open and keep open through both NLB addresses: an HTTP/1.1 keep-alive connection sending a request every 5 s, an HTTP/2 connection doing the same, and an SSE stream. Each must keep working, with the echo showing the probe's address, through the step and the next 2 minutes. Any failure stops the window: the new-connections-only assumption (§3) is wrong. |
| +4. In S2: the echo shows the probe's public address; a rate-limit burst from the probe is limited and a request from a second address is not; a request to an app whose holder is another box arrives with the probe's address in the signed `X-Forwarded-For`. |
| +5. In S2: 4 W6 drained restarts (2 leo, 2 sophie) under the probe, 0 errors. |
| +6. In S2: one group roll replacing a member, under the probe, 0 errors, the new member armed from the vN bootstrap. |
| +7. In S2: from a member, `curl` an NLB public address (hairpin redirect, unchanged path), 200. |
| +8. **Collision row:** from one host, 200 pairs of connections with the same source port to both NLB addresses (the lost-post repro), in S0 and then S2. In S2, target-side `ss` shows the pairs as distinct 4-tuples from the node addresses. S0 is expected to pass too (the repro never failed); the row shows the mechanism, not a before and after. |
| +9. **Rollback drill:** B443 to A80 in reverse through the script, each with its convergence record, then forward again. |
| +10. **S3 refusals, dry:** the step script refuses S0 to S2 and S2 to S0 against the live TG, and the module refuses an S3 plan. S3 itself is never entered. |
| + |
| +Flagship rows: 1, 2, 3 (one held connection of each kind), 4 (echo and burst), 5 (2 adi restarts), 8 in S2 only. |
| |
| ## 11. Open questions for Paul |
| |
| -1. **Client unknown (§4.6).** A trusted connection without a client address skips per-client limits rather than being refused. Refusing would break health checks before the flip. Recommend as written. |
| -2. **Hairpin redirect (§7.4).** Keep it now and retire it later as its own change. Recommend keep. |
| -3. **Fast rollback (§6.3).** Allow the direct `aws elbv2` single-attribute change in an abort, reconciled by a no-change tofu plan after. Recommend yes. |
| +1. **Hairpin redirect (§7.4).** Keep it now and retire it later as its own change. Recommend keep. |
| +2. **Direct rollback command (§6.3).** Allow `aws elbv2 modify-target-group-attributes` for preservation **on** only, in an abort or S3 incident, reconciled by a no-change tofu plan after. Recommend yes. |
| +3. **S3 incident cost (§6.5).** Closing every possibly-forged connection means drained restarts of every target and a member group roll. Recommend as written; it should never run. |
| 4. **When.** Build after this is approved; dev window on the first dev roll after vN; flagship before G6. Switch-on date for the dormant code is that dev window. |
| + |
| +## 12. Astra round 1 and where it went |
| + |
| +`docs/superpowers/specs/ASTRA-FINDINGS-edge-ppv2-R1.md`, NOT READY: one P1, six P2s. |
| + |
| +| Finding | Fault family | Disposition | |
| +|---|---|---| |
| +| P1: in S3 a client's own header is trusted, so S3 is forgery, not "limits off" | F1: client bytes becoming trusted identity | Accepted. §3 states the invariant; §6.2-§6.4 make S3 unreachable by the step script and detect it with the marker probe; §6.5 incident; §4.6 no longer claims S3 is bounded. | |
| +| P2: client unknown turns into a node address after forwarding | F2: missing identity across a hop | Accepted by removing client unknown: a `no-client` connection answers only the health check (§4.6), so nothing is forwarded without a client. | |
| +| P2: analytics hashes an empty address into one visitor | F2 | Resolved by the same change: no request reaches analytics without a client (§4.6). | |
| +| P2: the context mark needs a deferred-state contract | connection metadata without blocking | Accepted: §4.3 parse state, pointer in `ConnContext`, resolved on the serving goroutine, `Close` never waits; tests in §9. | |
| +| P2: the witness does not prove the cutover claims | rollout claims beyond evidence | Accepted: §3 states the assumption; §6.4 convergence per address and port gates each step; §10 row 3 holds connections across steps. | |
| +| P2: `aws_network_interfaces` has no private addresses | infra discovery | Accepted: singular lookups per zone (§8.1). | |
| +| P2: the member bootstrap binary refuses the new key | bootstrap compatibility | Accepted: §6.1 step 2 and the unwinding order. | |