package provision import ( "errors" "maps" "testing" "amadan.net/oficina/alta/console" "amadan.net/oficina/alta/console/fakeconsole" "amadan.net/oficina/alta/homeop" ) // The names are CARLOS's (internal/objstore EnvNames, default prefix // CARLOS_STORE) and the ones rastrillo's blobs.S3FromEnv reads; every one // is the platform's alone. func TestStoreKeysAreCARLOSsAndPlatformManaged(t *testing.T) { 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} if len(StoreKeys) != len(want) { t.Fatalf("store keys %v", StoreKeys) } for _, k := range StoreKeys { secret, ok := want[k.Name] if !ok || secret != k.Secret || k.Optional != (k.Name == "CARLOS_STORE_ENDPOINT") { t.Fatalf("store key %+v", k) } if !console.PlatformManaged(k.Name) { t.Fatalf("%s is not platform-managed", k.Name) } } for _, a := range Apps { if a.Store != (a.Slug == "slides") { t.Fatalf("%s store %v", a.Slug, a.Store) } for _, p := range Profiles { for _, k := range Recipe(a, Inputs{Label: "x", RunOptions: RunOptions{Profile: p, Samples: true}}) { if console.PlatformManaged(k.Name) { t.Fatalf("%s %s recipe names %s", a.Slug, p, k.Name) } } } } } // Paul's ruling, 2026-11-12: Slides' private-storage policy is on, so // every Slides instance Alta creates arrives with its own store. The run // finishes and the audit passes, under either profile, with or without a // deployment endpoint; Alta writes none of the store keys. func TestSlidesWithItsOwnStorePassesTheAudit(t *testing.T) { for _, c := range []struct { label string opt RunOptions endpoint string }{ {"store-out", RunOptions{Profile: ProfileOutside}, ""}, {"store-own", RunOptions{Profile: ProfileOwn}, "https://s3.example.test"}, } { h := newHarness(t) h.initialized() h.con.PrivateStorage["slides"] = true h.con.StoreEndpoint = c.endpoint e := h.engine(nil) run, err := e.CreateTeam(h.ctx(), NewTeam{Label: c.label, Name: "Store", Owner: ownerCanary, Apps: []string{"slides", "docs"}, Options: c.opt}) if err != nil { t.Fatal(err) } h.confirm(homeop.KindCreateTeam) if r := h.drive(e, run, 30); r.State != "done" { t.Fatalf("%s: %s %s", c.label, r.State, r.Code) } vars, _, _ := h.con.Env("slides", c.label) for _, k := range StoreKeys { if _, ok := vars[k.Name]; !ok && !(k.Optional && c.endpoint == "") { t.Fatalf("%s: no %s", c.label, k.Name) } } if got := vars["CARLOS_STORE_BUCKET"].Value; got != "carlos-"+account+"-slides-"+c.label { t.Fatalf("the bucket was rewritten: %q", got) } slides, _ := AppBySlug("slides") if err := e.AuditEnvironment(h.ctx(), c.label, slides); err != nil { t.Fatalf("%s audit: %v", c.label, err) } if h.scan("carlos-" + account + "-slides-" + c.label) { t.Fatal("a store coordinate reached Alta's database") } // Docs has no store: the policy is Slides' alone. if vars, _, _ := h.con.Env("docs", c.label); vars["CARLOS_STORE_BUCKET"] != (fakeconsole.Var{}) { t.Fatal("docs got a store") } } } // The store is Slides' alone: any other app holding the keys fails the // audit, and Slides holding part of the set, or a key in the wrong kind, // fails too. func TestStoreKeysOutsideSlidesOrIncompleteFailTheAudit(t *testing.T) { h := newHarness(t) h.initialized() e := h.engine(nil) apps := []string{"docs", "sheets", "slides", "calendar", "calls", "memoria"} run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "nostore", Name: "No store", Owner: ownerCanary, Apps: apps}) if err != nil { t.Fatal(err) } h.confirm(homeop.KindCreateTeam) if r := h.drive(e, run, 40); r.State != "done" { t.Fatalf("%s %s", r.State, r.Code) } secret := fakeVar{Kind: "secret", Envelope: sealFor(t, h, "slides", "store-secret")} store := map[string]fakeVar{ "CARLOS_STORE_BUCKET": {Kind: "env", Value: "b"}, "CARLOS_STORE_REGION": {Kind: "env", Value: "eu-west-1"}, "CARLOS_STORE_ACCESS_KEY": secret, "CARLOS_STORE_SECRET_KEY": secret, } audit := func(slug string, extra map[string]fakeVar, drop ...string) error { t.Helper() base, owner, _ := h.con.Env(slug, "nostore") vars := maps.Clone(base) maps.Copy(vars, extra) for _, d := range drop { delete(vars, d) } h.con.SetEnv(slug, "nostore", owner, vars) defer h.con.SetEnv(slug, "nostore", owner, base) app, _ := AppBySlug(slug) return e.AuditEnvironment(h.ctx(), "nostore", app) } var fl *Fail for _, slug := range apps { err := audit(slug, store) if slug == "slides" { if err != nil { t.Fatalf("slides with its store: %v", err) } continue } if !errors.As(err, &fl) || fl.Code != "audit-extra-key" { t.Fatalf("%s with a store: %v", slug, err) } } if err := audit("slides", nil); err != nil { t.Fatalf("slides without a store (created before the policy): %v", err) } for _, drop := range []string{"CARLOS_STORE_BUCKET", "CARLOS_STORE_REGION", "CARLOS_STORE_ACCESS_KEY", "CARLOS_STORE_SECRET_KEY"} { if err := audit("slides", store, drop); !errors.As(err, &fl) || fl.Code != "audit-store-partial" { t.Fatalf("slides store without %s: %v", drop, err) } } plain := maps.Clone(store) plain["CARLOS_STORE_SECRET_KEY"] = fakeVar{Kind: "env", Value: "plain"} if err := audit("slides", plain); !errors.As(err, &fl) || fl.Code != "audit-kind" { t.Fatalf("plain secret key: %v", err) } sealedBucket := maps.Clone(store) sealedBucket["CARLOS_STORE_ENDPOINT"] = secret if err := audit("slides", sealedBucket); !errors.As(err, &fl) || fl.Code != "audit-kind" { t.Fatalf("sealed endpoint: %v", err) } // A key outside CARLOS's set, under the store's prefix, is no // coordinate of it. odd := maps.Clone(store) odd["CARLOS_STORE_OTHER"] = fakeVar{Kind: "env", Value: "x"} if err := audit("slides", odd); !errors.As(err, &fl) || fl.Code != "audit-extra-key" { t.Fatalf("unknown store key: %v", err) } }