| 1 | package slides |
| 2 | |
| 3 | // Team mode: Home's roster decides standing (Home implementation/ |
| 4 | // team-members.md Part B §7; implementation/teammode-package.md §7.1, |
| 5 | // "Slides. As Sheets."). Configuration, the marker, the roster runtime |
| 6 | // and the probe are home/teammode's: SLIDES_TEAM_ID, |
| 7 | // _DIRECTORY_ORIGIN, _PROVISIONING_BASE64, _OPERATOR_KEYS_BASE64 and |
| 8 | // _ROSTER_TOKEN, all or none, and only beside SLIDES_HOME_*. Without |
| 9 | // them nothing in this file runs and Slides is the allowlist app it has |
| 10 | // always been. |
| 11 | |
| 12 | import ( |
| 13 | "context" |
| 14 | "database/sql" |
| 15 | |
| 16 | "amadan.net/oficina/home/connect" |
| 17 | "amadan.net/oficina/home/handoff/seal" |
| 18 | "amadan.net/oficina/home/teammode" |
| 19 | "amadan.net/rastrillo/rastrillo/migrate" |
| 20 | |
| 21 | "amadan.net/oficina/slides/internal/slides/teamstate" |
| 22 | ) |
| 23 | |
| 24 | var ( |
| 25 | errTeamConfig = teammode.ErrConfig |
| 26 | errTeamUnavailable = teammode.ErrUnavailable |
| 27 | ) |
| 28 | |
| 29 | // homeNamespace is Slides' Home storage namespace: connect's link and |
| 30 | // handoff sealing, and in team mode the roster evidence's. |
| 31 | const homeNamespace = "oficina/slides" |
| 32 | |
| 33 | // teamState is the activation marker and the roster evidence, in |
| 34 | // slides_team_required and slides_team_roster_evidence (migration set |
| 35 | // slides_team, teammode's own SQL). The marker is read before anything |
| 36 | // migrates (teamBootSchema). |
| 37 | var teamState = teammode.MustState("slides") |
| 38 | |
| 39 | // teamIssuerTables are the tables holding Home identities in an issuer |
| 40 | // column: connect's links and Slides' Home-only users. teammode.Open |
| 41 | // refuses activation while any row names another issuer. |
| 42 | var teamIssuerTables = []string{"home_identity_links", "slides_team_home_users"} |
| 43 | |
| 44 | // teamBoot is base plus team state: teammode's marker set, then Slides' |
| 45 | // Home-only users (whose triggers name users and home_identity_links, so |
| 46 | // it follows both). Applied only in team mode; once applied it stays, |
| 47 | // because the marker in it is what keeps the database in team mode. |
| 48 | func teamBoot(base *migrate.Set) *migrate.Set { |
| 49 | return migrate.Merge(base, teamState.Schema(), teamstate.Schema) |
| 50 | } |
| 51 | |
| 52 | // teamSettings verifies team configuration for this instance: nil, nil |
| 53 | // with team mode off; otherwise a verified record or errTeamConfig. Team |
| 54 | // mode exists only beside Home, and the record is verified against the |
| 55 | // same Home issuer, consumer ID and origin the bridge is built with. It |
| 56 | // touches no database, which is what lets newApp decide the schema |
| 57 | // before anything migrates. |
| 58 | func teamSettings(cfg *teammode.Config, home *connect.DecodedCredentials, origin string) (*teammode.Settings, error) { |
| 59 | if cfg == nil { |
| 60 | return nil, nil |
| 61 | } |
| 62 | if home == nil { |
| 63 | return nil, errTeamConfig |
| 64 | } |
| 65 | return teammode.Verify(cfg, &teammode.Home{Issuer: home.Issuer, ConsumerID: home.ConsumerID}, "slides", origin) |
| 66 | } |
| 67 | |
| 68 | // teamBootSchema is the schema newApp applies, chosen before anything is |
| 69 | // migrated. A database activated in team mode refuses to boot without |
| 70 | // team configuration, or with a different binding: an environment |
| 71 | // variable dropped in a redeploy must not quietly hand standing back to |
| 72 | // SLIDES_ALLOWED_ADDRESSES. |
| 73 | func teamBootSchema(ctx context.Context, db *sql.DB, settings *teammode.Settings, base *migrate.Set) (*migrate.Set, error) { |
| 74 | return teamState.BootSchema(ctx, db, settings, base, teamBoot(base)) |
| 75 | } |
| 76 | |
| 77 | // teamRuntime is the process's roster runtime and the database the |
| 78 | // Home-only users table lives in. |
| 79 | type teamRuntime struct { |
| 80 | *teammode.Runtime |
| 81 | db *sql.DB |
| 82 | } |
| 83 | |
| 84 | func (t *teamRuntime) issuer() string { return t.Bindings().Issuer } |
| 85 | |
| 86 | // configureTeam runs after the schema is applied and Home is configured, |
| 87 | // before serving. Without settings it does nothing. Anything else either |
| 88 | // produces a verified, activated runtime or refuses boot: there is no |
| 89 | // falling back to the allowlist. |
| 90 | func (a *app) configureTeam(settings *teammode.Settings, home *connect.DecodedCredentials) error { |
| 91 | if settings == nil { |
| 92 | return nil |
| 93 | } |
| 94 | if a.home == nil || home == nil { |
| 95 | return errTeamConfig |
| 96 | } |
| 97 | sealer, err := seal.New(home.DEK, homeNamespace) |
| 98 | if err != nil { |
| 99 | return errTeamConfig |
| 100 | } |
| 101 | w := a.db.Writer() |
| 102 | rt, err := teammode.Open(context.Background(), teammode.RuntimeConfig{Settings: settings, State: teamState, DB: w, Sealer: sealer, |
| 103 | IssuerTables: teamIssuerTables, Transport: a.cfg.teamTransport, Clock: a.cfg.teamClock}) |
| 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | a.team = &teamRuntime{Runtime: rt, db: w} |
| 108 | return nil |
| 109 | } |
| 110 | |