package adopt // Home preflight. Signed inspect gives each role's stream position, the // directory's generation and next team id, and any pending or open plan. // It does not give memberships or team_instances, and Home's db doors // admit people only, so the row counts come from Paul's read-only // `carlos db query` runs of HomeQueries, pasted back as counts. import ( "context" "errors" "slices" "time" "amadan.net/oficina/alta/homeop" "amadan.net/oficina/alta/provision" ) // homeTables are the tables whose rows the preflight counts. var homeTables = []string{ "teams", "memberships", "team_instances", "operator_http_teams", "roster_generation", "roster_consumers", "roster_tokens", "roster_operator_receipts", } // HomeQueries are the read-only queries Paul runs against Home, one count // per table, in a fixed order. func HomeQueries() []string { q := make([]string, len(homeTables)) for i, t := range homeTables { q[i] = "SELECT count(*) FROM " + t } return q } // expectedCount is the row count each phase requires of table t: nothing // before initialize; afterwards only initialize's generation and receipt. func expectedCount(phase, t string) int64 { if phase == PhasePrePlanA && (t == "roster_generation" || t == "roster_operator_receipts") { return 1 } return 0 } // CheckHome inspects both roles once, reads Alta's journal streams, and // checks them and the pasted counts against phase. It returns the record // to store and the refusal codes, in a fixed order; no codes means Home is // as phase requires. An error means the evidence could not be gathered. func CheckHome(ctx context.Context, h *homeop.Client, ps *provision.Store, counts map[string]int64, phase string) (HomePreflight, []string, error) { if phase != PhasePreInitialize && phase != PhasePrePlanA { return HomePreflight{}, nil, errors.New("adopt: unknown home preflight phase") } cred, err := inspect(ctx, h, homeop.Credential) if err != nil { return HomePreflight{}, nil, err } dir, err := inspect(ctx, h, homeop.Directory) if err != nil { return HomePreflight{}, nil, err } credSeq, credHead, _, err := ps.Stream(ctx, homeop.Credential) if err != nil { return HomePreflight{}, nil, errors.New("adopt: journal stream unreadable") } dirSeq, dirHead, _, err := ps.Stream(ctx, homeop.Directory) if err != nil { return HomePreflight{}, nil, errors.New("adopt: journal stream unreadable") } now := time.Now if h.Now != nil { now = h.Now } r := HomePreflight{ Phase: phase, CredentialSeq: cred.Sequence, CredentialHead: cred.Head, DirectorySeq: dir.Sequence, Generation: dir.Generation, ProposedID: dir.TeamID, ProposedSQID: dir.TeamSQID, Pending: cred.PendingPlan != "" || dir.PendingPlan != "", Open: cred.OpenPlan != "" || dir.OpenPlan != "", Counts: map[string]int64{}, AttestedAt: now(), } var codes []string refuse := func(bad bool, code string) { if bad { codes = append(codes, code) } } unused := func(in homeop.Inspection) bool { return in.Sequence == 0 && in.Head == "" } if phase == PhasePreInitialize { refuse(!unused(cred), "credential-stream-used") refuse(!unused(dir), "directory-stream-used") refuse(dir.Generation != nil, "generation-set") refuse(r.ProposedID != 1, "proposed-not-one") } else { // Only Alta's initialize has run on the directory: sequence 1, // and Home's head is the one Alta's journal holds. refuse(dirSeq != 1 || dir.Sequence != dirSeq || dir.Head != dirHead, "directory-stream-foreign") refuse(!unused(cred) && (cred.Sequence != credSeq || cred.Head != credHead), "credential-stream-foreign") refuse(dir.Generation == nil, "generation-missing") refuse(r.ProposedID != 1, "proposed-not-one") } // No plan may be in flight on either role. The spec allows none // before Plan A, so initialize's own window must have expired. refuse(r.Pending, "plan-pending") refuse(r.Open, "plan-open") for _, t := range homeTables { n, ok := counts[t] if !ok { codes = append(codes, "count-missing-"+t) continue } r.Counts[t] = n refuse(n != expectedCount(phase, t), "count-unexpected-"+t) } // A pasted table outside HomeQueries is a typo or a stray line. The // code is fixed: the name Paul typed is not repeated. for t := range counts { if !slices.Contains(homeTables, t) { codes = append(codes, "count-unknown-table") break } } return r, codes, nil } // inspect is one signed inspect of role. Its errors are fixed: no // endpoint, no body. func inspect(ctx context.Context, h *homeop.Client, role string) (homeop.Inspection, error) { in, o, err := h.Inspect(ctx, role) if err != nil || o != homeop.OK { return homeop.Inspection{}, errors.New("adopt: " + role + " inspect failed") } return in, nil }