oficina / alta Public

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

Plain git — no account needed to clone.

Download

Download this file

1package adopt
2
3// Home preflight. Signed inspect gives each role's stream position, the
4// directory's generation and next team id, and any pending or open plan.
5// It does not give memberships or team_instances, and Home's db doors
6// admit people only, so the row counts come from Paul's read-only
7// `carlos db query` runs of HomeQueries, pasted back as counts.
8
9import (
10 "context"
11 "errors"
12 "slices"
13 "time"
14
15 "amadan.net/oficina/alta/homeop"
16 "amadan.net/oficina/alta/provision"
17)
18
19// homeTables are the tables whose rows the preflight counts.
20var homeTables = []string{
21 "teams", "memberships", "team_instances", "operator_http_teams",
22 "roster_generation", "roster_consumers", "roster_tokens", "roster_operator_receipts",
23}
24
25// HomeQueries are the read-only queries Paul runs against Home, one count
26// per table, in a fixed order.
27func HomeQueries() []string {
28 q := make([]string, len(homeTables))
29 for i, t := range homeTables {
30 q[i] = "SELECT count(*) FROM " + t
31 }
32 return q
33}
34
35// expectedCount is the row count each phase requires of table t: nothing
36// before initialize; afterwards only initialize's generation and receipt.
37func expectedCount(phase, t string) int64 {
38 if phase == PhasePrePlanA && (t == "roster_generation" || t == "roster_operator_receipts") {
39 return 1
40 }
41 return 0
42}
43
44// CheckHome inspects both roles once, reads Alta's journal streams, and
45// checks them and the pasted counts against phase. It returns the record
46// to store and the refusal codes, in a fixed order; no codes means Home is
47// as phase requires. An error means the evidence could not be gathered.
48func CheckHome(ctx context.Context, h *homeop.Client, ps *provision.Store, counts map[string]int64, phase string) (HomePreflight, []string, error) {
49 if phase != PhasePreInitialize && phase != PhasePrePlanA {
50 return HomePreflight{}, nil, errors.New("adopt: unknown home preflight phase")
51 }
52 cred, err := inspect(ctx, h, homeop.Credential)
53 if err != nil {
54 return HomePreflight{}, nil, err
55 }
56 dir, err := inspect(ctx, h, homeop.Directory)
57 if err != nil {
58 return HomePreflight{}, nil, err
59 }
60 credSeq, credHead, _, err := ps.Stream(ctx, homeop.Credential)
61 if err != nil {
62 return HomePreflight{}, nil, errors.New("adopt: journal stream unreadable")
63 }
64 dirSeq, dirHead, _, err := ps.Stream(ctx, homeop.Directory)
65 if err != nil {
66 return HomePreflight{}, nil, errors.New("adopt: journal stream unreadable")
67 }
68
69 now := time.Now
70 if h.Now != nil {
71 now = h.Now
72 }
73 r := HomePreflight{
74 Phase: phase,
75 CredentialSeq: cred.Sequence, CredentialHead: cred.Head,
76 DirectorySeq: dir.Sequence,
77 Generation: dir.Generation,
78 ProposedID: dir.TeamID, ProposedSQID: dir.TeamSQID,
79 Pending: cred.PendingPlan != "" || dir.PendingPlan != "",
80 Open: cred.OpenPlan != "" || dir.OpenPlan != "",
81 Counts: map[string]int64{},
82 AttestedAt: now(),
83 }
84
85 var codes []string
86 refuse := func(bad bool, code string) {
87 if bad {
88 codes = append(codes, code)
89 }
90 }
91 unused := func(in homeop.Inspection) bool { return in.Sequence == 0 && in.Head == "" }
92 if phase == PhasePreInitialize {
93 refuse(!unused(cred), "credential-stream-used")
94 refuse(!unused(dir), "directory-stream-used")
95 refuse(dir.Generation != nil, "generation-set")
96 refuse(r.ProposedID != 1, "proposed-not-one")
97 } else {
98 // Only Alta's initialize has run on the directory: sequence 1,
99 // and Home's head is the one Alta's journal holds.
100 refuse(dirSeq != 1 || dir.Sequence != dirSeq || dir.Head != dirHead, "directory-stream-foreign")
101 refuse(!unused(cred) && (cred.Sequence != credSeq || cred.Head != credHead), "credential-stream-foreign")
102 refuse(dir.Generation == nil, "generation-missing")
103 refuse(r.ProposedID != 1, "proposed-not-one")
104 }
105 // No plan may be in flight on either role. The spec allows none
106 // before Plan A, so initialize's own window must have expired.
107 refuse(r.Pending, "plan-pending")
108 refuse(r.Open, "plan-open")
109 for _, t := range homeTables {
110 n, ok := counts[t]
111 if !ok {
112 codes = append(codes, "count-missing-"+t)
113 continue
114 }
115 r.Counts[t] = n
116 refuse(n != expectedCount(phase, t), "count-unexpected-"+t)
117 }
118 // A pasted table outside HomeQueries is a typo or a stray line. The
119 // code is fixed: the name Paul typed is not repeated.
120 for t := range counts {
121 if !slices.Contains(homeTables, t) {
122 codes = append(codes, "count-unknown-table")
123 break
124 }
125 }
126 return r, codes, nil
127}
128
129// inspect is one signed inspect of role. Its errors are fixed: no
130// endpoint, no body.
131func inspect(ctx context.Context, h *homeop.Client, role string) (homeop.Inspection, error) {
132 in, o, err := h.Inspect(ctx, role)
133 if err != nil || o != homeop.OK {
134 return homeop.Inspection{}, errors.New("adopt: " + role + " inspect failed")
135 }
136 return in, nil
137}
138