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