oficina / alta Public

Clone
git clone https://amadan.net/oficina/alta

Plain git — no account needed to clone.

Download

Download this file

1package provision
2
3import (
4 "errors"
5 "testing"
6
7 "amadan.net/oficina/alta/console"
8 "amadan.net/oficina/alta/homeop"
9)
10
11// The §8 guards, each with a test that fails if the guard is removed.
12// Every refusal must happen before a request leaves the process: the fake
13// console's request log is checked unchanged.
14
15func guardHarness(t *testing.T) (*harness, *Engine) {
16 h := newHarness(t)
17 h.initialized()
18 e := h.engine(nil)
19 run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "alpha", Name: "Alpha", Owner: ownerCanary, Apps: []string{"sheets", "calls"}})
20 if err != nil {
21 t.Fatal(err)
22 }
23 h.confirm(homeop.KindCreateTeam)
24 h.drive(e, run, 20)
25 return h, e
26}
27
28func refusedLocally(t *testing.T, h *harness, what string, err error) {
29 t.Helper()
30 if !errors.Is(err, console.ErrGuard) {
31 t.Fatalf("%s: want Alta's guard to refuse, got %v", what, err)
32 }
33}
34
35func TestGuardRefusesEmptyEnvironment(t *testing.T) {
36 h, e := guardHarness(t)
37 before := len(h.con.Requests)
38 _, err := e.cfg.Console.PutConfig(h.ctx(), "sheets", "", map[string]console.SetVar{"X": {Kind: "env", Value: "1"}}, nil, "")
39 refusedLocally(t, h, "empty environment", err)
40 _, _, err = e.cfg.Console.GetConfig(h.ctx(), "sheets", "")
41 refusedLocally(t, h, "empty environment read", err)
42 if len(h.con.Requests) != before {
43 t.Fatal("a refused request reached the console")
44 }
45}
46
47func TestGuardRefusesEnvironmentNotInAltasRecords(t *testing.T) {
48 h, e := guardHarness(t)
49 before := len(h.con.Requests)
50 // "default" and Paul's own named environments are never Alta's.
51 for _, env := range []string{"default", "slice2", "lagged", "beta"} {
52 _, err := e.cfg.Console.PutConfig(h.ctx(), "sheets", env, map[string]console.SetVar{"X": {Kind: "env", Value: "1"}}, nil, "")
53 refusedLocally(t, h, env, err)
54 }
55 // alpha's environment is Alta's for sheets and calls, not for docs.
56 _, err := e.cfg.Console.PutConfig(h.ctx(), "docs", "alpha", map[string]console.SetVar{"X": {Kind: "env", Value: "1"}}, nil, "")
57 refusedLocally(t, h, "alpha for docs", err)
58 if len(h.con.Requests) != before {
59 t.Fatal("a refused request reached the console")
60 }
61}
62
63func TestGuardRefusesHostsOutsideAltasShapes(t *testing.T) {
64 h, e := guardHarness(t)
65 before := len(h.con.Requests)
66 for _, host := range []string{
67 "sheets.oficina.works", // Paul's own
68 "sheets.bds.oncarlos.com", // Paul's route
69 "beta-sheets.bds.oncarlos.com", // a label Alta does not hold
70 "alpha-sheets.other.oncarlos.com", // another account
71 "alpha.sheets.example.com", // another domain
72 "alpha-docs.bds.oncarlos.com", // an app the team does not have
73 "x.alpha.sheets.oficina.works", // a deeper name
74 } {
75 err := e.cfg.Console.CreateInstance(h.ctx(), "sheets", host, "alpha", "")
76 refusedLocally(t, h, host, err)
77 err = e.cfg.Console.AttachDomain(h.ctx(), "sheets", host, RouteHost("alpha", "sheets", account), "")
78 refusedLocally(t, h, host+" attach", err)
79 err = e.cfg.Console.RestartHost(h.ctx(), "sheets", host, "")
80 refusedLocally(t, h, host+" restart", err)
81 }
82 if len(h.con.Requests) != before {
83 t.Fatal("a refused request reached the console")
84 }
85}
86
87func TestGuardRefusesDeleteOutsideADeprovisionRun(t *testing.T) {
88 h, e := guardHarness(t)
89 before := len(h.con.Requests)
90 refusedLocally(t, h, "delete instance", e.cfg.Console.DeleteInstance(h.ctx(), "sheets", RouteHost("alpha", "sheets", account)))
91 refusedLocally(t, h, "detach domain", e.cfg.Console.DetachDomain(h.ctx(), "sheets", PublicHost("alpha", "sheets")))
92 // A deprovision run for calls does not admit a DELETE for sheets.
93 h.tick(homeop.WindowLifetime * 1e9)
94 run, err := e.RetireApps(h.ctx(), "alpha", []string{"calls"}, "alpha")
95 if err != nil {
96 t.Fatal(err)
97 }
98 ctx := WithDeprovision(h.ctx(), run)
99 refusedLocally(t, h, "delete for an app the run does not name", e.cfg.Console.DeleteInstance(ctx, "sheets", RouteHost("alpha", "sheets", account)))
100 // A create-team run id is not a deprovision run.
101 refusedLocally(t, h, "delete under a create run", e.cfg.Console.DeleteInstance(WithDeprovision(h.ctx(), 1), "calls", RouteHost("alpha", "calls", account)))
102 if len(h.con.Requests) != before {
103 t.Fatal("a refused request reached the console")
104 }
105 if _, _, ok := h.con.Instance(RouteHost("alpha", "sheets", account)); !ok {
106 t.Fatal("sheets instance deleted")
107 }
108}
109
110func TestGuardRefusesPlatformManagedKeys(t *testing.T) {
111 h, e := guardHarness(t)
112 before := len(h.con.Requests)
113 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"} {
114 _, err := e.cfg.Console.PutConfig(h.ctx(), "sheets", "alpha", map[string]console.SetVar{k: {Kind: "env", Value: "x"}}, nil, "")
115 refusedLocally(t, h, "set "+k, err)
116 _, err = e.cfg.Console.PutConfig(h.ctx(), "sheets", "alpha", nil, []string{k}, "")
117 refusedLocally(t, h, "unset "+k, err)
118 }
119 // Memoria's AI pair is Paul's alone: never set, in any kind; a
120 // fork's blanked MEMORIA_AI may still be unset.
121 // So is Equipo's execution pair (team-bundle slice 3b).
122 for _, k := range []string{"MEMORIA_AI", "MEMORIA_TENSORX_KEY", "EQUIPO_EXECUTION_ENABLED", "EQUIPO_EXECUTION_MARKER_PATH"} {
123 for _, v := range []console.SetVar{{Kind: "env", Value: "on"}, {Kind: "secret", Envelope: "x"}} {
124 _, err := e.cfg.Console.PutConfig(h.ctx(), "sheets", "alpha", map[string]console.SetVar{k: v}, nil, "")
125 refusedLocally(t, h, "set "+k, err)
126 }
127 }
128 if len(h.con.Requests) != before {
129 t.Fatal("a refused request reached the console")
130 }
131 if _, err := e.cfg.Console.PutConfig(h.ctx(), "sheets", "alpha", nil, []string{"MEMORIA_AI"}, ""); err != nil {
132 t.Fatalf("unset of MEMORIA_AI refused: %v", err)
133 }
134 // And the recipe never names one.
135 for _, a := range Apps {
136 for _, k := range Recipe(a, Inputs{Label: "alpha"}) {
137 if console.PlatformManaged(k.Name) {
138 t.Fatalf("%s recipe names platform-managed %s", a.Slug, k.Name)
139 }
140 if console.OperatorOnly(k.Name) {
141 t.Fatalf("%s recipe names operator-only %s", a.Slug, k.Name)
142 }
143 }
144 }
145}
146
147// TestGuardAllowsPreflightReadsOnly: a preflight may read a proposed
148// label's hosts and environment (checking a label must not burn it), but
149// never write them.
150func TestGuardAllowsPreflightReadsOnly(t *testing.T) {
151 h, e := guardHarness(t)
152 ctx := withProposal(h.ctx(), "delta", "delta", []string{"docs"})
153 if _, _, err := e.cfg.Console.GetConfig(ctx, "docs", "delta"); err != nil {
154 t.Fatalf("preflight read: %v", err)
155 }
156 if _, _, err := e.cfg.Console.GetInstance(ctx, "docs", RouteHost("delta", "docs", account)); err != nil {
157 t.Fatalf("preflight read: %v", err)
158 }
159 _, err := e.cfg.Console.PutConfig(ctx, "docs", "delta", map[string]console.SetVar{"X": {Kind: "env", Value: "1"}}, nil, "")
160 refusedLocally(t, h, "preflight write", err)
161 refusedLocally(t, h, "preflight create", e.cfg.Console.CreateInstance(ctx, "docs", RouteHost("delta", "docs", account), "delta", ""))
162}
163