| 1 | package calls |
| 2 | |
| 3 | // Team mode: Home's roster decides standing (Home implementation/ |
| 4 | // team-members.md Part B §7; implementation/teammode-package.md §7.4). |
| 5 | // Configuration, the marker, the roster runtime and the probe are |
| 6 | // home/teammode's: CALLS_TEAM_ID, _DIRECTORY_ORIGIN, _PROVISIONING_BASE64, |
| 7 | // _OPERATOR_KEYS_BASE64 and _ROSTER_TOKEN, all or none, and only beside |
| 8 | // CALLS_HOME_*. Without them nothing in the team_*.go files runs and |
| 9 | // Calls is the allowlist app it has always been. |
| 10 | |
| 11 | import ( |
| 12 | "context" |
| 13 | "database/sql" |
| 14 | "net/http" |
| 15 | |
| 16 | "amadan.net/oficina/home/connect" |
| 17 | "amadan.net/oficina/home/connect/linkstore" |
| 18 | "amadan.net/oficina/home/handoff" |
| 19 | "amadan.net/oficina/home/handoff/seal" |
| 20 | "amadan.net/oficina/home/teammode" |
| 21 | "amadan.net/rastrillo/rastrillo/migrate" |
| 22 | |
| 23 | "amadan.net/oficina/calls/internal/calls/teamstate" |
| 24 | ) |
| 25 | |
| 26 | // homeNamespace is Calls' Home storage namespace: connect's link and |
| 27 | // handoff sealing, and in team mode the roster evidence's. |
| 28 | const homeNamespace = "oficina/calls" |
| 29 | |
| 30 | // teamState is the activation marker and the roster evidence, in |
| 31 | // calls_team_required and calls_team_roster_evidence (migration set |
| 32 | // calls_team, teammode's own SQL). The marker is read before anything |
| 33 | // migrates (teamBootSchema). |
| 34 | var teamState = teammode.MustState("calls") |
| 35 | |
| 36 | // teamIssuerTables are the tables holding Home identities in an issuer |
| 37 | // column: connect's links and Calls' Home-only persons. teammode.Open |
| 38 | // refuses activation while any row names another issuer. |
| 39 | var teamIssuerTables = []string{"home_identity_links", "calls_team_home_persons"} |
| 40 | |
| 41 | // homeSchema is what wireHome applies beside Calls' own schema: the |
| 42 | // handoff and link tables connect needs. |
| 43 | func homeSchema() *migrate.Set { |
| 44 | return migrate.Merge(migrate.MustFromFS(handoff.Migrations, "oficina_handoff"), migrate.MustFromFS(linkstore.Migrations, linkstore.SchemaNamespace)) |
| 45 | } |
| 46 | |
| 47 | // teamBoot is base plus Home's tables plus team state: teammode's marker |
| 48 | // set, then Calls' Home-only persons (whose triggers name persons and |
| 49 | // home_identity_links, so it follows both). Applied only in team mode; |
| 50 | // once applied it stays, because the marker in it is what keeps the |
| 51 | // database in team mode. |
| 52 | func teamBoot(base *migrate.Set) *migrate.Set { |
| 53 | return migrate.Merge(base, homeSchema(), teamState.Schema(), teamstate.Schema) |
| 54 | } |
| 55 | |
| 56 | // teamSettings verifies team configuration for this instance: nil, nil |
| 57 | // with team mode off; otherwise a verified record or ErrConfig. Team mode |
| 58 | // exists only beside Home, and the record is verified against the same |
| 59 | // Home issuer, consumer ID and origin the bridge is built with. It |
| 60 | // touches no database, which is what lets build decide the schema before |
| 61 | // anything migrates. |
| 62 | func teamSettings(cfg *teammode.Config, home *connect.DecodedCredentials, origin string) (*teammode.Settings, error) { |
| 63 | if cfg == nil { |
| 64 | return nil, nil |
| 65 | } |
| 66 | if home == nil { |
| 67 | return nil, teammode.ErrConfig |
| 68 | } |
| 69 | return teammode.Verify(cfg, &teammode.Home{Issuer: home.Issuer, ConsumerID: home.ConsumerID}, "calls", origin) |
| 70 | } |
| 71 | |
| 72 | // teamBootSchema is the schema build applies, chosen before anything is |
| 73 | // migrated. A database activated in team mode refuses to boot without |
| 74 | // team configuration, or with a different binding: an environment |
| 75 | // variable dropped in a redeploy must not quietly hand standing back to |
| 76 | // CALLS_ALLOWED_ADDRESSES. Off, and never activated, it is base: one |
| 77 | // sqlite_master read, and boot is otherwise unchanged. |
| 78 | func teamBootSchema(ctx context.Context, db *sql.DB, settings *teammode.Settings, base *migrate.Set) (*migrate.Set, error) { |
| 79 | return teamState.BootSchema(ctx, db, settings, base, teamBoot(base)) |
| 80 | } |
| 81 | |
| 82 | // teamRuntime is the process's roster runtime and the database the |
| 83 | // Home-only persons table lives in. |
| 84 | type teamRuntime struct { |
| 85 | *teammode.Runtime |
| 86 | db *sql.DB |
| 87 | } |
| 88 | |
| 89 | func (t *teamRuntime) issuer() string { return t.Bindings().Issuer } |
| 90 | |
| 91 | // configureTeam runs after the schema is applied and Home is wired, |
| 92 | // before serving. Without settings it does nothing. Anything else either |
| 93 | // produces a verified, activated runtime or refuses boot: there is no |
| 94 | // falling back to the allowlist. |
| 95 | func (a *app) configureTeam() error { |
| 96 | settings := a.cfg.team |
| 97 | if settings == nil { |
| 98 | return nil |
| 99 | } |
| 100 | if a.home == nil || a.cfg.home == nil { |
| 101 | return teammode.ErrConfig |
| 102 | } |
| 103 | sealer, err := seal.New(a.cfg.home.DEK, homeNamespace) |
| 104 | if err != nil { |
| 105 | return teammode.ErrConfig |
| 106 | } |
| 107 | w := a.db.Writer() |
| 108 | rt, err := teammode.Open(context.Background(), teammode.RuntimeConfig{Settings: settings, State: teamState, DB: w, Sealer: sealer, |
| 109 | IssuerTables: teamIssuerTables, Transport: a.cfg.teamTransport, Clock: a.cfg.teamClock}) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | a.team = &teamRuntime{Runtime: rt, db: w} |
| 114 | a.store.team = true |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | // teamProbe is GET /api/team, the readiness probe Alta polls at |
| 119 | // provisioning step 11 before it activates this instance in Home's |
| 120 | // directory: teammode's Probe, unauthenticated, no-store, |
| 121 | // {team_mode, consumer, roster} and no account data, and |
| 122 | // {"team_mode":false} with team mode off. |
| 123 | func (a *app) teamProbe(w http.ResponseWriter, r *http.Request) { |
| 124 | var rt *teammode.Runtime |
| 125 | if a.team != nil { |
| 126 | rt = a.team.Runtime |
| 127 | } |
| 128 | teammode.Probe(rt).ServeHTTP(w, r) |
| 129 | } |
| 130 | |