| 1 | // Package verdict is what integration decides, as a function of data. |
| 2 | // |
| 3 | // It is a library on purpose. Nothing in it reads git, runs nix, or looks |
| 4 | // at a clock: a caller gathers all of that into a Snapshot first, and this |
| 5 | // package turns the Snapshot into a decision. That is what makes the rules |
| 6 | // of dcr-439b771 statable once and testable without a repository, and it |
| 7 | // is why the refs transport — its first caller — is the only thing that |
| 8 | // has to change when a second one arrives. |
| 9 | package verdict |
| 10 | |
| 11 | // Integration is optimistic concurrency control over content-addressed |
| 12 | // evidence. A change is a transaction; its snapshot is the base tree its |
| 13 | // attestations were produced over; its read set is the union of its |
| 14 | // required checks' input closures; its write set is its delta. The |
| 15 | // integrator is the commit point, and the commit rule has two parts: the |
| 16 | // delta must apply cleanly to the current tip, and per required check the |
| 17 | // evidence must transfer. |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "sort" |
| 22 | "time" |
| 23 | ) |
| 24 | |
| 25 | // Outcome is what the integrator does with a change. |
| 26 | type Outcome string |
| 27 | |
| 28 | const ( |
| 29 | // Land: the delta applies and every required check's evidence stands. |
| 30 | Land Outcome = "land" |
| 31 | |
| 32 | // Stale: the change is behind, not wrong. Either the delta no longer |
| 33 | // applies or some evidence no longer transfers. The request stays put |
| 34 | // and retry is partial — only the named checks need new evidence. |
| 35 | Stale Outcome = "stale" |
| 36 | |
| 37 | // Reject: the evidence is not evidence. A note whose signature does |
| 38 | // not check out, text that is not the written form of what it says, or |
| 39 | // a statement about a different tree is not a staleness problem, and |
| 40 | // re-attesting would not fix it. |
| 41 | Reject Outcome = "reject" |
| 42 | ) |
| 43 | |
| 44 | // Kind is the two kinds of claim of archive/design/verification.md, which decide |
| 45 | // which transfer rule applies. |
| 46 | type Kind string |
| 47 | |
| 48 | const ( |
| 49 | Pure Kind = "pure" |
| 50 | Effectful Kind = "effectful" |
| 51 | ) |
| 52 | |
| 53 | // KindOf is the kind of claim a policy runner demands. It is the whole of |
| 54 | // the mapping from what the policy says about a check to which transfer |
| 55 | // rule governs its evidence, and it is the only thing that decides which |
| 56 | // rule runs. An unknown runner maps to no kind and gets no default: a |
| 57 | // check nobody has decided how to transfer is not one to land on a guess. |
| 58 | func KindOf(runner string) Kind { |
| 59 | switch runner { |
| 60 | case "nix": |
| 61 | return Pure |
| 62 | case "command": |
| 63 | return Effectful |
| 64 | } |
| 65 | return "" |
| 66 | } |
| 67 | |
| 68 | // Admissibility is what a verifier made of an attestation before any |
| 69 | // transfer rule was applied. |
| 70 | type Admissibility string |
| 71 | |
| 72 | const ( |
| 73 | // Admissible: the note verifies under a key the integrator holds, its |
| 74 | // text is the written form of what it says, and its subject is the |
| 75 | // tree the change produces. |
| 76 | Admissible Admissibility = "admissible" |
| 77 | |
| 78 | // UnknownSigner: the note is well formed but nobody who signed it is |
| 79 | // in the known-signers file. Evidence from a signer the integrator |
| 80 | // does not hold does not transfer. Deciding what to do about an |
| 81 | // unknown attester — re-verification, trust measurement — belongs to |
| 82 | // the trust backstop and is deliberately not here. |
| 83 | UnknownSigner Admissibility = "unknown-signer" |
| 84 | |
| 85 | // Tampered: everything else a verifier can refuse — a signature that |
| 86 | // does not check out over the text, text that is not its own written |
| 87 | // form, a statement whose subject digest is not the change's tree. |
| 88 | Tampered Admissibility = "tampered" |
| 89 | ) |
| 90 | |
| 91 | // Change is the integration primitive (ida-93e4f91): a delta targeting a |
| 92 | // version stream, with the provenance and the attestations produced over |
| 93 | // the tree it results in. The branch it arrived on is transport. |
| 94 | type Change struct { |
| 95 | // ID names the change. The refs transport takes it from the last |
| 96 | // segment of the request ref. |
| 97 | ID string |
| 98 | |
| 99 | // Target is the protected ref the delta targets, in full. |
| 100 | Target string |
| 101 | |
| 102 | // Base is the commit the delta was authored against, and Head the |
| 103 | // commit whose tree the attestations name. |
| 104 | Base string |
| 105 | Head string |
| 106 | |
| 107 | // Attested is the valley-tree-v1 digest of Head's tree — the snapshot |
| 108 | // the transaction read, and the subject every attestation names. |
| 109 | Attested string |
| 110 | |
| 111 | // Evidence is the attestations found for this change, by check name. |
| 112 | Evidence map[string]Evidence |
| 113 | } |
| 114 | |
| 115 | // Evidence is one attestation as the integrator read it back. |
| 116 | type Evidence struct { |
| 117 | Check string |
| 118 | Kind Kind |
| 119 | Admissible Admissibility |
| 120 | |
| 121 | // Result is what the statement recorded. A statement recording a |
| 122 | // failure is not evidence that a check passed. |
| 123 | Result string |
| 124 | |
| 125 | // Inputs and Derivation are the two re-derivable digests a pure claim |
| 126 | // carries: the digest of the check's input closure, and the digest of |
| 127 | // the derivation that closure builds. Both are compared, because the |
| 128 | // claim a pure attestation makes is the whole of what it records, and |
| 129 | // evidence that transfers is evidence whose whole claim still holds |
| 130 | // (dcr-0de694f). |
| 131 | Inputs string |
| 132 | Derivation string |
| 133 | |
| 134 | // ObservedAt is when an effectful check's result was observed. |
| 135 | ObservedAt time.Time |
| 136 | |
| 137 | // TextDigest is a SHA-256 over the bytes the signature covers. The |
| 138 | // transfer statement cites the original by this digest. |
| 139 | TextDigest string |
| 140 | |
| 141 | // Signer names the key whose signature the integrator accepted, for |
| 142 | // the reader of an outcome. Empty when none was accepted. |
| 143 | Signer string |
| 144 | |
| 145 | // Refusal is the sentence a verifier refused this attestation with, |
| 146 | // already extracted by whoever ran the verifier. This package never |
| 147 | // parses another program's output: a caller that drives a command-line |
| 148 | // verifier owns that command's conventions, and hands the result in |
| 149 | // structured. |
| 150 | Refusal string |
| 151 | } |
| 152 | |
| 153 | // Required is one check the policy demands of this change, as the deriver |
| 154 | // named it. |
| 155 | type Required struct { |
| 156 | Name string |
| 157 | |
| 158 | // Classes are the path classes that made this check required. An |
| 159 | // effectful check's scope is these classes' paths: an intervening |
| 160 | // landing inside one of them is what ends the observation's validity. |
| 161 | Classes []string |
| 162 | |
| 163 | // Runner and Attribute are how the check is run, from the composed |
| 164 | // policy's catalogue. |
| 165 | Runner string |
| 166 | Attribute string |
| 167 | |
| 168 | // Validity is Δ: how long an effectful observation of this check stays |
| 169 | // good. Zero means the policy declares no window, and absent a |
| 170 | // declared window the check is re-demanded — a window nobody wrote |
| 171 | // down is not a window the integrator may invent. |
| 172 | Validity time.Duration |
| 173 | } |
| 174 | |
| 175 | // Applied is what happened when the delta was applied to the current tip. |
| 176 | type Applied struct { |
| 177 | // Clean says the delta applied with no conflict. A conflict means |
| 178 | // resolving it would produce a tree nobody has authored, and new |
| 179 | // authorship needs new evidence. |
| 180 | Clean bool |
| 181 | |
| 182 | // Conflict is what the merge said, for the reader of the outcome. |
| 183 | Conflict string |
| 184 | |
| 185 | // Tree is the valley-tree-v1 digest of the resulting tree. Equal to |
| 186 | // the change's Attested digest exactly when nothing the change reads |
| 187 | // has moved, whatever else landed in between. |
| 188 | Tree string |
| 189 | } |
| 190 | |
| 191 | // Snapshot is everything about a change that had to be computed by |
| 192 | // touching git, nix, or the clock. A transport fills it in; the verdict is |
| 193 | // a function of it. |
| 194 | type Snapshot struct { |
| 195 | Apply Applied |
| 196 | |
| 197 | // Required is the check set derived from the diff by the policy |
| 198 | // machinery, read at the target tip and never from the submitted tree. |
| 199 | Required []Required |
| 200 | |
| 201 | // Closures holds each pure check's re-derivable digests as recomputed |
| 202 | // over the tree that would land, keyed by check name. A check missing |
| 203 | // from both this map and ClosureErrors was not recomputed because it |
| 204 | // did not need to be. |
| 205 | Closures map[string]Closure |
| 206 | |
| 207 | // ClosureErrors holds why a recomputation could not be done. A closure |
| 208 | // the integrator cannot compute is a check it cannot transfer. |
| 209 | ClosureErrors map[string]string |
| 210 | |
| 211 | // Touched are the policy classes that intervening landed changes |
| 212 | // touched, between the change's base and the current tip. |
| 213 | Touched map[string]bool |
| 214 | |
| 215 | // Now is when the judgement is being made, for the effectful window. |
| 216 | Now time.Time |
| 217 | } |
| 218 | |
| 219 | // Closure is what evaluating a pure check over a tree named, before |
| 220 | // anything was built: the digest of its input closure and the digest of |
| 221 | // its derivation. |
| 222 | type Closure struct { |
| 223 | Inputs string |
| 224 | Derivation string |
| 225 | } |
| 226 | |
| 227 | // CheckVerdict is one check's outcome at the commit point. |
| 228 | type CheckVerdict struct { |
| 229 | Name string |
| 230 | |
| 231 | // Transferred says the evidence stands on the landed tree. |
| 232 | Transferred bool |
| 233 | |
| 234 | // Rule is which transfer rule carried it: "unmoved" when the landed |
| 235 | // tree is the attested tree, "closure" when the recomputed input |
| 236 | // closure equalled the attested one, "untouched" when an effectful |
| 237 | // observation's scope was untouched and it was inside its window. |
| 238 | Rule string |
| 239 | |
| 240 | // Reason says why it did not transfer, in words a contributor can act |
| 241 | // on. Empty when it did. |
| 242 | Reason string |
| 243 | |
| 244 | // Evidence is the digest of the statement text this verdict is about, |
| 245 | // and Inputs the closure digest recomputed under the closure rule. |
| 246 | Evidence string |
| 247 | Inputs string |
| 248 | |
| 249 | Signer string |
| 250 | } |
| 251 | |
| 252 | // Verdict is what the integrator decided, and everything the outcome event |
| 253 | // and the transfer statement are written from. |
| 254 | type Verdict struct { |
| 255 | Outcome Outcome |
| 256 | |
| 257 | // Reason is the one-line summary a reader gets. For a rejection it is |
| 258 | // what was wrong; for staleness, which rule the change fell to. |
| 259 | Reason string |
| 260 | |
| 261 | // StaleReason distinguishes the two ways a change goes stale, as |
| 262 | // #RequestStale carries it: "conflict" or "evidence". |
| 263 | StaleReason string |
| 264 | |
| 265 | // Checks is the per-check verdict, in the order the checks were |
| 266 | // required. |
| 267 | Checks []CheckVerdict |
| 268 | |
| 269 | // Invalidated is exactly the checks that need new evidence — the |
| 270 | // event's payload, and the whole of what a contributor has to redo. |
| 271 | Invalidated []string |
| 272 | } |
| 273 | |
| 274 | // Decide applies the commit rule of dcr-439b771 to a change. |
| 275 | func Decide(ch Change, s Snapshot) Verdict { |
| 276 | required := append([]Required(nil), s.Required...) |
| 277 | sort.Slice(required, func(i, j int) bool { return required[i].Name < required[j].Name }) |
| 278 | |
| 279 | // First part of the commit rule: the delta must apply cleanly. A |
| 280 | // conflict is new authorship, so every check is re-demanded rather |
| 281 | // than a subset — there is no partial retry from a tree nobody wrote. |
| 282 | if !s.Apply.Clean { |
| 283 | v := Verdict{ |
| 284 | Outcome: Stale, |
| 285 | StaleReason: "conflict", |
| 286 | Reason: "the delta does not apply cleanly to the tip; resolving it is new authorship, so every check needs new evidence", |
| 287 | } |
| 288 | if s.Apply.Conflict != "" { |
| 289 | v.Reason += " (" + s.Apply.Conflict + ")" |
| 290 | } |
| 291 | for _, r := range required { |
| 292 | v.Checks = append(v.Checks, CheckVerdict{Name: r.Name, Reason: "the delta conflicts, so the tree this would be evidence about does not exist yet"}) |
| 293 | v.Invalidated = append(v.Invalidated, r.Name) |
| 294 | } |
| 295 | return v |
| 296 | } |
| 297 | |
| 298 | unmoved := s.Apply.Tree != "" && s.Apply.Tree == ch.Attested |
| 299 | |
| 300 | v := Verdict{Outcome: Land} |
| 301 | for _, r := range required { |
| 302 | cv := judge(ch, s, r, unmoved) |
| 303 | v.Checks = append(v.Checks, cv) |
| 304 | switch { |
| 305 | case cv.Rule == "rejected": |
| 306 | // A forgery ends the judgement: the change is not behind, its |
| 307 | // evidence is not evidence. |
| 308 | return Verdict{ |
| 309 | Outcome: Reject, |
| 310 | Reason: fmt.Sprintf("%s: %s", r.Name, cv.Reason), |
| 311 | Checks: v.Checks, |
| 312 | } |
| 313 | case !cv.Transferred: |
| 314 | v.Invalidated = append(v.Invalidated, r.Name) |
| 315 | } |
| 316 | } |
| 317 | if len(v.Invalidated) > 0 { |
| 318 | v.Outcome = Stale |
| 319 | v.StaleReason = "evidence" |
| 320 | v.Reason = fmt.Sprintf("%d of %d required checks no longer carry evidence that transfers", len(v.Invalidated), len(required)) |
| 321 | return v |
| 322 | } |
| 323 | switch { |
| 324 | // Nothing owed is an outcome, not a count of zero. Two ways to owe |
| 325 | // nothing, and a reader needs to know which: the diff was covered and |
| 326 | // asked nothing of, or there was no diff to ask anything of. The |
| 327 | // second is a change already in the target's history — its base is |
| 328 | // its head, because the base is the merge base with the tip — and |
| 329 | // saying so is what keeps a resubmitted request from reading as a |
| 330 | // change that passed every check it had. A base nobody recorded is |
| 331 | // not that case; it is a caller that filled in neither. |
| 332 | case len(required) == 0 && ch.Base != "" && ch.Base == ch.Head: |
| 333 | v.Reason = "this change is already in the target's history, so its delta is empty and the policy requires nothing of it" |
| 334 | case len(required) == 0: |
| 335 | v.Reason = "the policy requires no check of the paths this change touches" |
| 336 | case len(required) == 1: |
| 337 | v.Reason = "the one required check carries evidence that transfers" |
| 338 | default: |
| 339 | v.Reason = fmt.Sprintf("all %d required checks carry evidence that transfers", len(required)) |
| 340 | } |
| 341 | return v |
| 342 | } |
| 343 | |
| 344 | // judge is the second part of the commit rule, for one check. |
| 345 | func judge(ch Change, s Snapshot, r Required, unmoved bool) CheckVerdict { |
| 346 | cv := CheckVerdict{Name: r.Name} |
| 347 | ev, ok := ch.Evidence[r.Name] |
| 348 | if !ok { |
| 349 | cv.Reason = "no attestation for this check accompanies the change" |
| 350 | return cv |
| 351 | } |
| 352 | cv.Evidence, cv.Signer = ev.TextDigest, ev.Signer |
| 353 | |
| 354 | switch ev.Admissible { |
| 355 | case Tampered: |
| 356 | cv.Rule = "rejected" |
| 357 | cv.Reason = "the attestation does not verify as the statement it claims to be" |
| 358 | if ev.Refusal != "" { |
| 359 | cv.Reason += ": " + ev.Refusal |
| 360 | } |
| 361 | return cv |
| 362 | case UnknownSigner: |
| 363 | cv.Reason = "signed by no signer the integrator holds, so its evidence does not transfer" |
| 364 | return cv |
| 365 | } |
| 366 | if ev.Result != "passed" { |
| 367 | cv.Reason = fmt.Sprintf("the attestation records the check as %s", ev.Result) |
| 368 | return cv |
| 369 | } |
| 370 | |
| 371 | // Which transfer rule applies is the policy's to say, never the |
| 372 | // attestation's. The policy names a check's runner, and the runner is |
| 373 | // what decides whether the evidence is content-addressed or a |
| 374 | // notarization. An attestation whose own kind contradicts the runner is |
| 375 | // refused rather than judged by the rule it nominated for itself: |
| 376 | // taking the claim's word for it would let an attester opt out of |
| 377 | // closure comparison by declaring a notarization. |
| 378 | want := KindOf(r.Runner) |
| 379 | if want == "" { |
| 380 | cv.Rule = "rejected" |
| 381 | cv.Reason = fmt.Sprintf("the policy runs this check with %q, and no transfer rule is defined for that runner", r.Runner) |
| 382 | return cv |
| 383 | } |
| 384 | if ev.Kind != want { |
| 385 | cv.Rule = "rejected" |
| 386 | cv.Reason = fmt.Sprintf("the policy requires %s and the attestation claims %s", article(want), article(ev.Kind)) |
| 387 | return cv |
| 388 | } |
| 389 | |
| 390 | if want == Pure { |
| 391 | // Content-addressed evidence does not age: a pure attestation |
| 392 | // carries no expiry, because time is not among its inputs. |
| 393 | if unmoved { |
| 394 | cv.Transferred, cv.Rule = true, "unmoved" |
| 395 | return cv |
| 396 | } |
| 397 | if why, bad := s.ClosureErrors[r.Name]; bad { |
| 398 | cv.Reason = "the input closure could not be recomputed on the landed tree: " + why |
| 399 | return cv |
| 400 | } |
| 401 | got, ok := s.Closures[r.Name] |
| 402 | if !ok { |
| 403 | cv.Reason = "the input closure was not recomputed on the landed tree" |
| 404 | return cv |
| 405 | } |
| 406 | if got.Inputs != ev.Inputs { |
| 407 | cv.Reason = fmt.Sprintf("its input closure changed: attested %s, landed tree %s", Short(ev.Inputs), Short(got.Inputs)) |
| 408 | return cv |
| 409 | } |
| 410 | // The claim a pure attestation makes is the whole of what it |
| 411 | // records, so a derivation that moved under an unchanged closure |
| 412 | // is evidence about a computation this tree no longer presents. |
| 413 | if got.Derivation != ev.Derivation { |
| 414 | cv.Reason = fmt.Sprintf("its derivation changed: attested %s, landed tree %s", Short(ev.Derivation), Short(got.Derivation)) |
| 415 | return cv |
| 416 | } |
| 417 | cv.Transferred, cv.Rule, cv.Inputs = true, "closure", got.Inputs |
| 418 | return cv |
| 419 | } |
| 420 | |
| 421 | // An effectful observation transfers while its scope is untouched and |
| 422 | // it is recent enough. Serializable semantics against mutable world |
| 423 | // state are unattainable in principle, so the honest semantics are |
| 424 | // bounded staleness. |
| 425 | for _, class := range r.Classes { |
| 426 | if s.Touched[class] { |
| 427 | cv.Reason = fmt.Sprintf("an intervening landed change touched the %s class, which is what this check reads", class) |
| 428 | return cv |
| 429 | } |
| 430 | } |
| 431 | if r.Validity == 0 { |
| 432 | cv.Reason = "the policy declares no validity window for this effectful check, so its observation is re-demanded" |
| 433 | return cv |
| 434 | } |
| 435 | if age := s.Now.Sub(ev.ObservedAt); age >= r.Validity { |
| 436 | cv.Reason = fmt.Sprintf("the observation is %s old and the declared window is %s", age.Round(time.Second), r.Validity) |
| 437 | return cv |
| 438 | } |
| 439 | cv.Transferred, cv.Rule = true, "untouched" |
| 440 | return cv |
| 441 | } |
| 442 | |
| 443 | // article reads a kind as a reader would say it. |
| 444 | func article(k Kind) string { |
| 445 | if k == Effectful { |
| 446 | return "an effectful check" |
| 447 | } |
| 448 | if k == "" { |
| 449 | return "no kind at all" |
| 450 | } |
| 451 | return "a " + string(k) + " check" |
| 452 | } |
| 453 | |
| 454 | // Short abbreviates a digest for a reader. Exported because a caller |
| 455 | // reporting the same verdict should abbreviate the same way. |
| 456 | func Short(digest string) string { |
| 457 | if len(digest) > 12 { |
| 458 | return digest[:12] |
| 459 | } |
| 460 | return digest |
| 461 | } |