| 1 | // The verification policy of a valley project: which checks a change must |
| 2 | // carry, derived from the paths its diff touches. Policy is data, so it is |
| 3 | // a CUE document like the host config and the event vocabulary |
| 4 | // (ida-1ec03b1). |
| 5 | // |
| 6 | // A policy is composed from two documents, never one. The instance layer — |
| 7 | // the group's floor and its project-type templates — is carried in the |
| 8 | // instance repository. The project layer is carried in the project's own |
| 9 | // tree. The effective policy is the unification of the two, and because |
| 10 | // unification can only narrow, the instance layer can state things a |
| 11 | // project cannot take back. That is the whole of the mandatory-versus- |
| 12 | // overridable distinction; #Requires spells it out. |
| 13 | // |
| 14 | // Two tools read this file. `valley checks` composes the two layers with it |
| 15 | // and prints the checks the policy requires of a change, and the change's |
| 16 | // carry mode (dcr-f41f718); that deriver only reports. The Phase 3 |
| 17 | // integrator asks the deriver the same question and acts on the answer, |
| 18 | // which is the enforcement point (archive/design/integration.md). |
| 19 | // |
| 20 | // Like the host and event schemas, this file is deliberately not Nix. A |
| 21 | // policy names checks; it never names derivations, systems, or store |
| 22 | // paths, so it travels between machines unchanged. |
| 23 | // |
| 24 | // Compose an instance floor with a project policy and validate both: |
| 25 | // cue vet -c schema/verification.cue \ |
| 26 | // examples/policy/instance/*.cue examples/policy/project/*.cue |
| 27 | package verification |
| 28 | |
| 29 | import "list" |
| 30 | |
| 31 | // #Policy is one layer's contribution to a verification policy: the checks |
| 32 | // it can require, the path classes that require them, what a path no class |
| 33 | // covers costs, the restacker's cap, and whether an approval must be signed |
| 34 | // with a hardware key. Both layers are the same shape, because the |
| 35 | // composition is unification and unification needs both sides to agree on |
| 36 | // structure. |
| 37 | #Policy: { |
| 38 | // checks maps a check's name to how that check is run. Classes |
| 39 | // require checks by name; a name that is not a key here fails vet |
| 40 | // (see the cross-check below), because a required check nobody can |
| 41 | // run is the one failure this document must never have. Either layer |
| 42 | // may contribute entries; two layers describing the same check must |
| 43 | // describe it identically, or unification conflicts. |
| 44 | checks: [#Name]: #Check |
| 45 | |
| 46 | // The path classes, keyed by class name. Matching is a set operation, |
| 47 | // never first-match: a change's required checks are the union of |
| 48 | // `requires` over every class that at least one changed path matches. |
| 49 | // Classes may overlap on purpose — a knowledge node is both knowledge |
| 50 | // and prose — and overlapping classes compose to the maximum of what |
| 51 | // they ask for. That union is the whole of the "mixed commits take the |
| 52 | // max" rule (ida-1ec03b1): there is no separate mixed case to get |
| 53 | // wrong. |
| 54 | // |
| 55 | // The name is the key rather than a field, because classes compose |
| 56 | // across the two layers and CUE unifies lists positionally — two lists |
| 57 | // of different lengths simply conflict. Keying by name makes the |
| 58 | // composition field-wise instead: when both layers name the same |
| 59 | // class, it is one class, and its `paths` and `requires` are the union |
| 60 | // of what each layer contributed. |
| 61 | classes: [#Name]: #Class |
| 62 | |
| 63 | // The checks required when a changed path matches no class at all. |
| 64 | // A path outside every class means the policy is behind the tree, so |
| 65 | // the safe answer is the strictest set the project has rather than |
| 66 | // the empty one. An empty set is allowed, and is a deliberate |
| 67 | // statement that uncovered paths are unconstrained. |
| 68 | unclassified: #Requires |
| 69 | |
| 70 | // The restacker rebases a ready change when its target moves, and adds |
| 71 | // each rebased result to the change as a new version. Those are the |
| 72 | // change's automatic versions. maxAutomaticVersions caps how many the |
| 73 | // restacker adds to one change over the change's life. The cap is |
| 74 | // policy so that it is composed, and hashed into the policy digest, |
| 75 | // with everything else here. A layer that sets the cap writes a plain |
| 76 | // number. A second default would leave the cap with no default, and vet |
| 77 | // would fail. |
| 78 | restack: {maxAutomaticVersions: int & >=1 | *64} |
| 79 | |
| 80 | // approval.hardware says whether an approval of a change must be signed |
| 81 | // with a hardware key: one whose enrollment in the identity registry |
| 82 | // proved that the key is held on a device and that every signature |
| 83 | // takes a touch. `true` requires one. Left out, any enrolled approval |
| 84 | // key may sign. |
| 85 | // |
| 86 | // It chooses which keys an approval may be signed with. It never |
| 87 | // decides whether a change needs an approval. |
| 88 | // |
| 89 | // The layers compose the way #Requires does, and the value's form is |
| 90 | // the mandatory bit: |
| 91 | // |
| 92 | // - The floor writes a concrete `true`, which no project can drop: a |
| 93 | // project writing `false` conflicts, and `cue vet` names the field. |
| 94 | // - A template writes `bool | *true`, a default a project may turn off. |
| 95 | // - A project may always write `true`. |
| 96 | // |
| 97 | // A floor's concrete `false` would stop a project from writing `true`, |
| 98 | // so a floor writes `true` or leaves the field out. |
| 99 | // |
| 100 | // Both levels are optional, so a policy that leaves the requirement out |
| 101 | // exports no `approval` field, and the field adds nothing to its digest. |
| 102 | approval?: {hardware?: bool} |
| 103 | } |
| 104 | |
| 105 | // #Class is one path class and what touching it requires. |
| 106 | #Class: { |
| 107 | // The paths this class covers, as a set of globs. Patterns are matched |
| 108 | // against each changed path of the diff, relative to the repository |
| 109 | // root: `*` matches within one path segment, `**` matches any number |
| 110 | // of segments including none. Every side of the diff counts — an |
| 111 | // added, modified, or deleted path, and both the old and the new path |
| 112 | // of a rename — because a check's scope is what the tree looked like |
| 113 | // as much as what it looks like now. |
| 114 | // |
| 115 | // A pattern's value says whether the class covers it, and it carries |
| 116 | // the same mandatory bit as #Requires: a pattern the instance layer |
| 117 | // pinned to `true` cannot be dropped by a project. Without that, a |
| 118 | // project could narrow a floor class's coverage down to a path it |
| 119 | // never touches and escape the floor everywhere else. |
| 120 | paths: #Paths |
| 121 | |
| 122 | // The checks a change touching this class must carry. Every name must |
| 123 | // be a key of the composed policy's `checks`. |
| 124 | requires: #Requires |
| 125 | |
| 126 | // The checks that follow this class: they run on main after a change |
| 127 | // touching the class lands, and never gate the change. A check any |
| 128 | // matched class requires stays required, so a follow takes nothing |
| 129 | // from a floor's requirement. A project moves a check it may turn off |
| 130 | // from gating to following by writing `false` for it under `requires` |
| 131 | // and `true` under `follows`. Every name must be a key of the composed |
| 132 | // policy's `checks`. Left out, the class exports no `follows` field, |
| 133 | // and the field adds nothing to the policy digest. |
| 134 | follows?: #Requires |
| 135 | |
| 136 | // How far an approval of a change touching this class carries when the |
| 137 | // change is rebased onto a target that has moved. #Carry spells out the |
| 138 | // modes. |
| 139 | carry: #Carry |
| 140 | } |
| 141 | |
| 142 | // #Carry is the set of carry modes a class asks for, in the same |
| 143 | // set-as-struct encoding as #Requires. A class's resolved mode is the |
| 144 | // strictest mode whose key is `true`, and `same-paths` when no key is |
| 145 | // (#Resolve). |
| 146 | // |
| 147 | // An approval carries across a rebase only when the rebased change has |
| 148 | // exactly the files the approval saw. The mode adds a condition on the |
| 149 | // landings since the approval. From the strictest: |
| 150 | // |
| 151 | // - `exact` — there were none: the target has not moved. |
| 152 | // - `same-class` — none of them touched a path in any class the change |
| 153 | // touches. |
| 154 | // - `same-paths` — no condition. It is not a key: it is what a class with |
| 155 | // no key `true` asks for. |
| 156 | // |
| 157 | // The layers compose the way #Requires does, and the value's form is the |
| 158 | // mandatory bit: |
| 159 | // |
| 160 | // - The floor writes a concrete `true`, which no project can drop: a |
| 161 | // project writing `false` conflicts, and `cue vet` names the field. |
| 162 | // - A template writes `bool | *true`, a default a project may turn off. |
| 163 | // - A project may always add a stricter key. |
| 164 | // |
| 165 | // The modes are keys rather than one string field because the three layers |
| 166 | // unify. Two different strings conflict, so a string field could not say |
| 167 | // "the strictest of the layers". |
| 168 | #Carry: [#Mode]: bool |
| 169 | |
| 170 | // #Mode is a carry mode a class can ask for: either mode stricter than the |
| 171 | // default `same-paths`. |
| 172 | #Mode: "same-class" | "exact" |
| 173 | |
| 174 | // #Resolve reads a carry set as the one mode it means. |
| 175 | #Resolve: { |
| 176 | carry: #Carry |
| 177 | let on = [for m, v in carry if v {m}] |
| 178 | mode: [ |
| 179 | if list.Contains(on, "exact") {"exact"}, |
| 180 | if list.Contains(on, "same-class") {"same-class"}, |
| 181 | "same-paths", |
| 182 | ][0] |
| 183 | } |
| 184 | |
| 185 | // #Requires is a set of check names written as a struct, with the value |
| 186 | // saying whether the check is required. The value's *form* is the one bit |
| 187 | // that separates a mandatory requirement from a suggested one, expressed |
| 188 | // in CUE's own semantics rather than in a flag this schema would have to |
| 189 | // interpret: |
| 190 | // |
| 191 | // - `true` — concrete, and unification can only narrow, so a project |
| 192 | // supplying `false` conflicts and `cue vet` fails naming the field. |
| 193 | // This is what the instance floor writes for a check that is not |
| 194 | // negotiable. |
| 195 | // - `bool | *true` — a disjunction with a marked default, so it reads as |
| 196 | // `true` when nobody says otherwise and unifies cleanly with a |
| 197 | // project's `false`. This is what a project-type template writes for a |
| 198 | // check that is a sensible starting point rather than a rule. |
| 199 | // |
| 200 | // A project may always ADD names here, because the struct is open below |
| 201 | // the keys the instance layer pinned; it can never remove one the floor |
| 202 | // made concrete. |
| 203 | #Requires: [#Name]: bool |
| 204 | |
| 205 | // #Paths is the same set-as-struct encoding, keyed by glob rather than by |
| 206 | // check name, and read the same way: `true` pins the coverage, `bool | |
| 207 | // *true` offers it. |
| 208 | #Paths: [#Pattern]: bool |
| 209 | |
| 210 | // #Check is how one named check is run. In the reference implementation a |
| 211 | // check is a flake check: `nix build .#checks.<system>.<attribute>`, where |
| 212 | // the system is the runner's own and never the policy's — a policy that |
| 213 | // named a system would stop travelling. |
| 214 | #Check: { |
| 215 | // The runner kind, named the way schema/attestation.cue's #CheckRun |
| 216 | // names it, so a policy and the statement satisfying it agree on what |
| 217 | // a check is. "nix" builds a flake check attribute and claims purity |
| 218 | // strongly; "command" runs a command line and claims nothing beyond |
| 219 | // having run it, which is the effectful class of |
| 220 | // archive/design/verification.md. |
| 221 | runner: "nix" | "command" |
| 222 | |
| 223 | // The flake check attribute, for the nix runner. |
| 224 | attribute?: #Name |
| 225 | |
| 226 | // The command line, for the command runner. One line, because a |
| 227 | // statement about it is written as lines and the form has no escapes. |
| 228 | command?: =~"^[^\\x00-\\x1f\\x7f]+$" |
| 229 | |
| 230 | if runner == "nix" { |
| 231 | attribute: #Name |
| 232 | command?: _|_ |
| 233 | // A pure check's evidence does not age, because time is not among |
| 234 | // its inputs. A window declared over one would be a window nothing |
| 235 | // reads, so it is refused rather than ignored — and the refusal is |
| 236 | // a sentinel rather than bottom, so `cue vet` names the field. |
| 237 | validity?: "INVALID: a pure check's evidence does not age; a validity window is for an effectful check" |
| 238 | } |
| 239 | if runner == "command" { |
| 240 | command: =~"^[^\\x00-\\x1f\\x7f]+$" |
| 241 | attribute?: _|_ |
| 242 | } |
| 243 | |
| 244 | // Δ: how long an observation of this check stays good once made |
| 245 | // (dcr-439b771). It exists for effectful checks alone. A pure check's |
| 246 | // evidence is content-addressed and does not age, because time is not |
| 247 | // among its inputs; an effectful check reads mutable world state, |
| 248 | // which has no digest, so the honest semantics are bounded staleness |
| 249 | // — an observation transfers while nothing relevant has changed under |
| 250 | // it and it is recent enough. |
| 251 | // |
| 252 | // Optional, and its absence is not a default. Absent a declared |
| 253 | // window the integrator re-demands the check: a window nobody wrote |
| 254 | // down is not a window a tool may invent. Like every other field |
| 255 | // here it composes across the two layers, so a group can floor a |
| 256 | // window and a project can only narrow it. |
| 257 | validity?: #Duration |
| 258 | |
| 259 | // What running the check costs its runner. A "quick" check is cheap. |
| 260 | // A "slow" check takes minutes, or needs special hardware such as |
| 261 | // virtualisation support. |
| 262 | // |
| 263 | // The cost is a declared property. `valley checks` groups the required |
| 264 | // checks by it and can filter them by it. The integrator does not |
| 265 | // read it: approving runs every required check, and the integrator |
| 266 | // requires evidence for every required check, whatever its cost. |
| 267 | cost: *"quick" | "slow" |
| 268 | } |
| 269 | |
| 270 | // A validity window: a whole number of seconds, minutes, hours or days. |
| 271 | // Written as text rather than as a number so that the unit is in the |
| 272 | // document a person reads, and bounded to units a policy has a use for — |
| 273 | // a window in milliseconds is not a policy, and one in months is not a |
| 274 | // window. |
| 275 | #Duration: =~"^[1-9][0-9]*(s|m|h|d)$" |
| 276 | |
| 277 | // #Templates is the instance layer's set of project types. A project |
| 278 | // selects one by name and then narrows it, so a project that is an |
| 279 | // ordinary instance of its type needs almost no configuration of its own. |
| 280 | #Templates: [#Name]: #Policy |
| 281 | |
| 282 | // Names are lowercase, dash-separated, and short. They are quoted in |
| 283 | // integrator output and carried in attestation payloads, so they are held |
| 284 | // to the same shape as project names in schema/valley.cue. The shape also |
| 285 | // keeps every control character out of a class or check name, so each |
| 286 | // prints whole on a line of its own. |
| 287 | #Name: =~"^[a-z0-9][a-z0-9-]*$" |
| 288 | |
| 289 | // A path glob, relative to the repository root. A leading slash and a `..` |
| 290 | // segment are both rejected: a pattern that reaches outside the tree is |
| 291 | // describing something a diff cannot contain, and silently matching |
| 292 | // nothing is the expensive way to find that out. |
| 293 | // |
| 294 | // A control character is rejected too. Git allows one in a path, but a |
| 295 | // policy has no use for a pattern that matches one. |
| 296 | #Pattern: =~"^[^/]" & !~"(^|/)\\.\\.(/|$)" & !~"[\\x00-\\x1f\\x7f]" |
| 297 | |
| 298 | // ---------------------------------------------------------------------- |
| 299 | // The composition. |
| 300 | // |
| 301 | // The two layers meet here. The schema cannot tell which file a field came |
| 302 | // from, so the tool that composes a policy is what makes the floor a |
| 303 | // floor: `floor` and `templates` must be read from the instance |
| 304 | // repository's integrated tip, never from the project's tree. A project |
| 305 | // that vendored a copy of the floor and edited the copy would unify |
| 306 | // perfectly well — unification forces nothing on its own. What forces is |
| 307 | // provenance: changing the floor is a change landing in the instance |
| 308 | // repository, under that repository's own policy. |
| 309 | // |
| 310 | // The two halves resolve at different times, on purpose. The floor |
| 311 | // resolves at head, because a floor a project could pin is not a floor. |
| 312 | // Templates may be pinned, because they are defaults and a stale default |
| 313 | // is harmless. |
| 314 | |
| 315 | // The instance layer, read from the instance repository's integrated tip. |
| 316 | floor: #Policy |
| 317 | templates: #Templates |
| 318 | |
| 319 | // The project type that adds nothing, so that a project which declares no |
| 320 | // type still composes. It is defined here rather than left to each |
| 321 | // instance because it is a property of the composition, not of any group. |
| 322 | templates: none: {} |
| 323 | |
| 324 | // The project layer, read from the project's own tree. Neither field has |
| 325 | // to be written: `type` defaults to the project type that adds nothing, |
| 326 | // and an unwritten `project` is three empty sets. A project with no policy |
| 327 | // document at all therefore composes — to the floor exactly, which is the |
| 328 | // point of having one. |
| 329 | type: #Name | *"none" |
| 330 | project: #Policy |
| 331 | |
| 332 | // The effective policy for this project: what the integrator derives a |
| 333 | // change's required checks from. Naming an unknown project type is an |
| 334 | // evaluation error rather than a silent fallback. The `let` exists so the |
| 335 | // cross-check below can read the composition without `policy` referring to |
| 336 | // itself; every error still surfaces under `policy`, where a reader is |
| 337 | // looking. |
| 338 | let composed = floor & templates[type] & project |
| 339 | policy: composed |
| 340 | |
| 341 | // Every check a class requires or follows, and every check `unclassified` |
| 342 | // names, must exist in the composed catalogue. A name that does not lands in `checks` |
| 343 | // as an incomplete #Check, so `cue vet -c` fails with an error naming the |
| 344 | // missing check. Without this, a typo is not a vet error — it is a check |
| 345 | // that silently stops being required. The cross-check runs against the |
| 346 | // composition rather than either layer, because a floor may legitimately |
| 347 | // require a check whose definition a template supplies. |
| 348 | policy: { |
| 349 | for _, c in composed.classes for n, _ in c.requires { |
| 350 | checks: (n): _ |
| 351 | } |
| 352 | for _, c in composed.classes if c.follows != _|_ for n, _ in c.follows { |
| 353 | checks: (n): _ |
| 354 | } |
| 355 | for n, _ in composed.unclassified { |
| 356 | checks: (n): _ |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | // The resolved carry mode of every class of the composed policy, keyed by |
| 361 | // class name. A change's carry mode is the strictest of these among the |
| 362 | // classes its paths match, and an unclassified path is `same-paths`. |
| 363 | // `valley checks` reads this field and reports that mode. The field is |
| 364 | // hidden because the sentinel below rejects every other top-level field. |
| 365 | // |
| 366 | // It reads `policy` rather than `composed`, because a document may refine |
| 367 | // `policy` directly. A key written there is in `policy` and not in |
| 368 | // `composed`, and the deriver matches paths and reads requirements from |
| 369 | // `policy`. Reading `composed` here would miss a mode added that way, or a |
| 370 | // whole class. |
| 371 | _mode: {for n, c in policy.classes {(n): (#Resolve & {carry: c.carry}).mode}} |
| 372 | |
| 373 | // A file's top level cannot be closed (embedding a definition would open |
| 374 | // it instead — embedding lifts closedness), so reject stray top-level |
| 375 | // fields explicitly: anything but the composition's fields — a `class:` |
| 376 | // typo, a host-side concern like `enable:` — conflicts with this sentinel |
| 377 | // and fails vet with an error naming the field. |
| 378 | [!="floor" & !="templates" & !="type" & !="project" & !="policy"]: "INVALID: unknown top-level field; only \"floor\", \"templates\", \"type\", \"project\" and \"policy\" are allowed" |