| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "maps" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/alta/console" |
| 9 | "amadan.net/oficina/alta/console/fakeconsole" |
| 10 | "amadan.net/oficina/alta/homeop" |
| 11 | ) |
| 12 | |
| 13 | // The names are CARLOS's (internal/objstore EnvNames, default prefix |
| 14 | // CARLOS_STORE) and the ones rastrillo's blobs.S3FromEnv reads; every one |
| 15 | // is the platform's alone. |
| 16 | func TestStoreKeysAreCARLOSsAndPlatformManaged(t *testing.T) { |
| 17 | want := map[string]bool{"CARLOS_STORE_BUCKET": false, "CARLOS_STORE_REGION": false, "CARLOS_STORE_ENDPOINT": false, "CARLOS_STORE_ACCESS_KEY": true, "CARLOS_STORE_SECRET_KEY": true} |
| 18 | if len(StoreKeys) != len(want) { |
| 19 | t.Fatalf("store keys %v", StoreKeys) |
| 20 | } |
| 21 | for _, k := range StoreKeys { |
| 22 | secret, ok := want[k.Name] |
| 23 | if !ok || secret != k.Secret || k.Optional != (k.Name == "CARLOS_STORE_ENDPOINT") { |
| 24 | t.Fatalf("store key %+v", k) |
| 25 | } |
| 26 | if !console.PlatformManaged(k.Name) { |
| 27 | t.Fatalf("%s is not platform-managed", k.Name) |
| 28 | } |
| 29 | } |
| 30 | for _, a := range Apps { |
| 31 | if a.Store != (a.Slug == "slides") { |
| 32 | t.Fatalf("%s store %v", a.Slug, a.Store) |
| 33 | } |
| 34 | for _, p := range Profiles { |
| 35 | for _, k := range Recipe(a, Inputs{Label: "x", RunOptions: RunOptions{Profile: p, Samples: true}}) { |
| 36 | if console.PlatformManaged(k.Name) { |
| 37 | t.Fatalf("%s %s recipe names %s", a.Slug, p, k.Name) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Paul's ruling, 2026-11-12: Slides' private-storage policy is on, so |
| 45 | // every Slides instance Alta creates arrives with its own store. The run |
| 46 | // finishes and the audit passes, under either profile, with or without a |
| 47 | // deployment endpoint; Alta writes none of the store keys. |
| 48 | func TestSlidesWithItsOwnStorePassesTheAudit(t *testing.T) { |
| 49 | for _, c := range []struct { |
| 50 | label string |
| 51 | opt RunOptions |
| 52 | endpoint string |
| 53 | }{ |
| 54 | {"store-out", RunOptions{Profile: ProfileOutside}, ""}, |
| 55 | {"store-own", RunOptions{Profile: ProfileOwn}, "https://s3.example.test"}, |
| 56 | } { |
| 57 | h := newHarness(t) |
| 58 | h.initialized() |
| 59 | h.con.PrivateStorage["slides"] = true |
| 60 | h.con.StoreEndpoint = c.endpoint |
| 61 | e := h.engine(nil) |
| 62 | run, err := e.CreateTeam(h.ctx(), NewTeam{Label: c.label, Name: "Store", Owner: ownerCanary, Apps: []string{"slides", "docs"}, Options: c.opt}) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | h.confirm(homeop.KindCreateTeam) |
| 67 | if r := h.drive(e, run, 30); r.State != "done" { |
| 68 | t.Fatalf("%s: %s %s", c.label, r.State, r.Code) |
| 69 | } |
| 70 | vars, _, _ := h.con.Env("slides", c.label) |
| 71 | for _, k := range StoreKeys { |
| 72 | if _, ok := vars[k.Name]; !ok && !(k.Optional && c.endpoint == "") { |
| 73 | t.Fatalf("%s: no %s", c.label, k.Name) |
| 74 | } |
| 75 | } |
| 76 | if got := vars["CARLOS_STORE_BUCKET"].Value; got != "carlos-"+account+"-slides-"+c.label { |
| 77 | t.Fatalf("the bucket was rewritten: %q", got) |
| 78 | } |
| 79 | slides, _ := AppBySlug("slides") |
| 80 | if err := e.AuditEnvironment(h.ctx(), c.label, slides); err != nil { |
| 81 | t.Fatalf("%s audit: %v", c.label, err) |
| 82 | } |
| 83 | if h.scan("carlos-" + account + "-slides-" + c.label) { |
| 84 | t.Fatal("a store coordinate reached Alta's database") |
| 85 | } |
| 86 | // Docs has no store: the policy is Slides' alone. |
| 87 | if vars, _, _ := h.con.Env("docs", c.label); vars["CARLOS_STORE_BUCKET"] != (fakeconsole.Var{}) { |
| 88 | t.Fatal("docs got a store") |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // The store is Slides' alone: any other app holding the keys fails the |
| 94 | // audit, and Slides holding part of the set, or a key in the wrong kind, |
| 95 | // fails too. |
| 96 | func TestStoreKeysOutsideSlidesOrIncompleteFailTheAudit(t *testing.T) { |
| 97 | h := newHarness(t) |
| 98 | h.initialized() |
| 99 | e := h.engine(nil) |
| 100 | apps := []string{"docs", "sheets", "slides", "calendar", "calls", "memoria"} |
| 101 | run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "nostore", Name: "No store", Owner: ownerCanary, Apps: apps}) |
| 102 | if err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | h.confirm(homeop.KindCreateTeam) |
| 106 | if r := h.drive(e, run, 40); r.State != "done" { |
| 107 | t.Fatalf("%s %s", r.State, r.Code) |
| 108 | } |
| 109 | secret := fakeVar{Kind: "secret", Envelope: sealFor(t, h, "slides", "store-secret")} |
| 110 | store := map[string]fakeVar{ |
| 111 | "CARLOS_STORE_BUCKET": {Kind: "env", Value: "b"}, |
| 112 | "CARLOS_STORE_REGION": {Kind: "env", Value: "eu-west-1"}, |
| 113 | "CARLOS_STORE_ACCESS_KEY": secret, |
| 114 | "CARLOS_STORE_SECRET_KEY": secret, |
| 115 | } |
| 116 | audit := func(slug string, extra map[string]fakeVar, drop ...string) error { |
| 117 | t.Helper() |
| 118 | base, owner, _ := h.con.Env(slug, "nostore") |
| 119 | vars := maps.Clone(base) |
| 120 | maps.Copy(vars, extra) |
| 121 | for _, d := range drop { |
| 122 | delete(vars, d) |
| 123 | } |
| 124 | h.con.SetEnv(slug, "nostore", owner, vars) |
| 125 | defer h.con.SetEnv(slug, "nostore", owner, base) |
| 126 | app, _ := AppBySlug(slug) |
| 127 | return e.AuditEnvironment(h.ctx(), "nostore", app) |
| 128 | } |
| 129 | var fl *Fail |
| 130 | for _, slug := range apps { |
| 131 | err := audit(slug, store) |
| 132 | if slug == "slides" { |
| 133 | if err != nil { |
| 134 | t.Fatalf("slides with its store: %v", err) |
| 135 | } |
| 136 | continue |
| 137 | } |
| 138 | if !errors.As(err, &fl) || fl.Code != "audit-extra-key" { |
| 139 | t.Fatalf("%s with a store: %v", slug, err) |
| 140 | } |
| 141 | } |
| 142 | if err := audit("slides", nil); err != nil { |
| 143 | t.Fatalf("slides without a store (created before the policy): %v", err) |
| 144 | } |
| 145 | for _, drop := range []string{"CARLOS_STORE_BUCKET", "CARLOS_STORE_REGION", "CARLOS_STORE_ACCESS_KEY", "CARLOS_STORE_SECRET_KEY"} { |
| 146 | if err := audit("slides", store, drop); !errors.As(err, &fl) || fl.Code != "audit-store-partial" { |
| 147 | t.Fatalf("slides store without %s: %v", drop, err) |
| 148 | } |
| 149 | } |
| 150 | plain := maps.Clone(store) |
| 151 | plain["CARLOS_STORE_SECRET_KEY"] = fakeVar{Kind: "env", Value: "plain"} |
| 152 | if err := audit("slides", plain); !errors.As(err, &fl) || fl.Code != "audit-kind" { |
| 153 | t.Fatalf("plain secret key: %v", err) |
| 154 | } |
| 155 | sealedBucket := maps.Clone(store) |
| 156 | sealedBucket["CARLOS_STORE_ENDPOINT"] = secret |
| 157 | if err := audit("slides", sealedBucket); !errors.As(err, &fl) || fl.Code != "audit-kind" { |
| 158 | t.Fatalf("sealed endpoint: %v", err) |
| 159 | } |
| 160 | // A key outside CARLOS's set, under the store's prefix, is no |
| 161 | // coordinate of it. |
| 162 | odd := maps.Clone(store) |
| 163 | odd["CARLOS_STORE_OTHER"] = fakeVar{Kind: "env", Value: "x"} |
| 164 | if err := audit("slides", odd); !errors.As(err, &fl) || fl.Code != "audit-extra-key" { |
| 165 | t.Fatalf("unknown store key: %v", err) |
| 166 | } |
| 167 | } |
| 168 | |