| 1 | package altacompat |
| 2 | |
| 3 | // Alta's engine against Home's real operator engine (internal/operator at |
| 4 | // the pinned commit): the verifier, the canonical-bytes check, the stream, |
| 5 | // the plan store and its windows, the provenance fence, owner-seed, |
| 6 | // seed-team, provision, register (with the record verified under the |
| 7 | // pinned provisioning key), verify, activate, disable and retire, and |
| 8 | // both inspects. The HTTP transport below mirrors internal/home |
| 9 | // operator_http.go's routing and fixed outcomes; Home's own tests cover |
| 10 | // that transport, which needs the whole Home app to construct. |
| 11 | |
| 12 | import ( |
| 13 | "bytes" |
| 14 | "context" |
| 15 | "crypto/hmac" |
| 16 | "crypto/sha256" |
| 17 | "database/sql" |
| 18 | "encoding/base64" |
| 19 | "encoding/hex" |
| 20 | "errors" |
| 21 | "io" |
| 22 | "log/slog" |
| 23 | "net/http" |
| 24 | "net/http/httptest" |
| 25 | "path/filepath" |
| 26 | "strings" |
| 27 | "testing" |
| 28 | "time" |
| 29 | |
| 30 | "amadan.net/oficina/alta/console" |
| 31 | "amadan.net/oficina/alta/console/fakeconsole" |
| 32 | "amadan.net/oficina/alta/homeop" |
| 33 | "amadan.net/oficina/alta/provision" |
| 34 | sharedseal "amadan.net/oficina/home/handoff/seal" |
| 35 | "amadan.net/oficina/home/internal/home/directorydata" |
| 36 | "amadan.net/oficina/home/internal/operator" |
| 37 | "amadan.net/oficina/home/roster" |
| 38 | "amadan.net/rastrillo/rastrillo/assertion" |
| 39 | family "amadan.net/rastrillo/rastrillo/crypto" |
| 40 | rdb "amadan.net/rastrillo/rastrillo/db" |
| 41 | "amadan.net/rastrillo/rastrillo/migrate" |
| 42 | ) |
| 43 | |
| 44 | const ( |
| 45 | credOrigin = "https://id.oficina.works" |
| 46 | dirOrigin = "https://oficina.works" |
| 47 | owner = "till-canary@lagged.test" |
| 48 | account = "bds" |
| 49 | ) |
| 50 | |
| 51 | var apps = []string{"docs", "sheets", "slides", "calendar", "calls", "memoria"} |
| 52 | var adoptionApps = append(append([]string(nil), apps...), "equipo", "base") |
| 53 | |
| 54 | type home struct { |
| 55 | now time.Time |
| 56 | pepper []byte |
| 57 | prefilter [32]byte |
| 58 | dirPlans *operator.PlanStore |
| 59 | credPlans *operator.PlanStore |
| 60 | directory *operator.Directory |
| 61 | credential *operator.CredentialHTTP |
| 62 | dirV *operator.Verifier |
| 63 | credV *operator.Verifier |
| 64 | provPub string |
| 65 | dirDB *sql.DB |
| 66 | credDB *sql.DB |
| 67 | } |
| 68 | |
| 69 | func open(t *testing.T, set *migrate.Set) *sql.DB { |
| 70 | t.Helper() |
| 71 | h, err := rdb.Open(filepath.Join(t.TempDir(), "role.db"), nil) |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | t.Cleanup(func() { h.Close() }) |
| 76 | if _, err := migrate.Apply(context.Background(), h, set); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | return h.Writer() |
| 80 | } |
| 81 | |
| 82 | // newHome builds both roles. extraEnv adds HOME_CONSUMERS entries (the |
| 83 | // six canonical consumers, for adoption). |
| 84 | func newHome(t *testing.T, alta *family.Keypair, prefilter string, installation, registry, device string, extraEnv map[string]string) *home { |
| 85 | t.Helper() |
| 86 | h := &home{now: time.Unix(2_000_000_000, 0), pepper: bytes.Repeat([]byte{9}, 32)} |
| 87 | raw, _ := base64.RawURLEncoding.DecodeString(prefilter) |
| 88 | h.prefilter = sha256.Sum256(raw) |
| 89 | clock := func() time.Time { return h.now } |
| 90 | sealer, err := sharedseal.New(bytes.Repeat([]byte{6}, 32), "oficina/operator") |
| 91 | if err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | altaPub := base64.RawURLEncoding.EncodeToString(alta.SignPub()) |
| 95 | ownerKey, _ := family.Generate() |
| 96 | prov, _ := family.Generate() |
| 97 | page, _ := family.Generate() |
| 98 | browser, _ := family.Generate() |
| 99 | h.provPub = base64.RawURLEncoding.EncodeToString(prov.SignPub()) |
| 100 | |
| 101 | // The directory role. |
| 102 | // The directory's serving schema at the pinned commit |
| 103 | // (internal/home runtime.go ServingSchema): since 7bdddfa it includes |
| 104 | // the Team page's set, whose memberships.added_at seed-team writes. |
| 105 | h.dirDB = open(t, migrate.Merge(directorydata.Schema, directorydata.RosterSchema, directorydata.OperatorSchema, operator.DirectorySchema, operator.DirectoryHTTPSchema, directorydata.TeamSchema)) |
| 106 | h.dirV, err = operator.NewVerifier(operator.Pins{Role: "directory", Origin: dirOrigin, Installation: installation, Registry: registry, Device: device, PublicKey: altaPub, |
| 107 | Actions: []string{"directory.inspect", "directory.initialize", "directory.seed-team", "directory.provision-consumer", "directory.verify-consumer", "directory.activate-consumer", "directory.disable-consumer"}}) |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | de, err := operator.NewEngine(h.dirDB, h.dirV, sealer, bytes.Repeat([]byte{7}, 32), clock) |
| 112 | if err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | ov, err := operator.NewOwnerVerifier(base64.RawURLEncoding.EncodeToString(ownerKey.SignPub()), credOrigin, dirOrigin) |
| 116 | if err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | // internal/home directory_runtime.go directoryRouteAllowed: the reserved |
| 120 | // label admits only the canonical binding; every other label its own host. |
| 121 | route := func(r directorydata.Route) bool { |
| 122 | if r.Label == "oficina" { |
| 123 | return directorydata.CanonicalBinding(r.Label, r.App, r.ConsumerID, r.Origin) |
| 124 | } |
| 125 | return r.Origin == "https://"+r.Label+"."+r.App+".oficina.works" |
| 126 | } |
| 127 | h.directory, err = operator.NewDirectory(de, ov, prov, []roster.Key{{ID: "page1", Public: base64.RawURLEncoding.EncodeToString(page.SignPub())}}, directorydata.Config{Apps: adoptionApps, ValidateRoute: route}) |
| 128 | if err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | if h.dirPlans, err = operator.NewPlanStore(h.dirDB, "directory", sealer); err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | |
| 135 | // The credential role. |
| 136 | h.credDB = open(t, migrate.Merge(operator.CredentialSchema, operator.CredentialHTTPSchema)) |
| 137 | if _, err := h.credDB.Exec("CREATE TABLE people(person_ref TEXT PRIMARY KEY, blind_index TEXT UNIQUE)"); err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | if _, err := h.credDB.Exec("INSERT INTO people VALUES('pr_aaaaaaaaaaaaaaaaaaaaaaaaaa',?)", h.index(owner)); err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | h.credV, err = operator.NewVerifier(operator.Pins{Role: "credential", Origin: credOrigin, Installation: installation, Registry: registry, Device: device, PublicKey: altaPub, |
| 144 | Actions: []string{"credential.inspect", "credential.owner-seed", "credential.register-consumer", "credential.retire-consumer"}}) |
| 145 | if err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | ce, err := operator.NewEngine(h.credDB, h.credV, sealer, bytes.Repeat([]byte{8}, 32), clock) |
| 149 | if err != nil { |
| 150 | t.Fatal(err) |
| 151 | } |
| 152 | resolve := func(ctx context.Context, c *sql.Conn, address string, existing bool, _ time.Time) (string, bool, error) { |
| 153 | var ref string |
| 154 | err := c.QueryRowContext(ctx, "SELECT person_ref FROM people WHERE blind_index=?", h.index(address)).Scan(&ref) |
| 155 | if errors.Is(err, sql.ErrNoRows) { |
| 156 | return "", false, nil |
| 157 | } |
| 158 | return ref, err == nil, err |
| 159 | } |
| 160 | cred, err := operator.NewCredential(ce, dirOrigin, ownerKey, [][]byte{browser.SignPub()}, resolve) |
| 161 | if err != nil { |
| 162 | t.Fatal(err) |
| 163 | } |
| 164 | keys := map[string][]byte{"k1": browser.SignPub()} |
| 165 | admit := func(id, origin string) bool { |
| 166 | _, err := assertion.NewVerifier(credOrigin, origin, keys) |
| 167 | return err == nil && id != "" |
| 168 | } |
| 169 | env := map[string]string{"directory": dirOrigin, "alta": "https://alta-x.bds.oncarlos.com"} |
| 170 | for id, origin := range extraEnv { |
| 171 | env[id] = origin |
| 172 | } |
| 173 | if h.credential, err = operator.NewCredentialHTTP(cred, h.provPub, env, adoptionApps, h.index, admit); err != nil { |
| 174 | t.Fatal(err) |
| 175 | } |
| 176 | if h.credPlans, err = operator.NewPlanStore(h.credDB, "credential", sealer); err != nil { |
| 177 | t.Fatal(err) |
| 178 | } |
| 179 | return h |
| 180 | } |
| 181 | |
| 182 | func (h *home) index(address string) string { |
| 183 | m := hmac.New(sha256.New, h.pepper) |
| 184 | m.Write([]byte(strings.ToLower(address))) |
| 185 | return hex.EncodeToString(m.Sum(nil)) |
| 186 | } |
| 187 | |
| 188 | // handler mirrors internal/home operator_http.go's handleOperator. |
| 189 | func (h *home) handler(role string) http.Handler { |
| 190 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 191 | answer := func(status int, code string) { |
| 192 | w.Header().Set("Content-Type", "application/json") |
| 193 | w.WriteHeader(status) |
| 194 | _, _ = io.WriteString(w, `{"outcome":"`+code+`"}`) |
| 195 | } |
| 196 | if r.Method != http.MethodPost || (r.URL.Path != "/operator/v1" && r.URL.Path != "/operator/v1/plan") { |
| 197 | answer(404, "not_found") |
| 198 | return |
| 199 | } |
| 200 | if len(r.Header.Values("Cookie")) > 0 || len(r.Header.Values("Origin")) > 0 || r.Header.Get("Content-Type") != "application/json" { |
| 201 | answer(403, "browser") |
| 202 | return |
| 203 | } |
| 204 | tok, err := base64.RawURLEncoding.Strict().DecodeString(strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")) |
| 205 | if err != nil || sha256.Sum256(tok) != h.prefilter { |
| 206 | answer(401, "prefilter") |
| 207 | return |
| 208 | } |
| 209 | body, _ := io.ReadAll(io.LimitReader(r.Body, operator.BodyLimit+1)) |
| 210 | v, plans := h.dirV, h.dirPlans |
| 211 | if role == "credential" { |
| 212 | v, plans = h.credV, h.credPlans |
| 213 | } |
| 214 | result := func(out []byte, err error) { |
| 215 | switch { |
| 216 | case err == nil: |
| 217 | w.Header().Set("Content-Type", "application/json") |
| 218 | _, _ = w.Write(out) |
| 219 | case errors.Is(err, operator.ErrRefused): |
| 220 | answer(403, "refused") |
| 221 | default: |
| 222 | answer(503, "unavailable") |
| 223 | } |
| 224 | } |
| 225 | if r.URL.Path == "/operator/v1/plan" { |
| 226 | p, err := v.VerifyPlan(body, adoptionApps, h.now) |
| 227 | if err != nil { |
| 228 | answer(403, "refused") |
| 229 | return |
| 230 | } |
| 231 | index := "" |
| 232 | if role == "credential" && p.Owner != "" { |
| 233 | index = h.index(p.Owner) |
| 234 | var one int |
| 235 | if h.credDB.QueryRow("SELECT 1 FROM people WHERE blind_index=?", index).Scan(&one) != nil { |
| 236 | answer(403, "refused") |
| 237 | return |
| 238 | } |
| 239 | } |
| 240 | err = plans.Submit(context.Background(), p, index, h.now) |
| 241 | if errors.Is(err, operator.ErrBusy) { |
| 242 | answer(409, "busy") |
| 243 | return |
| 244 | } |
| 245 | result([]byte(`{"outcome":"pending"}`), err) |
| 246 | return |
| 247 | } |
| 248 | if _, err := v.Verify(body); err == nil { |
| 249 | if role == "directory" { |
| 250 | result(h.directory.ExecuteHTTP(r.Context(), body)) |
| 251 | } else { |
| 252 | result(h.credential.ExecuteHTTP(r.Context(), body)) |
| 253 | } |
| 254 | return |
| 255 | } |
| 256 | if role == "directory" { |
| 257 | result(h.directory.InspectHTTP(r.Context(), body)) |
| 258 | } else { |
| 259 | result(h.credential.InspectHTTP(r.Context(), body)) |
| 260 | } |
| 261 | }) |
| 262 | } |
| 263 | |
| 264 | // confirm is Paul on Home's confirm pages: Home's own copy of the pending |
| 265 | // plan, the credential role comparing the typed owner's blind index. |
| 266 | func (h *home) confirm(t *testing.T, role, typed string) operator.PlanView { |
| 267 | t.Helper() |
| 268 | plans := h.dirPlans |
| 269 | if role == "credential" { |
| 270 | plans = h.credPlans |
| 271 | } |
| 272 | p, err := plans.Pending(context.Background(), h.now) |
| 273 | if err != nil { |
| 274 | t.Fatalf("%s: no pending plan: %v", role, err) |
| 275 | } |
| 276 | v, err := plans.Open(context.Background(), p.ID, h.now, func(v operator.PlanView) bool { |
| 277 | return role == "directory" || v.OwnerIndex == "" || hmac.Equal([]byte(v.OwnerIndex), []byte(h.index(typed))) |
| 278 | }) |
| 279 | if err != nil { |
| 280 | t.Fatalf("%s: confirm: %v", role, err) |
| 281 | } |
| 282 | return v |
| 283 | } |
| 284 | |
| 285 | type rig struct { |
| 286 | home *home |
| 287 | con *fakeconsole.Console |
| 288 | engine *provision.Engine |
| 289 | cfg provision.Config // engine's, for an engine with an Observer |
| 290 | store *provision.Store |
| 291 | client *homeop.Client |
| 292 | } |
| 293 | |
| 294 | func newRig(t *testing.T) *rig { return newRigWith(t, nil, nil) } |
| 295 | |
| 296 | // newRigWith is newRig over a Home whose env manifest also holds |
| 297 | // extraEnv, with the engine's AfterAct hook. |
| 298 | func newRigWith(t *testing.T, extraEnv map[string]string, afterAct func(string) error) *rig { |
| 299 | t.Helper() |
| 300 | alta, _ := family.Generate() |
| 301 | pre, inst, reg, dev := homeop.Token(), homeop.Token(), homeop.Token(), homeop.Token() |
| 302 | h := newHome(t, alta, pre, inst, reg, dev, extraEnv) |
| 303 | dir := httptest.NewServer(h.handler("directory")) |
| 304 | cred := httptest.NewServer(h.handler("credential")) |
| 305 | t.Cleanup(dir.Close) |
| 306 | t.Cleanup(cred.Close) |
| 307 | con := fakeconsole.New(account, "alta") |
| 308 | t.Cleanup(con.Close) |
| 309 | tokens := map[string]string{} |
| 310 | for _, a := range provision.Apps { |
| 311 | tokens[a.Slug] = con.Mint(a.Slug) |
| 312 | // A git-describe stamp, as the apps' Makefiles write: Home |
| 313 | // (0cee9ba) activates on an observed token, not only a commit. |
| 314 | con.Versions[a.Slug] = "v0.1.7-3-gabc1234" |
| 315 | con.MailPrefix[a.Slug] = a.MailPrefixes[0] |
| 316 | } |
| 317 | con.Relay["calls"] = true |
| 318 | db, err := provision.OpenDB(filepath.Join(t.TempDir(), "alta.db")) |
| 319 | if err != nil { |
| 320 | t.Fatal(err) |
| 321 | } |
| 322 | t.Cleanup(func() { db.Close() }) |
| 323 | clock := func() time.Time { return h.now } |
| 324 | store, err := provision.NewStore(context.Background(), db, bytes.Repeat([]byte{4}, 32), clock) |
| 325 | if err != nil { |
| 326 | t.Fatal(err) |
| 327 | } |
| 328 | roles := map[string]homeop.Role{ |
| 329 | "credential": {Name: "credential", Origin: credOrigin, URL: cred.URL, Installation: inst, Registry: reg, Device: dev}, |
| 330 | "directory": {Name: "directory", Origin: dirOrigin, URL: dir.URL, Installation: inst, Registry: reg, Device: dev}, |
| 331 | } |
| 332 | client := &homeop.Client{Key: alta, Prefilter: pre, Roles: roles, Now: clock} |
| 333 | cfg := provision.Config{Store: store, Home: client, |
| 334 | Console: &console.Client{Base: con.URL(), Account: account, Tokens: tokens, Guard: provision.Guard{Store: store, Account: account}}, |
| 335 | Public: &http.Client{Transport: con.Transport()}, |
| 336 | HomeCfg: provision.HomeConfig{Issuer: credOrigin, Directory: dirOrigin, KeysBase64: "eyJrMSI6IkJBPT0ifQ==", Provisioner: h.provPub}, |
| 337 | Account: account, Now: clock, Logger: slog.New(slog.NewTextHandler(io.Discard, nil)), AfterAct: afterAct, |
| 338 | Debug: func(err error) { t.Logf("transient: %v", err) }} |
| 339 | return &rig{home: h, con: con, engine: provision.New(cfg), cfg: cfg, store: store, client: client} |
| 340 | } |
| 341 | |
| 342 | func (r *rig) drive(t *testing.T, run int64) provision.Run { |
| 343 | t.Helper() |
| 344 | var out provision.Run |
| 345 | for i := 0; i < 40; i++ { |
| 346 | _ = r.engine.Advance(context.Background(), run) |
| 347 | out, _ = r.store.Run(context.Background(), run) |
| 348 | if out.State == "done" || out.State == "failed" { |
| 349 | return out |
| 350 | } |
| 351 | r.con.Walk() |
| 352 | r.home.now = r.home.now.Add(3 * time.Second) |
| 353 | } |
| 354 | return out |
| 355 | } |
| 356 | |
| 357 | // TestAltaAgainstHomesRealOperator runs Alta's first production run and a |
| 358 | // whole team through Home's real code: initialize; create-team for all |
| 359 | // six apps (Docs through verify and activate); add-apps after the window |
| 360 | // closes; then retire. |
| 361 | func TestAltaAgainstHomesRealOperator(t *testing.T) { |
| 362 | r := newRig(t) |
| 363 | ctx := context.Background() |
| 364 | |
| 365 | run, err := r.engine.StartInitialize(ctx, provision.InitializeAttestation) |
| 366 | if err != nil { |
| 367 | t.Fatal(err) |
| 368 | } |
| 369 | r.home.confirm(t, "directory", "") |
| 370 | if got := r.drive(t, run); got.State != "done" { |
| 371 | t.Fatalf("initialize: %s %s", got.State, got.Code) |
| 372 | } |
| 373 | // Home keeps the initialize window open until it expires; a second |
| 374 | // confirm before then is 409. |
| 375 | r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) |
| 376 | |
| 377 | run, err = r.engine.CreateTeam(ctx, provision.NewTeam{Label: "lagged", Name: "Lagged", Owner: owner, Apps: apps}) |
| 378 | if err != nil { |
| 379 | t.Fatal(err) |
| 380 | } |
| 381 | r1, _ := r.store.Run(ctx, run) |
| 382 | dv := r.home.confirm(t, "directory", "") |
| 383 | cv := r.home.confirm(t, "credential", owner) |
| 384 | // Home's own copy, as Paul reads it on both pages. |
| 385 | if dv.Prefix() != homeop.PlanPrefix(r1.PlanID) || cv.Prefix() != dv.Prefix() || dv.Label != "lagged" || dv.DisplayName != "Lagged" || len(dv.Apps) != 6 || cv.OwnerIndex != r.home.index(owner) { |
| 386 | t.Fatalf("Home's copy: %+v / %+v", dv, cv) |
| 387 | } |
| 388 | if got := r.drive(t, run); got.State != "done" { |
| 389 | steps, _ := r.store.Steps(ctx, "lagged") |
| 390 | t.Fatalf("create-team: %s %s %+v", got.State, got.Code, steps) |
| 391 | } |
| 392 | var state string |
| 393 | if err := r.home.dirDB.QueryRow("SELECT state FROM team_instances WHERE consumer_id='lagged-docs'").Scan(&state); err != nil || state != "live" { |
| 394 | t.Fatalf("docs at the directory: %q %v", state, err) |
| 395 | } |
| 396 | if err := r.home.dirDB.QueryRow("SELECT state FROM team_instances WHERE consumer_id='lagged-sheets'").Scan(&state); err != nil || state != "live" { |
| 397 | t.Fatalf("sheets at the directory: %q %v", state, err) |
| 398 | } |
| 399 | if err := r.home.dirDB.QueryRow("SELECT state FROM team_instances WHERE consumer_id='lagged-slides'").Scan(&state); err != nil || state != "live" { |
| 400 | t.Fatalf("slides at the directory: %q %v", state, err) |
| 401 | } |
| 402 | if err := r.home.dirDB.QueryRow("SELECT state FROM team_instances WHERE consumer_id='lagged-memoria'").Scan(&state); err != nil || state != "live" { |
| 403 | t.Fatalf("memoria at the directory: %q %v", state, err) |
| 404 | } |
| 405 | if err := r.home.dirDB.QueryRow("SELECT state FROM team_instances WHERE consumer_id='lagged-calendar'").Scan(&state); err != nil || state != "live" { |
| 406 | t.Fatalf("calendar at the directory: %q %v", state, err) |
| 407 | } |
| 408 | // Every app has roster mode: all six are live at the directory. |
| 409 | var live int |
| 410 | if err := r.home.dirDB.QueryRow("SELECT count(*) FROM team_instances WHERE state='live'").Scan(&live); err != nil || live != 6 { |
| 411 | t.Fatalf("live at the directory: %d %v", live, err) |
| 412 | } |
| 413 | var n int |
| 414 | _ = r.home.credDB.QueryRow("SELECT count(*) FROM consumers WHERE state='active'").Scan(&n) |
| 415 | if n != 6 { |
| 416 | t.Fatalf("credential consumers %d", n) |
| 417 | } |
| 418 | // Alta's manifest digest is Home's. |
| 419 | table, _ := operator.ActiveConsumers(ctx, r.home.credDB) |
| 420 | union := map[string]string{"directory": dirOrigin, "alta": "https://alta-x.bds.oncarlos.com"} |
| 421 | for id, o := range table { |
| 422 | union[id] = o |
| 423 | } |
| 424 | want, _ := operator.ManifestDigest(union) |
| 425 | if got := homeop.ManifestDigest(union); got != want { |
| 426 | t.Fatalf("manifest digest %s, Home's %s", got, want) |
| 427 | } |
| 428 | // Docs' environment carries a record Docs itself would accept: the |
| 429 | // one Home signed, under the key Alta pinned. |
| 430 | vars, _, _ := r.con.Env("docs", "lagged") |
| 431 | rec, _ := base64.StdEncoding.DecodeString(vars["DOCS_TEAM_PROVISIONING_BASE64"].Value) |
| 432 | keys, _ := base64.StdEncoding.DecodeString(vars["DOCS_TEAM_OPERATOR_KEYS_BASE64"].Value) |
| 433 | if _, err := roster.VerifyProvisioning(rec, provision.ProvisionerKeys(r.home.provPub), roster.Bindings{Issuer: credOrigin, Directory: dirOrigin, Consumer: "lagged-docs", App: "docs", Team: vars["DOCS_TEAM_ID"].Value, Audience: "https://lagged.docs.oficina.works"}); err != nil || !strings.Contains(string(keys), r.home.provPub) { |
| 434 | t.Fatalf("docs record: %v", err) |
| 435 | } |
| 436 | // Alta's stream is Home's. |
| 437 | for _, role := range []string{"directory", "credential"} { |
| 438 | seq, head, _, _ := r.store.Stream(ctx, role) |
| 439 | db := r.home.dirDB |
| 440 | if role == "credential" { |
| 441 | db = r.home.credDB |
| 442 | } |
| 443 | var hs int64 |
| 444 | var hh string |
| 445 | _ = db.QueryRow("SELECT sequence,head FROM "+role+"_operator_state WHERE singleton=1").Scan(&hs, &hh) |
| 446 | if seq != hs || head != hh { |
| 447 | t.Fatalf("%s stream: Alta %d/%s, Home %d/%s", role, seq, head, hs, hh) |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | // Retire Calls: disable at the directory, retire at the credential |
| 452 | // origin, detach, delete. |
| 453 | r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) |
| 454 | run, err = r.engine.RetireApps(ctx, "lagged", []string{"calls"}, "lagged") |
| 455 | if err != nil { |
| 456 | t.Fatal(err) |
| 457 | } |
| 458 | r.home.confirm(t, "directory", "") |
| 459 | r.home.confirm(t, "credential", "") |
| 460 | if got := r.drive(t, run); got.State != "done" { |
| 461 | t.Fatalf("retire: %s %s", got.State, got.Code) |
| 462 | } |
| 463 | if err := r.home.dirDB.QueryRow("SELECT state FROM team_instances WHERE consumer_id='lagged-calls'").Scan(&state); err != nil || state != "disabled" { |
| 464 | t.Fatalf("calls at the directory: %q", state) |
| 465 | } |
| 466 | if err := r.home.credDB.QueryRow("SELECT state FROM consumers WHERE consumer_id='lagged-calls'").Scan(&state); err != nil || state != "retired" { |
| 467 | t.Fatalf("calls at the credential origin: %q", state) |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | // TestHomeRefusesWhatAltaMustNotDo checks the fences hold on Home's side |
| 472 | // for requests Alta signs correctly: no window, a label outside the |
| 473 | // window, an unknown owner. |
| 474 | func TestHomeRefusesWhatAltaMustNotDo(t *testing.T) { |
| 475 | r := newRig(t) |
| 476 | ctx := context.Background() |
| 477 | run, _ := r.engine.StartInitialize(ctx, provision.InitializeAttestation) |
| 478 | // Not confirmed: no window, nothing happens at Home. |
| 479 | if got := r.drive(t, run); got.State == "done" { |
| 480 | t.Fatal("initialized without a window") |
| 481 | } |
| 482 | var n int |
| 483 | _ = r.home.dirDB.QueryRow("SELECT count(*) FROM roster_generation").Scan(&n) |
| 484 | if n != 0 { |
| 485 | t.Fatal("generation written without a window") |
| 486 | } |
| 487 | r.home.confirm(t, "directory", "") |
| 488 | if got := r.drive(t, run); got.State != "done" { |
| 489 | t.Fatalf("initialize: %s %s", got.State, got.Code) |
| 490 | } |
| 491 | r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) |
| 492 | if _, err := r.engine.CreateTeam(ctx, provision.NewTeam{Label: "alpha", Name: "Alpha", Owner: "nobody@nowhere.test", Apps: []string{"docs"}}); err == nil { |
| 493 | t.Fatal("Home accepted a plan for an owner who never signed in") |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | // TestSeededAppsAgainstHomesRealOperator: an own team with Equipo and Base |
| 498 | // (oficina/alta design/team-bundle) reaches live at Home's real directory. |
| 499 | // The seeded apps carry no roster record; Home's activation takes Alta's |
| 500 | // evidence for them as it does for a roster app (spec §1.2). |
| 501 | func TestSeededAppsAgainstHomesRealOperator(t *testing.T) { |
| 502 | r := newRig(t) |
| 503 | ctx := context.Background() |
| 504 | run, err := r.engine.StartInitialize(ctx, provision.InitializeAttestation) |
| 505 | if err != nil { |
| 506 | t.Fatal(err) |
| 507 | } |
| 508 | r.home.confirm(t, "directory", "") |
| 509 | if got := r.drive(t, run); got.State != "done" { |
| 510 | t.Fatalf("initialize: %s %s", got.State, got.Code) |
| 511 | } |
| 512 | r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) |
| 513 | |
| 514 | bundle := append(append([]string{}, apps...), "equipo", "base") |
| 515 | run, err = r.engine.CreateTeam(ctx, provision.NewTeam{Label: "campbells", Name: "Campbells", Owner: owner, Apps: bundle, Options: provision.RunOptions{Profile: provision.ProfileOwn}}) |
| 516 | if err != nil { |
| 517 | t.Fatal(err) |
| 518 | } |
| 519 | r.home.confirm(t, "directory", "") |
| 520 | r.home.confirm(t, "credential", owner) |
| 521 | if got := r.drive(t, run); got.State != "done" { |
| 522 | steps, _ := r.store.Steps(ctx, "campbells") |
| 523 | t.Fatalf("create-team: %s %s %+v", got.State, got.Code, steps) |
| 524 | } |
| 525 | for _, id := range []string{"campbells-equipo", "campbells-base"} { |
| 526 | var state string |
| 527 | if err := r.home.dirDB.QueryRow("SELECT state FROM team_instances WHERE consumer_id=?", id).Scan(&state); err != nil || state != "live" { |
| 528 | t.Fatalf("%s at the directory: %q %v", id, state, err) |
| 529 | } |
| 530 | } |
| 531 | var live int |
| 532 | if err := r.home.dirDB.QueryRow("SELECT count(*) FROM team_instances WHERE state='live'").Scan(&live); err != nil || live != len(bundle) { |
| 533 | t.Fatalf("live at the directory: %d %v", live, err) |
| 534 | } |
| 535 | } |
| 536 | |