oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "amadan.net/rastrillo/rastrillo/sessions"
5 "context"
6 "database/sql"
7 "errors"
8 "net/http"
9 "sync/atomic"
10 "time"
11
12 "amadan.net/oficina/home/handoff/seal"
13 "amadan.net/oficina/home/roster"
14 "amadan.net/oficina/home/teammode"
15 "amadan.net/rastrillo/rastrillo/migrate"
16 "github.com/carlosframework/docs/internal/docs/linkstore"
17 "github.com/carlosframework/docs/internal/docs/principalstore"
18)
19
20// TeamConfig is explicit deployment configuration, never browser request input.
21// cmd/docs fills it from teammode.ConfigFromEnv("DOCS").
22type TeamConfig struct {
23 transport http.RoundTripper
24 clock roster.Clock
25 TeamID string
26 DirectoryOrigin string
27 ProvisioningBase64 string
28 OperatorKeysBase64 string
29 RosterToken string
30}
31
32// Docs' two team errors are teammode's, so a refusal from Verify, Open or
33// Guard is the same value the handlers already map to a refused boot and a
34// retryable 503.
35var errTeamConfig = teammode.ErrConfig
36var errTeamUnavailable = teammode.ErrUnavailable
37
38// teamState is the activation marker and roster evidence. Its schema is
39// byte-for-byte the docs_team/0001_team migration Docs applied before it
40// used teammode, so an activated database's ledger is unchanged
41// (TestTeamStateKeepsTheAppliedLedger).
42var teamState = teammode.MustState("docs")
43
44type teamSettings struct {
45 *teammode.Settings
46 transport http.RoundTripper
47 clock roster.Clock
48}
49
50// TeamBootSchema is BootSchema and the team sets. linkstore.ForPrincipals
51// sits just before principalstore.Schema: see its comment.
52var TeamBootSchema = migrate.Merge(BootSchema, linkstore.ForPrincipals, principalstore.Schema, teamState.Schema())
53
54func validatedTeamConfig(cfg *TeamConfig, home *homeSettings, origin string) (*teamSettings, error) {
55 if cfg == nil {
56 return nil, nil
57 }
58 if home == nil {
59 return nil, errTeamConfig
60 }
61 settings, e := teammode.Verify(&teammode.Config{TeamID: cfg.TeamID, DirectoryOrigin: cfg.DirectoryOrigin, ProvisioningBase64: cfg.ProvisioningBase64, OperatorKeysBase64: cfg.OperatorKeysBase64, RosterToken: cfg.RosterToken}, &teammode.Home{Issuer: home.issuer, ConsumerID: home.consumerID}, "docs", origin)
62 if e != nil {
63 return nil, errTeamConfig
64 }
65 return &teamSettings{Settings: settings, transport: cfg.transport, clock: cfg.clock}, nil
66}
67
68type teamRuntime struct {
69 // cache is the roster cache, behind teammode's runtime: its Check is
70 // the only way Docs asks it, and Access the only way a request does.
71 cache *teammode.Runtime
72 home *homeRoutes
73 readSession func(context.Context, *http.Request) (sessions.Session, bool)
74 principals *principalstore.Store
75 bindings roster.Bindings
76 db *sql.DB
77 // leaseRemaining stands in for Access.Remaining in tests (a contended
78 // roster answers 0; team_stream.go). nil in production.
79 leaseRemaining func(*requestAccess) time.Duration
80 // freshLeases counts freshLease's roster asks (tests).
81 freshLeases atomic.Int64
82}
83
84func newTeamRuntime(ctx context.Context, settings *teamSettings, db *sql.DB, home *homeSettings) (*teamRuntime, error) {
85 if settings == nil || db == nil || home == nil {
86 return nil, errTeamConfig
87 }
88 sealer, e := seal.New(home.dek, "oficina/docs")
89 if e != nil {
90 return nil, errTeamConfig
91 }
92 cache, e := teammode.Open(ctx, teammode.RuntimeConfig{Settings: settings.Settings, State: teamState, DB: db, Sealer: sealer, IssuerTables: []string{"home_identity_links", "docs_home_identities"}, Transport: settings.transport, Clock: settings.clock})
93 if e != nil {
94 if errors.Is(e, teammode.ErrConfig) {
95 return nil, errTeamConfig
96 }
97 return nil, errTeamUnavailable
98 }
99 b := cache.Bindings()
100 runtime := &teamRuntime{cache: cache, bindings: b, db: db}
101 // The allocation gate: a Home-only principal is created only while the
102 // roster says Member under a valid lease. connect's Unlinked hook asks
103 // AdmitUnlinked first (home_connect.go); this is the second gate,
104 // inside EnsureHome, so it holds for every caller.
105 principals, e := principalstore.New(db, principalstore.Config{Issuer: b.Issuer, Team: b.Team, RequireMembership: func(ctx context.Context, request principalstore.MembershipRequest) error {
106 if request.Team != b.Team {
107 return principalstore.ErrUnavailable
108 }
109 switch e := cache.AdmitUnlinked(ctx, request.Issuer, request.Subject); {
110 case e == nil:
111 return nil
112 case errors.Is(e, teammode.ErrNotMember):
113 return principalstore.ErrRefused
114 }
115 return principalstore.ErrUnavailable
116 }})
117 if e != nil {
118 return nil, errTeamUnavailable
119 }
120 runtime.principals = principals
121 return runtime, nil
122}
123