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