edge: replica calls go to an operator-set console address
Astra P1 on 22e2158c: naming the console's HOST in config still took its upstream from the route row, which a legacy direct-S3 fleet box can rewrite through its deployment record, redirecting the boxes' fleet bearer tokens to another loopback port or stopping coordination. CARLOS_REPLICA_CONSOLE_ADDR (hub host.env) is the console's address, refused at load unless a literal loopback IP and port. When set, replica calls go to a proxy built from that address alone, and the X-Carlos-Version the box checks is the hub's own build; no route field is read. Unset, selection is byte-identical to main (the one accountless console.service route). The account-owned fallback is gone. Witnesses: TestReplicaConsoleConfiguredEndpoint (distinct destinations; injected, system and rewritten rows never reached; endpoint down is a 502 with no version), TestReplicaConsoleUnconfiguredIgnoresAccountRows, TestParseReplicaConsoleAddr, TestReplicaConsoleAddrFromHostEnv. Main's TestFleetReplicaConsoleOnTunnelHost restored unchanged. Census re-proposed from main.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
docs/superpowers/notes/census-proposal-replica-console-account.md+2 −2internal/edge/edge.go+10 −7internal/edge/replicaconsole.go+84 −38internal/edge/replicaconsole_test.go+155 −53internal/provision/testdata/plan14-root-reads-chain.tsv+1 −1internal/provision/testdata/plan14-root-reads-replica-console-account.tsv+3 −3
CI passed — run details
Intent
<task-notification> <task-id>bvlzl6j3g</task-id> <tool-use-id>toolu_01JUZZVoL73VbhsJeNEXATpq</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/bvlzl6j3g.output</output-file> <status>completed</status> <summary>Background command "Run Astra's round-2 review" completed (exit code 0)</summary> </task-notification>
<paseo-system version="1" kind="agent-message" source-agent-id="2db0f16f-4bca-4575-a102-9361f2f9c3e2" message-id="3d4f4d9b-b719-4f13-9989-42353e1c9bfc" source-agent-title="Release v0.35.0 (dev only first)"> From the v0.35.1 release agent: dev fleet witness PASS at 03:53:45Z on r10r box-1 (v0.35.1): xfpecho GET / shows X-Forwarded-Proto "https", GET /force answers 200 "secure" (was 301 to itself on v0.35.0 at 03:22Z). Second problem for you, unchanged by the roll: tito-classic.bac, tito-id.bac, statelesssmoke.bab, cable.bab and drillcheck.bab.dev.oncarlos.com still answer 503 Retry-After 5 with x-carlos-answer: pending-host (same before the roll at 03:12Z; x-carlos-box ec3d9a55cb29 / ac47ed2358ff). railsdemo and shop answer 200. I have not dug into the box journals; the dev lock stays open to 05:30Z if you need anything from me. </paseo-system>
<task-notification> <task-id>bah6mntyt</task-id> <tool-use-id>toolu_01WK8dr3ZnqTDfs3NUPtNbjY</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/bah6mntyt.output</output-file> <status>completed</status> <summary>Background command "Report the stop and options to 8a3d302" completed (exit code 0)</summary> </task-notification>
Attributed to this commit: 44.9 g CO₂e (5,360,573 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on replica-console-account, 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/docs/superpowers/notes/census-proposal-replica-console-account.md b/docs/superpowers/notes/census-proposal-replica-console-account.md| index 94f494b80..975028b18 100644 |
| --- a/docs/superpowers/notes/census-proposal-replica-console-account.md |
| +++ b/docs/superpowers/notes/census-proposal-replica-console-account.md |
| @@ -4,7 +4,7 @@ Ruling `census-ruling-replica-console-account`. The independent reviewer approve |
| | unit | change | read sites | what changed | |
| |---|---|---|---| |
| -| `internal/edge/replicaconsole.go` `Edge.serveReplicaConsole` | body | unchanged (6) | the console route for replica calls is the host named by operator config CARLOS_REPLICA_CONSOLE_HOST (host.env, read once at hub setup, validated as a host name) when set, whatever its account; still app console, unit console.service, loopback upstream or socket, not alias, regional, via tunnel or activator-owned, exactly one. Unset, selection is unchanged (accountless only). Route fields alone never name an account-owned console, so an injected record cannot take over or make it ambiguous. Witness TestReplicaConsoleRouteShape. Memory-only reads, no new read source, no write. | |
| +| `internal/edge/replicaconsole.go` `Edge.serveReplicaConsole` | body | unchanged (6) | when operator config CARLOS_REPLICA_CONSOLE_ADDR (hub host.env, read once in enableTunnelsFromEnv, refused at load unless a literal loopback IP and port) is set, replica calls go to a proxy built only from that address, stamping the hub build as X-Carlos-Version; no route field is read, so records can neither choose, redirect nor disable the endpoint. Unset, the selection is byte-identical to main (accountless console.service local route). Witnesses TestReplicaConsoleConfiguredEndpoint (distinct destinations, injected and rewritten rows, endpoint down), TestReplicaConsoleUnconfiguredIgnoresAccountRows, TestParseReplicaConsoleAddr, TestReplicaConsoleAddrFromHostEnv. Same reads, no new read source, no write. | |
| Envelopes re-pinned: `internal/edge`. |
| @@ -14,6 +14,6 @@ Recorded dispositions, which each change must still satisfy: |
| - `internal/edge/replicaconsole.go` `Edge.serveReplicaConsole`: legacy, task accepted-legacy. Basis: accepted-legacy;rev3=census-ruling-stateless-coordinator-route;basis=Reads existing in-memory route fields to select a unique accountless console.service local upstream; refuses missing or ambiguous console, tenant routes, remote and tunnel upstreams. Reuses proxy and version wrapper; leaves fleet identity authentication at console.;rev3=census-ruling-retry-after-503;basis=the missing/ambiguous-console 503s go through UnavailableHeader (adds Retry-After 2 and answer/box headers); selection unchanged; no read added |
| -Seal: `846d1f22e6eb6b2cd8b3086fa554f15deba70418c6542b46b2e2a6a138b6c332` → `0a1771b2e24bbfb63f397da2b37fe29aa6e12b13f1961a62e7f670ab0aad2c0c`. |
| +Seal: `846d1f22e6eb6b2cd8b3086fa554f15deba70418c6542b46b2e2a6a138b6c332` → `87e151a55efb12fb84e9c5939f2ead9058527d8213e2eb16ee6c0f109787bb16`. |
| Overlay: `internal/provision/testdata/plan14-root-reads-replica-console-account.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/internal/edge/edge.go b/internal/edge/edge.go| index 07e8e1b3f..f6fc9eadb 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -116,12 +116,12 @@ type Activator interface { |
| // zero-downtime deploys never drop a live connection. |
| type Edge struct { |
| shutdownStarted func(time.Time) |
| - // replicaConsoleHost is the operator-configured console route for |
| - // fleet replica calls (replicaConsoleHostEnv); "" is the accountless |
| + // replicaConsole forwards fleet replica calls to the operator-configured |
| + // console endpoint (replicaConsoleAddrEnv); nil uses the accountless |
| // console.service route. Set before serving. |
| - replicaConsoleHost string |
| - haIngress atomic.Pointer[haIngressLookup] |
| - haIngressBlocked atomic.Pointer[map[string]bool] |
| + replicaConsole http.Handler |
| + haIngress atomic.Pointer[haIngressLookup] |
| + haIngressBlocked atomic.Pointer[map[string]bool] |
| // ReplicaProbe is configured before the first syncReplicaProbes call. |
| // Nil uses the private box landing over the existing tunnel transport. |
| ReplicaProbe func(context.Context, string, string) (replicas.Copy, error) |
| @@ -5457,11 +5457,14 @@ func enableTunnelsFromEnv(e *Edge) error { |
| return fmt.Errorf("CARLOS_TUNNEL_CLIENTS: %q is not a valid tunnel name — want [a-z0-9-]{1,32}, and it must match a route's --tunnel to carry anything", name) |
| } |
| } |
| - consoleHost, err := replicaConsoleHostFromEnv(os.Getenv) |
| + consoleAddr, err := parseReplicaConsoleAddr(os.Getenv(replicaConsoleAddrEnv)) |
| if err != nil { |
| return err |
| } |
| - e.replicaConsoleHost = consoleHost |
| + if consoleAddr != "" { |
| + e.replicaConsole = newReplicaConsoleProxy(consoleAddr) |
| + log.Printf("edge: fleet replica calls go to the console at %s (%s)", consoleAddr, replicaConsoleAddrEnv) |
| + } |
| e.EnableTunnels(NewTunnelHub(host, clients)) |
| // Rebuild the route table so tunnel-backed routes that were skipped (or |
| // left on their old socket/addr) by the startup reload pick up the hub, |
diff --git a/internal/edge/replicaconsole.go b/internal/edge/replicaconsole.go| index 5e8d9f214..508310010 100644 |
| --- a/internal/edge/replicaconsole.go |
| +++ b/internal/edge/replicaconsole.go |
| @@ -2,60 +2,106 @@ package edge |
| import ( |
| "fmt" |
| + "net" |
| "net/http" |
| + "net/http/httputil" |
| + "net/netip" |
| + "strconv" |
| "strings" |
| + "time" |
| - "github.com/carlosframework/platform/internal/controlname" |
| + "github.com/carlosframework/platform/internal/version" |
| ) |
| -// replicaConsoleHostEnv names, in the hub's host.env, the console route the |
| -// hub hands fleet boxes' replica calls to. It is operator configuration, |
| -// the only source trusted to say which route is the console: a route's own |
| -// fields come from records, and records are not a tenancy boundary (a |
| -// legacy direct-S3 fleet box can write deployment records). A self-hosted |
| -// deployment, whose console is installed into its operator account, sets |
| -// it; the platform's own hubs need not. |
| -const replicaConsoleHostEnv = "CARLOS_REPLICA_CONSOLE_HOST" |
| +// replicaConsoleAddrEnv names, in the hub's host.env, the loopback address |
| +// of the console process the hub hands fleet boxes' replica calls to. It is |
| +// operator configuration, and the only source trusted for that endpoint: |
| +// route rows come from deployment records, and records are not a tenancy |
| +// boundary (a legacy direct-S3 fleet box can write them), so no route field |
| +// may choose, redirect or disable where those calls and their fleet bearer |
| +// tokens go. A self-hosted deployment whose console is installed into its |
| +// operator account sets it (Tito: 127.0.0.1:8090); the platform's own hubs |
| +// need not. |
| +const replicaConsoleAddrEnv = "CARLOS_REPLICA_CONSOLE_ADDR" |
| -// WithReplicaConsoleHost sets the configured console host (tests; the |
| -// process reads replicaConsoleHostEnv in enableTunnelsFromEnv). |
| -func WithReplicaConsoleHost(host string) RunOption { |
| - return func(e *Edge) { e.replicaConsoleHost = host } |
| +// WithReplicaConsoleAddr configures the replica console endpoint (tests; |
| +// the process reads replicaConsoleAddrEnv in enableTunnelsFromEnv). addr |
| +// must already have passed parseReplicaConsoleAddr. |
| +func WithReplicaConsoleAddr(addr string) RunOption { |
| + return func(e *Edge) { e.replicaConsole = newReplicaConsoleProxy(addr) } |
| } |
| -// replicaConsoleHostFromEnv reads and checks replicaConsoleHostEnv. Unset |
| -// is "" and no error; a value that is not a host name fails the boot, |
| -// naming the variable, rather than leave every replica call refused. |
| -func replicaConsoleHostFromEnv(getenv func(string) string) (string, error) { |
| - host := strings.ToLower(strings.TrimSpace(getenv(replicaConsoleHostEnv))) |
| - if host != "" && !controlname.ValidHostShape(host) { |
| - return "", fmt.Errorf("%s: %q is not a host name", replicaConsoleHostEnv, host) |
| +// parseReplicaConsoleAddr checks replicaConsoleAddrEnv's value: unset is "" |
| +// and no error; otherwise a literal loopback IP and a port, so the endpoint |
| +// is always this box's own process. A host name (localhost included) or any |
| +// other address fails the boot, naming the variable. |
| +func parseReplicaConsoleAddr(raw string) (string, error) { |
| + raw = strings.TrimSpace(raw) |
| + if raw == "" { |
| + return "", nil |
| + } |
| + host, port, err := net.SplitHostPort(raw) |
| + if err != nil { |
| + return "", fmt.Errorf("%s: %q is not ip:port: %v", replicaConsoleAddrEnv, raw, err) |
| + } |
| + ip, err := netip.ParseAddr(host) |
| + if err != nil || !ip.IsLoopback() || ip.Zone() != "" { |
| + return "", fmt.Errorf("%s: %q must be a loopback IP address, such as 127.0.0.1:8090", replicaConsoleAddrEnv, raw) |
| + } |
| + if n, err := strconv.Atoi(port); err != nil || n < 1 || n > 65535 { |
| + return "", fmt.Errorf("%s: %q has no valid port", replicaConsoleAddrEnv, raw) |
| + } |
| + return net.JoinHostPort(ip.String(), port), nil |
| +} |
| + |
| +// newReplicaConsoleProxy forwards replica calls to the configured console |
| +// endpoint. Nothing in it comes from route data: the upstream is addr, and |
| +// X-Carlos-Version, which the box reads only as proof that the console |
| +// answered (internal/host replicaCoordinatorAPI.post), is this hub's own |
| +// build. A failed dial answers 502 with no version, which the box reads as |
| +// the console unreachable. |
| +func newReplicaConsoleProxy(addr string) http.Handler { |
| + return &httputil.ReverseProxy{ |
| + Rewrite: func(pr *httputil.ProxyRequest) { |
| + setForwarded(pr) |
| + pr.Out.URL.Scheme = "http" |
| + pr.Out.URL.Host = addr |
| + pr.Out.Host = pr.In.Host |
| + }, |
| + Transport: &http.Transport{ |
| + DialContext: (&net.Dialer{Timeout: 5 * time.Second}).DialContext, |
| + MaxIdleConns: 8, |
| + IdleConnTimeout: 90 * time.Second, |
| + ResponseHeaderTimeout: 30 * time.Second, |
| + }, |
| + ModifyResponse: func(resp *http.Response) error { |
| + resp.Header.Set("X-Carlos-Version", version.Build) |
| + return nil |
| + }, |
| + ErrorHandler: func(w http.ResponseWriter, _ *http.Request, _ error) { |
| + w.WriteHeader(http.StatusBadGateway) |
| + }, |
| } |
| - return host, nil |
| } |
| -// serveReplicaConsole reuses the local console route, including its version |
| -// header. Only POSTs on the reserved tunnel host enter here; other methods |
| -// keep the hub's 404. Authentication remains the console's fleetIdentityOK. |
| +// serveReplicaConsole hands a box's replica call to the console. Only POSTs |
| +// on the reserved tunnel host enter here; other methods keep the hub's 404. |
| +// Authentication remains the console's fleetIdentityOK. |
| // |
| -// The console route is a row for app console with unit console.service, on |
| -// a loopback upstream (or a socket), that is not an alias, regional, via a |
| -// tunnel or activator-owned. With replicaConsoleHostEnv set, it is that |
| -// host's row and no other, whatever its account. Unset, it is the one such |
| -// row with no account, the platform console's shape, as before: an |
| -// account-owned row is never chosen and never makes it ambiguous. |
| +// With replicaConsoleAddrEnv set, the call goes to that endpoint and route |
| +// data plays no part. Unset, the console is the one route for app console |
| +// with unit console.service and no account (the platform console's shape), |
| +// on a loopback upstream or a socket, not an alias, regional, via a tunnel |
| +// or activator-owned, and its proxy (with its version header) is reused. |
| func (e *Edge) serveReplicaConsole(w http.ResponseWriter, r *http.Request) { |
| - configured := e.replicaConsoleHost |
| + if e.replicaConsole != nil { |
| + e.replicaConsole.ServeHTTP(w, e.withForwardKey(r)) |
| + return |
| + } |
| var console http.Handler |
| for host, up := range *e.upstreams.Load() { |
| rt := up.route |
| - if configured != "" && host != configured { |
| - continue |
| - } |
| - if configured == "" && rt.Account != "" { |
| - continue |
| - } |
| - if host != rt.Host || up.alias || up.regional != 0 || up.viaTunnel || rt.App != "console" || rt.Unit != "console.service" || rt.ActivatorOwned() { |
| + if host != rt.Host || up.alias || up.regional != 0 || up.viaTunnel || rt.Account != "" || rt.App != "console" || rt.Unit != "console.service" || rt.ActivatorOwned() { |
| continue |
| } |
| if rt.Socket == "" { |
diff --git a/internal/edge/replicaconsole_test.go b/internal/edge/replicaconsole_test.go| index 15fe2bdd5..f665415a6 100644 |
| --- a/internal/edge/replicaconsole_test.go |
| +++ b/internal/edge/replicaconsole_test.go |
| @@ -86,55 +86,138 @@ func TestFleetReplicaConsoleOnTunnelHost(t *testing.T) { |
| } |
| }) |
| } |
| + // A tenant app named console must not become the reserved API upstream. |
| + rt.Account = "a" |
| + if err := reg.Upsert(rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := e.Reload(); err != nil { |
| + t.Fatal(err) |
| + } |
| + w := httptest.NewRecorder() |
| + e.ServeHTTP(w, httptest.NewRequest("POST", "https://tunnel.test"+prefix+"coordinator", nil)) |
| + if w.Code != http.StatusServiceUnavailable { |
| + t.Fatalf("tenant console: %d", w.Code) |
| + } |
| } |
| -// TestReplicaConsoleRouteShape (#511): which local route the hub hands a |
| -// box's replica call to. Route fields come from records, which are not a |
| -// tenancy boundary (a legacy direct-S3 fleet box can write deployment |
| -// records), so only operator configuration (CARLOS_REPLICA_CONSOLE_HOST) |
| -// can name an account-owned console, as a self-hosted deployment's is. |
| -// Unconfigured, the hub takes the one accountless console.service route, |
| -// and an account-owned console.service row (an injected record) is neither |
| -// chosen nor makes it ambiguous. |
| -func TestReplicaConsoleRouteShape(t *testing.T) { |
| - var hits atomic.Int32 |
| - console := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| - hits.Add(1) |
| - _, _ = io.WriteString(w, `{"lease":{}}`) |
| - })) |
| - defer console.Close() |
| - addr := console.Listener.Addr().String() |
| - consoleRow := func(host, account string) registry.Route { |
| - return registry.Route{Host: host, Account: account, App: "console", Unit: "console.service", Kind: "instance", Addr: addr, Status: "ready", Version: "v-test"} |
| - } |
| - // Tito's shape: the console installed into its operator account, and a |
| - // platform row for the same app with no unit. |
| - selfHosted := []registry.Route{consoleRow("console.staging.example.com", "bab"), |
| - {Host: "console.bab.staging.example.com", Account: "bab", App: "console", Kind: "instance", Addr: addr, Status: "ready", Platform: true}} |
| - tenant := registry.Route{Host: "console.tenant.test", Account: "a", App: "console", Kind: "service", Addr: addr, Status: "ready"} |
| - remote := consoleRow("console.remote.test", "bab") |
| - remote.Addr = "192.0.2.10:8090" |
| +// TestReplicaConsoleConfiguredEndpoint (#511): with CARLOS_REPLICA_CONSOLE_ADDR |
| +// set, a box's replica call goes to that endpoint and nowhere else, whatever |
| +// the registry holds. Route rows come from deployment records, which a |
| +// legacy direct-S3 fleet box can write: an injected console.service row, a |
| +// system console row, or the configured console's own row rewritten to |
| +// another loopback port neither take the call nor make it ambiguous. The |
| +// version header the box needs is the hub's build, never a route's. |
| +func TestReplicaConsoleConfiguredEndpoint(t *testing.T) { |
| + server := func(name string, hits *atomic.Int32) *httptest.Server { |
| + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + hits.Add(1) |
| + if r.Header.Get("Authorization") != "Bearer fleet-token" || r.URL.Path != "/api/fleet/replicas/a/app/h/coordinator" { |
| + t.Errorf("%s got %s %v", name, r.URL, r.Header) |
| + } |
| + _, _ = io.WriteString(w, name) |
| + })) |
| + t.Cleanup(s.Close) |
| + return s |
| + } |
| + var configuredHits, systemHits, injectedHits, rewrittenHits atomic.Int32 |
| + configured := server("configured", &configuredHits) |
| + system := server("system", &systemHits) |
| + injected := server("injected", &injectedHits) |
| + rewritten := server("rewritten", &rewrittenHits) |
| + addr, err := parseReplicaConsoleAddr(configured.Listener.Addr().String()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := openReg(t) |
| + rows := []registry.Route{ |
| + // Tito's shape: the console row in its operator account, at the |
| + // configured address, plus the platform row for the same app. |
| + {Host: "console.staging.example.com", Account: "bab", App: "console", Unit: "console.service", Kind: "instance", Addr: addr, Status: "ready", Version: "route-version"}, |
| + {Host: "console.bab.staging.example.com", Account: "bab", App: "console", Kind: "instance", Addr: addr, Status: "ready", Platform: true}, |
| + {Host: "console.test", App: "console", Unit: "console.service", Kind: "instance", Addr: system.Listener.Addr().String(), Status: "ready", Version: "route-version"}, |
| + {Host: "injected.test", Account: "x", App: "console", Unit: "console.service", Kind: "instance", Addr: injected.Listener.Addr().String(), Status: "ready", Version: "route-version"}, |
| + } |
| + for _, rt := range rows { |
| + if err := reg.Upsert(rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + e, err := New(reg, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer e.probers.Stop() |
| + WithReplicaConsoleAddr(addr)(e) |
| + e.EnableTunnels(NewTunnelHub("tunnel.test", nil)) |
| + post := func() *httptest.ResponseRecorder { |
| + t.Helper() |
| + req := httptest.NewRequest("POST", "https://tunnel.test/api/fleet/replicas/a/app/h/coordinator", strings.NewReader(`{"op":"acquire"}`)) |
| + req.Header.Set("Authorization", "Bearer fleet-token") |
| + w := httptest.NewRecorder() |
| + e.ServeHTTP(w, req) |
| + return w |
| + } |
| + check := func(stage string) { |
| + t.Helper() |
| + w := post() |
| + if w.Code != 200 || w.Body.String() != "configured" { |
| + t.Fatalf("%s: %d %q", stage, w.Code, w.Body.String()) |
| + } |
| + if v := w.Header().Get("X-Carlos-Version"); v == "" || v == "route-version" { |
| + t.Fatalf("%s: version header %q, want the hub's build", stage, v) |
| + } |
| + } |
| + check("configured") |
| + // The configured console's own row rewritten to another loopback |
| + // port, as a record writer could: the call still goes to the endpoint. |
| + rows[0].Addr = rewritten.Listener.Addr().String() |
| + if err := reg.Upsert(rows[0]); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := e.Reload(); err != nil { |
| + t.Fatal(err) |
| + } |
| + check("row rewritten") |
| + if systemHits.Load()+injectedHits.Load()+rewrittenHits.Load() != 0 { |
| + t.Fatalf("route upstreams reached: system %d injected %d rewritten %d", systemHits.Load(), injectedHits.Load(), rewrittenHits.Load()) |
| + } |
| + // The endpoint down: 502 with no version, which the box reads as the |
| + // console unreachable; no route upstream stands in. |
| + configured.Close() |
| + if w := post(); w.Code != http.StatusBadGateway || w.Header().Get("X-Carlos-Version") != "" { |
| + t.Fatalf("endpoint down: %d %q", w.Code, w.Header().Get("X-Carlos-Version")) |
| + } |
| + if systemHits.Load()+injectedHits.Load()+rewrittenHits.Load() != 0 { |
| + t.Fatal("a route upstream stood in for the configured endpoint") |
| + } |
| +} |
| + |
| +// TestReplicaConsoleUnconfiguredIgnoresAccountRows: without the setting, an |
| +// account-owned console.service row beside the system console neither takes |
| +// the call nor makes it ambiguous, and alone it is not the console. |
| +func TestReplicaConsoleUnconfiguredIgnoresAccountRows(t *testing.T) { |
| + var systemHits, ownedHits atomic.Int32 |
| + system := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { systemHits.Add(1) })) |
| + defer system.Close() |
| + owned := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ownedHits.Add(1) })) |
| + defer owned.Close() |
| + ownedRow := registry.Route{Host: "console.staging.example.com", Account: "bab", App: "console", Unit: "console.service", Kind: "instance", Addr: owned.Listener.Addr().String(), Status: "ready", Version: "v"} |
| + systemRow := registry.Route{Host: "console.test", App: "console", Unit: "console.service", Kind: "instance", Addr: system.Listener.Addr().String(), Status: "ready", Version: "v"} |
| for _, tc := range []struct { |
| - name string |
| - configured string |
| - routes []registry.Route |
| - code int |
| - answer string |
| + name string |
| + rows []registry.Route |
| + code int |
| + answer string |
| }{ |
| - {"system console", "", []registry.Route{consoleRow("console.test", "")}, 200, ""}, |
| - {"system console beside an injected account-owned console unit", "", []registry.Route{consoleRow("console.test", ""), consoleRow("injected.test", "x")}, 200, ""}, |
| - {"account-owned console unconfigured", "", selfHosted, 503, "replica-console-missing"}, |
| - {"two system consoles", "", []registry.Route{consoleRow("console.test", ""), consoleRow("console2.test", "")}, 503, "replica-console-ambiguous"}, |
| - {"configured account-owned console", "console.staging.example.com", selfHosted, 200, ""}, |
| - {"configured console beside an injected console unit", "console.staging.example.com", append([]registry.Route{consoleRow("injected.test", "x"), consoleRow("console.test", "")}, selfHosted...), 200, ""}, |
| - {"configured host is a tenant app named console", "console.tenant.test", []registry.Route{tenant}, 503, "replica-console-missing"}, |
| - {"configured host is not on this box", "console.remote.test", []registry.Route{remote}, 503, "replica-console-missing"}, |
| - {"configured host absent", "console.staging.example.com", []registry.Route{consoleRow("console.test", "")}, 503, "replica-console-missing"}, |
| - {"tenant app named console", "", []registry.Route{tenant}, 503, "replica-console-missing"}, |
| + {"system beside account-owned", []registry.Route{systemRow, ownedRow}, 200, ""}, |
| + {"account-owned alone", []registry.Route{ownedRow}, 503, "replica-console-missing"}, |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| + systemHits.Store(0) |
| + ownedHits.Store(0) |
| reg := openReg(t) |
| - for _, rt := range tc.routes { |
| + for _, rt := range tc.rows { |
| if err := reg.Upsert(rt); err != nil { |
| t.Fatal(err) |
| } |
| @@ -144,28 +227,47 @@ func TestReplicaConsoleRouteShape(t *testing.T) { |
| t.Fatal(err) |
| } |
| defer e.probers.Stop() |
| - WithReplicaConsoleHost(tc.configured)(e) |
| e.EnableTunnels(NewTunnelHub("tunnel.test", nil)) |
| - before := hits.Load() |
| w := httptest.NewRecorder() |
| - e.ServeHTTP(w, httptest.NewRequest("POST", "https://tunnel.test/api/fleet/replicas/a/app/h/coordinator", strings.NewReader(`{"op":"acquire"}`))) |
| - if w.Code != tc.code || w.Header().Get("X-Carlos-Answer") != tc.answer || (hits.Load() > before) != (tc.code == 200) { |
| - t.Fatalf("got %d %q, console hits %d", w.Code, w.Header().Get("X-Carlos-Answer"), hits.Load()-before) |
| + e.ServeHTTP(w, httptest.NewRequest("POST", "https://tunnel.test/api/fleet/replicas/a/app/h/coordinator", strings.NewReader(`{}`))) |
| + if w.Code != tc.code || w.Header().Get("X-Carlos-Answer") != tc.answer || ownedHits.Load() != 0 || (systemHits.Load() == 1) != (tc.code == 200) { |
| + t.Fatalf("%d %q system %d owned %d", w.Code, w.Header().Get("X-Carlos-Answer"), systemHits.Load(), ownedHits.Load()) |
| } |
| }) |
| } |
| } |
| -func TestReplicaConsoleHostFromEnv(t *testing.T) { |
| - for in, want := range map[string]string{"": "", " Console.Staging.Example.com ": "console.staging.example.com"} { |
| - got, err := replicaConsoleHostFromEnv(func(string) string { return in }) |
| +func TestParseReplicaConsoleAddr(t *testing.T) { |
| + for in, want := range map[string]string{"": "", " 127.0.0.1:8090 ": "127.0.0.1:8090", "[::1]:8090": "[::1]:8090", "127.0.0.2:1": "127.0.0.2:1"} { |
| + got, err := parseReplicaConsoleAddr(in) |
| if err != nil || got != want { |
| t.Errorf("%q: %q %v", in, got, err) |
| } |
| } |
| - for _, bad := range []string{"https://console.example.com", "console.example.com:8090", "a b"} { |
| - if _, err := replicaConsoleHostFromEnv(func(string) string { return bad }); err == nil { |
| - t.Errorf("%q accepted", bad) |
| + for _, bad := range []string{"localhost:8090", "10.0.0.1:8090", "0.0.0.0:8090", "console.example.com:8090", "127.0.0.1", "127.0.0.1:0", "127.0.0.1:65536", "127.0.0.1:http", "http://127.0.0.1:8090", "[fe80::1%eth0]:8090"} { |
| + if got, err := parseReplicaConsoleAddr(bad); err == nil { |
| + t.Errorf("%q accepted as %q", bad, got) |
| } |
| } |
| } |
| + |
| +// TestReplicaConsoleAddrFromHostEnv: the hub reads the setting when it arms |
| +// its tunnels, and a bad value fails the boot. |
| +func TestReplicaConsoleAddrFromHostEnv(t *testing.T) { |
| + reg := openReg(t) |
| + e, err := New(reg, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer e.probers.Stop() |
| + t.Setenv("CARLOS_FLEET", "") |
| + t.Setenv("CARLOS_TUNNEL_HOST", "tunnel.test") |
| + t.Setenv(replicaConsoleAddrEnv, "10.0.0.1:8090") |
| + if err := enableTunnelsFromEnv(e); err == nil || !strings.Contains(err.Error(), replicaConsoleAddrEnv) { |
| + t.Fatalf("bad value: %v", err) |
| + } |
| + t.Setenv(replicaConsoleAddrEnv, "127.0.0.1:8090") |
| + if err := enableTunnelsFromEnv(e); err != nil || e.replicaConsole == nil { |
| + t.Fatalf("good value: %v, configured %v", err, e.replicaConsole != nil) |
| + } |
| +} |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index 0e59c1756..3487f0b8a 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -210,4 +210,4 @@ plan14-root-reads-same-box-edge-fence.tsv 76c986ad534d23071100a9a573ffb38afc60b2 |
| plan14-root-reads-g6-fence.tsv 3f43afa4b0c18ad678a952b48b511e74e849992cd629b9aa1442bbe0055d5631 |
| plan14-root-reads-offline-move-stage-refusals-339.tsv 846d1f22e6eb6b2cd8b3086fa554f15deba70418c6542b46b2e2a6a138b6c332 |
| plan14-root-reads-fleet-tunnel-tls-state.tsv 846d1f22e6eb6b2cd8b3086fa554f15deba70418c6542b46b2e2a6a138b6c332 |
| -plan14-root-reads-replica-console-account.tsv 0a1771b2e24bbfb63f397da2b37fe29aa6e12b13f1961a62e7f670ab0aad2c0c |
| +plan14-root-reads-replica-console-account.tsv 87e151a55efb12fb84e9c5939f2ead9058527d8213e2eb16ee6c0f109787bb16 |
diff --git a/internal/provision/testdata/plan14-root-reads-replica-console-account.tsv b/internal/provision/testdata/plan14-root-reads-replica-console-account.tsv| index fc3063974..1f9cca79e 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-replica-console-account.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-replica-console-account.tsv |
| @@ -1,5 +1,5 @@ |
| # Census overlay replica-console-account, written by census-propose (#369). census-ruling-replica-console-account, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-replica-console-account.md. |
| # 1 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| -@legacy c4c72ca605e10266524712b9671eaeeb706bb111d6ecc450f2ed03479f4eaf48 internal/edge/replicaconsole.go Edge.serveReplicaConsole legacy accepted-legacy accepted-legacy;rev3=census-ruling-stateless-coordinator-route;basis=Reads existing in-memory route fields to select a unique accountless console.service local upstream; refuses missing or ambiguous console, tenant routes, remote and tunnel upstreams. Reuses proxy and version wrapper; leaves fleet identity authentication at console.;rev3=census-ruling-retry-after-503;basis=the missing/ambiguous-console 503s go through UnavailableHeader (adds Retry-After 2 and answer/box headers); selection unchanged; no read added;rev3=census-ruling-replica-console-account;basis=the console route for replica calls is the host named by operator config CARLOS_REPLICA_CONSOLE_HOST (host.env, read once at hub setup, validated as a host name) when set, whatever its account; still app console, unit console.service, loopback upstream or socket, not alias, regional, via tunnel or activator-owned, exactly one. Unset, selection is unchanged (accountless only). Route fields alone never name an account-owned console, so an injected record cannot take over or make it ambiguous. Witness TestReplicaConsoleRouteShape. Memory-only reads, no new read source, no write. registry:Account@288,registry:App@395,registry:Host@336,registry:Socket@493,registry:Unit@418,whole:Route@195 4e337c8890a66f8da6e672f243ebed593cc659abdf17387898bbf8732f63be79 |
| -internal/edge/replicaconsole.go Edge.serveReplicaConsole legacy accepted-legacy accepted-legacy;rev3=census-ruling-stateless-coordinator-route;basis=Reads existing in-memory route fields to select a unique accountless console.service local upstream; refuses missing or ambiguous console, tenant routes, remote and tunnel upstreams. Reuses proxy and version wrapper; leaves fleet identity authentication at console.;rev3=census-ruling-retry-after-503;basis=the missing/ambiguous-console 503s go through UnavailableHeader (adds Retry-After 2 and answer/box headers); selection unchanged; no read added;rev3=census-ruling-replica-console-account;basis=the console route for replica calls is the host named by operator config CARLOS_REPLICA_CONSOLE_HOST (host.env, read once at hub setup, validated as a host name) when set, whatever its account; still app console, unit console.service, loopback upstream or socket, not alias, regional, via tunnel or activator-owned, exactly one. Unset, selection is unchanged (accountless only). Route fields alone never name an account-owned console, so an injected record cannot take over or make it ambiguous. Witness TestReplicaConsoleRouteShape. Memory-only reads, no new read source, no write. registry:Account@288,registry:App@395,registry:Host@336,registry:Socket@493,registry:Unit@418,whole:Route@195 4e337c8890a66f8da6e672f243ebed593cc659abdf17387898bbf8732f63be79 |
| -@coverage internal/edge 02c7760f7ac62bb0f5ca4940275e2fd3aacf76bd4b5187e4afe73827c57fff8e |
| +@legacy 12edbd2e5eccec79415e4d335e95074759a35a5279200bf2500e4de01182b9f2 internal/edge/replicaconsole.go Edge.serveReplicaConsole legacy accepted-legacy accepted-legacy;rev3=census-ruling-stateless-coordinator-route;basis=Reads existing in-memory route fields to select a unique accountless console.service local upstream; refuses missing or ambiguous console, tenant routes, remote and tunnel upstreams. Reuses proxy and version wrapper; leaves fleet identity authentication at console.;rev3=census-ruling-retry-after-503;basis=the missing/ambiguous-console 503s go through UnavailableHeader (adds Retry-After 2 and answer/box headers); selection unchanged; no read added;rev3=census-ruling-replica-console-account;basis=when operator config CARLOS_REPLICA_CONSOLE_ADDR (hub host.env, read once in enableTunnelsFromEnv, refused at load unless a literal loopback IP and port) is set, replica calls go to a proxy built only from that address, stamping the hub build as X-Carlos-Version; no route field is read, so records can neither choose, redirect nor disable the endpoint. Unset, the selection is byte-identical to main (accountless console.service local route). Witnesses TestReplicaConsoleConfiguredEndpoint (distinct destinations, injected and rewritten rows, endpoint down), TestReplicaConsoleUnconfiguredIgnoresAccountRows, TestParseReplicaConsoleAddr, TestReplicaConsoleAddrFromHostEnv. Same reads, no new read source, no write. registry:Account@332,registry:App@352,registry:Host@273,registry:Socket@450,registry:Unit@375,whole:Route@254 31e8a1bc5fd1903daed1e82189e214b46ba6348761f2300525788b6a90393f97 |
| +internal/edge/replicaconsole.go Edge.serveReplicaConsole legacy accepted-legacy accepted-legacy;rev3=census-ruling-stateless-coordinator-route;basis=Reads existing in-memory route fields to select a unique accountless console.service local upstream; refuses missing or ambiguous console, tenant routes, remote and tunnel upstreams. Reuses proxy and version wrapper; leaves fleet identity authentication at console.;rev3=census-ruling-retry-after-503;basis=the missing/ambiguous-console 503s go through UnavailableHeader (adds Retry-After 2 and answer/box headers); selection unchanged; no read added;rev3=census-ruling-replica-console-account;basis=when operator config CARLOS_REPLICA_CONSOLE_ADDR (hub host.env, read once in enableTunnelsFromEnv, refused at load unless a literal loopback IP and port) is set, replica calls go to a proxy built only from that address, stamping the hub build as X-Carlos-Version; no route field is read, so records can neither choose, redirect nor disable the endpoint. Unset, the selection is byte-identical to main (accountless console.service local route). Witnesses TestReplicaConsoleConfiguredEndpoint (distinct destinations, injected and rewritten rows, endpoint down), TestReplicaConsoleUnconfiguredIgnoresAccountRows, TestParseReplicaConsoleAddr, TestReplicaConsoleAddrFromHostEnv. Same reads, no new read source, no write. registry:Account@332,registry:App@352,registry:Host@273,registry:Socket@450,registry:Unit@375,whole:Route@254 31e8a1bc5fd1903daed1e82189e214b46ba6348761f2300525788b6a90393f97 |
| +@coverage internal/edge c21d213f8c8d08e121493dc87a9211045ae20a45fc256b7dc672b8e6d09d24a1 |