| 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 a change owes (dcr-f41f718); that deriver only |
| 16 | // reports. The Phase 3 integrator asks the deriver the same question and |
| 17 | // acts on the answer, which is the enforcement point (design/integration.md). |
| 18 | // |
| 19 | // Like the host and event schemas, this file is deliberately not Nix. A |
| 20 | // policy names checks; it never names derivations, systems, or store |
| 21 | // paths, so it travels between machines unchanged. |
| 22 | // |
| 23 | // Compose an instance floor with a project policy and validate both: |
| 24 | // cue vet -c schema/verification.cue \ |
| 25 | // examples/policy/instance/*.cue examples/policy/project/*.cue |
| 26 | package verification |
| 27 | |
| 28 | // #Policy is one layer's contribution to a verification policy: the checks |
| 29 | // it can require, the path classes that require them, and what a path no |
| 30 | // class covers costs. Both layers are the same shape, because the |
| 31 | // composition is unification and unification needs both sides to agree on |
| 32 | // structure. |
| 33 | #Policy: { |
| 34 | // checks maps a check's name to how that check is run. Classes |
| 35 | // require checks by name; a name that is not a key here fails vet |
| 36 | // (see the cross-check below), because a required check nobody can |
| 37 | // run is the one failure this document must never have. Either layer |
| 38 | // may contribute entries; two layers describing the same check must |
| 39 | // describe it identically, or unification conflicts. |
| 40 | checks: [#Name]: #Check |
| 41 | |
| 42 | // The path classes, keyed by class name. Matching is a set operation, |
| 43 | // never first-match: a change's required checks are the union of |
| 44 | // `requires` over every class that at least one changed path matches. |
| 45 | // Classes may overlap on purpose — a knowledge node is both knowledge |
| 46 | // and prose — and overlapping classes compose to the maximum of what |
| 47 | // they ask for. That union is the whole of the "mixed commits take the |
| 48 | // max" rule (ida-1ec03b1): there is no separate mixed case to get |
| 49 | // wrong. |
| 50 | // |
| 51 | // The name is the key rather than a field, because classes compose |
| 52 | // across the two layers and CUE unifies lists positionally — two lists |
| 53 | // of different lengths simply conflict. Keying by name makes the |
| 54 | // composition field-wise instead: when both layers name the same |
| 55 | // class, it is one class, and its `paths` and `requires` are the union |
| 56 | // of what each layer contributed. |
| 57 | classes: [#Name]: #Class |
| 58 | |
| 59 | // The checks required when a changed path matches no class at all. |
| 60 | // A path outside every class means the policy is behind the tree, so |
| 61 | // the safe answer is the strictest set the project has rather than |
| 62 | // the empty one. An empty set is allowed, and is a deliberate |
| 63 | // statement that uncovered paths are unconstrained. |
| 64 | unclassified: #Requires |
| 65 | } |
| 66 | |
| 67 | // #Class is one path class and what touching it requires. |
| 68 | #Class: { |
| 69 | // The paths this class covers, as a set of globs. Patterns are matched |
| 70 | // against each changed path of the diff, relative to the repository |
| 71 | // root: `*` matches within one path segment, `**` matches any number |
| 72 | // of segments including none. Every side of the diff counts — an |
| 73 | // added, modified, or deleted path, and both the old and the new path |
| 74 | // of a rename — because a check's scope is what the tree looked like |
| 75 | // as much as what it looks like now. |
| 76 | // |
| 77 | // A pattern's value says whether the class covers it, and it carries |
| 78 | // the same mandatory bit as #Requires: a pattern the instance layer |
| 79 | // pinned to `true` cannot be dropped by a project. Without that, a |
| 80 | // project could narrow a floor class's coverage down to a path it |
| 81 | // never touches and escape the floor everywhere else. |
| 82 | paths: #Paths |
| 83 | |
| 84 | // The checks a change touching this class must carry. Every name must |
| 85 | // be a key of the composed policy's `checks`. |
| 86 | requires: #Requires |
| 87 | } |
| 88 | |
| 89 | // #Requires is a set of check names written as a struct, with the value |
| 90 | // saying whether the check is required. The value's *form* is the one bit |
| 91 | // that separates a mandatory requirement from a suggested one, expressed |
| 92 | // in CUE's own semantics rather than in a flag this schema would have to |
| 93 | // interpret: |
| 94 | // |
| 95 | // - `true` — concrete, and unification can only narrow, so a project |
| 96 | // supplying `false` conflicts and `cue vet` fails naming the field. |
| 97 | // This is what the instance floor writes for a check that is not |
| 98 | // negotiable. |
| 99 | // - `bool | *true` — a disjunction with a marked default, so it reads as |
| 100 | // `true` when nobody says otherwise and unifies cleanly with a |
| 101 | // project's `false`. This is what a project-type template writes for a |
| 102 | // check that is a sensible starting point rather than a rule. |
| 103 | // |
| 104 | // A project may always ADD names here, because the struct is open below |
| 105 | // the keys the instance layer pinned; it can never remove one the floor |
| 106 | // made concrete. |
| 107 | #Requires: [#Name]: bool |
| 108 | |
| 109 | // #Paths is the same set-as-struct encoding, keyed by glob rather than by |
| 110 | // check name, and read the same way: `true` pins the coverage, `bool | |
| 111 | // *true` offers it. |
| 112 | #Paths: [#Pattern]: bool |
| 113 | |
| 114 | // #Check is how one named check is run. In the reference implementation a |
| 115 | // check is a flake check: `nix build .#checks.<system>.<attribute>`, where |
| 116 | // the system is the runner's own and never the policy's — a policy that |
| 117 | // named a system would stop travelling. |
| 118 | #Check: { |
| 119 | // The runner kind, named the way schema/attestation.cue's #CheckRun |
| 120 | // names it, so a policy and the statement satisfying it agree on what |
| 121 | // a check is. "nix" builds a flake check attribute and claims purity |
| 122 | // strongly; "command" runs a command line and claims nothing beyond |
| 123 | // having run it, which is the effectful class of |
| 124 | // design/verification.md. |
| 125 | runner: "nix" | "command" |
| 126 | |
| 127 | // The flake check attribute, for the nix runner. |
| 128 | attribute?: #Name |
| 129 | |
| 130 | // The command line, for the command runner. One line, because a |
| 131 | // statement about it is written as lines and the form has no escapes. |
| 132 | command?: =~"^[^\\x00-\\x1f\\x7f]+$" |
| 133 | |
| 134 | if runner == "nix" { |
| 135 | attribute: #Name |
| 136 | command?: _|_ |
| 137 | // A pure check's evidence does not age, because time is not among |
| 138 | // its inputs. A window declared over one would be a window nothing |
| 139 | // reads, so it is refused rather than ignored — and the refusal is |
| 140 | // a sentinel rather than bottom, so `cue vet` names the field. |
| 141 | validity?: "INVALID: a pure check's evidence does not age; a validity window is for an effectful check" |
| 142 | } |
| 143 | if runner == "command" { |
| 144 | command: =~"^[^\\x00-\\x1f\\x7f]+$" |
| 145 | attribute?: _|_ |
| 146 | } |
| 147 | |
| 148 | // Δ: how long an observation of this check stays good once made |
| 149 | // (dcr-439b771). It exists for effectful checks alone. A pure check's |
| 150 | // evidence is content-addressed and does not age, because time is not |
| 151 | // among its inputs; an effectful check reads mutable world state, |
| 152 | // which has no digest, so the honest semantics are bounded staleness |
| 153 | // — an observation transfers while nothing relevant has changed under |
| 154 | // it and it is recent enough. |
| 155 | // |
| 156 | // Optional, and its absence is not a default. Absent a declared |
| 157 | // window the integrator re-demands the check: a window nobody wrote |
| 158 | // down is not a window a tool may invent. Like every other field |
| 159 | // here it composes across the two layers, so a group can floor a |
| 160 | // window and a project can only narrow it. |
| 161 | validity?: #Duration |
| 162 | } |
| 163 | |
| 164 | // A validity window: a whole number of seconds, minutes, hours or days. |
| 165 | // Written as text rather than as a number so that the unit is in the |
| 166 | // document a person reads, and bounded to units a policy has a use for — |
| 167 | // a window in milliseconds is not a policy, and one in months is not a |
| 168 | // window. |
| 169 | #Duration: =~"^[1-9][0-9]*(s|m|h|d)$" |
| 170 | |
| 171 | // #Templates is the instance layer's set of project types. A project |
| 172 | // selects one by name and then narrows it, so a project that is an |
| 173 | // ordinary instance of its type needs almost no configuration of its own. |
| 174 | #Templates: [#Name]: #Policy |
| 175 | |
| 176 | // Names are lowercase, dash-separated, and short. They are quoted in |
| 177 | // integrator output and carried in attestation payloads, so they are held |
| 178 | // to the same shape as project names in schema/valley.cue. |
| 179 | #Name: =~"^[a-z0-9][a-z0-9-]*$" |
| 180 | |
| 181 | // A path glob, relative to the repository root. A leading slash and a `..` |
| 182 | // segment are both rejected: a pattern that reaches outside the tree is |
| 183 | // describing something a diff cannot contain, and silently matching |
| 184 | // nothing is the expensive way to find that out. |
| 185 | #Pattern: =~"^[^/]" & !~"(^|/)\\.\\.(/|$)" |
| 186 | |
| 187 | // ---------------------------------------------------------------------- |
| 188 | // The composition. |
| 189 | // |
| 190 | // The two layers meet here. The schema cannot tell which file a field came |
| 191 | // from, so the tool that composes a policy is what makes the floor a |
| 192 | // floor: `floor` and `templates` must be read from the instance |
| 193 | // repository's integrated tip, never from the project's tree. A project |
| 194 | // that vendored a copy of the floor and edited the copy would unify |
| 195 | // perfectly well — unification forces nothing on its own. What forces is |
| 196 | // provenance: changing the floor is a change landing in the instance |
| 197 | // repository, under that repository's own policy. |
| 198 | // |
| 199 | // The two halves resolve at different times, on purpose. The floor |
| 200 | // resolves at head, because a floor a project could pin is not a floor. |
| 201 | // Templates may be pinned, because they are defaults and a stale default |
| 202 | // is harmless. |
| 203 | |
| 204 | // The instance layer, read from the instance repository's integrated tip. |
| 205 | floor: #Policy |
| 206 | templates: #Templates |
| 207 | |
| 208 | // The project type that adds nothing, so that a project which declares no |
| 209 | // type still composes. It is defined here rather than left to each |
| 210 | // instance because it is a property of the composition, not of any group. |
| 211 | templates: none: {} |
| 212 | |
| 213 | // The project layer, read from the project's own tree. Neither field has |
| 214 | // to be written: `type` defaults to the project type that adds nothing, |
| 215 | // and an unwritten `project` is three empty sets. A project with no policy |
| 216 | // document at all therefore composes — to the floor exactly, which is the |
| 217 | // point of having one. |
| 218 | type: #Name | *"none" |
| 219 | project: #Policy |
| 220 | |
| 221 | // The effective policy for this project: what the integrator derives a |
| 222 | // change's required checks from. Naming an unknown project type is an |
| 223 | // evaluation error rather than a silent fallback. The `let` exists so the |
| 224 | // cross-check below can read the composition without `policy` referring to |
| 225 | // itself; every error still surfaces under `policy`, where a reader is |
| 226 | // looking. |
| 227 | let composed = floor & templates[type] & project |
| 228 | policy: composed |
| 229 | |
| 230 | // Every check a class requires, and every check `unclassified` names, must |
| 231 | // exist in the composed catalogue. A name that does not lands in `checks` |
| 232 | // as an incomplete #Check, so `cue vet -c` fails with an error naming the |
| 233 | // missing check. Without this, a typo is not a vet error — it is a check |
| 234 | // that silently stops being required. The cross-check runs against the |
| 235 | // composition rather than either layer, because a floor may legitimately |
| 236 | // require a check whose definition a template supplies. |
| 237 | policy: { |
| 238 | for _, c in composed.classes for n, _ in c.requires { |
| 239 | checks: (n): _ |
| 240 | } |
| 241 | for n, _ in composed.unclassified { |
| 242 | checks: (n): _ |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // A file's top level cannot be closed (embedding a definition would open |
| 247 | // it instead — embedding lifts closedness), so reject stray top-level |
| 248 | // fields explicitly: anything but the composition's fields — a `class:` |
| 249 | // typo, a host-side concern like `enable:` — conflicts with this sentinel |
| 250 | // and fails vet with an error naming the field. |
| 251 | [!="floor" & !="templates" & !="type" & !="project" & !="policy"]: "INVALID: unknown top-level field; only \"floor\", \"templates\", \"type\", \"project\" and \"policy\" are allowed" |