| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "crypto/rand" |
| 5 | "crypto/sha256" |
| 6 | "encoding/base64" |
| 7 | "encoding/hex" |
| 8 | stdjson "encoding/json" |
| 9 | "fmt" |
| 10 | "regexp" |
| 11 | "slices" |
| 12 | "strings" |
| 13 | |
| 14 | "amadan.net/oficina/home/roster" |
| 15 | ) |
| 16 | |
| 17 | // LabelPattern is Alta's label rule (§5): deliberately tighter than |
| 18 | // Home's ValidLabel, so every derived host stays well inside DNS limits. |
| 19 | var LabelPattern = regexp.MustCompile(`^[a-z][a-z0-9-]{1,30}[a-z0-9]$`) |
| 20 | |
| 21 | // ReservedLabels are never offered to a new team. This is the one list |
| 22 | // (a test pins it): a label becomes the first DNS label of every team |
| 23 | // host and the team's name at Home, so it must never collide with a name |
| 24 | // the suite or the platform already answers to (Paul, 2026-11-12). |
| 25 | var ReservedLabels = map[string]bool{ |
| 26 | // The suite's own canonical apps (docs.oficina.works and the rest), |
| 27 | // which Home adopts as an existing team on its own path (Home |
| 28 | // discussion 103), and the dashboard team they already carry. |
| 29 | "oficina": true, |
| 30 | // Every app's name, provisioned by Alta or not yet: a team called |
| 31 | // "docs" would own docs.calls.oficina.works and read as an app. |
| 32 | "docs": true, "sheets": true, "slides": true, "calendar": true, |
| 33 | "calls": true, "memoria": true, "equipo": true, "fichas": true, "mapa": true, |
| 34 | // The suite's and the platform's own hosts and paths. |
| 35 | "api": true, "auth": true, "healthz": true, "static": true, "www": true, |
| 36 | "id": true, "admin": true, "portero": true, "home": true, "alta": true, |
| 37 | "base": true, "forget": true, |
| 38 | } |
| 39 | |
| 40 | // LabelRefusal is the code a label is refused with at step 0, or "" for a |
| 41 | // label Alta will offer. A label never holds "--": that is the shape |
| 42 | // of IDNA's xn-- (punycode) labels, which would render as another name. |
| 43 | func LabelRefusal(label string) string { |
| 44 | switch { |
| 45 | case !LabelPattern.MatchString(label): |
| 46 | return "label-shape" |
| 47 | case strings.Contains(label, "--"): |
| 48 | return "label-double-hyphen" |
| 49 | case ReservedLabels[label]: |
| 50 | return "label-reserved" |
| 51 | } |
| 52 | return "" |
| 53 | } |
| 54 | |
| 55 | // PublicDomain is the only public suffix Home's directory admits for a |
| 56 | // team instance (internal/home directoryRouteAllowed). |
| 57 | const PublicDomain = "oficina.works" |
| 58 | |
| 59 | // App is one Oficina app as Alta provisions it: the env prefix it reads, |
| 60 | // whether it has roster mode, which SMTP prefixes its binary reads, and |
| 61 | // whether it needs the platform relay. |
| 62 | type App struct { |
| 63 | // AdoptionOnly admits an existing canonical instance, without offering an |
| 64 | // unimplemented recipe for fresh team instances. No app is adoption-only |
| 65 | // since Equipo and Base gained recipes (team-bundle spec, slices 1–2); |
| 66 | // the flag stays for the next app that is. |
| 67 | AdoptionOnly bool |
| 68 | // Seeded is an app with no Home roster mode whose owners are seeded |
| 69 | // from its environment (EQUIPO_OWNERS, BASE_OWNER). It reaches |
| 70 | // steps 11–12 through its seeded /api/team probe instead of a roster |
| 71 | // (team-bundle spec §1.2), and its recipe writes the owner's address. |
| 72 | Seeded bool |
| 73 | // OwnOnly is an app offered only under profile own (team-bundle spec |
| 74 | // §5.1 rule 3): its roster diverges from Home's, which is acceptable |
| 75 | // only for Paul's own teams until it has roster mode. |
| 76 | OwnOnly bool |
| 77 | Slug string |
| 78 | Name string |
| 79 | Prefix string |
| 80 | // Roster is true for an app with team (roster) mode on main. Only |
| 81 | // those reach steps 11–12; the rest stop at 10 with their directory |
| 82 | // row pending (§6). |
| 83 | Roster bool |
| 84 | // MailPrefixes are the <PREFIX>_SMTP_* sets the binary reads, in its |
| 85 | // own order. The mail walk writes the set the app declared; a set the |
| 86 | // binary does not read leaves it without mail, which step 7 refuses. |
| 87 | MailPrefixes []string |
| 88 | // SubmitPrefix is the one prefix whose From the binary reads when it |
| 89 | // is switched to CARLOS mail submission: it calls rastrillo's |
| 90 | // mail.FromCarlos with this prefix's stem, so it reads only |
| 91 | // MailFromKey(SubmitPrefix). CARLOS delivers the From under the stem |
| 92 | // of the app's declared prefix, so a switched app declared under any |
| 93 | // other prefix (Slides under SLIDES_SMTP, say) gets a From its binary |
| 94 | // never reads, and step 7 refuses it. CARLOS_SMTP for every app but |
| 95 | // Docs, which is declared and read as DOCS_SMTP. Required: an app |
| 96 | // with none refuses every From. |
| 97 | SubmitPrefix string |
| 98 | // Relay is true when CARLOS_RELAY is a PLATFORM key (Calls). |
| 99 | Relay bool |
| 100 | // Store is true for an app whose instances may carry an object store |
| 101 | // of their own (Slides: the private-storage policy). Its StoreKeys |
| 102 | // are PLATFORM keys the audit admits, and never requires. |
| 103 | Store bool |
| 104 | // Dashboard is true for an app whose binary reads |
| 105 | // OFICINA_DASHBOARD_TEAM (internal/<app>/dashboard.go): the own |
| 106 | // profile names the team there. |
| 107 | Dashboard bool |
| 108 | } |
| 109 | |
| 110 | // Apps is the compiled registry, in Home's order (directory_runtime.go). |
| 111 | var Apps = []App{ |
| 112 | {Slug: "docs", Name: "Docs", Prefix: "DOCS", Roster: true, MailPrefixes: []string{"DOCS_SMTP"}, SubmitPrefix: "DOCS_SMTP", Dashboard: true}, |
| 113 | {Slug: "sheets", Name: "Sheets", Prefix: "SHEETS", Roster: true, MailPrefixes: []string{"CARLOS_SMTP"}, SubmitPrefix: "CARLOS_SMTP", Dashboard: true}, |
| 114 | {Slug: "slides", Name: "Slides", Prefix: "SLIDES", Roster: true, MailPrefixes: []string{"SLIDES_SMTP", "CARLOS_SMTP"}, SubmitPrefix: "CARLOS_SMTP", Dashboard: true, Store: true}, |
| 115 | {Slug: "calendar", Name: "Calendar", Prefix: "CALENDAR", Roster: true, MailPrefixes: []string{"CALENDAR_SMTP", "CARLOS_SMTP"}, SubmitPrefix: "CARLOS_SMTP", Dashboard: true}, |
| 116 | {Slug: "calls", Name: "Calls", Prefix: "CALLS", Roster: true, MailPrefixes: []string{"CALLS_SMTP", "CARLOS_SMTP"}, SubmitPrefix: "CARLOS_SMTP", Relay: true}, |
| 117 | {Slug: "memoria", Name: "Memoria", Prefix: "MEMORIA", Roster: true, MailPrefixes: []string{"CARLOS_SMTP"}, SubmitPrefix: "CARLOS_SMTP"}, |
| 118 | {Slug: "equipo", Name: "Equipo", Prefix: "EQUIPO", Seeded: true, OwnOnly: true, MailPrefixes: []string{"CARLOS_SMTP"}, SubmitPrefix: "CARLOS_SMTP"}, |
| 119 | {Slug: "base", Name: "Base", Prefix: "BASE", Seeded: true, OwnOnly: true, MailPrefixes: []string{"CARLOS_SMTP"}, SubmitPrefix: "CARLOS_SMTP"}, |
| 120 | } |
| 121 | |
| 122 | // ProvisioningApps are the apps with supported fresh-instance recipes. |
| 123 | func ProvisioningApps() []App { |
| 124 | var out []App |
| 125 | for _, a := range Apps { |
| 126 | if !a.AdoptionOnly { |
| 127 | out = append(out, a) |
| 128 | } |
| 129 | } |
| 130 | return out |
| 131 | } |
| 132 | |
| 133 | // AppBySlug finds a registry app. |
| 134 | func AppBySlug(slug string) (App, bool) { |
| 135 | for _, a := range Apps { |
| 136 | if a.Slug == slug { |
| 137 | return a, true |
| 138 | } |
| 139 | } |
| 140 | return App{}, false |
| 141 | } |
| 142 | |
| 143 | // Slugs is the registry's slugs. |
| 144 | func Slugs() []string { |
| 145 | out := make([]string, len(Apps)) |
| 146 | for i, a := range Apps { |
| 147 | out[i] = a.Slug |
| 148 | } |
| 149 | return out |
| 150 | } |
| 151 | |
| 152 | // SortApps orders apps as the registry does and drops unknowns. |
| 153 | func SortApps(apps []string) []string { |
| 154 | var out []string |
| 155 | for _, a := range Apps { |
| 156 | if slices.Contains(apps, a.Slug) { |
| 157 | out = append(out, a.Slug) |
| 158 | } |
| 159 | } |
| 160 | return out |
| 161 | } |
| 162 | |
| 163 | // Hosts derives a team instance's two hostnames. |
| 164 | func RouteHost(label, app, account string) string { |
| 165 | return label + "-" + app + "." + account + ".oncarlos.com" |
| 166 | } |
| 167 | |
| 168 | // PublicHost, PublicOrigin and ConsumerID name a team's public tuple. The |
| 169 | // adoption label is the one exception to label.app: Home's canonical |
| 170 | // tuple for the six existing instances is host <app>.oficina.works and |
| 171 | // consumer <app> (homeop.CanonicalOrigin). Every other label is unchanged. |
| 172 | func PublicHost(label, app string) string { |
| 173 | if label == AdoptionLabel { |
| 174 | return app + "." + PublicDomain |
| 175 | } |
| 176 | return label + "." + app + "." + PublicDomain |
| 177 | } |
| 178 | func PublicOrigin(label, app string) string { |
| 179 | return "https://" + PublicHost(label, app) |
| 180 | } |
| 181 | func ConsumerID(label, app string) string { |
| 182 | if label == AdoptionLabel { |
| 183 | return app |
| 184 | } |
| 185 | return label + "-" + app |
| 186 | } |
| 187 | |
| 188 | // AdoptionLabel is the directory label of the existing suite's team. It is |
| 189 | // reserved (ReservedLabels), so no ordinary run reaches the canonical |
| 190 | // answers above. |
| 191 | const AdoptionLabel = "oficina" |
| 192 | |
| 193 | // Class is a recipe key's treatment (§5). |
| 194 | type Class string |
| 195 | |
| 196 | const ( |
| 197 | Generated Class = "generated" |
| 198 | Set Class = "set" |
| 199 | Platform Class = "platform" |
| 200 | ) |
| 201 | |
| 202 | // Key is one recipe key. Value is known for SET keys only; a GENERATED |
| 203 | // key's value exists only on its way to the console, sealed. |
| 204 | type Key struct { |
| 205 | Name string |
| 206 | Class Class |
| 207 | Value string |
| 208 | Secret bool |
| 209 | // OwnerAddress marks a SET key whose value is the owner's address |
| 210 | // (the stopgap allowlist). After step 13 Alta no longer knows it. |
| 211 | OwnerAddress bool |
| 212 | // FromHome marks a GENERATED key whose value Home minted (the roster |
| 213 | // bearer), fetched by exact replay of step 5. |
| 214 | FromHome bool |
| 215 | } |
| 216 | |
| 217 | // HomeConfig is the Home material every recipe carries: Alta's pinned |
| 218 | // copies, none of them secret. |
| 219 | type HomeConfig struct { |
| 220 | Issuer string // credential origin, https://id.oficina.works |
| 221 | Directory string // https://oficina.works |
| 222 | KeysBase64 string // the assertion keys manifest, as ALTA_HOME_KEYS_BASE64 |
| 223 | Provisioner string // the directory's provisioning public key, base64url |
| 224 | } |
| 225 | |
| 226 | // Profile is a run's recipe variant (Paul, 2026-11-12). |
| 227 | type Profile string |
| 228 | |
| 229 | const ( |
| 230 | // ProfileOutside is the recipes as the design has them: for anyone |
| 231 | // else's team (an outsider's), and the default. |
| 232 | ProfileOutside Profile = "outside" |
| 233 | // ProfileOwn is for Paul's own teams. It differs from outside only in |
| 234 | // Calls' quality reports (on), each dashboard app's |
| 235 | // OFICINA_DASHBOARD_TEAM (the label), and Memoria's AI pair, which the |
| 236 | // audit admits once Paul has set it by hand (OperatorKeys). Alta never |
| 237 | // holds or writes the TensorX key. |
| 238 | ProfileOwn Profile = "own" |
| 239 | ) |
| 240 | |
| 241 | // Profiles is every profile, in the order the run form offers them. |
| 242 | var Profiles = []Profile{ProfileOutside, ProfileOwn} |
| 243 | |
| 244 | // RunOptions is what Paul chooses per run beside its apps, recorded on the |
| 245 | // run and shown with its plan. |
| 246 | type RunOptions struct { |
| 247 | Profile Profile |
| 248 | // Samples sets DOCS_SAMPLE_DOCUMENTS: a new person's Docs starts with |
| 249 | // the sample documents. Separate from the profile, because only a |
| 250 | // sample team gets it. |
| 251 | Samples bool |
| 252 | } |
| 253 | |
| 254 | // Normal is o with an unset profile read as outside; ok is false for a |
| 255 | // profile Alta does not know. |
| 256 | func (o RunOptions) Normal() (RunOptions, bool) { |
| 257 | switch o.Profile { |
| 258 | case "": |
| 259 | o.Profile = ProfileOutside |
| 260 | case ProfileOutside, ProfileOwn: |
| 261 | default: |
| 262 | return o, false |
| 263 | } |
| 264 | return o, true |
| 265 | } |
| 266 | |
| 267 | // Inputs is what a recipe is built from. |
| 268 | type Inputs struct { |
| 269 | Label string |
| 270 | // Account is the CARLOS account's sqid, for a value that names an |
| 271 | // instance's platform host (Equipo's execution marker, OperatorKeys). |
| 272 | Account string |
| 273 | Owner string // empty once deleted (§4) |
| 274 | TeamSQID string |
| 275 | Record []byte // step 5's signed record (Docs) |
| 276 | Home HomeConfig |
| 277 | CallsZone string |
| 278 | // RunOptions is the run's profile and samples choice. The zero value |
| 279 | // is the outside recipe without samples. |
| 280 | RunOptions |
| 281 | } |
| 282 | |
| 283 | // Recipe is one app's GENERATED and SET keys. PLATFORM keys are added by |
| 284 | // PlatformKeys once the walks have delivered them. |
| 285 | func Recipe(app App, in Inputs) []Key { |
| 286 | p := app.Prefix |
| 287 | keys := []Key{ |
| 288 | {Name: p + "_ORIGIN", Class: Set, Value: PublicOrigin(in.Label, app.Slug)}, |
| 289 | {Name: p + "_INSTANCE_KEY", Class: Generated, Secret: true}, |
| 290 | {Name: p + "_HOME_ISSUER", Class: Set, Value: in.Home.Issuer}, |
| 291 | {Name: p + "_HOME_CONSUMER_ID", Class: Set, Value: ConsumerID(in.Label, app.Slug)}, |
| 292 | {Name: p + "_HOME_KEYS_BASE64", Class: Set, Value: in.Home.KeysBase64}, |
| 293 | {Name: p + "_HOME_DEK_BASE64", Class: Generated, Secret: true}, |
| 294 | {Name: "HOME_DIRECTORY_ORIGIN", Class: Set, Value: in.Home.Directory}, |
| 295 | } |
| 296 | own := in.Profile == ProfileOwn |
| 297 | if own && app.Dashboard { |
| 298 | // The team's own dashboard feed in Home (home/dashboard): the |
| 299 | // provider answers only for this team's name. |
| 300 | keys = append(keys, Key{Name: "OFICINA_DASHBOARD_TEAM", Class: Set, Value: in.Label}) |
| 301 | } |
| 302 | switch app.Slug { |
| 303 | case "docs": |
| 304 | keys = append(keys, |
| 305 | Key{Name: "DOCS_CONTENT_KEY", Class: Generated, Secret: true}, |
| 306 | Key{Name: "DOCS_ALLOWED_ADDRESSES", Class: Set, Value: ""}, |
| 307 | ) |
| 308 | if in.Samples { |
| 309 | // cmd/docs reads it permissively ("1", "true", "yes"; anything |
| 310 | // else is off), and seeds each new person's list once. |
| 311 | keys = append(keys, Key{Name: "DOCS_SAMPLE_DOCUMENTS", Class: Set, Value: "1"}) |
| 312 | } |
| 313 | keys = append(keys, teamKeys(p, in)...) |
| 314 | case "sheets": |
| 315 | // Team mode (oficina/sheets team-mode @ b0264ff): SHEETS_OWNERS is |
| 316 | // not needed beside SHEETS_TEAM_*, so the owner is not written. |
| 317 | keys = append(keys, Key{Name: "SHEETS_DEK", Class: Generated, Secret: true}) |
| 318 | keys = append(keys, teamKeys(p, in)...) |
| 319 | case "slides": |
| 320 | // Uploads (Paul, 2026-11-12): Slides' app-wide private-storage |
| 321 | // policy gives every instance Alta creates, under either profile, |
| 322 | // its environment's own bucket at step 8, and CARLOS's objstore |
| 323 | // walk delivers CARLOS_STORE_* into the environment. Those are |
| 324 | // PLATFORM keys (StoreKeys): never written by Alta, admitted by |
| 325 | // the audit, and optional, since an instance created before the |
| 326 | // policy has none and its uploads answer 503. |
| 327 | // Team mode (oficina/slides team-mode @ 80fd3c9, through Home's |
| 328 | // teammode): SLIDES_ALLOWED_ADDRESSES is optional beside |
| 329 | // SLIDES_TEAM_*, so the owner is not written. |
| 330 | keys = append(keys, teamKeys(p, in)...) |
| 331 | case "calendar": |
| 332 | // Team mode (oficina/calendar #35, team-mode @ e8e509d, through |
| 333 | // Home's teammode): CALENDAR_ALLOWED_ADDRESSES is optional beside |
| 334 | // CALENDAR_TEAM_*, so the owner is not written. |
| 335 | keys = append(keys, Key{Name: "CALENDAR_DEK", Class: Generated, Secret: true}) |
| 336 | keys = append(keys, teamKeys(p, in)...) |
| 337 | case "calls": |
| 338 | // Team mode (oficina/meet #31, team-mode, through Home's |
| 339 | // teammode; spec §16.9): CALLS_ALLOWED_ADDRESSES is optional |
| 340 | // beside CALLS_TEAM_*, so the owner is not written. CARLOS_RELAY |
| 341 | // stays PLATFORM, minted per environment. No CALLS_APNS_*: cmd/calls |
| 342 | // does not read them, so a team's Calls has no push. |
| 343 | keys = append(keys, |
| 344 | Key{Name: "CALLS_DEK", Class: Generated, Secret: true}, |
| 345 | Key{Name: "CALLS_ZONE", Class: Set, Value: in.CallsZone}, |
| 346 | // A team's call-quality logs would land in the operator's |
| 347 | // `carlos logs`; off until that team's owner and Paul agree, |
| 348 | // and on for Paul's own teams. |
| 349 | Key{Name: "CALLS_QUALITY_REPORTS", Class: Set, Value: qualityReports(own)}, |
| 350 | ) |
| 351 | keys = append(keys, teamKeys(p, in)...) |
| 352 | case "memoria": |
| 353 | // AI off: MEMORIA_AI, _TENSORX_KEY, _SIBLINGS, _SIBLING_TOKEN_* |
| 354 | // and _ALLOW_LOGGED_MAIL are simply absent (Paul: AI is off, and |
| 355 | // his key is never used). In both profiles: memoria refuses to |
| 356 | // boot with MEMORIA_AI=on and no MEMORIA_TENSORX_KEY, and Alta |
| 357 | // never holds the key, so an own team's AI is Paul's post-run |
| 358 | // step (OperatorKeys). |
| 359 | // Team mode (oficina/memoria #32, team-mode @ 88f6cec, through |
| 360 | // Home's teammode): MEMORIA_OWNERS is optional beside |
| 361 | // MEMORIA_TEAM_*, so the owner is not written. MEMORIA_TEAM is the |
| 362 | // sibling transport's host label, which in team mode must be the |
| 363 | // first label of MEMORIA_ORIGIN (team_siblings.go): both are the |
| 364 | // team's label. |
| 365 | keys = append(keys, |
| 366 | Key{Name: "MEMORIA_DEK", Class: Generated, Secret: true}, |
| 367 | Key{Name: "MEMORIA_TEAM", Class: Set, Value: in.Label}, |
| 368 | ) |
| 369 | keys = append(keys, teamKeys(p, in)...) |
| 370 | case "equipo": |
| 371 | // Seeded (team-bundle spec §1.2): no roster mode, so the owner is |
| 372 | // written and Alta's wait rule keeps the address until this |
| 373 | // environment is written. Execution is not written here: it is |
| 374 | // Paul's post-run pair (OperatorKeys). The assistant host is the |
| 375 | // label's own Base, written whether or not Base exists yet: |
| 376 | // Equipo admits only that origin, so naming one not yet created is |
| 377 | // harmless, and the value never depends on the run (plan rev 2). |
| 378 | // No EQUIPO_AGENT_*: agent access for fresh teams is a follow-up. |
| 379 | keys = append(keys, |
| 380 | ownerKey("EQUIPO_OWNERS", in), |
| 381 | Key{Name: "EQUIPO_ASSISTANT_HOSTS", Class: Set, Value: PublicOrigin(in.Label, "base")}, |
| 382 | ) |
| 383 | case "base": |
| 384 | // Seeded, no Store (team-bundle spec §2.2): no BASE_PLATFORM_*, |
| 385 | // BASE_APPS_DOMAIN or BASE_ASSERTION_KEY, so installs are off. |
| 386 | // Base comes last (step 0, BaseRefusal): the team has every other |
| 387 | // app, so the tile list is fixed and Ask always has its Equipo. |
| 388 | keys = append(keys, |
| 389 | ownerKey("BASE_OWNER", in), |
| 390 | Key{Name: "BASE_NAME", Class: Set, Value: in.Label}, |
| 391 | Key{Name: "BASE_SLUG", Class: Set, Value: in.Label}, |
| 392 | Key{Name: "BASE_OFICINA_APPS", Class: Set, Value: BaseOficinaApps(in.Label)}, |
| 393 | Key{Name: "BASE_EQUIPO_ORIGIN", Class: Set, Value: PublicOrigin(in.Label, "equipo")}, |
| 394 | ) |
| 395 | } |
| 396 | return keys |
| 397 | } |
| 398 | |
| 399 | // BaseOficinaApps is BASE_OFICINA_APPS for label: every other registry app |
| 400 | // as id=origin, in registry order (Base internal/config/oficina.go). Base |
| 401 | // is not in its own list: an unknown id refuses Base's boot. |
| 402 | func BaseOficinaApps(label string) string { |
| 403 | var parts []string |
| 404 | for _, a := range Apps { |
| 405 | if a.Slug != "base" { |
| 406 | parts = append(parts, a.Slug+"="+PublicOrigin(label, a.Slug)) |
| 407 | } |
| 408 | } |
| 409 | return strings.Join(parts, ",") |
| 410 | } |
| 411 | |
| 412 | // BaseSlugMax is Base's longest BASE_SLUG (Base internal/config maxSlug). |
| 413 | const BaseSlugMax = 19 |
| 414 | |
| 415 | // AppsRefusal is step 0's rule for the apps a run names, beside what the |
| 416 | // team already has (active), under the run's profile: "" to proceed. |
| 417 | func AppsRefusal(label string, apps []string, active map[string]bool, p Profile) string { |
| 418 | has := func(slug string) bool { return active[slug] || slices.Contains(apps, slug) } |
| 419 | for _, slug := range apps { |
| 420 | app, ok := AppBySlug(slug) |
| 421 | switch { |
| 422 | case !ok, app.AdoptionOnly: |
| 423 | return "unknown-app" |
| 424 | case app.OwnOnly && p != ProfileOwn: |
| 425 | return "app-own-only" |
| 426 | } |
| 427 | } |
| 428 | if slices.Contains(apps, "base") { |
| 429 | if len(label) > BaseSlugMax { |
| 430 | return "label-too-long-for-base" |
| 431 | } |
| 432 | for _, a := range Apps { |
| 433 | if a.Slug != "base" && !has(a.Slug) { |
| 434 | return "base-needs-team-apps" |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | return "" |
| 439 | } |
| 440 | |
| 441 | func qualityReports(own bool) string { |
| 442 | if own { |
| 443 | return "on" |
| 444 | } |
| 445 | return "off" |
| 446 | } |
| 447 | |
| 448 | // OperatorKey is a key Paul sets by hand after a run, never Alta: the |
| 449 | // audit admits it, under its profile, only beside every other key of the |
| 450 | // app's OperatorKeys, with its kind and (for an env key) its value. |
| 451 | type OperatorKey struct { |
| 452 | Name string |
| 453 | Secret bool |
| 454 | Value string |
| 455 | } |
| 456 | |
| 457 | // OperatorKeys is app's post-run group under in's profile. On an own |
| 458 | // team: Memoria's AI (MEMORIA_AI=on and the TensorX key, both or |
| 459 | // neither; memoria refuses to boot with one alone), and Equipo's |
| 460 | // execution (team-bundle spec slice 3b: EQUIPO_EXECUTION_ENABLED=1 and |
| 461 | // the marker inside the instance's own CARLOS directory, whose name is |
| 462 | // its platform host; a path naming any other directory fails Equipo's |
| 463 | // boot, and the exact value is what the audit admits). Outside teams |
| 464 | // have none. |
| 465 | func OperatorKeys(app App, in Inputs) []OperatorKey { |
| 466 | if in.Profile != ProfileOwn { |
| 467 | return nil |
| 468 | } |
| 469 | switch app.Slug { |
| 470 | case "equipo": |
| 471 | return []OperatorKey{ |
| 472 | {Name: "EQUIPO_EXECUTION_ENABLED", Value: "1"}, |
| 473 | {Name: "EQUIPO_EXECUTION_MARKER_PATH", Value: ExecutionMarkerPath(in.Label, in.Account)}, |
| 474 | } |
| 475 | case "memoria": |
| 476 | default: |
| 477 | return nil |
| 478 | } |
| 479 | return []OperatorKey{ |
| 480 | {Name: "MEMORIA_AI", Value: "on"}, |
| 481 | {Name: "MEMORIA_TENSORX_KEY", Secret: true}, |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // ExecutionMarkerPath is a team Equipo's EQUIPO_EXECUTION_MARKER_PATH: |
| 486 | // under the instance's own /data/instances/<platform host>, apart from its |
| 487 | // database, as the canonical instance's is. |
| 488 | func ExecutionMarkerPath(label, account string) string { |
| 489 | return "/data/instances/" + RouteHost(label, "equipo", account) + "/execution/witness" |
| 490 | } |
| 491 | |
| 492 | // profileWrites is every env key some run's choice writes into app's |
| 493 | // environment for label beyond the outside recipe, with the exact value |
| 494 | // it writes: a later run may take back out a key that is still exactly |
| 495 | // Alta's own, and nothing else (a foreign value is an extra key). |
| 496 | func profileWrites(app App, label string) map[string]string { |
| 497 | base := map[string]bool{} |
| 498 | for _, k := range Recipe(app, Inputs{Label: label}) { |
| 499 | base[k.Name] = true |
| 500 | } |
| 501 | out := map[string]string{} |
| 502 | for _, p := range Profiles { |
| 503 | for _, k := range Recipe(app, Inputs{Label: label, RunOptions: RunOptions{Profile: p, Samples: true}}) { |
| 504 | if !base[k.Name] && k.Class == Set { |
| 505 | out[k.Name] = k.Value |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | return out |
| 510 | } |
| 511 | |
| 512 | // OperatorState is how much of app's post-run group an environment holds. |
| 513 | type OperatorState int |
| 514 | |
| 515 | const ( |
| 516 | OperatorNone OperatorState = iota // no key of the group (or no group) |
| 517 | OperatorPartial // some, not all |
| 518 | OperatorWhole // all, each with its kind and value |
| 519 | OperatorInvalid // all, but one has the wrong kind or value |
| 520 | ) |
| 521 | |
| 522 | // operatorState reads an environment's keys (name → kind, value) against |
| 523 | // group. |
| 524 | func operatorState(group []OperatorKey, kind func(name string) (k, value string, ok bool)) OperatorState { |
| 525 | held, bad := 0, false |
| 526 | for _, g := range group { |
| 527 | k, v, ok := kind(g.Name) |
| 528 | if !ok { |
| 529 | continue |
| 530 | } |
| 531 | held++ |
| 532 | switch { |
| 533 | case g.Secret && k != "secret", !g.Secret && k != "env", !g.Secret && v != g.Value: |
| 534 | bad = true |
| 535 | } |
| 536 | } |
| 537 | switch { |
| 538 | case held == 0: |
| 539 | return OperatorNone |
| 540 | case held < len(group): |
| 541 | return OperatorPartial |
| 542 | case bad: |
| 543 | return OperatorInvalid |
| 544 | } |
| 545 | return OperatorWhole |
| 546 | } |
| 547 | |
| 548 | // ownerKey is a stopgap allowlist holding the owner's address, for an app |
| 549 | // without roster mode (Track 1's shape). Every app has roster mode now, so |
| 550 | // no recipe uses it; it stays so the address rules (§4: deleted once every |
| 551 | // allowlist is written, retyped if needed again) keep one definition. |
| 552 | func ownerKey(name string, in Inputs) Key { |
| 553 | return Key{Name: name, Class: Set, Value: in.Owner, OwnerAddress: true} |
| 554 | } |
| 555 | |
| 556 | // NeedsOwner reports whether app's recipe writes the owner's address. |
| 557 | func NeedsOwner(app App) bool { |
| 558 | for _, k := range Recipe(app, Inputs{}) { |
| 559 | if k.OwnerAddress { |
| 560 | return true |
| 561 | } |
| 562 | } |
| 563 | return false |
| 564 | } |
| 565 | |
| 566 | // teamKeys is a roster-mode app's <P>_TEAM_* five, as Docs and Sheets read |
| 567 | // them: the team sqid, the directory, the signed provisioning record and |
| 568 | // the directory's provisioning key (SET), and Home's roster bearer |
| 569 | // (GENERATED by Home, sealed, fetched by exact replay of step 5). |
| 570 | func teamKeys(p string, in Inputs) []Key { |
| 571 | return []Key{ |
| 572 | {Name: p + "_TEAM_ROSTER_TOKEN", Class: Generated, Secret: true, FromHome: true}, |
| 573 | {Name: p + "_TEAM_ID", Class: Set, Value: in.TeamSQID}, |
| 574 | {Name: p + "_TEAM_DIRECTORY_ORIGIN", Class: Set, Value: in.Home.Directory}, |
| 575 | {Name: p + "_TEAM_PROVISIONING_BASE64", Class: Set, Value: base64.StdEncoding.EncodeToString(in.Record)}, |
| 576 | {Name: p + "_TEAM_OPERATOR_KEYS_BASE64", Class: Set, Value: operatorKeys(in.Home.Provisioner)}, |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | // TeamKeyNames is the names teamKeys produces for app: the <P>_TEAM_ five |
| 581 | // an adoption must never write. |
| 582 | func TeamKeyNames(app App) []string { |
| 583 | keys := teamKeys(app.Prefix, Inputs{}) |
| 584 | names := make([]string, len(keys)) |
| 585 | for i, k := range keys { |
| 586 | names[i] = k.Name |
| 587 | } |
| 588 | return names |
| 589 | } |
| 590 | |
| 591 | // operatorKeys is <P>_TEAM_OPERATOR_KEYS_BASE64: the directory's |
| 592 | // provisioning key as a one-entry []roster.Key, JSON, standard base64. |
| 593 | func operatorKeys(public string) string { |
| 594 | if public == "" { |
| 595 | return "" |
| 596 | } |
| 597 | raw, err := base64.RawURLEncoding.DecodeString(public) |
| 598 | if err != nil { |
| 599 | return "" |
| 600 | } |
| 601 | sum := sha256.Sum256(raw) |
| 602 | b, _ := stdjson.Marshal([]roster.Key{{ID: hex.EncodeToString(sum[:]), Public: public}}) |
| 603 | return base64.StdEncoding.EncodeToString(b) |
| 604 | } |
| 605 | |
| 606 | // ProvisionerKeys is the same key for roster.VerifyProvisioning. |
| 607 | func ProvisionerKeys(public string) []roster.Key { |
| 608 | raw, err := base64.RawURLEncoding.DecodeString(public) |
| 609 | if err != nil { |
| 610 | return nil |
| 611 | } |
| 612 | sum := sha256.Sum256(raw) |
| 613 | return []roster.Key{{ID: hex.EncodeToString(sum[:]), Public: public}} |
| 614 | } |
| 615 | |
| 616 | var smtpSuffixes = []string{"HOST", "PORT", "USER", "PASS", "FROM"} |
| 617 | |
| 618 | // SMTPKeys is a prefix's five. |
| 619 | func SMTPKeys(prefix string) []string { |
| 620 | out := make([]string, len(smtpSuffixes)) |
| 621 | for i, s := range smtpSuffixes { |
| 622 | out[i] = prefix + "_" + s |
| 623 | } |
| 624 | return out |
| 625 | } |
| 626 | |
| 627 | var smtpKey = regexp.MustCompile(`^([A-Z][A-Z0-9_]*_SMTP)_(HOST|PORT|USER|PASS|FROM)$`) |
| 628 | |
| 629 | // mailFromKey is the From variable CARLOS delivers for an app switched to |
| 630 | // platform mail submission. Its stem is the declared prefix without |
| 631 | // _SMTP: CARLOS_SMTP gives CARLOS_MAIL_FROM, DOCS_SMTP gives |
| 632 | // DOCS_MAIL_FROM (carlos internal/mail MailFromName). |
| 633 | var mailFromKey = regexp.MustCompile(`^([A-Z][A-Z0-9_]*)_MAIL_FROM$`) |
| 634 | |
| 635 | // MailFromKey is the From variable a switched app's environment holds for |
| 636 | // prefix, in place of SMTPKeys(prefix). "" for a prefix that does not end |
| 637 | // in _SMTP, which CARLOS refuses to switch. |
| 638 | func MailFromKey(prefix string) string { |
| 639 | stem, ok := strings.CutSuffix(prefix, "_SMTP") |
| 640 | if !ok || stem == "" { |
| 641 | return "" |
| 642 | } |
| 643 | return stem + "_MAIL_FROM" |
| 644 | } |
| 645 | |
| 646 | // PlatformState is what the walks have delivered into one environment. |
| 647 | type PlatformState struct { |
| 648 | // Keys are the PLATFORM keys present that this app reads. |
| 649 | Keys []string |
| 650 | // Missing is the PLATFORM keys still to arrive. |
| 651 | Missing []string |
| 652 | // WrongPrefix names a prefix delivered that the binary does not read: |
| 653 | // five SMTP variables outside MailPrefixes, or a From whose stem is |
| 654 | // not SubmitPrefix's. Either is a declaration under the wrong prefix |
| 655 | // (§5, §6 step 0). |
| 656 | WrongPrefix string |
| 657 | // Store is the object-store keys present, for an app with a Store. |
| 658 | // Kept apart from Keys: the store arrives at step 8, after step 7 |
| 659 | // records the binding, and is never waited for. |
| 660 | Store []string |
| 661 | } |
| 662 | |
| 663 | // StoreKey is one object-store coordinate as CARLOS's objstore walk |
| 664 | // delivers it (internal/objstore EnvNames and writeEnv, prefix |
| 665 | // CARLOS_STORE, the one Slides' declaration and rastrillo's |
| 666 | // blobs.S3FromEnv use): bucket and region as env, both key halves sealed. |
| 667 | // The endpoint is delivered only by a deployment that has one. |
| 668 | type StoreKey struct { |
| 669 | Name string |
| 670 | Secret bool |
| 671 | Optional bool |
| 672 | } |
| 673 | |
| 674 | // StoreKeys is the set, all-or-none but for the endpoint. Alta's console |
| 675 | // guard refuses any write of them (console.PlatformManaged). |
| 676 | var StoreKeys = []StoreKey{ |
| 677 | {Name: "CARLOS_STORE_BUCKET"}, |
| 678 | {Name: "CARLOS_STORE_REGION"}, |
| 679 | {Name: "CARLOS_STORE_ENDPOINT", Optional: true}, |
| 680 | {Name: "CARLOS_STORE_ACCESS_KEY", Secret: true}, |
| 681 | {Name: "CARLOS_STORE_SECRET_KEY", Secret: true}, |
| 682 | } |
| 683 | |
| 684 | // PlatformKeys classifies an environment's present key names against |
| 685 | // what the app's binary reads. |
| 686 | // |
| 687 | // Mail arrives one of two ways. An app on SMTP gets the five |
| 688 | // <PREFIX>_SMTP_* of its declared prefix. An app switched to CARLOS mail |
| 689 | // submission gets only <stem>_MAIL_FROM, and its instances send through |
| 690 | // the box's mailer with no credential at all: the agent adds |
| 691 | // CARLOS_MAIL=submit and the control socket at start, never the bundle. |
| 692 | // The binary reads exactly one From, MailFromKey(app.SubmitPrefix); a |
| 693 | // From under any other stem is a declaration under the wrong prefix, |
| 694 | // refused like a wrong SMTP prefix, because the switched binary would |
| 695 | // find no From and refuse to start. Either channel is mail; the From |
| 696 | // alone is not waited past for the five. During a switch-on CARLOS |
| 697 | // delivers the From before it takes the five out, and during a |
| 698 | // switch-off the five before it takes the From out, so an environment |
| 699 | // caught between holds both and every one present is admitted. |
| 700 | func PlatformKeys(app App, present map[string]bool) PlatformState { |
| 701 | var st PlatformState |
| 702 | prefixes := map[string]int{} |
| 703 | submit := map[string]bool{} |
| 704 | for k := range present { |
| 705 | if m := smtpKey.FindStringSubmatch(k); m != nil { |
| 706 | prefixes[m[1]]++ |
| 707 | } else if m := mailFromKey.FindStringSubmatch(k); m != nil { |
| 708 | submit[m[1]+"_SMTP"] = true |
| 709 | } |
| 710 | } |
| 711 | for p := range prefixes { |
| 712 | if !slices.Contains(app.MailPrefixes, p) { |
| 713 | st.WrongPrefix = p |
| 714 | } |
| 715 | } |
| 716 | for p := range submit { |
| 717 | if p != app.SubmitPrefix { |
| 718 | st.WrongPrefix = p |
| 719 | } |
| 720 | } |
| 721 | smtp := "" |
| 722 | for _, p := range app.MailPrefixes { |
| 723 | if prefixes[p] > 0 { |
| 724 | smtp = p |
| 725 | break |
| 726 | } |
| 727 | } |
| 728 | switched := submit[app.SubmitPrefix] |
| 729 | var want []string |
| 730 | if switched { |
| 731 | want = append(want, MailFromKey(app.SubmitPrefix)) |
| 732 | } |
| 733 | switch { |
| 734 | case smtp != "": |
| 735 | want = append(want, SMTPKeys(smtp)...) |
| 736 | case !switched: |
| 737 | want = append(want, SMTPKeys(app.MailPrefixes[0])...) |
| 738 | } |
| 739 | if app.Relay { |
| 740 | want = append(want, "CARLOS_RELAY") |
| 741 | } |
| 742 | if app.Store { |
| 743 | for _, k := range StoreKeys { |
| 744 | if present[k.Name] { |
| 745 | st.Store = append(st.Store, k.Name) |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | for _, k := range want { |
| 750 | if present[k] { |
| 751 | st.Keys = append(st.Keys, k) |
| 752 | } else { |
| 753 | st.Missing = append(st.Missing, k) |
| 754 | } |
| 755 | } |
| 756 | return st |
| 757 | } |
| 758 | |
| 759 | // EnvironmentBinding is §7's environment_binding: base64url(SHA-256) of |
| 760 | // Alta's canonical recipe JSON for this instance — sorted names, each with |
| 761 | // its class and, for a SET key, the SHA-256 of its value. GENERATED and |
| 762 | // PLATFORM keys contribute name and class only. |
| 763 | func EnvironmentBinding(env string, keys []Key, platform []string) string { |
| 764 | type row = [3]string |
| 765 | var rows []row |
| 766 | for _, k := range keys { |
| 767 | h := "" |
| 768 | if k.Class == Set { |
| 769 | s := sha256.Sum256([]byte(k.Value)) |
| 770 | h = hex.EncodeToString(s[:]) |
| 771 | } |
| 772 | rows = append(rows, row{k.Name, string(k.Class), h}) |
| 773 | } |
| 774 | for _, k := range platform { |
| 775 | rows = append(rows, row{k, string(Platform), ""}) |
| 776 | } |
| 777 | slices.SortFunc(rows, func(a, b row) int { return strings.Compare(a[0], b[0]) }) |
| 778 | b, _ := stdjson.Marshal([]any{"oficina/alta/environment-binding/v1", env, rows}) |
| 779 | sum := sha256.Sum256(b) |
| 780 | return base64.RawURLEncoding.EncodeToString(sum[:]) |
| 781 | } |
| 782 | |
| 783 | // freshKey is a GENERATED value: 32 random bytes, standard base64, the |
| 784 | // shape every DEK and instance key in the suite takes. |
| 785 | func freshKey() string { |
| 786 | b := make([]byte, 32) |
| 787 | if _, err := rand.Read(b); err != nil { |
| 788 | panic(fmt.Sprintf("provision: no randomness: %v", err)) |
| 789 | } |
| 790 | return base64.StdEncoding.EncodeToString(b) |
| 791 | } |
| 792 | |
| 793 | // RetypeAllowed reports whether Paul may retype a forgotten owner address |
| 794 | // for a team with these apps (name → active|retired). Only a stopgap |
| 795 | // allowlist still needs one (none since every app has roster mode). A |
| 796 | // seeded app's owner is never retyped: the address Home confirmed at |
| 797 | // create-team is the only one a seeded recipe may write, and a retyped |
| 798 | // address, checked for shape alone, would make someone else its owner |
| 799 | // (team-bundle, Fable's whole-branch review P2-2). With retype refused, |
| 800 | // a held owner is always Home's confirmed one. |
| 801 | // |
| 802 | // That holds only while no stopgap-allowlist app exists. If one returns |
| 803 | // beside a seeded app, retype is allowed again and ownOnlyFits would take |
| 804 | // a retyped address for a late seeded app: mark held owners as retyped |
| 805 | // first, and have ownOnlyFits refuse them. |
| 806 | func RetypeAllowed(apps map[string]string) bool { |
| 807 | for slug, st := range apps { |
| 808 | if app, ok := AppBySlug(slug); ok && st == "active" && NeedsOwner(app) && !app.Seeded { |
| 809 | return true |
| 810 | } |
| 811 | } |
| 812 | return false |
| 813 | } |
| 814 | |