| 1 | package operator |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/base64" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/home/internal/home/directorydata" |
| 11 | "amadan.net/oficina/home/roster" |
| 12 | family "amadan.net/rastrillo/rastrillo/crypto" |
| 13 | "github.com/go-json-experiment/json" |
| 14 | ) |
| 15 | |
| 16 | func adoptionRoute(r directorydata.Route) bool { |
| 17 | if r.Label == "oficina" { |
| 18 | return directorydata.CanonicalBinding(r.Label, r.App, r.ConsumerID, r.Origin) |
| 19 | } |
| 20 | return r.Origin == "https://"+r.Label+"."+r.App+".oficina.works" |
| 21 | } |
| 22 | |
| 23 | func adoptionDirectory(t *testing.T) *httpDirectory { |
| 24 | t.Helper() |
| 25 | f := newHTTPDirectory(t) |
| 26 | f.directory.policy.ValidateRoute = adoptionRoute |
| 27 | f.openPlan(t, func(p *Plan) { p.Kind, p.Apps, p.Label, p.DisplayName = "initialize", nil, "", "" }) |
| 28 | if _, e := f.run(t, "directory.initialize", func(b InputBase) any { |
| 29 | return InitializeInput{b, nil, token(10), "operator-attests-never-provisioned"} |
| 30 | }); e != nil { |
| 31 | t.Fatal(e) |
| 32 | } |
| 33 | *f.now = f.now.Add(WindowLifetime * time.Second) |
| 34 | f.openPlan(t, func(p *Plan) { p.Label, p.ExistingTeam = "oficina", "Uk" }) |
| 35 | return f |
| 36 | } |
| 37 | |
| 38 | func adoptionPending(t *testing.T) (*httpDirectory, VerificationInput) { |
| 39 | t.Helper() |
| 40 | f := adoptionDirectory(t) |
| 41 | if _, e := f.seedHTTP(t, 1, "oficina", "PRIVATE-TEAM-NAME-CANARY"); e != nil { |
| 42 | t.Fatal(e) |
| 43 | } |
| 44 | raw := f.input(t, "directory.provision-consumer", func(b InputBase) any { |
| 45 | return ProvisionInput{b, token(10), "Uk", "docs", "docs", "https://docs.oficina.works", 0} |
| 46 | }) |
| 47 | out, e := f.directory.ExecuteHTTP(context.Background(), raw) |
| 48 | if e != nil { |
| 49 | t.Fatal(e) |
| 50 | } |
| 51 | again, e := f.directory.ExecuteHTTP(context.Background(), raw) |
| 52 | if e != nil || !bytes.Equal(out, again) || countRows(t, f.db, "roster_consumers") != 1 { |
| 53 | t.Fatal("provision replay", e) |
| 54 | } |
| 55 | var result Result |
| 56 | if json.Unmarshal(out, &result) != nil { |
| 57 | t.Fatal("result") |
| 58 | } |
| 59 | f.bearer = result.Bearer |
| 60 | var digest []byte |
| 61 | if e = f.db.QueryRow("SELECT record_digest FROM directory_operator_consumer_records").Scan(&digest); e != nil { |
| 62 | t.Fatal(e) |
| 63 | } |
| 64 | return f, VerificationInput{Consumer: "docs", TeamSQID: "Uk", App: "docs", Audience: "https://docs.oficina.works", Generation: token(10), RecordSequence: 1, RecordDigest: base64.RawURLEncoding.EncodeToString(digest), Epoch: 2, Owner: "pr_aaaaaaaaaaaaaaaaaaaaaaaaaa", Issued: f.now.Unix(), Expires: f.now.Unix() + 600} |
| 65 | } |
| 66 | |
| 67 | func TestExistingPlanScopePersistenceAndExpiry(t *testing.T) { |
| 68 | f := newHTTPDirectory(t) |
| 69 | p, raw := f.signPlan(t, func(p *Plan) { p.Label, p.ExistingTeam = "oficina", "Uk" }) |
| 70 | verified, e := f.engine.verifier.VerifyPlan(raw, httpApps, *f.now) |
| 71 | if e != nil || verified.ExistingTeam != "Uk" { |
| 72 | t.Fatal(e) |
| 73 | } |
| 74 | if e = f.plans.Submit(context.Background(), p, "", *f.now); e != nil { |
| 75 | t.Fatal(e) |
| 76 | } |
| 77 | // A new reader, as after restart, must see the stored scope. |
| 78 | reopened, _ := NewPlanStore(f.db, "directory", f.engine.sealer) |
| 79 | v, e := reopened.Pending(context.Background(), *f.now) |
| 80 | if e != nil || v.ExistingTeam != "Uk" { |
| 81 | t.Fatal("scope not durable", e) |
| 82 | } |
| 83 | if _, e = reopened.Open(context.Background(), p.PlanID, *f.now, func(v PlanView) bool { return v.ExistingTeam == "Uk" }); e != nil { |
| 84 | t.Fatal(e) |
| 85 | } |
| 86 | *f.now = f.now.Add(WindowLifetime * time.Second) |
| 87 | if e = reopened.Sweep(context.Background(), *f.now); e != nil || countRows(t, f.db, "operator_plans") != 0 { |
| 88 | t.Fatal("scope survived expiry", e) |
| 89 | } |
| 90 | for name, edit := range map[string]func(*Plan){ |
| 91 | "label": func(p *Plan) { p.Label = "other" }, "retire": func(p *Plan) { p.Kind = "retire-apps"; p.DisplayName = "" }, |
| 92 | "initialize": func(p *Plan) { p.Kind = "initialize"; p.Label = ""; p.Apps = nil; p.DisplayName = "" }, |
| 93 | "invalid-sqid": func(p *Plan) { p.ExistingTeam = "oficina" }, "zero-sqid": func(p *Plan) { p.ExistingTeam = "bM" }, |
| 94 | "ordinary-create-reserved": func(p *Plan) { p.ExistingTeam = "" }, |
| 95 | "ordinary-add-reserved": func(p *Plan) { p.ExistingTeam = ""; p.Kind = "add-apps"; p.DisplayName = "" }, |
| 96 | "unknown-app": func(p *Plan) { p.Apps = []string{"unknown"} }, |
| 97 | } { |
| 98 | t.Run(name, func(t *testing.T) { |
| 99 | q := p |
| 100 | edit(&q) |
| 101 | if _, e := SignPlan(f.key, q, append(httpApps, "unknown")); e != ErrRefused { |
| 102 | t.Fatal("invalid adoption scope admitted", e) |
| 103 | } |
| 104 | }) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestExistingReservedLabelAndNativeWritesRefused(t *testing.T) { |
| 109 | f := newDirectoryFixture(t) |
| 110 | f.initialize(t) |
| 111 | if _, e := f.seed(t, 1, "oficina"); e != ErrRefused { |
| 112 | t.Fatal("native seed claimed reserved label", e) |
| 113 | } |
| 114 | h := adoptionDirectory(t) |
| 115 | if _, e := h.seedHTTP(t, 2, "oficina", "PRIVATE-TEAM-NAME-CANARY"); e != ErrRefused { |
| 116 | t.Fatal("different valid seed team", e) |
| 117 | } |
| 118 | if _, e := h.seedHTTP(t, 1, "oficina", "PRIVATE-TEAM-NAME-CANARY"); e != nil { |
| 119 | t.Fatal(e) |
| 120 | } |
| 121 | for name, edit := range map[string]func(*ProvisionInput){ |
| 122 | "team": func(p *ProvisionInput) { p.TeamSQID = "gb" }, "consumer": func(p *ProvisionInput) { p.Consumer = "other" }, |
| 123 | "new-origin": func(p *ProvisionInput) { p.Audience = "https://oficina.docs.oficina.works" }, |
| 124 | "other-app-origin": func(p *ProvisionInput) { p.Audience = "https://sheets.oficina.works" }, |
| 125 | } { |
| 126 | t.Run(name, func(t *testing.T) { |
| 127 | _, e := h.run(t, "directory.provision-consumer", func(b InputBase) any { |
| 128 | p := ProvisionInput{b, token(10), "Uk", "docs", "docs", "https://docs.oficina.works", 0} |
| 129 | edit(&p) |
| 130 | return p |
| 131 | }) |
| 132 | if e != ErrRefused { |
| 133 | t.Fatal(e) |
| 134 | } |
| 135 | }) |
| 136 | } |
| 137 | raw := h.input(t, "directory.provision-consumer", func(b InputBase) any { |
| 138 | return ProvisionInput{b, token(10), "Uk", "docs", "docs", "https://docs.oficina.works", 0} |
| 139 | }) |
| 140 | if _, e := h.directory.Execute(context.Background(), raw); e != ErrRefused { |
| 141 | t.Fatal("native canonical provision", e) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestExistingActivationDiscoveryAndRevocation(t *testing.T) { |
| 146 | f, in := adoptionPending(t) |
| 147 | store, e := directorydata.New(f.db, f.engine.sealer, directorydata.Config{Apps: httpApps, ValidateRoute: adoptionRoute}) |
| 148 | if e != nil { |
| 149 | t.Fatal(e) |
| 150 | } |
| 151 | if choices, e := store.AgentChoices(context.Background(), in.Owner); e != nil || len(choices) != 0 { |
| 152 | t.Fatal("pending visible", e) |
| 153 | } |
| 154 | wrong := in |
| 155 | wrong.TeamSQID = "gb" |
| 156 | if _, e = f.run(t, "directory.verify-consumer", func(b InputBase) any { wrong.InputBase = b; return wrong }); e != ErrRefused { |
| 157 | t.Fatal("wrong verify team", e) |
| 158 | } |
| 159 | raw := f.input(t, "directory.verify-consumer", func(b InputBase) any { in.InputBase = b; return in }) |
| 160 | if _, e = f.directory.Execute(context.Background(), raw); e != ErrRefused { |
| 161 | t.Fatal("native canonical verification", e) |
| 162 | } |
| 163 | out, e := f.directory.ExecuteHTTP(context.Background(), raw) |
| 164 | if e != nil { |
| 165 | t.Fatal(e) |
| 166 | } |
| 167 | var window Result |
| 168 | if json.Unmarshal(out, &window) != nil { |
| 169 | t.Fatal("verify result") |
| 170 | } |
| 171 | ev := activation(in, window.Attempt) |
| 172 | ev.AliasHost = "docs.oficina.works" |
| 173 | if _, e = f.run(t, "directory.activate-consumer", func(b InputBase) any { return ActivationInput{b, ev} }); e != ErrRefused { |
| 174 | t.Fatal("new-instance policy on existing app", e) |
| 175 | } |
| 176 | ev.Policy = existingActivationPolicy |
| 177 | for name, edit := range map[string]func(*ActivationEvidence){"team": func(e *ActivationEvidence) { e.TeamSQID = "gb" }, "origin": func(e *ActivationEvidence) { e.Audience = "https://oficina.docs.oficina.works" }, "manifest": func(e *ActivationEvidence) { e.ManifestDigest = "" }} { |
| 178 | t.Run(name, func(t *testing.T) { |
| 179 | p := ev |
| 180 | edit(&p) |
| 181 | if _, e := f.run(t, "directory.activate-consumer", func(b InputBase) any { return ActivationInput{b, p} }); e != ErrRefused { |
| 182 | t.Fatal(e) |
| 183 | } |
| 184 | }) |
| 185 | } |
| 186 | raw = f.input(t, "directory.activate-consumer", func(b InputBase) any { return ActivationInput{b, ev} }) |
| 187 | if _, e = f.directory.Execute(context.Background(), raw); e != ErrRefused { |
| 188 | t.Fatal("native adoption activation", e) |
| 189 | } |
| 190 | out, e = f.directory.ExecuteHTTP(context.Background(), raw) |
| 191 | if e != nil { |
| 192 | t.Fatal(e) |
| 193 | } |
| 194 | again, e := f.directory.ExecuteHTTP(context.Background(), raw) |
| 195 | if e != nil || !bytes.Equal(out, again) { |
| 196 | t.Fatal("activation replay", e) |
| 197 | } |
| 198 | choices, e := store.AgentChoices(context.Background(), in.Owner) |
| 199 | if e != nil || len(choices) != 1 || choices[0].Origin != in.Audience { |
| 200 | t.Fatal("live canonical discovery", e, choices) |
| 201 | } |
| 202 | // The old reader cannot consume these rows. Only an adoption-capable |
| 203 | // build is a valid post-activation rollback target. |
| 204 | old, _ := directorydata.New(f.db, f.engine.sealer, directorydata.Config{Apps: httpApps, ValidateRoute: func(r directorydata.Route) bool { return r.Origin == "https://"+r.Label+"."+r.App+".oficina.works" }}) |
| 205 | if _, e = old.AgentChoices(context.Background(), in.Owner); e != directorydata.ErrUnavailable { |
| 206 | t.Fatal("old reader compatibility unexpectedly changed", e) |
| 207 | } |
| 208 | compatible, _ := directorydata.New(f.db, f.engine.sealer, directorydata.Config{Apps: httpApps, ValidateRoute: adoptionRoute}) |
| 209 | if choices, e = compatible.AgentChoices(context.Background(), in.Owner); e != nil || len(choices) != 1 { |
| 210 | t.Fatal("compatible rollback reader", e) |
| 211 | } |
| 212 | if _, e = f.db.Exec("DELETE FROM memberships WHERE person_ref=?", in.Owner); e != nil { |
| 213 | t.Fatal(e) |
| 214 | } |
| 215 | if choices, e = store.AgentChoices(context.Background(), in.Owner); e != nil || len(choices) != 0 { |
| 216 | t.Fatal("removed member retained discovery", e) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func openCredentialAdoption(t *testing.T, f *httpCredential) { |
| 221 | openCredentialAdoptionPlan(t, f, "create-team", "Uk", "docs") |
| 222 | } |
| 223 | |
| 224 | func openCredentialAdoptionPlan(t *testing.T, f *httpCredential, kind, team string, apps ...string) { |
| 225 | t.Helper() |
| 226 | p := Plan{Version: 1, Purpose: "operator-plan", Role: "credential", Origin: f.auth.Origin, Installation: token(1), Registry: token(2), Device: token(3), PlanID: token(f.attempt + 100), Kind: kind, Apps: apps, ExistingTeam: team, Issued: f.now.Unix(), Expires: f.now.Unix() + 600} |
| 227 | index := "" |
| 228 | if kind == "create-team" { |
| 229 | p.Owner = ownerCanary |
| 230 | index = credentialIndex(f.pepper, ownerCanary) |
| 231 | } |
| 232 | raw, e := SignPlan(f.key, p, httpApps) |
| 233 | if e != nil { |
| 234 | t.Fatal(e) |
| 235 | } |
| 236 | p, e = f.http.c.engine.verifier.VerifyPlan(raw, httpApps, *f.now) |
| 237 | if e != nil { |
| 238 | t.Fatal(e) |
| 239 | } |
| 240 | if e = f.plans.Submit(context.Background(), p, index, *f.now); e != nil { |
| 241 | t.Fatal(e) |
| 242 | } |
| 243 | if _, e = f.plans.Open(context.Background(), p.PlanID, *f.now, func(v PlanView) bool { return v.ExistingTeam == team && (kind != "add-apps" || v.OwnerIndex == "") }); e != nil { |
| 244 | t.Fatal(e) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | func TestExistingAddAppsKeepsTeamAndRegistrations(t *testing.T) { |
| 249 | f, _ := adoptionPending(t) |
| 250 | *f.now = f.now.Add(WindowLifetime * time.Second) |
| 251 | f.openPlan(t, func(p *Plan) { |
| 252 | p.Kind, p.Label, p.DisplayName, p.ExistingTeam, p.Apps = "add-apps", "oficina", "", "Uk", []string{"sheets"} |
| 253 | }) |
| 254 | out, e := f.run(t, "directory.provision-consumer", func(b InputBase) any { |
| 255 | return ProvisionInput{b, token(10), "Uk", "sheets", "sheets", "https://sheets.oficina.works", 0} |
| 256 | }) |
| 257 | if e != nil { |
| 258 | t.Fatal("add canonical app", e) |
| 259 | } |
| 260 | if countRows(t, f.db, "teams") != 1 || countRows(t, f.db, "memberships") != 1 || countRows(t, f.db, "team_instances") != 2 { |
| 261 | t.Fatal("add apps replaced the team") |
| 262 | } |
| 263 | var result Result |
| 264 | if json.Unmarshal(out, &result) != nil { |
| 265 | t.Fatal("provision result") |
| 266 | } |
| 267 | c := newHTTPCredential(t) |
| 268 | c.http.env["sheets"] = "https://sheets.oficina.works" |
| 269 | openCredentialAdoption(t, c) |
| 270 | if _, e = c.run(t, "credential.owner-seed", ownerSeedInput(ownerCanary, true)); e != nil { |
| 271 | t.Fatal(e) |
| 272 | } |
| 273 | *c.now = c.now.Add(WindowLifetime * time.Second) |
| 274 | openCredentialAdoptionPlan(t, c, "add-apps", "Uk", "sheets") |
| 275 | // Use the actual directory record and signer. Both fixtures share the |
| 276 | // canonical issuer/directory; this tests derivative verification too. |
| 277 | c.http.provisioning = []roster.Key{{ID: keyID(f.directory.signer.SignPub()), Public: base64.RawURLEncoding.EncodeToString(f.directory.signer.SignPub())}} |
| 278 | out, e = c.run(t, "credential.register-consumer", func(b InputBase) any { return RegisterInput{b, result.Record} }) |
| 279 | if e != nil { |
| 280 | t.Fatal("existing sheets registration", e) |
| 281 | } |
| 282 | var registered Result |
| 283 | if json.Unmarshal(out, ®istered) != nil { |
| 284 | t.Fatal("register result") |
| 285 | } |
| 286 | want, _ := ManifestDigest(c.http.env) |
| 287 | if registered.Manifest != want || countRows(t, c.db, "consumers") != 0 { |
| 288 | t.Fatal("add-apps modified registry") |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | func TestExistingConfirmedTeamScopeIsIndependentOfStoredTeam(t *testing.T) { |
| 293 | t.Run("seed", func(t *testing.T) { |
| 294 | f := adoptionDirectory(t) |
| 295 | *f.now = f.now.Add(WindowLifetime * time.Second) |
| 296 | f.openPlan(t, func(p *Plan) { p.Label, p.ExistingTeam = "oficina", "gb" }) |
| 297 | if _, e := f.seedHTTP(t, 1, "oficina", "PRIVATE-TEAM-NAME-CANARY"); e != ErrRefused { |
| 298 | t.Fatal("valid next team escaped confirmed scope", e) |
| 299 | } |
| 300 | }) |
| 301 | t.Run("provision", func(t *testing.T) { |
| 302 | f, _ := adoptionPending(t) |
| 303 | *f.now = f.now.Add(WindowLifetime * time.Second) |
| 304 | f.openPlan(t, func(p *Plan) { |
| 305 | p.Kind, p.Label, p.DisplayName, p.ExistingTeam, p.Apps = "add-apps", "oficina", "", "gb", []string{"sheets"} |
| 306 | }) |
| 307 | if _, e := f.run(t, "directory.provision-consumer", func(b InputBase) any { |
| 308 | return ProvisionInput{b, token(10), "Uk", "sheets", "sheets", "https://sheets.oficina.works", 0} |
| 309 | }); e != ErrRefused { |
| 310 | t.Fatal("valid stored team escaped confirmed scope", e) |
| 311 | } |
| 312 | }) |
| 313 | t.Run("verify", func(t *testing.T) { |
| 314 | f, in := adoptionPending(t) |
| 315 | *f.now = f.now.Add(WindowLifetime * time.Second) |
| 316 | f.openPlan(t, func(p *Plan) { p.Kind, p.Label, p.DisplayName, p.ExistingTeam = "add-apps", "oficina", "", "gb" }) |
| 317 | in.Issued = f.now.Unix() |
| 318 | in.Expires = in.Issued + 600 |
| 319 | if _, e := f.run(t, "directory.verify-consumer", func(b InputBase) any { in.InputBase = b; return in }); e != ErrRefused { |
| 320 | t.Fatal("valid pending binding escaped confirmed scope", e) |
| 321 | } |
| 322 | }) |
| 323 | } |
| 324 | |
| 325 | func TestExistingRegistrationPreservesManifestAndOwnerScope(t *testing.T) { |
| 326 | f := newHTTPCredential(t) |
| 327 | openCredentialAdoption(t, f) |
| 328 | if _, e := f.run(t, "credential.owner-seed", func(b InputBase) any { return OwnerInput{b, "https://oficina.works", 2, "gb", ownerCanary, true} }); e != ErrRefused { |
| 329 | t.Fatal("different owner-seed team", e) |
| 330 | } |
| 331 | if _, e := f.run(t, "credential.owner-seed", ownerSeedInput(ownerCanary, true)); e != nil { |
| 332 | t.Fatal(e) |
| 333 | } |
| 334 | record := f.record(t, f.dirKey, "docs", "docs", "https://docs.oficina.works") |
| 335 | out, e := f.run(t, "credential.register-consumer", func(b InputBase) any { return RegisterInput{b, record} }) |
| 336 | if e != nil { |
| 337 | t.Fatal(e) |
| 338 | } |
| 339 | var result Result |
| 340 | if json.Unmarshal(out, &result) != nil { |
| 341 | t.Fatal("result") |
| 342 | } |
| 343 | want, _ := ManifestDigest(f.http.env) |
| 344 | if result.Manifest != want || countRows(t, f.db, "consumers") != 0 || f.http.env["docs"] != "https://docs.oficina.works" { |
| 345 | t.Fatal("existing registration changed") |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | func TestExistingRegistrationRejectsSubstitutions(t *testing.T) { |
| 350 | for _, name := range []string{"missing", "wrong-origin", "wrong-id", "table-clash", "wrong-key", "new-origin", "other-team", "ordinary-window"} { |
| 351 | t.Run(name, func(t *testing.T) { |
| 352 | f := newHTTPCredential(t) |
| 353 | if name == "ordinary-window" { |
| 354 | f.openPlan(t, "create-team", "docs") |
| 355 | } else { |
| 356 | openCredentialAdoption(t, f) |
| 357 | } |
| 358 | if _, e := f.run(t, "credential.owner-seed", ownerSeedInput(ownerCanary, true)); e != nil { |
| 359 | t.Fatal(e) |
| 360 | } |
| 361 | key := f.dirKey |
| 362 | consumer, origin := "docs", "https://docs.oficina.works" |
| 363 | switch name { |
| 364 | case "missing": |
| 365 | delete(f.http.env, "docs") |
| 366 | case "wrong-origin": |
| 367 | f.http.env["docs"] = "https://other.docs.oficina.works" |
| 368 | case "wrong-id": |
| 369 | consumer = "other" |
| 370 | case "table-clash": |
| 371 | if _, e := f.db.Exec("INSERT INTO consumers VALUES('docs','https://other.docs.oficina.works','docs','active','2026-11-02',?)", token(85)); e != nil { |
| 372 | t.Fatal(e) |
| 373 | } |
| 374 | case "wrong-key": |
| 375 | key, _ = family.Generate() |
| 376 | case "new-origin": |
| 377 | origin = "https://oficina.docs.oficina.works" |
| 378 | case "other-team": |
| 379 | if _, e := f.db.Exec("UPDATE operator_plans SET existing_team='gb'"); e != nil { |
| 380 | t.Fatal(e) |
| 381 | } |
| 382 | } |
| 383 | record := f.record(t, key, consumer, "docs", origin) |
| 384 | if _, e := f.run(t, "credential.register-consumer", func(b InputBase) any { return RegisterInput{b, record} }); e != ErrRefused { |
| 385 | t.Fatal("substitution admitted", e) |
| 386 | } |
| 387 | }) |
| 388 | } |
| 389 | } |
| 390 | |