| 1 | package calendar |
| 2 | |
| 3 | // Team mode: Home's roster decides standing (Home implementation/ |
| 4 | // team-members.md Part B §7; implementation/teammode-package.md §7.3). |
| 5 | // Configuration, the marker, the roster runtime and the probe are |
| 6 | // home/teammode's: CALENDAR_TEAM_ID, _DIRECTORY_ORIGIN, |
| 7 | // _PROVISIONING_BASE64, _OPERATOR_KEYS_BASE64 and _ROSTER_TOKEN, all or |
| 8 | // none, and only beside CALENDAR_HOME_*. Without them nothing in this |
| 9 | // file runs and Calendar is the allowlist app it has always been. |
| 10 | |
| 11 | import ( |
| 12 | "context" |
| 13 | "database/sql" |
| 14 | |
| 15 | "amadan.net/oficina/home/connect" |
| 16 | "amadan.net/oficina/home/handoff/seal" |
| 17 | "amadan.net/oficina/home/teammode" |
| 18 | "amadan.net/rastrillo/rastrillo/migrate" |
| 19 | |
| 20 | "amadan.net/oficina/calendar/internal/calendar/teamstate" |
| 21 | ) |
| 22 | |
| 23 | var ( |
| 24 | errTeamConfig = teammode.ErrConfig |
| 25 | errTeamUnavailable = teammode.ErrUnavailable |
| 26 | ) |
| 27 | |
| 28 | // homeNamespace is Calendar's Home storage namespace: connect's link and |
| 29 | // handoff sealing, and in team mode the roster evidence's. |
| 30 | const homeNamespace = "oficina/calendar" |
| 31 | |
| 32 | // teamState is the activation marker and the roster evidence, in |
| 33 | // calendar_team_required and calendar_team_roster_evidence (migration set |
| 34 | // calendar_team, teammode's own SQL). The marker is read before anything |
| 35 | // migrates (teamBootSchema). |
| 36 | var teamState = teammode.MustState("calendar") |
| 37 | |
| 38 | // teamIssuerTables are the tables holding Home identities in an issuer |
| 39 | // column: connect's links and Calendar's Home-only people. teammode.Open |
| 40 | // refuses activation while any row names another issuer. |
| 41 | var teamIssuerTables = []string{"home_identity_links", "calendar_team_home_people"} |
| 42 | |
| 43 | // teamBoot is base plus team state: teammode's marker set, then |
| 44 | // Calendar's Home-only people (whose triggers name people and |
| 45 | // home_identity_links, so it follows both). Applied only in team mode; |
| 46 | // once applied it stays, because the marker in it is what keeps the |
| 47 | // 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 New decide the schema before |
| 57 | // 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}, "calendar", origin) |
| 66 | } |
| 67 | |
| 68 | // teamBootSchema is the schema New 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 | // CALENDAR_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 people 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 | |