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