pathmapcheck, routes listing, docs, rollout runbook (#186)
scripts/pathmapcheck is the read-only pre-roll check (spec §8 step 1): it lists every claim under domains/ with the store's own listing, reads each with domains.GetClaim, folds front and target homes from every edges file with edge.FoldedHome, and exits 1 when a target is homed on another box or on none. A listing error, an unreadable or undecodable claim, and an unreadable or unparseable edges file all fail the verdict, named by key. TestRoutesListingOmitsOrphanPathRows pins the routes listing's host filter now that rows exist for hosts on other boxes (plan R1 P3-12). Comments in apiroutes.go, assetpolicy.go and domains_cli.go, the domains paths footer, and the 09-02 spec's §6 and §11 point at the new design. The rollout runbook covers the pre-roll check on dev and flagship, the full roll, the hub scaffold's one-minute version poll and the uncached cross-box pages.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+635
−8
docs/superpowers/runbooks/path-routes-across-boxes-rollout.md+102 −0docs/superpowers/specs/2026-09-02-path-based-routing-design.md+15 −0domains_cli.go+11 −6internal/console/apiroutes.go+4 −1internal/console/apiroutes_paths_test.go+53 −0internal/host/assetpolicy.go+4 −1scripts/pathmapcheck/main.go+233 −0scripts/pathmapcheck/main_test.go+213 −0
diff --git a/docs/superpowers/runbooks/path-routes-across-boxes-rollout.md b/docs/superpowers/runbooks/path-routes-across-boxes-rollout.md| new file mode 100644 |
| index 000000000..d4a9a73b7 |
| --- /dev/null |
| +++ b/docs/superpowers/runbooks/path-routes-across-boxes-rollout.md |
| @@ -0,0 +1,102 @@ |
| +# Path routes across boxes: rollout |
| + |
| +This runbook rolls the release that lets a path prefix reach a target on |
| +another box (#186, spec |
| +`docs/superpowers/specs/2026-11-19-path-routes-across-boxes-design.md`). |
| +There is no feature switch and no capability gate (spec §8). Safety comes |
| +from doing three things in order, on each deployment, dev first: |
| + |
| +1. Check that no published map already names a cross-box target. |
| +2. Roll the release to every box. |
| +3. Only then let anything publish a cross-box map. |
| + |
| +## What changes when the release lands |
| + |
| +Today a prefix whose target is homed on another box than the hostname |
| +derives no row anywhere, and its requests reach the hostname's own route. |
| +After the roll, that same map starts sending those requests to the target. |
| +Nobody is expected to rely on the old fall-through (`ti.to` is not behind |
| +the edge, and the Rastrillo hub scaffold has not shipped), and step 1 |
| +proves it. |
| + |
| +## Step 1: the pre-roll check (read-only) |
| + |
| +Run `scripts/pathmapcheck` from the release checkout against each control |
| +bucket. It only lists and reads objects; it never writes. |
| + |
| +```sh |
| +AWS_PROFILE=kass go run ./scripts/pathmapcheck -bucket carlos-dev-640012953448 -region eu-west-1 |
| +AWS_PROFILE=keymail go run ./scripts/pathmapcheck -bucket carlos-flagship-271376211898 -region eu-west-1 |
| +``` |
| + |
| +It reads every published claim under `domains/`, keeps the ones with a |
| +path map, and works out each front's and each target's home box from every |
| +edges file under `control/edges/`, the way an edge does. It prints one line |
| +per map: |
| + |
| +``` |
| +hub.example.com (box-a): 3 prefixes, 1 cross-box, e.g. /paulca -> t1.example.com (box-b) |
| +``` |
| + |
| +and ends with `OK` (exit 0) or `FAIL` (exit 1). `FAIL` means at least one |
| +of these: |
| + |
| +- a target is homed on a different box than its front, or on no box |
| + (`no home`); |
| +- a claim or edges file could not be listed, read or decoded. Its key is |
| + printed after `unreadable:`. An unreadable claim might be a cross-box |
| + map, so the check does not pass over it. |
| + |
| +Record both outputs on the branch and on #186. On `FAIL`, stop: tell Paul |
| +which maps and keys it named, and wait for his go before rolling. A |
| +transient read error is worth one re-run first; a second `FAIL` goes to |
| +Paul. |
| + |
| +A static site that every box serves can show up as cross-box, because the |
| +fold picks one box to dial. That is a false alarm, but it is still Paul's |
| +call to wave it through. |
| + |
| +Any other deployment that takes this release (Tito's, for example) needs |
| +the same check against its own bucket before it upgrades. Tell its |
| +operator. |
| + |
| +## Step 2: the full roll |
| + |
| +Roll the release by the release and roll recipes, with Paul's GO, dev first |
| +and then flagship. The roll is finished only when every box of the |
| +deployment runs the new version. A box left on an old version can answer a |
| +cross-box hop for a region HA target with a 503 (spec §8, accepted with the |
| +cut of the capability gate), so a pinned or forgotten box must be rolled or |
| +reported. |
| + |
| +Between steps 1 and 3 nothing names a cross-box target, so mixed versions |
| +during the roll cannot route a request across boxes. |
| + |
| +After the dev roll, run the dev witness (spec §9): a hub on one dev box and |
| +a tenant on another; `curl https://<hub>/<slug>/api/version` through each |
| +box's ingress returns the tenant's `X-Carlos-Version`. Record it on #186. |
| + |
| +## Step 3: cross-box maps may be published |
| + |
| +Only after step 2 has finished on a deployment may anything publish a map |
| +with a cross-box target there. The first publisher is the Rastrillo hub |
| +scaffold. Tell the Rastrillo session when a deployment is ready, and make |
| +sure its docs say it needs this release on every box. |
| + |
| +The scaffold's version poll (its step 6) must allow at least a minute |
| +before it marks a tenant `failed`. A brand-new tenant on another box is |
| +reachable through the hub only once its box has republished its edges file |
| +and the hub's box has polled it: up to about 35 seconds (spec §6). |
| + |
| +## What behaves differently across boxes |
| + |
| +- **No response cache for cross-box tenant pages** (spec §7.2, rev 5.1). |
| + A request that reaches a target on a box that does not serve the front is |
| + never looked up in or stored to the response cache. Every cacheable page |
| + of a tenant reached across boxes reaches the app, so a hibernating tenant |
| + wakes for it. Same-box path routes cache exactly as before, and purge is |
| + unchanged. |
| +- **The password gate** refuses a login POST whose `next` path selects a |
| + target that is not a local target route (spec §7.3). |
| +- **Door calls** never select across boxes (spec §7.4). |
| +- **Regional names** never select by path (spec §7.1). |
diff --git a/docs/superpowers/specs/2026-09-02-path-based-routing-design.md b/docs/superpowers/specs/2026-09-02-path-based-routing-design.md| index c20c1375c..97d104f1c 100644 |
| --- a/docs/superpowers/specs/2026-09-02-path-based-routing-design.md |
| +++ b/docs/superpowers/specs/2026-09-02-path-based-routing-design.md |
| @@ -375,6 +375,14 @@ elsewhere" because the remedies differ. Making cross-box path targets actually |
| work needs a backhaul equivalent for path targets, which is a design change, |
| not a fix. See §11. |
| +> **Lifted (2026-11-20, #186).** The design change this paragraph asks for is |
| +> `2026-11-19-path-routes-across-boxes-design.md`. Every box now derives a |
| +> path-routed host's rows, a front box hops a request to a target homed on |
| +> another box, and a box serving a target whose front it does not serve |
| +> selects it locally. A local target keeps every check above. The new spec |
| +> has the eligibility rules (§5.1, §5.3), the one-hop rule (§5.4) and the |
| +> rollout ordering (§8). |
| + |
| **Attribution: a path hit returns the TARGET's upstream entry wholesale.** |
| Everything downstream that reads the resolved entry reads the target's route, |
| not the requested hostname's. Four consequences, three of them correct by |
| @@ -579,6 +587,13 @@ any of it. |
| target route are homed on the same box, and decide whether making it work |
| across boxes is wanted (a design change, not a fix). |
| + **Resolved (2026-11-20, #186):** wanted, and designed and built in |
| + `2026-11-19-path-routes-across-boxes-design.md`. A target on another box |
| + is reached by one hop from the front's box. Before the release that |
| + carries it rolls, `scripts/pathmapcheck` lists every published map whose |
| + target is homed on another box than its front |
| + (`docs/superpowers/runbooks/path-routes-across-boxes-rollout.md`). |
| + |
| **Resolved: a path-routed app purging its own cache.** Shipped as a known gap |
| and closed straight after, once it became clear the platform might carry more |
| than a two-app migration. |
diff --git a/domains_cli.go b/domains_cli.go| index 9aacb916c..7c672a8a1 100644 |
| --- a/domains_cli.go |
| +++ b/domains_cli.go |
| @@ -463,11 +463,16 @@ func printDomainClaims(recs []apiDomainRecord) { |
| // path segments its traffic is split by, and the route each is served by. |
| // |
| // This is desired state, and the distinction from `carlos routes` is the whole |
| -// reason the verb exists. `carlos routes` prints the route_paths rows a box |
| -// derived, and the reconcile pass silently drops any entry whose target is not |
| -// a route of the same account on that box. So a prefix that appears HERE and |
| -// not there is a flip the platform published and the deployment refused — |
| -// which is the state an operator debugs, and which no other surface shows. The |
| +// reason the verb exists. `carlos routes` prints the route_paths rows the |
| +// console's box derived. Since path routes across boxes (#186, spec |
| +// 2026-11-19 §5.1) every box derives a row for every prefix of every |
| +// path-routed host, whether or not it serves the host or the target, except |
| +// that the reconcile pass drops an entry whose target IS a route on that box |
| +// but is not ready or belongs to another account. So a prefix that appears |
| +// HERE and not there is a flip the platform published and the box refused — |
| +// which is the state an operator debugs, and which no other surface shows. A |
| +// row whose target lives on another box is not proof that it serves: the |
| +// edge checks the target's published eligibility on each request (§5.3). The |
| // footer says so rather than leaving the reader to know it. |
| // |
| // READ-ONLY, and there is deliberately no sibling that writes: the platform |
| @@ -512,7 +517,7 @@ func runDomainsPaths(args []string) { |
| fmt.Fprintf(w, "/%s\t%s\n", p, paths[p]) |
| } |
| w.Flush() |
| - mutedf(stdout, "(published desired state; \"carlos routes --app %s\" shows which of these a box is actually serving)", app2) |
| + mutedf(stdout, "(published desired state; \"carlos routes --app %s\" shows which of these a box accepted)", app2) |
| } |
| // runDomainsPlacements prints one hostname's placements (#447): per region, |
diff --git a/internal/console/apiroutes.go b/internal/console/apiroutes.go| index 30a8bbfb5..5c7617b1f 100644 |
| --- a/internal/console/apiroutes.go |
| +++ b/internal/console/apiroutes.go |
| @@ -205,7 +205,10 @@ func (s *Server) handleAPIRoutes(w http.ResponseWriter, r *http.Request) { |
| // hostname is not in the answer above: the reconcile pass derives |
| // every path-routed host's rows on every box (path routes across |
| // boxes, spec §5.1), including hosts this console's box does not |
| - // serve, so a row's host need not be a route here. |
| + // serve, so a row's host need not be a route here. Those rows |
| + // carry the caller's own stamps, so the tenancy compare |
| + // passes them and only this filter keeps them out |
| + // (TestRoutesListingOmitsOrphanPathRows). |
| // release.RouteAccount, matching the routes filter above rather |
| // than comparing the column raw. The warning there applies here |
| // word for word: a second, private notion of "whose row is this" |
diff --git a/internal/console/apiroutes_paths_test.go b/internal/console/apiroutes_paths_test.go| new file mode 100644 |
| index 000000000..a2da15331 |
| --- /dev/null |
| +++ b/internal/console/apiroutes_paths_test.go |
| @@ -0,0 +1,53 @@ |
| +package console |
| + |
| +import ( |
| + "net/http" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// TestRoutesListingOmitsOrphanPathRows: since path routes across boxes |
| +// (#186, spec §5.1) the reconcile pass derives a path-routed host's rows on |
| +// every box, including boxes that do not serve the host. Such a row is |
| +// stamped to the caller's own account and app, so the tenancy compare lets |
| +// it through; only the host filter keeps a prefix off a listing whose |
| +// routes do not include its host. The listing shows prefixes under the |
| +// routes it answers with and nothing else (plan R1 P3-12). |
| +func TestRoutesListingOmitsOrphanPathRows(t *testing.T) { |
| + fx, token := routesFixture(t, "code-routes-orphan-paths") |
| + reg, err := registry.Open(fx.regPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, p := range []registry.RoutePath{ |
| + // A host this box serves: listed. |
| + {Host: "connect.go.tito.io", Prefix: "paulca", Target: "router-a.go.tito.io", Account: "bab", App: "titogo"}, |
| + // A host homed on another box, derived here with its targets |
| + // elsewhere too: same account, same app, no route on this box. |
| + {Host: "hub.go.tito.io", Prefix: "paulca", Target: "tenant-b.go.tito.io", Account: "bab", App: "titogo"}, |
| + {Host: "hub.go.tito.io", Prefix: "bob", Target: "router-a.go.tito.io", Account: "bab", App: "titogo"}, |
| + } { |
| + if err := reg.UpsertPath(p); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + status, out := getJSON(t, routesURL(fx, "titogo"), token) |
| + if status != http.StatusOK { |
| + t.Fatalf("GET routes = %d %v", status, out) |
| + } |
| + if _, listed := hostsOf(t, out)["hub.go.tito.io"]; listed { |
| + t.Fatalf("routes = %v, hub.go.tito.io has no route on this box", out["routes"]) |
| + } |
| + rows, ok := out["paths"].([]any) |
| + if !ok { |
| + t.Fatalf("response has no paths array: %v", out) |
| + } |
| + if len(rows) != 1 { |
| + t.Fatalf("paths = %v, want only connect.go.tito.io's row", out["paths"]) |
| + } |
| + m, _ := rows[0].(map[string]any) |
| + if str(t, m, "host") != "connect.go.tito.io" || str(t, m, "prefix") != "paulca" { |
| + t.Fatalf("path row = %v, want connect.go.tito.io /paulca", m) |
| + } |
| +} |
diff --git a/internal/host/assetpolicy.go b/internal/host/assetpolicy.go| index 9f56a235a..1b02b836f 100644 |
| --- a/internal/host/assetpolicy.go |
| +++ b/internal/host/assetpolicy.go |
| @@ -214,7 +214,10 @@ type assetInputs struct { |
| byHost map[string]registry.Route |
| // aliasOf maps each exact alias name (never "*.") to its primary. |
| aliasOf map[string]string |
| - // pathHosts holds every hostname with a route_paths row. |
| + // pathHosts holds every hostname with a route_paths row. Since path |
| + // routes across boxes (#186) every box holds the rows of every |
| + // path-routed host, served here or not, so the refusal below is the same |
| + // answer on every box. |
| pathHosts map[string]bool |
| // owners is servedOwners over the served rows. |
| owners map[string]*hostOwner |
diff --git a/scripts/pathmapcheck/main.go b/scripts/pathmapcheck/main.go| new file mode 100644 |
| index 000000000..5b7a460d8 |
| --- /dev/null |
| +++ b/scripts/pathmapcheck/main.go |
| @@ -0,0 +1,233 @@ |
| +// Command pathmapcheck is the read-only pre-roll check for path routes |
| +// across boxes (#186, spec 2026-11-19 §8 step 1). It lists every published |
| +// path map in a deployment's control bucket and flags each target whose |
| +// home box differs from its front's, or that no box homes. Exit 0: no map |
| +// names such a target. Exit 1: one does, or something it had to read could |
| +// not be read. |
| +// |
| +// AWS_PROFILE=kass go run ./scripts/pathmapcheck -bucket <control bucket> -region <region> |
| +// |
| +// It reads published claims, not derived route_paths rows: until the roll, |
| +// no box derives a row for a target homed elsewhere, so the rows cannot |
| +// show the maps this check exists to find. It fails closed: a listing |
| +// error, a claim or edges file it cannot read, or one it cannot decode is |
| +// named and fails the verdict (domains.ListClaims skips those, which is why |
| +// it is not used here). It only lists and gets; it never writes. |
| +package main |
| + |
| +import ( |
| + "context" |
| + "flag" |
| + "fmt" |
| + "io" |
| + "os" |
| + "sort" |
| + "strings" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/edge" |
| + "github.com/carlosframework/platform/internal/edgesfile" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// examples is how many flagged prefixes one map's line shows. |
| +const examples = 3 |
| + |
| +// flagged is one prefix whose target is not on its front's box. |
| +type flagged struct { |
| + prefix, target, home string // home "" means no box homes target |
| +} |
| + |
| +// pathMap is one published map as the check sees it. |
| +type pathMap struct { |
| + front, home string // home "" means no box homes the front |
| + prefixes int |
| + cross []flagged // sorted by prefix |
| +} |
| + |
| +// report is the check's whole answer. |
| +type report struct { |
| + maps []pathMap // sorted by front |
| + // unread names every claim or edges file that could not be read or |
| + // decoded, with why. |
| + unread []string |
| +} |
| + |
| +// ok is the verdict: every map readable, every target on its front's box. |
| +func (r report) ok() bool { |
| + if len(r.unread) > 0 { |
| + return false |
| + } |
| + for _, m := range r.maps { |
| + if len(m.cross) > 0 { |
| + return false |
| + } |
| + } |
| + return true |
| +} |
| + |
| +func homeLabel(box string) string { |
| + if box == "" { |
| + return "no home" |
| + } |
| + return box |
| +} |
| + |
| +func (r report) write(w io.Writer) { |
| + crossMaps := 0 |
| + for _, m := range r.maps { |
| + fmt.Fprintf(w, "%s (%s): %d prefixes, %d cross-box", m.front, homeLabel(m.home), m.prefixes, len(m.cross)) |
| + if len(m.cross) > 0 { |
| + crossMaps++ |
| + var eg []string |
| + for _, c := range m.cross[:min(examples, len(m.cross))] { |
| + eg = append(eg, fmt.Sprintf("/%s -> %s (%s)", c.prefix, c.target, homeLabel(c.home))) |
| + } |
| + fmt.Fprintf(w, ", e.g. %s", strings.Join(eg, ", ")) |
| + } |
| + fmt.Fprintln(w) |
| + } |
| + for _, u := range r.unread { |
| + fmt.Fprintf(w, "unreadable: %s\n", u) |
| + } |
| + if r.ok() { |
| + fmt.Fprintf(w, "OK: %d path map(s), every target homed on its front's box\n", len(r.maps)) |
| + return |
| + } |
| + fmt.Fprintf(w, "FAIL: %d of %d path map(s) name a target on another box or on none; %d object(s) unreadable\n", crossMaps, len(r.maps), len(r.unread)) |
| +} |
| + |
| +// check reads every claim and every edges file in s and reports each path |
| +// map. A listing error is returned; a single unreadable object is recorded |
| +// in the report and fails its verdict. |
| +func check(ctx context.Context, s release.Store) (report, error) { |
| + var r report |
| + claims, err := readClaims(ctx, s, &r) |
| + if err != nil { |
| + return report{}, err |
| + } |
| + bodies, err := readEdges(ctx, s, &r) |
| + if err != nil { |
| + return report{}, err |
| + } |
| + homes := map[string]string{} |
| + homeOf := func(host string) (string, error) { |
| + if box, seen := homes[host]; seen { |
| + return box, nil |
| + } |
| + box, _, err := edge.FoldedHome(bodies, "", host) |
| + if err != nil { |
| + return "", err |
| + } |
| + homes[host] = box |
| + return box, nil |
| + } |
| + for _, c := range claims { |
| + home, err := homeOf(c.Host) |
| + if err != nil { |
| + return report{}, err |
| + } |
| + m := pathMap{front: c.Host, home: home, prefixes: len(c.Paths)} |
| + for prefix, target := range c.Paths { |
| + th, err := homeOf(target) |
| + if err != nil { |
| + return report{}, err |
| + } |
| + if home == "" || th != home { |
| + m.cross = append(m.cross, flagged{prefix: prefix, target: target, home: th}) |
| + } |
| + } |
| + sort.Slice(m.cross, func(i, j int) bool { return m.cross[i].prefix < m.cross[j].prefix }) |
| + r.maps = append(r.maps, m) |
| + } |
| + sort.Slice(r.maps, func(i, j int) bool { return r.maps[i].front < r.maps[j].front }) |
| + sort.Strings(r.unread) |
| + return r, nil |
| +} |
| + |
| +// readClaims returns every readable claim with a path map, recording each |
| +// key it could not read or decode. |
| +func readClaims(ctx context.Context, s release.Store, r *report) ([]domains.Claim, error) { |
| + keys, err := s.List(ctx, domains.ClaimsPrefix) |
| + if err != nil { |
| + return nil, fmt.Errorf("list %s: %w", domains.ClaimsPrefix, err) |
| + } |
| + var out []domains.Claim |
| + for _, key := range keys { |
| + name, _ := strings.CutPrefix(key, domains.ClaimsPrefix) |
| + host, isClaim := strings.CutSuffix(name, ".json") |
| + if !isClaim || host == "" || strings.Contains(host, "/") { |
| + r.unread = append(r.unread, key+": not a claim key") |
| + continue |
| + } |
| + c, err := domains.GetClaim(ctx, s, host) |
| + if err != nil { |
| + r.unread = append(r.unread, fmt.Sprintf("%s: %v", key, err)) |
| + continue |
| + } |
| + if len(c.Paths) > 0 { |
| + out = append(out, *c) |
| + } |
| + } |
| + return out, nil |
| +} |
| + |
| +// readEdges returns every edges file that reads and parses, keyed by bucket |
| +// key, recording each one that does not. Keys are filtered as the edge's |
| +// home map filters them: flat, <box>.json. |
| +func readEdges(ctx context.Context, s release.Store, r *report) (map[string][]byte, error) { |
| + keys, err := s.List(ctx, edgesfile.Prefix) |
| + if err != nil { |
| + return nil, fmt.Errorf("list %s: %w", edgesfile.Prefix, err) |
| + } |
| + bodies := map[string][]byte{} |
| + for _, key := range keys { |
| + name, _ := strings.CutPrefix(key, edgesfile.Prefix) |
| + if !strings.HasSuffix(name, ".json") || strings.Contains(name, "/") { |
| + continue |
| + } |
| + body, _, err := s.Get(ctx, key) |
| + if err == nil { |
| + // FoldedHome parses before it folds, so one file alone is the |
| + // parse check, and the error names the key. |
| + _, _, err = edge.FoldedHome(map[string][]byte{key: body}, "", "") |
| + } |
| + if err != nil { |
| + r.unread = append(r.unread, fmt.Sprintf("%s: %v", key, err)) |
| + continue |
| + } |
| + bodies[key] = body |
| + } |
| + return bodies, nil |
| +} |
| + |
| +// run checks s, writes the report to w and answers the exit status. |
| +func run(ctx context.Context, s release.Store, w io.Writer) int { |
| + r, err := check(ctx, s) |
| + if err != nil { |
| + fmt.Fprintf(w, "FAIL: %v\n", err) |
| + return 1 |
| + } |
| + r.write(w) |
| + if !r.ok() { |
| + return 1 |
| + } |
| + return 0 |
| +} |
| + |
| +func main() { |
| + bucket := flag.String("bucket", "", "the deployment's control bucket") |
| + region := flag.String("region", "", "the bucket's AWS region (default: the profile's)") |
| + flag.Parse() |
| + if *bucket == "" || flag.NArg() > 0 { |
| + fmt.Fprintln(os.Stderr, "usage: pathmapcheck -bucket <control bucket> [-region <region>]") |
| + os.Exit(2) |
| + } |
| + ctx := context.Background() |
| + s, err := release.NewS3StoreIn(ctx, *bucket, *region) |
| + if err != nil { |
| + fmt.Fprintf(os.Stderr, "pathmapcheck: open %s: %v\n", *bucket, err) |
| + os.Exit(1) |
| + } |
| + os.Exit(run(ctx, s, os.Stdout)) |
| +} |
diff --git a/scripts/pathmapcheck/main_test.go b/scripts/pathmapcheck/main_test.go| new file mode 100644 |
| index 000000000..a09e6d57b |
| --- /dev/null |
| +++ b/scripts/pathmapcheck/main_test.go |
| @@ -0,0 +1,213 @@ |
| +package main |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "fmt" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/edgesfile" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// failingStore is a store whose Get fails for the keys in fail. |
| +type failingStore struct { |
| + release.Store |
| + fail map[string]error |
| +} |
| + |
| +func (s failingStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + if err, ok := s.fail[key]; ok { |
| + return nil, "", err |
| + } |
| + return s.Store.Get(ctx, key) |
| +} |
| + |
| +// fixture is a bucket holding claims and edges files. |
| +type fixture struct { |
| + t *testing.T |
| + s release.Store |
| +} |
| + |
| +func newFixture(t *testing.T) *fixture { |
| + return &fixture{t: t, s: release.NewDirStore(t.TempDir())} |
| +} |
| + |
| +func (f *fixture) put(key string, body []byte) { |
| + f.t.Helper() |
| + if _, err := f.s.Put(context.Background(), key, body, ""); err != nil { |
| + f.t.Fatal(err) |
| + } |
| +} |
| + |
| +// claim publishes host's claim with paths (nil for a host with no map). |
| +func (f *fixture) claim(host string, paths map[string]string) { |
| + f.t.Helper() |
| + body, err := json.Marshal(domains.Claim{Schema: 1, Host: host, Account: "bab", App: "hub", Route: host, Paths: paths}) |
| + if err != nil { |
| + f.t.Fatal(err) |
| + } |
| + f.put(domains.ClaimKey(host), body) |
| +} |
| + |
| +// box publishes box's edges file homing hosts. |
| +func (f *fixture) box(box string, n int, hosts ...string) { |
| + f.t.Helper() |
| + entries := map[string]any{} |
| + for _, h := range hosts { |
| + entries[h] = map[string]any{"role": "home", "account": "bab", "app": "hub"} |
| + } |
| + body, err := json.Marshal(map[string]any{"v": 1, "box": box, "eip": fmt.Sprintf("192.0.2.%d", n), "hosts": entries}) |
| + if err != nil { |
| + f.t.Fatal(err) |
| + } |
| + f.put(edgesfile.Key(box), body) |
| +} |
| + |
| +func (f *fixture) run(s release.Store) (int, string) { |
| + f.t.Helper() |
| + var out bytes.Buffer |
| + code := run(context.Background(), s, &out) |
| + return code, out.String() |
| +} |
| + |
| +// TestPathMapCheckFindsAllRemoteMap: a map whose every target is homed on |
| +// another box is the case the roll must not meet unannounced. No box derives |
| +// a row for it today (the 09-02 rule), so only the published claim shows it. |
| +func TestPathMapCheckFindsAllRemoteMap(t *testing.T) { |
| + f := newFixture(t) |
| + f.claim("hub.example.com", map[string]string{"paulca": "t1.example.com", "bob": "t2.example.com"}) |
| + f.claim("plain.example.com", nil) |
| + f.box("box-a", 1, "hub.example.com", "plain.example.com") |
| + f.box("box-b", 2, "t1.example.com", "t2.example.com") |
| + code, out := f.run(f.s) |
| + if code != 1 { |
| + t.Fatalf("exit %d, want 1:\n%s", code, out) |
| + } |
| + for _, want := range []string{"hub.example.com (box-a): 2 prefixes, 2 cross-box", "/bob -> t2.example.com (box-b)", "/paulca -> t1.example.com (box-b)", "FAIL"} { |
| + if !strings.Contains(out, want) { |
| + t.Fatalf("output lacks %q:\n%s", want, out) |
| + } |
| + } |
| + if strings.Contains(out, "plain.example.com") { |
| + t.Fatalf("a claim with no map is listed:\n%s", out) |
| + } |
| +} |
| + |
| +// TestPathMapCheckSameBoxMapClean: every target on the front's box passes, |
| +// and the map is still listed. |
| +func TestPathMapCheckSameBoxMapClean(t *testing.T) { |
| + f := newFixture(t) |
| + f.claim("hub.example.com", map[string]string{"paulca": "t1.example.com", "bob": "t2.example.com"}) |
| + f.box("box-a", 1, "hub.example.com", "t1.example.com", "t2.example.com") |
| + f.box("box-b", 2, "other.example.com") |
| + code, out := f.run(f.s) |
| + if code != 0 { |
| + t.Fatalf("exit %d, want 0:\n%s", code, out) |
| + } |
| + if !strings.Contains(out, "hub.example.com (box-a): 2 prefixes, 0 cross-box") || !strings.Contains(out, "OK") { |
| + t.Fatalf("output:\n%s", out) |
| + } |
| +} |
| + |
| +// TestPathMapCheckUnknownTargetReported: a target no box homes is not |
| +// known to be on the front's box, so it fails like a cross-box one. |
| +func TestPathMapCheckUnknownTargetReported(t *testing.T) { |
| + f := newFixture(t) |
| + f.claim("hub.example.com", map[string]string{"paulca": "t1.example.com", "ghost": "nowhere.example.com"}) |
| + // A front no box homes, pointing at a target no box homes: two |
| + // unknowns are not one box. |
| + f.claim("lost.example.com", map[string]string{"x": "gone.example.com"}) |
| + f.box("box-a", 1, "hub.example.com", "t1.example.com") |
| + code, out := f.run(f.s) |
| + if code != 1 { |
| + t.Fatalf("exit %d, want 1:\n%s", code, out) |
| + } |
| + for _, want := range []string{ |
| + "hub.example.com (box-a): 2 prefixes, 1 cross-box, e.g. /ghost -> nowhere.example.com (no home)", |
| + "lost.example.com (no home): 1 prefixes, 1 cross-box, e.g. /x -> gone.example.com (no home)", |
| + } { |
| + if !strings.Contains(out, want) { |
| + t.Fatalf("output lacks %q:\n%s", want, out) |
| + } |
| + } |
| +} |
| + |
| +// TestPathMapCheckFailedClaimReadFails: a claim the bucket would not hand |
| +// over may be a cross-box map, so the verdict fails and names its key. |
| +func TestPathMapCheckFailedClaimReadFails(t *testing.T) { |
| + f := newFixture(t) |
| + f.claim("hub.example.com", map[string]string{"paulca": "t1.example.com"}) |
| + f.claim("broken.example.com", map[string]string{"x": "t9.example.com"}) |
| + f.box("box-a", 1, "hub.example.com", "t1.example.com") |
| + s := failingStore{Store: f.s, fail: map[string]error{domains.ClaimKey("broken.example.com"): errors.New("access denied")}} |
| + code, out := f.run(s) |
| + if code != 1 { |
| + t.Fatalf("exit %d, want 1:\n%s", code, out) |
| + } |
| + if !strings.Contains(out, "domains/broken.example.com.json") || !strings.Contains(out, "access denied") { |
| + t.Fatalf("output does not name the unreadable claim:\n%s", out) |
| + } |
| + if !strings.Contains(out, "hub.example.com (box-a): 1 prefixes, 0 cross-box") { |
| + t.Fatalf("the readable claims are not reported:\n%s", out) |
| + } |
| +} |
| + |
| +// TestPathMapCheckMalformedClaimFails: unparseable bytes and a schema this |
| +// binary does not understand both fail closed, each named. |
| +func TestPathMapCheckMalformedClaimFails(t *testing.T) { |
| + f := newFixture(t) |
| + f.claim("hub.example.com", map[string]string{"paulca": "t1.example.com"}) |
| + f.put(domains.ClaimKey("garbled.example.com"), []byte("{not json")) |
| + f.put(domains.ClaimKey("future.example.com"), []byte(`{"schema":9,"host":"future.example.com"}`)) |
| + f.box("box-a", 1, "hub.example.com", "t1.example.com") |
| + code, out := f.run(f.s) |
| + if code != 1 { |
| + t.Fatalf("exit %d, want 1:\n%s", code, out) |
| + } |
| + for _, key := range []string{"domains/garbled.example.com.json", "domains/future.example.com.json"} { |
| + if !strings.Contains(out, key) { |
| + t.Fatalf("output does not name %s:\n%s", key, out) |
| + } |
| + } |
| +} |
| + |
| +// TestPathMapCheckBadEdgesFileFails: homes come from every edges file, so |
| +// one that cannot be read or parsed fails the verdict too, named. |
| +func TestPathMapCheckBadEdgesFileFails(t *testing.T) { |
| + f := newFixture(t) |
| + f.claim("hub.example.com", map[string]string{"paulca": "t1.example.com"}) |
| + f.box("box-a", 1, "hub.example.com", "t1.example.com") |
| + f.put(edgesfile.Key("box-b"), []byte(`{"box":"box-b"}`)) |
| + f.box("box-c", 3, "t2.example.com") |
| + s := failingStore{Store: f.s, fail: map[string]error{edgesfile.Key("box-c"): errors.New("slow down")}} |
| + code, out := f.run(s) |
| + if code != 1 { |
| + t.Fatalf("exit %d, want 1:\n%s", code, out) |
| + } |
| + for _, key := range []string{edgesfile.Key("box-b"), edgesfile.Key("box-c")} { |
| + if !strings.Contains(out, key) { |
| + t.Fatalf("output does not name %s:\n%s", key, out) |
| + } |
| + } |
| +} |
| + |
| +// listFailStore fails every listing. |
| +type listFailStore struct{ release.Store } |
| + |
| +func (listFailStore) List(context.Context, string) ([]string, error) { |
| + return nil, errors.New("list refused") |
| +} |
| + |
| +// TestPathMapCheckListingErrorFails: no inventory, no verdict. |
| +func TestPathMapCheckListingErrorFails(t *testing.T) { |
| + f := newFixture(t) |
| + code, out := f.run(listFailStore{f.s}) |
| + if code != 1 || !strings.Contains(out, "list refused") { |
| + t.Fatalf("exit %d, want 1 naming the listing error:\n%s", code, out) |
| + } |
| +} |