| 1 | package main |
| 2 | |
| 3 | // Reading a change's evidence. Every judgement about whether an attestation |
| 4 | // is what it claims to be is `attest verify`'s: the note's signatures, the |
| 5 | // text being the one written form of what it says, the statement satisfying |
| 6 | // the schema, and its subject digest being the tree in front of the |
| 7 | // verifier. This file runs that verifier and classifies what it said. |
| 8 | // |
| 9 | // Nothing here re-implements the envelope or the written form. What it does |
| 10 | // do is read values out of text that `attest verify` has already established |
| 11 | // is the written form of what it says — where a line is a key, one space, |
| 12 | // and a value, with no escapes. That is a lookup, not a parser, and it is |
| 13 | // sound only in that order: verify first, read second. |
| 14 | |
| 15 | import ( |
| 16 | "bytes" |
| 17 | "crypto/sha256" |
| 18 | "encoding/hex" |
| 19 | "fmt" |
| 20 | "os" |
| 21 | "os/exec" |
| 22 | "path" |
| 23 | "sort" |
| 24 | "strings" |
| 25 | "time" |
| 26 | ) |
| 27 | |
| 28 | const ( |
| 29 | attestationPrefix = "refs/the-valley/attestations" |
| 30 | statementNote = "statement.note" |
| 31 | |
| 32 | predicatePure = "the-valley/check/pure/v1" |
| 33 | predicateEffect = "the-valley/check/effectful/v1" |
| 34 | predicateTransfer = "the-valley/integration/transfer/v1" |
| 35 | ) |
| 36 | |
| 37 | // unknownSigner is the one sentence `attest verify` refuses a well-formed |
| 38 | // note with when nobody who signed it is a key the verifier holds. Every |
| 39 | // other refusal is a forgery as far as the integrator is concerned, so this |
| 40 | // classification fails closed: an unrecognised message is a rejection. |
| 41 | const unknownSigner = "no signature on this note is by a key the verifier holds" |
| 42 | |
| 43 | // collectEvidence finds every attestation stored for a subject digest and |
| 44 | // classifies it. Attestations are stored one ref per signer |
| 45 | // (dcr-0de694f), so a subject may carry several notes for one check; the |
| 46 | // first admissible one is the evidence, and a note that does not verify at |
| 47 | // all is reported so the verdict can reject rather than quietly look past |
| 48 | // it. |
| 49 | func (in *integrator) collectEvidence(wt *worktree, rev, subject string) (map[string]Evidence, error) { |
| 50 | refs, err := git(in.repo, "for-each-ref", "--format=%(refname)", attestationPrefix+"/"+subject) |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | found := map[string]Evidence{} |
| 55 | for _, ref := range strings.Fields(refs) { |
| 56 | listing, err := git(in.repo, "ls-tree", "-r", "--name-only", ref) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | for _, entry := range strings.Fields(listing) { |
| 61 | if path.Base(entry) != statementNote { |
| 62 | continue |
| 63 | } |
| 64 | note, err := git(in.repo, "cat-file", "blob", ref+":"+entry) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | ev, err := in.classify(wt, rev, []byte(note)) |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | ev.ref, ev.entry = ref, entry |
| 73 | // A check with an admissible note keeps it; otherwise the |
| 74 | // first thing found stands, so a forgery is never hidden by a |
| 75 | // good note arriving after it. |
| 76 | if old, seen := found[ev.Check]; seen && old.Admissible == Admissible { |
| 77 | continue |
| 78 | } |
| 79 | found[ev.Check] = ev |
| 80 | } |
| 81 | } |
| 82 | return found, nil |
| 83 | } |
| 84 | |
| 85 | // classify runs `attest verify` over one note and reads the statement back. |
| 86 | func (in *integrator) classify(wt *worktree, rev string, note []byte) (Evidence, error) { |
| 87 | text, err := noteText(note) |
| 88 | if err != nil { |
| 89 | return Evidence{}, err |
| 90 | } |
| 91 | ev := Evidence{ |
| 92 | note: note, |
| 93 | text: text, |
| 94 | TextDigest: sha256hex(text), |
| 95 | Check: statementValue(text, "predicate.check.name"), |
| 96 | Result: statementValue(text, "predicate.result"), |
| 97 | } |
| 98 | switch statementValue(text, "predicateType") { |
| 99 | case predicatePure: |
| 100 | ev.Kind = Pure |
| 101 | ev.Inputs = statementValue(text, "predicate.inputs.valley-inputs-v1") |
| 102 | case predicateEffect: |
| 103 | ev.Kind = Effectful |
| 104 | if at := statementValue(text, "predicate.observedAt"); at != "" { |
| 105 | if t, err := time.Parse("2006-01-02T15:04:05Z", at); err == nil { |
| 106 | ev.ObservedAt = t |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | if ev.Check == "" { |
| 111 | ev.Check = "(unnamed)" |
| 112 | } |
| 113 | |
| 114 | file, err := os.CreateTemp("", "valley-integrator-note") |
| 115 | if err != nil { |
| 116 | return ev, err |
| 117 | } |
| 118 | defer os.Remove(file.Name()) |
| 119 | if _, err := file.Write(note); err != nil { |
| 120 | return ev, err |
| 121 | } |
| 122 | file.Close() |
| 123 | |
| 124 | cmd := exec.Command(in.attest, "verify", |
| 125 | "--note", file.Name(), |
| 126 | "--known-keys", in.knownSigners, |
| 127 | "--schema", in.attestSchema, |
| 128 | "--repo", wt.dir, "--rev", rev) |
| 129 | out, err := cmd.CombinedOutput() |
| 130 | switch { |
| 131 | case err == nil: |
| 132 | ev.Admissible = Admissible |
| 133 | ev.Signer = verifiedSigner(string(out)) |
| 134 | case strings.Contains(string(out), unknownSigner): |
| 135 | ev.Admissible = UnknownSigner |
| 136 | default: |
| 137 | ev.Admissible = Tampered |
| 138 | ev.refusal = strings.TrimSpace(string(out)) |
| 139 | } |
| 140 | return ev, nil |
| 141 | } |
| 142 | |
| 143 | // verifiedSigner reads the name whose signature checked out, from the |
| 144 | // verifier's own report. |
| 145 | func verifiedSigner(out string) string { |
| 146 | for _, line := range strings.Split(out, "\n") { |
| 147 | if fields := strings.Fields(line); len(fields) == 3 && fields[0] == "signature" && fields[1] == "ok" { |
| 148 | return fields[2] |
| 149 | } |
| 150 | } |
| 151 | return "" |
| 152 | } |
| 153 | |
| 154 | // noteText returns the bytes a signature covers: everything before the |
| 155 | // blank line that separates the text from the signature block. The last |
| 156 | // blank line wins, so text holding one of its own does not move the split |
| 157 | // — the rule dcr-de9d996 fixes. |
| 158 | func noteText(note []byte) ([]byte, error) { |
| 159 | split := bytes.LastIndex(note, []byte("\n\n")) |
| 160 | if split < 0 { |
| 161 | return nil, fmt.Errorf("this is not a note: no blank line separates the text from its signatures") |
| 162 | } |
| 163 | return note[:split+1], nil |
| 164 | } |
| 165 | |
| 166 | // statementValue reads one line's value out of statement text. Sound only |
| 167 | // because the form has no escapes and `attest verify` has established the |
| 168 | // text is the written form of what it says: one line, one key, one value. |
| 169 | func statementValue(text []byte, key string) string { |
| 170 | for _, line := range strings.Split(string(text), "\n") { |
| 171 | if k, v, ok := strings.Cut(line, " "); ok && k == key { |
| 172 | return v |
| 173 | } |
| 174 | } |
| 175 | return "" |
| 176 | } |
| 177 | |
| 178 | func sha256hex(b []byte) string { |
| 179 | sum := sha256.Sum256(b) |
| 180 | return hex.EncodeToString(sum[:]) |
| 181 | } |
| 182 | |
| 183 | // treeDigest asks attest for a revision's valley tree digest. One |
| 184 | // implementation of the content-addressing scheme, in the program that |
| 185 | // defines it. |
| 186 | func (in *integrator) treeDigest(wt *worktree, rev string) (string, error) { |
| 187 | out, err := exec.Command(in.attest, "digest", "--repo", wt.dir, "--rev", rev).Output() |
| 188 | if err != nil { |
| 189 | return "", fmt.Errorf("digesting %s: %w", short(rev), err) |
| 190 | } |
| 191 | digest := strings.TrimSpace(string(out)) |
| 192 | scheme, value, ok := strings.Cut(digest, ":") |
| 193 | if !ok || scheme != "valley-tree-v1" { |
| 194 | return "", fmt.Errorf("attest digest printed %q", digest) |
| 195 | } |
| 196 | return value, nil |
| 197 | } |
| 198 | |
| 199 | // closures recomputes the input-closure digest of each pure check over the |
| 200 | // tree that would land. `attest inputs` evaluates and digests; it builds |
| 201 | // nothing, so this stays off the latency path the commit point serializes. |
| 202 | func (in *integrator) closures(wt *worktree, rev string, required []Required) (map[string]string, map[string]string) { |
| 203 | got, bad := map[string]string{}, map[string]string{} |
| 204 | names := make([]string, 0, len(required)) |
| 205 | byAttr := map[string]string{} |
| 206 | for _, r := range required { |
| 207 | if r.Runner != "nix" { |
| 208 | continue |
| 209 | } |
| 210 | names = append(names, r.Name) |
| 211 | byAttr[r.Name] = r.Attribute |
| 212 | } |
| 213 | sort.Strings(names) |
| 214 | for _, name := range names { |
| 215 | args := []string{"inputs", "--repo", wt.dir, "--rev", rev, "--check", name + "=" + byAttr[name]} |
| 216 | if in.system != "" { |
| 217 | args = append(args, "--system", in.system) |
| 218 | } |
| 219 | out, err := exec.Command(in.attest, args...).CombinedOutput() |
| 220 | if err != nil { |
| 221 | bad[name] = firstLine(string(out)) |
| 222 | continue |
| 223 | } |
| 224 | fields := strings.Fields(string(out)) |
| 225 | if len(fields) < 2 || !strings.HasPrefix(fields[1], "valley-inputs-v1:") { |
| 226 | bad[name] = "attest inputs printed " + firstLine(string(out)) |
| 227 | continue |
| 228 | } |
| 229 | got[name] = strings.TrimPrefix(fields[1], "valley-inputs-v1:") |
| 230 | } |
| 231 | return got, bad |
| 232 | } |
| 233 | |
| 234 | func firstLine(s string) string { |
| 235 | s = strings.TrimSpace(s) |
| 236 | if i := strings.IndexByte(s, '\n'); i >= 0 { |
| 237 | return s[:i] |
| 238 | } |
| 239 | return s |
| 240 | } |