oficina / memoria Public

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

Plain git — no account needed to clone.

Download

Download this file

1package memoria
2
3// Team mode: Home's roster decides standing (Home implementation/
4// team-members.md Part B §7; implementation/teammode-package.md §7.2).
5// Configuration, the marker, the roster runtime and the probe are
6// home/teammode's: MEMORIA_TEAM_ID, _DIRECTORY_ORIGIN,
7// _PROVISIONING_BASE64, _OPERATOR_KEYS_BASE64 and _ROSTER_TOKEN, all or
8// none, and only beside MEMORIA_HOME_*. Without them nothing in this file
9// runs and Memoria is the owners-list app it has always been.
10//
11// Not MEMORIA_TEAM. That is the sibling transport's host label (§6.4), a
12// different thing with an unfortunately close name; team_siblings.go is
13// where the two meet.
14
15import (
16 "context"
17 "database/sql"
18 "net/http"
19
20 "amadan.net/oficina/home/connect"
21 "amadan.net/oficina/home/connect/linkstore"
22 "amadan.net/oficina/home/handoff"
23 "amadan.net/oficina/home/handoff/seal"
24 "amadan.net/oficina/home/roster"
25 "amadan.net/oficina/home/teammode"
26 "amadan.net/rastrillo/rastrillo/migrate"
27
28 "amadan.net/oficina/memoria/internal/memoria/teamstate"
29)
30
31var (
32 errTeamConfig = teammode.ErrConfig
33 errTeamUnavailable = teammode.ErrUnavailable
34)
35
36// homeNamespace is Memoria's Home storage namespace: connect's link and
37// handoff sealing (WireHome), and in team mode the roster evidence's.
38const homeNamespace = "oficina/memoria"
39
40// teamState is the activation marker and the roster evidence, in
41// memoria_team_required and memoria_team_roster_evidence (migration set
42// memoria_team, teammode's own SQL). The marker is read before anything
43// migrates (teamBootSchema).
44var teamState = teammode.MustState("memoria")
45
46// teamIssuerTables are the tables holding Home identities in an issuer
47// column: connect's links and Memoria's Home-only members. teammode.Open
48// refuses activation while any row names another issuer.
49var teamIssuerTables = []string{"home_identity_links", "memoria_team_home_members"}
50
51// homeSchema is what WireHome applies: Memoria's Home identity rows,
52// handoff's pending exchanges and connect's links.
53func homeSchema() *migrate.Set {
54 return migrate.Merge(homeIdentitySchema, migrate.MustFromFS(handoff.Migrations, "oficina_handoff"), migrate.MustFromFS(linkstore.Migrations, linkstore.SchemaNamespace))
55}
56
57// teamBoot is base plus team state: the Home schema (applied here rather
58// than waiting for WireHome, because the Home-only table's triggers name
59// its tables), teammode's marker set, then Memoria's Home-only members.
60// Applied only in team mode; once applied it stays, because the marker in
61// it is what keeps the database in team mode. WireHome's own apply of the
62// Home schema afterwards finds every migration already recorded.
63func teamBoot(base *migrate.Set) *migrate.Set {
64 return migrate.Merge(base, homeSchema(), teamState.Schema(), teamstate.Schema)
65}
66
67// teamSettings verifies team configuration for this instance: nil, nil
68// with team mode off; otherwise a verified record or errTeamConfig. Team
69// mode exists only beside Home, and the record is verified against the
70// same Home issuer, consumer ID and origin the bridge is built with. It
71// touches no database, which is what lets AppWith decide the schema
72// before anything migrates.
73func teamSettings(cfg *teammode.Config, home *connect.DecodedCredentials, origin string) (*teammode.Settings, error) {
74 if cfg == nil {
75 return nil, nil
76 }
77 if home == nil {
78 return nil, errTeamConfig
79 }
80 return teammode.Verify(cfg, &teammode.Home{Issuer: home.Issuer, ConsumerID: home.ConsumerID}, "memoria", origin)
81}
82
83// teamBootSchema is the schema AppWith applies, chosen before anything is
84// migrated. A database activated in team mode refuses to boot without
85// team configuration, or with a different binding: an environment
86// variable dropped in a redeploy must not quietly hand standing back to
87// MEMORIA_OWNERS.
88func teamBootSchema(ctx context.Context, db *sql.DB, settings *teammode.Settings, base *migrate.Set) (*migrate.Set, error) {
89 return teamState.BootSchema(ctx, db, settings, base, teamBoot(base))
90}
91
92// teamRuntime is the process's roster runtime and the database the
93// Home-only members table lives in.
94type teamRuntime struct {
95 *teammode.Runtime
96 db *sql.DB
97}
98
99func (t *teamRuntime) issuer() string { return t.Bindings().Issuer }
100
101// teamWiring is what AppWith learnt about team mode before anything
102// migrated, kept for WireHome: the verified settings, and the test
103// directory and clock (nil in production, where teammode uses its own
104// client and the real clock).
105type teamWiring struct {
106 settings *teammode.Settings
107 transport http.RoundTripper
108 clock roster.Clock
109}
110
111// teamMode reports whether this instance is configured for team mode,
112// whether or not the runtime has been opened yet. Every gate asks this,
113// never a.team != nil: a team-mode instance whose runtime is missing
114// must refuse, not fall back to the owners list (requireSession).
115func (a *app) teamMode() bool { return a.teamWiring != nil }
116
117// configureTeam runs from WireHome, after the bridge exists and before
118// serving. Without settings it does nothing. Anything else either
119// produces a verified, activated runtime or refuses boot: there is no
120// falling back to the owners list.
121func (a *app) configureTeam(home *connect.DecodedCredentials) error {
122 if a.teamWiring == nil {
123 return nil
124 }
125 if a.home == nil || home == nil {
126 return errTeamConfig
127 }
128 sealer, err := seal.New(home.DEK, homeNamespace)
129 if err != nil {
130 return errTeamConfig
131 }
132 w := a.db.Writer()
133 rt, err := teammode.Open(context.Background(), teammode.RuntimeConfig{Settings: a.teamWiring.settings, State: teamState, DB: w, Sealer: sealer,
134 IssuerTables: teamIssuerTables, Transport: a.teamWiring.transport, Clock: a.teamWiring.clock})
135 if err != nil {
136 return err
137 }
138 a.team = &teamRuntime{Runtime: rt, db: w}
139 return nil
140}
141
142// teamProbe is GET /api/team, the readiness probe Alta polls before it
143// activates this instance in Home's directory: teammode's Probe,
144// unauthenticated, no-store, {team_mode, consumer, roster} and no account
145// data, and {"team_mode":false} with team mode off.
146func (a *app) teamProbe(w http.ResponseWriter, r *http.Request) {
147 var rt *teammode.Runtime
148 if a.team != nil {
149 rt = a.team.Runtime
150 }
151 teammode.Probe(rt).ServeHTTP(w, r)
152}
153