| 1 | package main |
| 2 | |
| 3 | // The check state valley changes --checks and valley pending --checks |
| 4 | // list for the active version of each ready change whose active version |
| 5 | // has not landed: the checks the policy requires of it, and for each the |
| 6 | // state of the note the integrator would judge it by. |
| 7 | // |
| 8 | // The required checks are derived as the check worker derives them |
| 9 | // (worker.go): over the head's merge base with main, under both policy |
| 10 | // layers read at main's tip on origin. Each nix check's note is read as |
| 11 | // coverage reads it (coverage.go), from origin's attestation refs for the |
| 12 | // head's tree, verified under the host's published known-signers file: |
| 13 | // |
| 14 | // - passed: the note judged is one coverage would reuse; |
| 15 | // - failed: the note judged verifies, is signed by a signer who counts |
| 16 | // in this project, and coverage would not reuse it, because it |
| 17 | // records the check as failed or because it is the wrong kind, runner |
| 18 | // or attribute. why says which; |
| 19 | // - pending: no note about the check verifies and is signed by a signer |
| 20 | // who counts. why says why a note origin holds does not count, when |
| 21 | // one is there. |
| 22 | // |
| 23 | // A check with a runner other than nix is listed as other, since the host |
| 24 | // worker does not run it, and it does not count toward the overall state. |
| 25 | // VALLEY_KNOWN_SIGNERS does not narrow what is read here: the listing says |
| 26 | // what the integrator would judge. |
| 27 | // |
| 28 | // It is behind --checks because it costs seconds a repository: reading |
| 29 | // the policy with cue is most of it, and attest verify for each note the |
| 30 | // rest. The reads are batched by repository: the policy is read once, |
| 31 | // origin's attestation refs for every head are listed in one ls-remote and |
| 32 | // fetched in one fetch, and the host's signers are read once. A read that fails |
| 33 | // leaves the changes it was for without check state, and says so on |
| 34 | // stderr; it never reads as no evidence. |
| 35 | |
| 36 | import ( |
| 37 | "fmt" |
| 38 | "io" |
| 39 | "os" |
| 40 | "strings" |
| 41 | ) |
| 42 | |
| 43 | // The check states of a version's checks. |
| 44 | const ( |
| 45 | checkPassed = "passed" |
| 46 | checkFailed = "failed" |
| 47 | checkPending = "pending" |
| 48 | checkOther = "other" |
| 49 | ) |
| 50 | |
| 51 | // jsonVersionChecks is a version's check state, as valley-changes/v1 |
| 52 | // writes it under checks. |
| 53 | type jsonVersionChecks struct { |
| 54 | State string `json:"state"` |
| 55 | Tree string `json:"tree"` |
| 56 | Required []jsonCheckState `json:"required"` |
| 57 | } |
| 58 | |
| 59 | // jsonCheckState is one required check and the state of its note. |
| 60 | type jsonCheckState struct { |
| 61 | Name string `json:"name"` |
| 62 | State string `json:"state"` |
| 63 | Signer string `json:"signer,omitempty"` |
| 64 | Why string `json:"why,omitempty"` |
| 65 | } |
| 66 | |
| 67 | // addChecks gives the active version of each pending change in view its |
| 68 | // check state. view is this repository's. |
| 69 | func (l listed) addChecks(view *jsonRepo, stderr io.Writer) { |
| 70 | if l.err != nil { |
| 71 | return |
| 72 | } |
| 73 | pending := map[[2]string]bool{} |
| 74 | for _, n := range l.snap.all { |
| 75 | if pendingChange(l.snap, n) { |
| 76 | pending[[2]string{n.record.Target, n.id}] = true |
| 77 | } |
| 78 | } |
| 79 | var heads []*jsonVersion |
| 80 | for i := range view.Changes { |
| 81 | c := &view.Changes[i] |
| 82 | if pending[[2]string{c.Target, c.ChangeID}] { |
| 83 | heads = append(heads, &c.Versions[c.Active-1]) |
| 84 | } |
| 85 | } |
| 86 | if len(heads) == 0 { |
| 87 | return |
| 88 | } |
| 89 | warn := func(format string, args ...any) { |
| 90 | fmt.Fprintf(stderr, "valley: %s: %s\n", l.name, fmt.Sprintf(format, args...)) |
| 91 | } |
| 92 | if l.snap.tip == "" { |
| 93 | warn("origin has no main to read the policy at, so no ready change lists its checks") |
| 94 | return |
| 95 | } |
| 96 | instance, layer, done, err := policyLayers(l.g, l.snap.tip) |
| 97 | defer done() |
| 98 | if err != nil { |
| 99 | warn("the policy at main did not read, so no ready change lists its checks: %v", err) |
| 100 | return |
| 101 | } |
| 102 | schema, err := schemaFile(l.g, "") |
| 103 | if err != nil { |
| 104 | warn("%v, so no ready change lists its checks", err) |
| 105 | return |
| 106 | } |
| 107 | model, err := policyModel(schema, instance, layer) |
| 108 | if err != nil { |
| 109 | warn("the policy at main did not read, so no ready change lists its checks: %v", err) |
| 110 | return |
| 111 | } |
| 112 | repo := l.g.dir |
| 113 | if repo == "" { |
| 114 | repo = "." |
| 115 | } |
| 116 | |
| 117 | // Each head's required checks and tree digest. |
| 118 | type head struct { |
| 119 | v *jsonVersion |
| 120 | required []jsonRequired |
| 121 | asked []nixCheck |
| 122 | digest string |
| 123 | } |
| 124 | var read []head |
| 125 | var patterns []string |
| 126 | for _, v := range heads { |
| 127 | say := func(format string, args ...any) { |
| 128 | warn("v%d of %s lists no checks: %s", v.N, v.Head, fmt.Sprintf(format, args...)) |
| 129 | } |
| 130 | base, err := l.g.output(true, "merge-base", l.snap.tip, v.Head) |
| 131 | if err != nil { |
| 132 | say("it shares no history with main") |
| 133 | continue |
| 134 | } |
| 135 | doc, err := deriveUnder(l.g, model, base, v.Head) |
| 136 | if err != nil { |
| 137 | say("its checks did not derive: %v", err) |
| 138 | continue |
| 139 | } |
| 140 | digest, err := treeDigest(l.g, repo, v.Head) |
| 141 | if err != nil { |
| 142 | say("%v", err) |
| 143 | continue |
| 144 | } |
| 145 | h := head{v: v, required: doc.Required, digest: digest} |
| 146 | for _, q := range doc.Required { |
| 147 | if q.Runner == "nix" { |
| 148 | h.asked = append(h.asked, nixCheck{q.Name, q.Attribute}) |
| 149 | } |
| 150 | } |
| 151 | read = append(read, h) |
| 152 | patterns = append(patterns, attestationRefs+digest+"/*") |
| 153 | } |
| 154 | if len(read) == 0 { |
| 155 | return |
| 156 | } |
| 157 | |
| 158 | // The evidence origin holds for every head, listed and fetched at once. |
| 159 | o, err := listOrigin(l.g, patterns...) |
| 160 | if err != nil { |
| 161 | warn("could not list the evidence origin holds, so no ready change lists its checks") |
| 162 | return |
| 163 | } |
| 164 | var oids []string |
| 165 | for _, oid := range o.refs { |
| 166 | oids = append(oids, oid) |
| 167 | } |
| 168 | if len(oids) > 0 { |
| 169 | if err := fetch(l.g, oids...); err != nil { |
| 170 | warn("could not fetch the evidence origin holds, so no ready change lists its checks") |
| 171 | return |
| 172 | } |
| 173 | } |
| 174 | keys, p, unsigned := hostSigners(l.g) |
| 175 | if unsigned == "" { |
| 176 | defer os.Remove(keys) |
| 177 | } |
| 178 | var here counting |
| 179 | if p != nil { |
| 180 | here = counting{project: project(l.g), integrator: p.name, projects: p.projects} |
| 181 | } |
| 182 | |
| 183 | for _, h := range read { |
| 184 | stored := storedFor(h.digest, o.refs) |
| 185 | var judged map[string]judgment |
| 186 | unjudged := unsigned |
| 187 | if unjudged == "" && len(stored) > 0 && len(h.asked) > 0 { |
| 188 | judged, unjudged, err = judge(l.g, repo, h.v.Head, keys, "", here, stored, o.refs, h.asked) |
| 189 | if err != nil { |
| 190 | warn("v%d of %s lists no checks: %v", h.v.N, h.v.Head, err) |
| 191 | continue |
| 192 | } |
| 193 | } |
| 194 | h.v.Checks = checkStates(h.digest, h.required, judged, unsigned, unjudged) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // checkStates is a version's check state, from the note judged for each |
| 199 | // nix check. unsigned says why no note could be verified, and unjudged why |
| 200 | // the integrator cannot judge the tree's evidence at all. |
| 201 | func checkStates(digest string, required []jsonRequired, judged map[string]judgment, unsigned, unjudged string) *jsonVersionChecks { |
| 202 | out := &jsonVersionChecks{State: checkPassed, Tree: digest, Required: []jsonCheckState{}} |
| 203 | for _, q := range required { |
| 204 | s := jsonCheckState{Name: q.Name} |
| 205 | j, ok := judged[q.Name] |
| 206 | switch { |
| 207 | case q.Runner != "nix": |
| 208 | s.State = checkOther |
| 209 | case unsigned != "": |
| 210 | s.State, s.Why = checkPending, unsigned |
| 211 | case unjudged != "": |
| 212 | s.State, s.Why = checkFailed, unjudged+", so the integrator cannot judge this tree's evidence" |
| 213 | case !ok: |
| 214 | s.State = checkPending |
| 215 | case j.note.refusal != "": |
| 216 | s.State, s.Why = checkPending, j.note.refusal |
| 217 | case j.why != "": |
| 218 | s.State, s.Signer, s.Why = checkFailed, j.note.signer, j.why |
| 219 | default: |
| 220 | s.State, s.Signer = checkPassed, j.note.signer |
| 221 | } |
| 222 | switch { |
| 223 | case s.State == checkFailed: |
| 224 | out.State = checkFailed |
| 225 | case s.State == checkPending && out.State == checkPassed: |
| 226 | out.State = checkPending |
| 227 | } |
| 228 | out.Required = append(out.Required, s) |
| 229 | } |
| 230 | return out |
| 231 | } |
| 232 | |
| 233 | // checksLine is valley pending's line for a version's check state: |
| 234 | // "checks: 18 passed, 1 failed (valley-request)". |
| 235 | func checksLine(c *jsonVersionChecks) string { |
| 236 | if len(c.Required) == 0 { |
| 237 | return "checks: none required" |
| 238 | } |
| 239 | count := map[string]int{} |
| 240 | var failed []string |
| 241 | for _, q := range c.Required { |
| 242 | count[q.State]++ |
| 243 | if q.State == checkFailed { |
| 244 | failed = append(failed, q.Name) |
| 245 | } |
| 246 | } |
| 247 | var parts []string |
| 248 | for _, state := range []string{checkPassed, checkFailed, checkPending, checkOther} { |
| 249 | if count[state] == 0 { |
| 250 | continue |
| 251 | } |
| 252 | part := fmt.Sprintf("%d %s", count[state], state) |
| 253 | if state == checkFailed { |
| 254 | part += " (" + strings.Join(failed, ", ") + ")" |
| 255 | } |
| 256 | parts = append(parts, part) |
| 257 | } |
| 258 | return "checks: " + strings.Join(parts, ", ") |
| 259 | } |