package calls // Team mode: Home's roster decides standing (Home implementation/ // team-members.md Part B §7; implementation/teammode-package.md §7.4). // Configuration, the marker, the roster runtime and the probe are // home/teammode's: CALLS_TEAM_ID, _DIRECTORY_ORIGIN, _PROVISIONING_BASE64, // _OPERATOR_KEYS_BASE64 and _ROSTER_TOKEN, all or none, and only beside // CALLS_HOME_*. Without them nothing in the team_*.go files runs and // Calls is the allowlist app it has always been. import ( "context" "database/sql" "net/http" "amadan.net/oficina/home/connect" "amadan.net/oficina/home/connect/linkstore" "amadan.net/oficina/home/handoff" "amadan.net/oficina/home/handoff/seal" "amadan.net/oficina/home/teammode" "amadan.net/rastrillo/rastrillo/migrate" "amadan.net/oficina/calls/internal/calls/teamstate" ) // homeNamespace is Calls' Home storage namespace: connect's link and // handoff sealing, and in team mode the roster evidence's. const homeNamespace = "oficina/calls" // teamState is the activation marker and the roster evidence, in // calls_team_required and calls_team_roster_evidence (migration set // calls_team, teammode's own SQL). The marker is read before anything // migrates (teamBootSchema). var teamState = teammode.MustState("calls") // teamIssuerTables are the tables holding Home identities in an issuer // column: connect's links and Calls' Home-only persons. teammode.Open // refuses activation while any row names another issuer. var teamIssuerTables = []string{"home_identity_links", "calls_team_home_persons"} // homeSchema is what wireHome applies beside Calls' own schema: the // handoff and link tables connect needs. func homeSchema() *migrate.Set { return migrate.Merge(migrate.MustFromFS(handoff.Migrations, "oficina_handoff"), migrate.MustFromFS(linkstore.Migrations, linkstore.SchemaNamespace)) } // teamBoot is base plus Home's tables plus team state: teammode's marker // set, then Calls' Home-only persons (whose triggers name persons and // home_identity_links, so it follows both). Applied only in team mode; // once applied it stays, because the marker in it is what keeps the // database in team mode. func teamBoot(base *migrate.Set) *migrate.Set { return migrate.Merge(base, homeSchema(), teamState.Schema(), teamstate.Schema) } // teamSettings verifies team configuration for this instance: nil, nil // with team mode off; otherwise a verified record or ErrConfig. Team mode // exists only beside Home, and the record is verified against the same // Home issuer, consumer ID and origin the bridge is built with. It // touches no database, which is what lets build decide the schema before // anything migrates. func teamSettings(cfg *teammode.Config, home *connect.DecodedCredentials, origin string) (*teammode.Settings, error) { if cfg == nil { return nil, nil } if home == nil { return nil, teammode.ErrConfig } return teammode.Verify(cfg, &teammode.Home{Issuer: home.Issuer, ConsumerID: home.ConsumerID}, "calls", origin) } // teamBootSchema is the schema build applies, chosen before anything is // migrated. A database activated in team mode refuses to boot without // team configuration, or with a different binding: an environment // variable dropped in a redeploy must not quietly hand standing back to // CALLS_ALLOWED_ADDRESSES. Off, and never activated, it is base: one // sqlite_master read, and boot is otherwise unchanged. func teamBootSchema(ctx context.Context, db *sql.DB, settings *teammode.Settings, base *migrate.Set) (*migrate.Set, error) { return teamState.BootSchema(ctx, db, settings, base, teamBoot(base)) } // teamRuntime is the process's roster runtime and the database the // Home-only persons table lives in. type teamRuntime struct { *teammode.Runtime db *sql.DB } func (t *teamRuntime) issuer() string { return t.Bindings().Issuer } // configureTeam runs after the schema is applied and Home is wired, // before serving. Without settings it does nothing. Anything else either // produces a verified, activated runtime or refuses boot: there is no // falling back to the allowlist. func (a *app) configureTeam() error { settings := a.cfg.team if settings == nil { return nil } if a.home == nil || a.cfg.home == nil { return teammode.ErrConfig } sealer, err := seal.New(a.cfg.home.DEK, homeNamespace) if err != nil { return teammode.ErrConfig } w := a.db.Writer() rt, err := teammode.Open(context.Background(), teammode.RuntimeConfig{Settings: settings, State: teamState, DB: w, Sealer: sealer, IssuerTables: teamIssuerTables, Transport: a.cfg.teamTransport, Clock: a.cfg.teamClock}) if err != nil { return err } a.team = &teamRuntime{Runtime: rt, db: w} a.store.team = true return nil } // teamProbe is GET /api/team, the readiness probe Alta polls at // provisioning step 11 before it activates this instance in Home's // directory: teammode's Probe, unauthenticated, no-store, // {team_mode, consumer, roster} and no account data, and // {"team_mode":false} with team mode off. func (a *app) teamProbe(w http.ResponseWriter, r *http.Request) { var rt *teammode.Runtime if a.team != nil { rt = a.team.Runtime } teammode.Probe(rt).ServeHTTP(w, r) }