Docs: GET /api/team readiness probe
Unauthenticated GET /api/team answers {team_mode:false} with team mode off, and {team_mode, consumer, roster: ok|stale|unknown} with it on - no account data, no-store, no cookie, 405 for other methods - per Home team-members.md s7, for Alta's step-11 readiness check. Roster state comes from the roster cache's Check on a placeholder person plus a since-boot definite-answer flag set by the one shared check helper; a probe may trigger a roster refresh within the cache's own 5 s / 30 s bounds, so Alta's polling pulls the first roster during Home's verify window (ruled acceptable). Reviewed by Claude Fable 5.1 (claude-fable-5-1), discussion 99, APPROVED; CI green at 1b3c291.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Squashed from api-team-probe into main.
6 files changed,
+263
−2
internal/docs/app.go+3 −0internal/docs/errors_test.go+1 −0internal/docs/team_access.go+1 −1internal/docs/team_config.go+7 −1internal/docs/team_probe.go+90 −0internal/docs/team_probe_test.go+161 −0
CI passed — run details
diff --git a/internal/docs/app.go b/internal/docs/app.go| index 180f0fe..1518d7c 100644 |
| --- a/internal/docs/app.go |
| +++ b/internal/docs/app.go |
| @@ -636,6 +636,9 @@ func New(cfg Config) (*App, error) { |
| // sits above the RequireSession group rather than inside it with an |
| // exception carved into the middleware. |
| r.Get("/p/{token}", h.publicShow) |
| + // Alta's readiness probe (team_probe.go): unauthenticated, no account |
| + // data, and {"team_mode":false} on an instance not in team mode. |
| + r.Get(teamProbePath, h.teamProbe) |
| // Help → Fonts and Help → Colophon (colophon.go): public, like every |
| // Oficina app's open-source page; the door links the Colophon. |
| r.Get(fontsPath, h.helpFonts) |
diff --git a/internal/docs/errors_test.go b/internal/docs/errors_test.go| index 06bced7..37249d5 100644 |
| --- a/internal/docs/errors_test.go |
| +++ b/internal/docs/errors_test.go |
| @@ -260,6 +260,7 @@ func TestEveryPlainTextErrorIsClassified(t *testing.T) { |
| "stream.go:handlers.stream": {7, "live update stream"}, |
| "team_access.go:teamRuntime.admission": {1, "shared guard, identity missing: a wiring failure, not a page state"}, |
| "team_emission.go:writeGuardedBody": {1, "response already guarded"}, |
| + "team_probe.go:handlers.teamProbe": {1, "Alta's readiness probe JSON, fetched by a provisioner, never a page"}, |
| } |
| got := httpErrorSites(t) |
| var problems []string |
diff --git a/internal/docs/team_access.go b/internal/docs/team_access.go| index a3abbfa..7e6ce07 100644 |
| --- a/internal/docs/team_access.go |
| +++ b/internal/docs/team_access.go |
| @@ -56,7 +56,7 @@ func (s *Store) access(ctx context.Context, actor Actor) (*requestAccess, bool) |
| func (t *teamRuntime) resolveAccess(ctx context.Context, id requestIdentity) *requestAccess { |
| a := &requestAccess{runtime: t, actor: id.Actor, subject: id.HomeSubject, decision: roster.Decision{State: roster.NotMember}} |
| if id.HomeSubject != "" { |
| - a.decision = t.cache.Check(ctx, t.bindings.Issuer, t.bindings.Team, id.HomeSubject) |
| + a.decision = t.check(ctx, id.HomeSubject) |
| } |
| return a |
| } |
diff --git a/internal/docs/team_config.go b/internal/docs/team_config.go| index 33c2d6a..b99f120 100644 |
| --- a/internal/docs/team_config.go |
| +++ b/internal/docs/team_config.go |
| @@ -8,6 +8,7 @@ import ( |
| "encoding/base64" |
| "errors" |
| "net/http" |
| + "sync/atomic" |
| "amadan.net/oficina/home/handoff/seal" |
| "amadan.net/oficina/home/roster" |
| @@ -80,6 +81,8 @@ type teamRuntime struct { |
| principals *principalstore.Store |
| bindings roster.Bindings |
| db *sql.DB |
| + // rosterHeld: some check since boot had a complete roster (team_probe.go). |
| + rosterHeld atomic.Bool |
| } |
| func newTeamRuntime(ctx context.Context, settings *teamSettings, db *sql.DB, home *homeSettings) (*teamRuntime, error) { |
| @@ -107,7 +110,10 @@ func newTeamRuntime(ctx context.Context, settings *teamSettings, db *sql.DB, hom |
| } |
| runtime := &teamRuntime{cache: cache, bindings: b, db: db} |
| principals, e := principalstore.New(db, principalstore.Config{Issuer: b.Issuer, Team: b.Team, RequireMembership: func(ctx context.Context, request principalstore.MembershipRequest) error { |
| - d := cache.Check(ctx, request.Issuer, request.Team, request.Subject) |
| + if request.Issuer != b.Issuer || request.Team != b.Team { |
| + return principalstore.ErrUnavailable |
| + } |
| + d := runtime.check(ctx, request.Subject) |
| if d.State == roster.Member && d.Lease.Valid() { |
| return nil |
| } |
diff --git a/internal/docs/team_probe.go b/internal/docs/team_probe.go| new file mode 100644 |
| index 0000000..77a0167 |
| --- /dev/null |
| +++ b/internal/docs/team_probe.go |
| @@ -0,0 +1,90 @@ |
| +package docs |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "net/http" |
| + "strings" |
| + |
| + "amadan.net/oficina/home/roster" |
| + "amadan.net/rastrillo/rastrillo" |
| +) |
| + |
| +// teamProbePath is the readiness probe Alta polls at provisioning step 11, |
| +// before it activates this instance in Home's directory (Home |
| +// implementation/team-members.md §7; Alta provision/steps.go TeamProbe). |
| +// |
| +// It is unauthenticated and answers only {team_mode, consumer, roster}: |
| +// whether this instance runs in team mode, the consumer ID its verified |
| +// provisioning record names, and how its roster cache stands. No account |
| +// data — no subject, role, member count, address, team ID or issuer — and |
| +// nothing that differs with who is asking. With team mode off it answers |
| +// {"team_mode":false} and nothing else. |
| +const teamProbePath = "/api/team" |
| + |
| +type teamProbe struct { |
| + TeamMode bool `json:"team_mode"` |
| + Consumer string `json:"consumer,omitempty"` |
| + Roster string `json:"roster,omitempty"` |
| +} |
| + |
| +// rosterProbeSubject is a well-formed person ref the probe asks the cache |
| +// about. Its answer is never used, only whether there was one: Member or |
| +// NotMember both mean the cache holds a complete roster younger than |
| +// roster.MaximumAge, which is what "ok" claims. |
| +var rosterProbeSubject = "pr_" + strings.Repeat("2", 26) |
| + |
| +// check is the only way this runtime asks the roster cache, so that every |
| +// definite answer — a request's, a principal allocation's or the probe's — |
| +// records that a complete roster has been held since boot. |
| +func (t *teamRuntime) check(ctx context.Context, subject string) roster.Decision { |
| + d := t.cache.Check(ctx, t.bindings.Issuer, t.bindings.Team, subject) |
| + if d.State != roster.Unknown { |
| + t.rosterHeld.Store(true) |
| + } |
| + return d |
| +} |
| + |
| +// rosterState is the probe's roster field: |
| +// |
| +// - "ok": a complete roster, fetched within roster.MaximumAge, decides |
| +// standing now. |
| +// - "stale": one was held since this process started, but it has aged out |
| +// and could not be refreshed, so standing is Unknown. |
| +// - "unknown": no complete roster has been held since boot. |
| +// |
| +// Asking the cache starts a refresh when one is due and waits for it (at |
| +// most roster.FetchTimeout), exactly as a signed-in request does — which is |
| +// what lets Alta's polling pull the first roster while Home's verify window |
| +// is open. The cache's own single flight and RetryAfter backoff bound how |
| +// often an unauthenticated caller can make this instance ask the directory. |
| +func (t *teamRuntime) rosterState(ctx context.Context) string { |
| + if t.check(ctx, rosterProbeSubject).State != roster.Unknown { |
| + return "ok" |
| + } |
| + if t.rosterHeld.Load() { |
| + return "stale" |
| + } |
| + return "unknown" |
| +} |
| + |
| +func (h *handlers) teamProbe(w http.ResponseWriter, r *http.Request) { |
| + probe := teamProbe{} |
| + if team := h.store.team; team != nil { |
| + probe = teamProbe{TeamMode: true, Consumer: team.bindings.Consumer, Roster: team.rosterState(r.Context())} |
| + } |
| + body, err := json.Marshal(probe) |
| + if err != nil { |
| + h.logger.Error("docs: encode team probe", "err", err) |
| + http.Error(w, rastrillo.T(r, "docs.error.generic"), http.StatusInternalServerError) |
| + return |
| + } |
| + w.Header().Set("Content-Type", "application/json; charset=utf-8") |
| + // Readiness changes from one second to the next; a cached "ok" would |
| + // let Alta activate an instance whose roster has since gone stale. |
| + w.Header().Set("Cache-Control", "no-store") |
| + w.Header().Set("X-Content-Type-Options", "nosniff") |
| + if _, err = w.Write(body); err != nil { |
| + h.logger.Error("docs: write team probe", "err", err) |
| + } |
| +} |
diff --git a/internal/docs/team_probe_test.go b/internal/docs/team_probe_test.go| new file mode 100644 |
| index 0000000..d4605c4 |
| --- /dev/null |
| +++ b/internal/docs/team_probe_test.go |
| @@ -0,0 +1,161 @@ |
| +package docs |
| + |
| +import ( |
| + "bytes" |
| + "encoding/json" |
| + "net/http" |
| + "net/http/httptest" |
| + "reflect" |
| + "sort" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/home/roster" |
| +) |
| + |
| +// probeGet answers GET /api/team as Alta sees it: no cookie, no session. |
| +func probeGet(t *testing.T, h http.Handler) *httptest.ResponseRecorder { |
| + t.Helper() |
| + rec := httptest.NewRecorder() |
| + h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, bridgeOrigin+teamProbePath, nil)) |
| + return rec |
| +} |
| + |
| +// probeBody checks the transport half of every answer (200, JSON, no-store, |
| +// no cookie) and returns the body's members, so a test can assert the exact |
| +// key set rather than only the keys it knows about. |
| +func probeBody(t *testing.T, rec *httptest.ResponseRecorder) map[string]any { |
| + t.Helper() |
| + if rec.Code != http.StatusOK { |
| + t.Fatalf("status %d: %s", rec.Code, rec.Body) |
| + } |
| + if got := rec.Header().Get("Content-Type"); got != "application/json; charset=utf-8" { |
| + t.Fatalf("Content-Type %q", got) |
| + } |
| + if got := rec.Header().Get("Cache-Control"); got != "no-store" { |
| + t.Fatalf("Cache-Control %q, want no-store", got) |
| + } |
| + if c := rec.Result().Cookies(); len(c) != 0 { |
| + t.Fatalf("probe set cookies: %v", c) |
| + } |
| + var body map[string]any |
| + if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { |
| + t.Fatalf("not JSON: %q", rec.Body) |
| + } |
| + return body |
| +} |
| + |
| +func probeKeys(m map[string]any) []string { |
| + var out []string |
| + for k := range m { |
| + out = append(out, k) |
| + } |
| + sort.Strings(out) |
| + return out |
| +} |
| + |
| +func TestTeamProbeOffWithoutTeamMode(t *testing.T) { |
| + standalone := newSampleDocumentsInternalTestApp(t, "0") |
| + home := newRouterHomeFixture(t) |
| + for name, h := range map[string]http.Handler{"standalone": standalone.Handler(), "home without team": home.app.Handler()} { |
| + t.Run(name, func(t *testing.T) { |
| + rec := probeGet(t, h) |
| + probeBody(t, rec) |
| + if got := rec.Body.String(); got != `{"team_mode":false}` { |
| + t.Fatalf("body %s", got) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestTeamProbeShapeAndRosterStates(t *testing.T) { |
| + f := newTeamRouterFixture(t) |
| + h := f.app.Handler() |
| + want := func(roster string) { |
| + t.Helper() |
| + body := probeBody(t, probeGet(t, h)) |
| + if k := probeKeys(body); !reflect.DeepEqual(k, []string{"consumer", "roster", "team_mode"}) { |
| + t.Fatalf("keys %v", k) |
| + } |
| + if body["team_mode"] != true || body["consumer"] != "router-docs" || body["roster"] != roster { |
| + t.Fatalf("body %v, want roster %q", body, roster) |
| + } |
| + } |
| + members := f.directory.members |
| + |
| + // Booted, and the directory has never answered: nothing held since boot. |
| + f.directory.change(members, true) |
| + want("unknown") |
| + f.clock.step(roster.RetryAfter) |
| + want("unknown") |
| + |
| + // The probe's own poll pulls the first roster, as Alta's does during |
| + // Home's verify window. |
| + f.directory.change(members, false) |
| + f.clock.step(roster.RetryAfter) |
| + want("ok") |
| + f.clock.step(roster.MaximumAge - time.Second) |
| + f.directory.change(members, true) |
| + want("ok") // inside MaximumAge a failed refresh does not unseat it |
| + |
| + // Aged out with the directory still down: held once, not now. |
| + f.clock.step(2 * time.Second) |
| + want("stale") |
| + f.clock.step(roster.RetryAfter) |
| + want("stale") |
| + |
| + // And back. |
| + f.directory.change(members, false) |
| + f.clock.step(roster.RetryAfter) |
| + want("ok") |
| +} |
| + |
| +func TestTeamProbeCarriesNoAccountData(t *testing.T) { |
| + f := newTeamRouterFixture(t) |
| + anonymous := probeGet(t, f.app.Handler()) |
| + probeBody(t, anonymous) |
| + |
| + // A signed-in member asking gets the same bytes: nothing differs with |
| + // who is asking. |
| + b := f.browser() |
| + nonce := b.start(t, "/auth/start?return_to=%2F", "") |
| + routerRedirect(t, b.exchange(t, nonce, teamSubject('b'), time.Now()), "/") |
| + member := b.do("GET", teamProbePath, "") |
| + probeBody(t, member) |
| + if !bytes.Equal(member.Body.Bytes(), anonymous.Body.Bytes()) { |
| + t.Fatalf("member saw %s, anonymous %s", member.Body, anonymous.Body) |
| + } |
| + |
| + body := anonymous.Body.String() |
| + for _, secret := range []string{ |
| + "pr_", teamSubject('a'), teamSubject('b'), teamSubject('c'), rosterProbeSubject, |
| + "alice@example.com", "bob@example.com", "@", |
| + bridgeIssuer, "TeamA", "https://home.example", bridgeOrigin, |
| + "member", "owner", "epoch", "generation", "count", |
| + } { |
| + if strings.Contains(body, secret) { |
| + t.Fatalf("probe leaked %q: %s", secret, body) |
| + } |
| + } |
| +} |
| + |
| +func TestTeamProbeAnswersOnlyGet(t *testing.T) { |
| + f := newTeamRouterFixture(t) |
| + b := f.browser() |
| + for _, method := range []string{http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete} { |
| + t.Run(method, func(t *testing.T) { |
| + // Same-origin, so the CSRF guard passes it and the router decides. |
| + r := b.request(method, teamProbePath, "") |
| + r.Header.Set("Origin", bridgeOrigin) |
| + r.Header.Set("Sec-Fetch-Site", "same-origin") |
| + rec := b.serve(r) |
| + if rec.Code != http.StatusMethodNotAllowed || rec.Header().Get("Allow") != "GET" { |
| + t.Fatalf("status %d Allow %q", rec.Code, rec.Header().Get("Allow")) |
| + } |
| + if strings.Contains(rec.Body.String(), "team_mode") { |
| + t.Fatalf("answered the probe: %s", rec.Body) |
| + } |
| + }) |
| + } |
| +} |