| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "maps" |
| 7 | "sync" |
| 8 | "sync/atomic" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/home/internal/operator" |
| 12 | "amadan.net/rastrillo/rastrillo/assertion" |
| 13 | ) |
| 14 | |
| 15 | // consumerRegistry is the credential origin's effective consumer list |
| 16 | // (team-provisioning-amendment.md §7): HOME_CONSUMERS ∪ the active rows of |
| 17 | // the consumers table, held as one immutable map behind an atomic pointer |
| 18 | // and swapped whole after each committed registry write. Every reader — |
| 19 | // /authorize, the assertion handoff, sign-out, the dashboard check, the |
| 20 | // per-response CSP — reads the union through it. |
| 21 | type consumerRegistry struct { |
| 22 | env map[string]string |
| 23 | current atomic.Pointer[map[string]string] |
| 24 | reload sync.Mutex |
| 25 | } |
| 26 | |
| 27 | // errConsumerCollision refuses boot: an ID or origin in both HOME_CONSUMERS |
| 28 | // and the table. That takes suite sign-in down, deliberately; the runbook is |
| 29 | // to remove the env entry and redeploy. |
| 30 | var errConsumerCollision = errExchangeConfig |
| 31 | |
| 32 | func newConsumerRegistry(env map[string]string) *consumerRegistry { |
| 33 | r := &consumerRegistry{env: maps.Clone(env)} |
| 34 | union := maps.Clone(env) |
| 35 | r.current.Store(&union) |
| 36 | return r |
| 37 | } |
| 38 | |
| 39 | // load reads the table and swaps in the union. Any row (retired included) |
| 40 | // sharing an ID or origin with the env manifest is a collision. |
| 41 | func (r *consumerRegistry) load(ctx context.Context, db *sql.DB, credential string, keys map[string][]byte) error { |
| 42 | r.reload.Lock() |
| 43 | defer r.reload.Unlock() |
| 44 | ctx, cancel := context.WithTimeout(ctx, 5*time.Second) |
| 45 | defer cancel() |
| 46 | envOrigins := map[string]bool{} |
| 47 | for _, origin := range r.env { |
| 48 | envOrigins[origin] = true |
| 49 | } |
| 50 | rows, err := db.QueryContext(ctx, "SELECT consumer_id,origin FROM consumers LIMIT 4097") |
| 51 | if err != nil { |
| 52 | return errExchangeConfig |
| 53 | } |
| 54 | defer rows.Close() |
| 55 | for rows.Next() { |
| 56 | var id, origin string |
| 57 | if rows.Scan(&id, &origin) != nil { |
| 58 | return errExchangeConfig |
| 59 | } |
| 60 | if _, clash := r.env[id]; clash || envOrigins[origin] { |
| 61 | return errConsumerCollision |
| 62 | } |
| 63 | } |
| 64 | if rows.Err() != nil { |
| 65 | return errExchangeConfig |
| 66 | } |
| 67 | active, err := operator.ActiveConsumers(ctx, db) |
| 68 | if err != nil { |
| 69 | return errExchangeConfig |
| 70 | } |
| 71 | union := maps.Clone(r.env) |
| 72 | admits := registryAdmits(credential, keys) |
| 73 | for id, origin := range active { |
| 74 | if !admits(id, origin) { |
| 75 | return errExchangeConfig |
| 76 | } |
| 77 | union[id] = origin |
| 78 | } |
| 79 | r.current.Store(&union) |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | func (r *consumerRegistry) snapshot() map[string]string { return *r.current.Load() } |
| 84 | |
| 85 | // consumers is the one reader every credential-origin path uses. |
| 86 | func (a *App) consumers() map[string]string { |
| 87 | if a.runtime == nil { |
| 88 | return nil |
| 89 | } |
| 90 | if a.runtime.registry != nil { |
| 91 | return a.runtime.registry.snapshot() |
| 92 | } |
| 93 | return a.runtime.consumers |
| 94 | } |
| 95 | |
| 96 | // registryAdmits is the one admission rule for a table consumer: the reload |
| 97 | // applies it to every row, and credential.register-consumer applies it before |
| 98 | // it writes one, so no committed row can be one the next boot refuses. |
| 99 | func registryAdmits(credential string, keys map[string][]byte) func(id, origin string) bool { |
| 100 | return func(id, origin string) bool { |
| 101 | if !consumerName(id) { |
| 102 | return false |
| 103 | } |
| 104 | _, err := assertion.NewVerifier(credential, origin, keys) |
| 105 | return err == nil |
| 106 | } |
| 107 | } |
| 108 | |