oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3// Team mode: the roster decides standing (Home implementation/
4// team-members.md Part B, §7). Configuration, the marker, the roster
5// runtime and the probe are home/teammode's (Home implementation/
6// teammode-package.md §6 maps each piece of Sheets' own copy onto it):
7// SHEETS_TEAM_ID, _DIRECTORY_ORIGIN, _PROVISIONING_BASE64,
8// _OPERATOR_KEYS_BASE64 and _ROSTER_TOKEN, all or none, and only beside
9// SHEETS_HOME_*. Without them nothing in this file runs and Sheets is the
10// allowlist app it has always been.
11
12import (
13 "context"
14 "database/sql"
15 "net/http"
16
17 "amadan.net/oficina/home/connect"
18 "amadan.net/oficina/home/handoff/seal"
19 "amadan.net/oficina/home/roster"
20 "amadan.net/oficina/home/teammode"
21 "amadan.net/rastrillo/rastrillo/migrate"
22
23 "amadan.net/oficina/sheets/internal/sheets/teamstate"
24)
25
26var (
27 errTeamConfig = teammode.ErrConfig
28 errTeamUnavailable = teammode.ErrUnavailable
29)
30
31// teamState is the marker and the roster evidence, in Sheets' own
32// tables (sheets_team_required, sheets_team_roster_evidence). Their
33// schema stays teamstate.Schema, never teamState.Schema(): Sheets'
34// 0001_team.sql also holds the Home-only member table, and the
35// migration ledger checksums the whole file.
36var teamState = teammode.MustState("sheets")
37
38// teamIssuerTables are the tables holding Home identities in an issuer
39// column: connect's links and Sheets' Home-only members. teammode.Open
40// refuses activation while any row names another issuer.
41var teamIssuerTables = []string{"home_identity_links", "sheets_team_home_members"}
42
43// TeamBootSchema is BootSchema plus team state, applied only when team
44// mode is configured. Once applied it stays: the marker in it is what
45// keeps the database in team mode.
46var TeamBootSchema = migrate.Merge(BootSchema, teamstate.Schema)
47
48// TeamSettings verifies team configuration for this instance: nil, nil
49// with team mode off; otherwise a verified record or errTeamConfig. Team
50// mode exists only beside Home, so the Home credentials are decoded here
51// for their issuer and consumer ID. It touches no database, which is
52// what lets main decide the schema before anything migrates.
53func TeamSettings(cfg *teammode.Config, home *connect.Credentials, origin string) (*teammode.Settings, error) {
54 if cfg == nil {
55 return nil, nil
56 }
57 creds, err := connect.DecodeCredentials(home, origin)
58 if err != nil || creds == nil {
59 return nil, errTeamConfig
60 }
61 return teammode.Verify(cfg, &teammode.Home{Issuer: creds.Issuer, ConsumerID: creds.ConsumerID}, "sheets", origin)
62}
63
64// BootSchemaFor is the schema main applies, chosen before anything is
65// migrated. A database activated in team mode refuses to boot without
66// team configuration, or with a different binding: an environment
67// variable dropped in a redeploy must not quietly hand standing back to
68// SHEETS_OWNERS.
69func BootSchemaFor(ctx context.Context, db *sql.DB, settings *teammode.Settings) (*migrate.Set, error) {
70 return teamState.BootSchema(ctx, db, settings, BootSchema, TeamBootSchema)
71}
72
73// teamRuntime is the process's roster runtime and the database the
74// Home-only member table lives in.
75type teamRuntime struct {
76 *teammode.Runtime
77 db *sql.DB
78}
79
80func (t *teamRuntime) issuer() string { return t.Bindings().Issuer }
81
82// ConfigureTeam runs after ConfigureHome and before serving. Without
83// team settings (Config.Team) it does nothing. Anything else either
84// produces a verified, activated runtime or refuses boot: there is no
85// falling back to the allowlist.
86func (a *App) ConfigureTeam() error {
87 settings := a.teamSettings
88 if settings == nil {
89 return nil
90 }
91 if a.home == nil || a.homeCreds == nil || a.db == nil {
92 return errTeamConfig
93 }
94 // The settings were verified against the Home credentials main
95 // decoded; these are the ones the bridge was built with. They must
96 // be the same deployment.
97 b := settings.Bindings()
98 if b.Issuer != a.homeCreds.Issuer || b.Consumer != a.homeCreds.ConsumerID || b.Audience != a.origin {
99 return errTeamConfig
100 }
101 d, err := a.db.DB()
102 if err != nil {
103 return errTeamUnavailable
104 }
105 sealer, err := seal.New(a.homeCreds.DEK, homeNamespace)
106 if err != nil {
107 return errTeamConfig
108 }
109 rt, err := teammode.Open(context.Background(), teammode.RuntimeConfig{Settings: settings, State: teamState, DB: d, Sealer: sealer,
110 IssuerTables: teamIssuerTables, Transport: a.teamTransport, Clock: a.teamClock})
111 if err != nil {
112 return err
113 }
114 a.team = &teamRuntime{Runtime: rt, db: d}
115 return nil
116}
117
118// teamTest is what a test sets on an App before ConfigureTeam: a
119// directory that answers in memory and a clock it can step. Production
120// leaves both nil.
121type teamTest struct {
122 teamTransport http.RoundTripper
123 teamClock roster.Clock
124}
125