| 1 | package main |
| 2 | |
| 3 | import ( |
| 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. |
| 16 | type registry struct { |
| 17 | Boundaries map[string]boundary `json:"boundaries"` |
| 18 | Principals map[string]principal `json:"principals"` |
| 19 | Genesis string `json:"genesis"` |
| 20 | } |
| 21 | |
| 22 | type boundary struct { |
| 23 | Kind string `json:"kind"` |
| 24 | } |
| 25 | |
| 26 | type 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 | |
| 33 | type key struct { |
| 34 | Class string `json:"class"` |
| 35 | Bound string `json:"bound"` |
| 36 | Public string `json:"public"` |
| 37 | Signs string `json:"signs"` |
| 38 | } |
| 39 | |
| 40 | type grant struct { |
| 41 | Boundary string `json:"boundary"` |
| 42 | } |
| 43 | |
| 44 | const ( |
| 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 | // boundaryApprove is a boundary kind the grants artifact is rendered |
| 54 | // from: the pre-receive hook's check of writes to the integration |
| 55 | // request namespace (valleyhook/). boundaryRequest is the same kind |
| 56 | // under the name it had first, and a registry may use either. |
| 57 | boundaryApprove = "approve" |
| 58 | boundaryRequest = "request" |
| 59 | |
| 60 | // boundaryRestack is the other: the pre-receive hook's check of the |
| 61 | // host restacker's writes, a version added to another principal's |
| 62 | // ready change and a restack report (valleyhook/). |
| 63 | boundaryRestack = "restack" |
| 64 | ) |
| 65 | |
| 66 | // readRegistry takes the registry out of a repository's tip, vets it, and |
| 67 | // exports it. |
| 68 | // |
| 69 | // The tip is taken apart with ls-tree and show rather than checked out, the |
| 70 | // same way the integrator reads the floor: a worktree would write into a |
| 71 | // repository this program has no business writing, and reading is the whole |
| 72 | // of what it needs. cue is shelled out to for the same reason the rest of |
| 73 | // the machinery shells out to it โ one pinned tool, one behaviour. |
| 74 | // |
| 75 | // Nothing is written outside the scratch directory, so every failure here |
| 76 | // leaves the artifacts as they were. |
| 77 | func readRegistry(repo, ref, dir, schema string) (registry, string, error) { |
| 78 | var r registry |
| 79 | |
| 80 | commit, err := git(repo, "rev-parse", ref) |
| 81 | if err != nil { |
| 82 | return r, "", fmt.Errorf("%s has no %s: %w", repo, ref, err) |
| 83 | } |
| 84 | |
| 85 | tree := ref + ":" + dir |
| 86 | listing, err := git(repo, "ls-tree", "--name-only", tree) |
| 87 | if err != nil { |
| 88 | return r, "", fmt.Errorf("no registry: %s has no %s/ at %s: %w", repo, dir, ref, err) |
| 89 | } |
| 90 | |
| 91 | scratch, err := os.MkdirTemp("", "valley-identity") |
| 92 | if err != nil { |
| 93 | return r, "", err |
| 94 | } |
| 95 | defer os.RemoveAll(scratch) |
| 96 | |
| 97 | // The registry is a CUE package: the directory is the enumeration, and |
| 98 | // the documents directly in it are what unify. |
| 99 | var files []string |
| 100 | for _, name := range strings.Split(strings.TrimSpace(listing), "\n") { |
| 101 | if !strings.HasSuffix(name, ".cue") { |
| 102 | continue |
| 103 | } |
| 104 | body, err := git(repo, "show", tree+"/"+name) |
| 105 | if err != nil { |
| 106 | return r, "", fmt.Errorf("reading %s/%s from %s at %s: %w", dir, name, repo, ref, err) |
| 107 | } |
| 108 | path := filepath.Join(scratch, name) |
| 109 | if err := os.WriteFile(path, []byte(body), 0o644); err != nil { |
| 110 | return r, "", err |
| 111 | } |
| 112 | files = append(files, path) |
| 113 | } |
| 114 | if len(files) == 0 { |
| 115 | return r, "", fmt.Errorf("no registry: %s holds no *.cue in %s/ at %s", repo, dir, ref) |
| 116 | } |
| 117 | |
| 118 | if out, err := cue(append([]string{"vet", "-c", schema}, files...)); err != nil { |
| 119 | return r, "", fmt.Errorf("the registry at %s does not satisfy %s:\n%s", commit, schema, out) |
| 120 | } |
| 121 | out, err := cue(append([]string{"export", schema}, files...)) |
| 122 | if err != nil { |
| 123 | return r, "", fmt.Errorf("exporting the registry at %s:\n%s", commit, out) |
| 124 | } |
| 125 | if err := json.Unmarshal([]byte(out), &r); err != nil { |
| 126 | return r, "", fmt.Errorf("the exported registry is not the shape this program reads: %w", err) |
| 127 | } |
| 128 | return r, commit, nil |
| 129 | } |
| 130 | |
| 131 | // git runs git over the instance repository with replacement refs off. A |
| 132 | // replacement ref makes one object stand in for another wherever git looks |
| 133 | // it up, so a refs/replace/<blob> pushed to the instance repository could |
| 134 | // swap in a registry that authorizes any key it liked without moving the |
| 135 | // ref this program reads. The repository is read as it is. |
| 136 | func git(repo string, args ...string) (string, error) { |
| 137 | cmd := exec.Command("git", append([]string{"--no-replace-objects", "-C", repo}, args...)...) |
| 138 | cmd.Env = gitEnvironment(os.Environ()) |
| 139 | out, err := cmd.Output() |
| 140 | if err != nil { |
| 141 | return "", commandError(err) |
| 142 | } |
| 143 | return strings.TrimRight(string(out), "\n"), nil |
| 144 | } |
| 145 | |
| 146 | // gitEnvironment is the environment git runs in: the caller's, less every |
| 147 | // GIT_ variable, and with git's own configuration fixed the way sigverify |
| 148 | // fixes it. A GIT_DIR, GIT_OBJECT_DIRECTORY or GIT_CONFIG_PARAMETERS in the |
| 149 | // caller's environment could point git at other objects or change how it |
| 150 | // reads them, and system and user configuration are not read. What is not |
| 151 | // git's own is inherited. |
| 152 | func gitEnvironment(environ []string) []string { |
| 153 | var env []string |
| 154 | for _, kv := range environ { |
| 155 | if !strings.HasPrefix(kv, "GIT_") { |
| 156 | env = append(env, kv) |
| 157 | } |
| 158 | } |
| 159 | return append(env, |
| 160 | "GIT_CONFIG_NOSYSTEM=1", |
| 161 | "GIT_CONFIG_SYSTEM=/dev/null", |
| 162 | "GIT_CONFIG_GLOBAL=/dev/null", |
| 163 | "GIT_NO_REPLACE_OBJECTS=1", |
| 164 | "GIT_ATTR_NOSYSTEM=1", |
| 165 | "GIT_TERMINAL_PROMPT=0", |
| 166 | ) |
| 167 | } |
| 168 | |
| 169 | // cue returns combined output, because its diagnosis is the error message |
| 170 | // worth passing on. |
| 171 | func cue(args []string) (string, error) { |
| 172 | out, err := exec.Command("cue", args...).CombinedOutput() |
| 173 | return strings.TrimRight(string(out), "\n"), err |
| 174 | } |
| 175 | |
| 176 | // commandError carries what a failing command said, which is otherwise |
| 177 | // dropped by exec's "exit status 1". |
| 178 | func commandError(err error) error { |
| 179 | var ee *exec.ExitError |
| 180 | if errors.As(err, &ee) && len(ee.Stderr) > 0 { |
| 181 | return fmt.Errorf("%w: %s", err, strings.TrimSpace(string(ee.Stderr))) |
| 182 | } |
| 183 | return err |
| 184 | } |