package provision import ( "errors" "testing" "amadan.net/oficina/alta/console" "amadan.net/oficina/alta/homeop" ) // The ยง8 guards, each with a test that fails if the guard is removed. // Every refusal must happen before a request leaves the process: the fake // console's request log is checked unchanged. func guardHarness(t *testing.T) (*harness, *Engine) { h := newHarness(t) h.initialized() e := h.engine(nil) run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "alpha", Name: "Alpha", Owner: ownerCanary, Apps: []string{"sheets", "calls"}}) if err != nil { t.Fatal(err) } h.confirm(homeop.KindCreateTeam) h.drive(e, run, 20) return h, e } func refusedLocally(t *testing.T, h *harness, what string, err error) { t.Helper() if !errors.Is(err, console.ErrGuard) { t.Fatalf("%s: want Alta's guard to refuse, got %v", what, err) } } func TestGuardRefusesEmptyEnvironment(t *testing.T) { h, e := guardHarness(t) before := len(h.con.Requests) _, err := e.cfg.Console.PutConfig(h.ctx(), "sheets", "", map[string]console.SetVar{"X": {Kind: "env", Value: "1"}}, nil, "") refusedLocally(t, h, "empty environment", err) _, _, err = e.cfg.Console.GetConfig(h.ctx(), "sheets", "") refusedLocally(t, h, "empty environment read", err) if len(h.con.Requests) != before { t.Fatal("a refused request reached the console") } } func TestGuardRefusesEnvironmentNotInAltasRecords(t *testing.T) { h, e := guardHarness(t) before := len(h.con.Requests) // "default" and Paul's own named environments are never Alta's. for _, env := range []string{"default", "slice2", "lagged", "beta"} { _, err := e.cfg.Console.PutConfig(h.ctx(), "sheets", env, map[string]console.SetVar{"X": {Kind: "env", Value: "1"}}, nil, "") refusedLocally(t, h, env, err) } // alpha's environment is Alta's for sheets and calls, not for docs. _, err := e.cfg.Console.PutConfig(h.ctx(), "docs", "alpha", map[string]console.SetVar{"X": {Kind: "env", Value: "1"}}, nil, "") refusedLocally(t, h, "alpha for docs", err) if len(h.con.Requests) != before { t.Fatal("a refused request reached the console") } } func TestGuardRefusesHostsOutsideAltasShapes(t *testing.T) { h, e := guardHarness(t) before := len(h.con.Requests) for _, host := range []string{ "sheets.oficina.works", // Paul's own "sheets.bds.oncarlos.com", // Paul's route "beta-sheets.bds.oncarlos.com", // a label Alta does not hold "alpha-sheets.other.oncarlos.com", // another account "alpha.sheets.example.com", // another domain "alpha-docs.bds.oncarlos.com", // an app the team does not have "x.alpha.sheets.oficina.works", // a deeper name } { err := e.cfg.Console.CreateInstance(h.ctx(), "sheets", host, "alpha", "") refusedLocally(t, h, host, err) err = e.cfg.Console.AttachDomain(h.ctx(), "sheets", host, RouteHost("alpha", "sheets", account), "") refusedLocally(t, h, host+" attach", err) err = e.cfg.Console.RestartHost(h.ctx(), "sheets", host, "") refusedLocally(t, h, host+" restart", err) } if len(h.con.Requests) != before { t.Fatal("a refused request reached the console") } } func TestGuardRefusesDeleteOutsideADeprovisionRun(t *testing.T) { h, e := guardHarness(t) before := len(h.con.Requests) refusedLocally(t, h, "delete instance", e.cfg.Console.DeleteInstance(h.ctx(), "sheets", RouteHost("alpha", "sheets", account))) refusedLocally(t, h, "detach domain", e.cfg.Console.DetachDomain(h.ctx(), "sheets", PublicHost("alpha", "sheets"))) // A deprovision run for calls does not admit a DELETE for sheets. h.tick(homeop.WindowLifetime * 1e9) run, err := e.RetireApps(h.ctx(), "alpha", []string{"calls"}, "alpha") if err != nil { t.Fatal(err) } ctx := WithDeprovision(h.ctx(), run) refusedLocally(t, h, "delete for an app the run does not name", e.cfg.Console.DeleteInstance(ctx, "sheets", RouteHost("alpha", "sheets", account))) // A create-team run id is not a deprovision run. refusedLocally(t, h, "delete under a create run", e.cfg.Console.DeleteInstance(WithDeprovision(h.ctx(), 1), "calls", RouteHost("alpha", "calls", account))) if len(h.con.Requests) != before { t.Fatal("a refused request reached the console") } if _, _, ok := h.con.Instance(RouteHost("alpha", "sheets", account)); !ok { t.Fatal("sheets instance deleted") } } func TestGuardRefusesPlatformManagedKeys(t *testing.T) { h, e := guardHarness(t) before := len(h.con.Requests) for _, k := range []string{"CARLOS_SMTP_PASS", "DOCS_SMTP_USER", "SHEETS_SMTP_HOST", "CARLOS_MAIL_FROM", "DOCS_MAIL_FROM", "CARLOS_MAIL", "CARLOS_RELAY", "CARLOS_MAIL_EVENTS_KEY", "SLIDES_SCAN_CALLBACK_SECRET", "CARLOS_STORE_BUCKET", "CARLOS_STORE_REGION", "CARLOS_STORE_ENDPOINT", "CARLOS_STORE_ACCESS_KEY", "CARLOS_STORE_SECRET_KEY"} { _, err := e.cfg.Console.PutConfig(h.ctx(), "sheets", "alpha", map[string]console.SetVar{k: {Kind: "env", Value: "x"}}, nil, "") refusedLocally(t, h, "set "+k, err) _, err = e.cfg.Console.PutConfig(h.ctx(), "sheets", "alpha", nil, []string{k}, "") refusedLocally(t, h, "unset "+k, err) } // Memoria's AI pair is Paul's alone: never set, in any kind; a // fork's blanked MEMORIA_AI may still be unset. // So is Equipo's execution pair (team-bundle slice 3b). for _, k := range []string{"MEMORIA_AI", "MEMORIA_TENSORX_KEY", "EQUIPO_EXECUTION_ENABLED", "EQUIPO_EXECUTION_MARKER_PATH"} { for _, v := range []console.SetVar{{Kind: "env", Value: "on"}, {Kind: "secret", Envelope: "x"}} { _, err := e.cfg.Console.PutConfig(h.ctx(), "sheets", "alpha", map[string]console.SetVar{k: v}, nil, "") refusedLocally(t, h, "set "+k, err) } } if len(h.con.Requests) != before { t.Fatal("a refused request reached the console") } if _, err := e.cfg.Console.PutConfig(h.ctx(), "sheets", "alpha", nil, []string{"MEMORIA_AI"}, ""); err != nil { t.Fatalf("unset of MEMORIA_AI refused: %v", err) } // And the recipe never names one. for _, a := range Apps { for _, k := range Recipe(a, Inputs{Label: "alpha"}) { if console.PlatformManaged(k.Name) { t.Fatalf("%s recipe names platform-managed %s", a.Slug, k.Name) } if console.OperatorOnly(k.Name) { t.Fatalf("%s recipe names operator-only %s", a.Slug, k.Name) } } } } // TestGuardAllowsPreflightReadsOnly: a preflight may read a proposed // label's hosts and environment (checking a label must not burn it), but // never write them. func TestGuardAllowsPreflightReadsOnly(t *testing.T) { h, e := guardHarness(t) ctx := withProposal(h.ctx(), "delta", "delta", []string{"docs"}) if _, _, err := e.cfg.Console.GetConfig(ctx, "docs", "delta"); err != nil { t.Fatalf("preflight read: %v", err) } if _, _, err := e.cfg.Console.GetInstance(ctx, "docs", RouteHost("delta", "docs", account)); err != nil { t.Fatalf("preflight read: %v", err) } _, err := e.cfg.Console.PutConfig(ctx, "docs", "delta", map[string]console.SetVar{"X": {Kind: "env", Value: "1"}}, nil, "") refusedLocally(t, h, "preflight write", err) refusedLocally(t, h, "preflight create", e.cfg.Console.CreateInstance(ctx, "docs", RouteHost("delta", "docs", account), "delta", "")) }