gunk-dev/the-valley

registry.go

sourceebd98d24666emain's published tip history
4.2 KiB
1package main
2
3import (
4 "encoding/json"
5 "errors"
6 "fmt"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strings"
11)
12
13// The registry as the schema exports it (schema/identity.cue). The derived
14// fields the schema uses to hold the floor โ€” genesisGovernance,
15// externalGovernance โ€” are checked by `cue vet` and read by nobody here.
16type registry struct {
17 Boundaries map[string]boundary `json:"boundaries"`
18 Principals map[string]principal `json:"principals"`
19 Genesis string `json:"genesis"`
20}
21
22type boundary struct {
23 Kind string `json:"kind"`
24}
25
26type principal struct {
27 Kind string `json:"kind"`
28 Keys []key `json:"keys"`
29 Grants map[string]grant `json:"grants"`
30 Expires string `json:"expires"`
31}
32
33type key struct {
34 Class string `json:"class"`
35 Bound string `json:"bound"`
36 Public string `json:"public"`
37 Signs string `json:"signs"`
38}
39
40type grant struct {
41 Boundary string `json:"boundary"`
42}
43
44// boundaryGitPush is the boundary kind the authorized_keys artifact is
45// rendered from: sshd on a host serving the instance's repositories.
46const boundaryGitPush = "git-push"
47
48// readRegistry takes the registry out of a repository's tip, vets it, and
49// exports it.
50//
51// The tip is taken apart with ls-tree and show rather than checked out, the
52// same way the integrator reads the floor: a worktree would write into a
53// repository this program has no business writing, and reading is the whole
54// of what it needs. cue is shelled out to for the same reason the rest of
55// the machinery shells out to it โ€” one pinned tool, one behaviour.
56//
57// Nothing is written outside the scratch directory, so every failure here
58// leaves the artifacts as they were.
59func readRegistry(repo, ref, dir, schema string) (registry, string, error) {
60 var r registry
61
62 commit, err := git(repo, "rev-parse", ref)
63 if err != nil {
64 return r, "", fmt.Errorf("%s has no %s: %w", repo, ref, err)
65 }
66
67 tree := ref + ":" + dir
68 listing, err := git(repo, "ls-tree", "--name-only", tree)
69 if err != nil {
70 return r, "", fmt.Errorf("no registry: %s has no %s/ at %s: %w", repo, dir, ref, err)
71 }
72
73 scratch, err := os.MkdirTemp("", "valley-identity")
74 if err != nil {
75 return r, "", err
76 }
77 defer os.RemoveAll(scratch)
78
79 // The registry is a CUE package: the directory is the enumeration, and
80 // the documents directly in it are what unify.
81 var files []string
82 for _, name := range strings.Split(strings.TrimSpace(listing), "\n") {
83 if !strings.HasSuffix(name, ".cue") {
84 continue
85 }
86 body, err := git(repo, "show", tree+"/"+name)
87 if err != nil {
88 return r, "", fmt.Errorf("reading %s/%s from %s at %s: %w", dir, name, repo, ref, err)
89 }
90 path := filepath.Join(scratch, name)
91 if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
92 return r, "", err
93 }
94 files = append(files, path)
95 }
96 if len(files) == 0 {
97 return r, "", fmt.Errorf("no registry: %s holds no *.cue in %s/ at %s", repo, dir, ref)
98 }
99
100 if out, err := cue(append([]string{"vet", "-c", schema}, files...)); err != nil {
101 return r, "", fmt.Errorf("the registry at %s does not satisfy %s:\n%s", commit, schema, out)
102 }
103 out, err := cue(append([]string{"export", schema}, files...))
104 if err != nil {
105 return r, "", fmt.Errorf("exporting the registry at %s:\n%s", commit, out)
106 }
107 if err := json.Unmarshal([]byte(out), &r); err != nil {
108 return r, "", fmt.Errorf("the exported registry is not the shape this program reads: %w", err)
109 }
110 return r, commit, nil
111}
112
113func git(repo string, args ...string) (string, error) {
114 out, err := exec.Command("git", append([]string{"-C", repo}, args...)...).Output()
115 if err != nil {
116 return "", commandError(err)
117 }
118 return strings.TrimRight(string(out), "\n"), nil
119}
120
121// cue returns combined output, because its diagnosis is the error message
122// worth passing on.
123func cue(args []string) (string, error) {
124 out, err := exec.Command("cue", args...).CombinedOutput()
125 return strings.TrimRight(string(out), "\n"), err
126}
127
128// commandError carries what a failing command said, which is otherwise
129// dropped by exec's "exit status 1".
130func commandError(err error) error {
131 var ee *exec.ExitError
132 if errors.As(err, &ee) && len(ee.Stderr) > 0 {
133 return fmt.Errorf("%w: %s", err, strings.TrimSpace(string(ee.Stderr)))
134 }
135 return err
136}