gunk-dev/the-valley

registry.go

source3d782f3eac20main's published tip history
4.4 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
44const (
45 // boundaryGitPush is the boundary kind the authorized_keys artifact is
46 // rendered from: sshd on a host serving the instance's repositories.
47 boundaryGitPush = "git-push"
48
49 // boundaryRegistry is the boundary kind that admits a change to the
50 // registry document itself. A render leaving nobody there is refused.
51 boundaryRegistry = "registry"
52)
53
54// readRegistry takes the registry out of a repository's tip, vets it, and
55// exports it.
56//
57// The tip is taken apart with ls-tree and show rather than checked out, the
58// same way the integrator reads the floor: a worktree would write into a
59// repository this program has no business writing, and reading is the whole
60// of what it needs. cue is shelled out to for the same reason the rest of
61// the machinery shells out to it โ€” one pinned tool, one behaviour.
62//
63// Nothing is written outside the scratch directory, so every failure here
64// leaves the artifacts as they were.
65func readRegistry(repo, ref, dir, schema string) (registry, string, error) {
66 var r registry
67
68 commit, err := git(repo, "rev-parse", ref)
69 if err != nil {
70 return r, "", fmt.Errorf("%s has no %s: %w", repo, ref, err)
71 }
72
73 tree := ref + ":" + dir
74 listing, err := git(repo, "ls-tree", "--name-only", tree)
75 if err != nil {
76 return r, "", fmt.Errorf("no registry: %s has no %s/ at %s: %w", repo, dir, ref, err)
77 }
78
79 scratch, err := os.MkdirTemp("", "valley-identity")
80 if err != nil {
81 return r, "", err
82 }
83 defer os.RemoveAll(scratch)
84
85 // The registry is a CUE package: the directory is the enumeration, and
86 // the documents directly in it are what unify.
87 var files []string
88 for _, name := range strings.Split(strings.TrimSpace(listing), "\n") {
89 if !strings.HasSuffix(name, ".cue") {
90 continue
91 }
92 body, err := git(repo, "show", tree+"/"+name)
93 if err != nil {
94 return r, "", fmt.Errorf("reading %s/%s from %s at %s: %w", dir, name, repo, ref, err)
95 }
96 path := filepath.Join(scratch, name)
97 if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
98 return r, "", err
99 }
100 files = append(files, path)
101 }
102 if len(files) == 0 {
103 return r, "", fmt.Errorf("no registry: %s holds no *.cue in %s/ at %s", repo, dir, ref)
104 }
105
106 if out, err := cue(append([]string{"vet", "-c", schema}, files...)); err != nil {
107 return r, "", fmt.Errorf("the registry at %s does not satisfy %s:\n%s", commit, schema, out)
108 }
109 out, err := cue(append([]string{"export", schema}, files...))
110 if err != nil {
111 return r, "", fmt.Errorf("exporting the registry at %s:\n%s", commit, out)
112 }
113 if err := json.Unmarshal([]byte(out), &r); err != nil {
114 return r, "", fmt.Errorf("the exported registry is not the shape this program reads: %w", err)
115 }
116 return r, commit, nil
117}
118
119func git(repo string, args ...string) (string, error) {
120 out, err := exec.Command("git", append([]string{"-C", repo}, args...)...).Output()
121 if err != nil {
122 return "", commandError(err)
123 }
124 return strings.TrimRight(string(out), "\n"), nil
125}
126
127// cue returns combined output, because its diagnosis is the error message
128// worth passing on.
129func cue(args []string) (string, error) {
130 out, err := exec.Command("cue", args...).CombinedOutput()
131 return strings.TrimRight(string(out), "\n"), err
132}
133
134// commandError carries what a failing command said, which is otherwise
135// dropped by exec's "exit status 1".
136func commandError(err error) error {
137 var ee *exec.ExitError
138 if errors.As(err, &ee) && len(ee.Stderr) > 0 {
139 return fmt.Errorf("%w: %s", err, strings.TrimSpace(string(ee.Stderr)))
140 }
141 return err
142}