| 1 | package main |
| 2 | |
| 3 | // coverage says which of a head's required nix checks already have evidence |
| 4 | // on origin that approval can reuse, so that approve (bin/valley) runs only |
| 5 | // the rest. A check worker publishes such evidence, and so does an earlier |
| 6 | // approval. |
| 7 | // |
| 8 | // Only what origin holds counts. coverage lists origin's attestation refs |
| 9 | // for the head's tree, fetches the objects they name by ID, and reads the |
| 10 | // notes from those objects. It never reads a local ref, so a note that |
| 11 | // was stored here and never published is not mistaken for evidence the |
| 12 | // integrator will see. A listing or a fetch that fails is an error, never |
| 13 | // an empty answer. |
| 14 | // |
| 15 | // A check is covered when one of those refs holds a note for it that |
| 16 | // meets every one of these: |
| 17 | // |
| 18 | // - attest verify accepts it: the note is in the one written form, it |
| 19 | // satisfies the schema, a key in the known-signers file signed it, and |
| 20 | // its subject is the head's tree; |
| 21 | // - it is pure evidence, from the nix runner, for this check's name and |
| 22 | // this check's flake attribute; |
| 23 | // - it records the check as passed. |
| 24 | // |
| 25 | // The known-signers file is the one a transfer statement is verified with |
| 26 | // (signers.go): VALLEY_KNOWN_SIGNERS or the serving host's publication. |
| 27 | // With none, nothing is covered and every check runs. |
| 28 | // |
| 29 | // The attribute test is stricter than the integrator's rule for an |
| 30 | // unmoved tree, which compares no attribute. A command check is never |
| 31 | // covered: approve runs it again. The integrator judges every note again |
| 32 | // when it decides the request, so coverage decides only what approve |
| 33 | // builds, never what lands. |
| 34 | // |
| 35 | // The answer is one line per fact, fields separated by single spaces: |
| 36 | // |
| 37 | // digest <valley-tree-v1 digest of the head's tree> |
| 38 | // stored <ref> <object ID> one per attestation ref origin holds for it |
| 39 | // covered <check> <ref> <signer>+<key hash> |
| 40 | // missing <check> |
| 41 | // |
| 42 | // with a covered or a missing line for every check asked about, in the |
| 43 | // order asked. |
| 44 | |
| 45 | import ( |
| 46 | "errors" |
| 47 | "fmt" |
| 48 | "io" |
| 49 | "os" |
| 50 | "os/exec" |
| 51 | "regexp" |
| 52 | "sort" |
| 53 | "strings" |
| 54 | ) |
| 55 | |
| 56 | const coverageUsage = "usage: valley-go coverage --head <sha> [--check <name>=<attribute>]..." |
| 57 | |
| 58 | const predicatePure = "the-valley/check/pure/v1" |
| 59 | |
| 60 | // checkName is a check's name or flake attribute as the policy schema |
| 61 | // admits one (#Name in schema/verification.cue). |
| 62 | var checkName = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*$`) |
| 63 | |
| 64 | // nixCheck is one required check the nix runner runs: its name, and the |
| 65 | // flake attribute the policy builds it from. |
| 66 | type nixCheck struct{ name, attribute string } |
| 67 | |
| 68 | func runCoverage(g git, args []string, w, stderr io.Writer) error { |
| 69 | var head string |
| 70 | var checks []nixCheck |
| 71 | for i := 0; i < len(args); i++ { |
| 72 | opt, value, inline := strings.Cut(args[i], "=") |
| 73 | if opt != "--head" && opt != "--check" { |
| 74 | return failure(coverageUsage) |
| 75 | } |
| 76 | if !inline { |
| 77 | if i+1 == len(args) { |
| 78 | return failure(coverageUsage) |
| 79 | } |
| 80 | i++ |
| 81 | value = args[i] |
| 82 | } |
| 83 | if opt == "--head" { |
| 84 | head = value |
| 85 | continue |
| 86 | } |
| 87 | name, attribute, ok := strings.Cut(value, "=") |
| 88 | if !ok || !checkName.MatchString(name) || !checkName.MatchString(attribute) { |
| 89 | return failure(fmt.Sprintf("--check %q: expected <name>=<attribute>", value)) |
| 90 | } |
| 91 | checks = append(checks, nixCheck{name, attribute}) |
| 92 | } |
| 93 | if head == "" { |
| 94 | return failure(coverageUsage) |
| 95 | } |
| 96 | oid, err := g.output(true, "rev-parse", "--verify", "--quiet", "--end-of-options", head+"^{commit}") |
| 97 | if err != nil || !isObjectID(oid) { |
| 98 | return failure("no such commit: " + head) |
| 99 | } |
| 100 | head = oid |
| 101 | repo := g.dir |
| 102 | if repo == "" { |
| 103 | repo = "." |
| 104 | } |
| 105 | |
| 106 | out, err := attestTool(g, "digest", "--repo", repo, "--rev", head) |
| 107 | digest, ok := strings.CutPrefix(strings.TrimSpace(out), "valley-tree-v1:") |
| 108 | if err != nil || !ok || digest == "" { |
| 109 | return failure("could not digest the tree of " + head) |
| 110 | } |
| 111 | |
| 112 | // Every attestation ref origin holds for the tree, signer by signer. |
| 113 | prefix := attestationRefs + digest + "/" |
| 114 | o, err := listOrigin(g, prefix+"*") |
| 115 | if err != nil { |
| 116 | return failure("could not list the evidence origin holds for this tree") |
| 117 | } |
| 118 | listed := o.refs |
| 119 | var stored []string |
| 120 | for ref := range listed { |
| 121 | if signer, ok := strings.CutPrefix(ref, prefix); ok && signer != "" && !strings.Contains(signer, "/") { |
| 122 | stored = append(stored, ref) |
| 123 | } |
| 124 | } |
| 125 | sort.Strings(stored) |
| 126 | |
| 127 | var lines []string |
| 128 | lines = append(lines, "digest "+digest) |
| 129 | for _, ref := range stored { |
| 130 | lines = append(lines, "stored "+ref+" "+listed[ref]) |
| 131 | } |
| 132 | covered, err := cover(g, repo, head, stored, listed, checks, stderr) |
| 133 | if err != nil { |
| 134 | return err |
| 135 | } |
| 136 | for _, c := range checks { |
| 137 | if by, ok := covered[c.name]; ok { |
| 138 | lines = append(lines, "covered "+c.name+" "+by) |
| 139 | } else { |
| 140 | lines = append(lines, "missing "+c.name) |
| 141 | } |
| 142 | } |
| 143 | _, err = io.WriteString(w, strings.Join(lines, "\n")+"\n") |
| 144 | return err |
| 145 | } |
| 146 | |
| 147 | // cover is the checks the stored refs hold acceptable evidence for, each |
| 148 | // mapped to "<ref> <signer>". It fetches the refs' objects only when there |
| 149 | // are checks to look for. |
| 150 | func cover(g git, repo, head string, stored []string, listed map[string]string, checks []nixCheck, stderr io.Writer) (map[string]string, error) { |
| 151 | covered := map[string]string{} |
| 152 | if len(stored) == 0 || len(checks) == 0 { |
| 153 | return covered, nil |
| 154 | } |
| 155 | var oids []string |
| 156 | for _, ref := range stored { |
| 157 | oids = append(oids, listed[ref]) |
| 158 | } |
| 159 | if err := fetch(g, oids...); err != nil { |
| 160 | return nil, failure("could not fetch the evidence origin holds for this tree") |
| 161 | } |
| 162 | keys, _ := verification.resolve() |
| 163 | if keys == "" { |
| 164 | fmt.Fprintf(stderr, "valley: no known-signers file to verify the evidence on origin with, so none of it is reused\n") |
| 165 | return covered, nil |
| 166 | } |
| 167 | for _, c := range checks { |
| 168 | for _, ref := range stored { |
| 169 | blob, err := g.output(true, "rev-parse", "--verify", "--quiet", listed[ref]+":"+c.name+"/statement.note") |
| 170 | if err != nil { |
| 171 | continue // this signer attested no such check |
| 172 | } |
| 173 | note, err := g.output(true, "cat-file", "blob", blob) |
| 174 | if err != nil { |
| 175 | return nil, fmt.Errorf("could not read %s from %s", c.name, ref) |
| 176 | } |
| 177 | signer, why, err := accept(g, repo, head, keys, c, note+"\n") |
| 178 | if err != nil { |
| 179 | return nil, err |
| 180 | } |
| 181 | if why != "" { |
| 182 | fmt.Fprintf(stderr, "valley: not reusing %s from %s: %s\n", c.name, ref, why) |
| 183 | continue |
| 184 | } |
| 185 | covered[c.name] = ref + " " + signer |
| 186 | break |
| 187 | } |
| 188 | } |
| 189 | return covered, nil |
| 190 | } |
| 191 | |
| 192 | // accept judges one note for one check. It returns the signer when the |
| 193 | // note is acceptable evidence for the check, and why not when it is not. |
| 194 | // The note is verified before anything is read out of its text, since |
| 195 | // statementValue is a lookup that is sound only over text attest verify |
| 196 | // has established is the written form of what it says. |
| 197 | func accept(g git, repo, head, keys string, c nixCheck, note string) (signer, why string, err error) { |
| 198 | file, err := os.CreateTemp("", "valley-coverage-*.note") |
| 199 | if err != nil { |
| 200 | return "", "", err |
| 201 | } |
| 202 | defer os.Remove(file.Name()) |
| 203 | _, err = file.WriteString(note) |
| 204 | if cerr := file.Close(); err == nil { |
| 205 | err = cerr |
| 206 | } |
| 207 | if err != nil { |
| 208 | return "", "", err |
| 209 | } |
| 210 | out, err := attestTool(g, "verify", "--note", file.Name(), "--known-keys", keys, "--repo", repo, "--rev", head) |
| 211 | if err != nil { |
| 212 | var exit *exec.ExitError |
| 213 | if !errors.As(err, &exit) { |
| 214 | return "", "", fmt.Errorf("could not run attest verify: %v", err) |
| 215 | } |
| 216 | return "", "attest verify refused it: " + refusal(out), nil |
| 217 | } |
| 218 | for _, line := range strings.Split(out, "\n") { |
| 219 | if f := strings.Fields(line); len(f) == 3 && f[0] == "signature" && f[1] == "ok" { |
| 220 | signer = f[2] |
| 221 | break |
| 222 | } |
| 223 | } |
| 224 | text := note[:strings.LastIndex(note, "\n\n")+1] // verified, so it holds the blank line |
| 225 | for _, want := range [][2]string{ |
| 226 | {"predicateType", predicatePure}, |
| 227 | {"predicate.check.name", c.name}, |
| 228 | {"predicate.check.runner", "nix"}, |
| 229 | {"predicate.check.attribute", c.attribute}, |
| 230 | {"predicate.result", "passed"}, |
| 231 | } { |
| 232 | if got := statementValue(text, want[0]); got != want[1] { |
| 233 | return "", fmt.Sprintf("its %s is %q, not %q", want[0], got, want[1]), nil |
| 234 | } |
| 235 | } |
| 236 | if signer == "" { |
| 237 | return "", "attest verify named no signer", nil |
| 238 | } |
| 239 | return signer, "", nil |
| 240 | } |
| 241 | |
| 242 | // attestTool runs the attest helper in g's repository and environment, |
| 243 | // and returns what it printed on stdout and stderr together. |
| 244 | func attestTool(g git, args ...string) (string, error) { |
| 245 | cmd := exec.Command("attest", args...) |
| 246 | cmd.Env = g.env |
| 247 | cmd.Dir = g.dir |
| 248 | out, err := cmd.CombinedOutput() |
| 249 | return string(out), err |
| 250 | } |
| 251 | |
| 252 | // statementValue reads one key's value from statement text, where each |
| 253 | // line is a key, one space and a value. |
| 254 | func statementValue(text, key string) string { |
| 255 | for _, line := range strings.Split(text, "\n") { |
| 256 | if k, v, ok := strings.Cut(line, " "); ok && k == key { |
| 257 | return v |
| 258 | } |
| 259 | } |
| 260 | return "" |
| 261 | } |
| 262 | |
| 263 | // refusal is the sentence attest refused with: the line it prefixes with |
| 264 | // its own name, or else the last line it printed. |
| 265 | func refusal(out string) string { |
| 266 | last := "" |
| 267 | for _, line := range strings.Split(strings.TrimSpace(out), "\n") { |
| 268 | if why, ok := strings.CutPrefix(line, "attest: "); ok { |
| 269 | return strings.TrimSpace(why) |
| 270 | } |
| 271 | if strings.TrimSpace(line) != "" { |
| 272 | last = strings.TrimSpace(line) |
| 273 | } |
| 274 | } |
| 275 | return last |
| 276 | } |