gunk-dev/the-valley

verification.cue

sourced9c8073fd613main's published tip history
18.4 KiB
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
27package verification
28
29import "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 // approval.required records whether policy requires a change to carry
101 // a signed approval to land: `true` records the requirement, and left
102 // out, the policy records none. The composed policy reader reads it, and
103 // no landing decision does.
104 //
105 // Its layers compose the way approval.hardware's do: the floor's
106 // concrete `true` cannot be dropped, a template's `bool | *true` is a
107 // default a project may turn off, and a project may always write
108 // `true`.
109 //
110 // Every field of `approval` is optional, so a policy that writes none
111 // exports no `approval` field, and the fields add nothing to its digest.
112 // A field written out is in the export, `false` included: a policy
113 // writing `required: false` digests differently from one leaving it
114 // out, though neither records a requirement.
115 approval?: {
116 hardware?: bool
117 required?: bool
118 }
119}
120
121// #Class is one path class and what touching it requires.
122#Class: {
123 // The paths this class covers, as a set of globs. Patterns are matched
124 // against each changed path of the diff, relative to the repository
125 // root: `*` matches within one path segment, `**` matches any number
126 // of segments including none. Every side of the diff counts — an
127 // added, modified, or deleted path, and both the old and the new path
128 // of a rename — because a check's scope is what the tree looked like
129 // as much as what it looks like now.
130 //
131 // A pattern's value says whether the class covers it, and it carries
132 // the same mandatory bit as #Requires: a pattern the instance layer
133 // pinned to `true` cannot be dropped by a project. Without that, a
134 // project could narrow a floor class's coverage down to a path it
135 // never touches and escape the floor everywhere else.
136 paths: #Paths
137
138 // The checks a change touching this class must carry. Every name must
139 // be a key of the composed policy's `checks`.
140 requires: #Requires
141
142 // The checks that follow this class: they run on main after a change
143 // touching the class lands, and never gate the change. A check any
144 // matched class requires stays required, so a follow takes nothing
145 // from a floor's requirement. A project moves a check it may turn off
146 // from gating to following by writing `false` for it under `requires`
147 // and `true` under `follows`. Every name must be a key of the composed
148 // policy's `checks`. Left out, the class exports no `follows` field,
149 // and the field adds nothing to the policy digest.
150 follows?: #Requires
151
152 // How far an approval of a change touching this class carries when the
153 // change is rebased onto a target that has moved. #Carry spells out the
154 // modes.
155 carry: #Carry
156}
157
158// #Carry is the set of carry modes a class asks for, in the same
159// set-as-struct encoding as #Requires. A class's resolved mode is the
160// strictest mode whose key is `true`, and `same-paths` when no key is
161// (#Resolve).
162//
163// An approval carries across a rebase only when the rebased change has
164// exactly the files the approval saw. The mode adds a condition on the
165// landings since the approval. From the strictest:
166//
167// - `exact` — there were none: the target has not moved.
168// - `same-class` — none of them touched a path in any class the change
169// touches.
170// - `same-paths` — no condition. It is not a key: it is what a class with
171// no key `true` asks for.
172//
173// The layers compose the way #Requires does, and the value's form is the
174// mandatory bit:
175//
176// - The floor writes a concrete `true`, which no project can drop: a
177// project writing `false` conflicts, and `cue vet` names the field.
178// - A template writes `bool | *true`, a default a project may turn off.
179// - A project may always add a stricter key.
180//
181// The modes are keys rather than one string field because the three layers
182// unify. Two different strings conflict, so a string field could not say
183// "the strictest of the layers".
184#Carry: [#Mode]: bool
185
186// #Mode is a carry mode a class can ask for: either mode stricter than the
187// default `same-paths`.
188#Mode: "same-class" | "exact"
189
190// #Resolve reads a carry set as the one mode it means.
191#Resolve: {
192 carry: #Carry
193 let on = [for m, v in carry if v {m}]
194 mode: [
195 if list.Contains(on, "exact") {"exact"},
196 if list.Contains(on, "same-class") {"same-class"},
197 "same-paths",
198 ][0]
199}
200
201// #Requires is a set of check names written as a struct, with the value
202// saying whether the check is required. The value's *form* is the one bit
203// that separates a mandatory requirement from a suggested one, expressed
204// in CUE's own semantics rather than in a flag this schema would have to
205// interpret:
206//
207// - `true` — concrete, and unification can only narrow, so a project
208// supplying `false` conflicts and `cue vet` fails naming the field.
209// This is what the instance floor writes for a check that is not
210// negotiable.
211// - `bool | *true` — a disjunction with a marked default, so it reads as
212// `true` when nobody says otherwise and unifies cleanly with a
213// project's `false`. This is what a project-type template writes for a
214// check that is a sensible starting point rather than a rule.
215//
216// A project may always ADD names here, because the struct is open below
217// the keys the instance layer pinned; it can never remove one the floor
218// made concrete.
219#Requires: [#Name]: bool
220
221// #Paths is the same set-as-struct encoding, keyed by glob rather than by
222// check name, and read the same way: `true` pins the coverage, `bool |
223// *true` offers it.
224#Paths: [#Pattern]: bool
225
226// #Check is how one named check is run. In the reference implementation a
227// check is a flake check: `nix build .#checks.<system>.<attribute>`, where
228// the system is the runner's own and never the policy's — a policy that
229// named a system would stop travelling.
230#Check: {
231 // The runner kind, named the way schema/attestation.cue's #CheckRun
232 // names it, so a policy and the statement satisfying it agree on what
233 // a check is. "nix" builds a flake check attribute and claims purity
234 // strongly; "command" runs a command line and claims nothing beyond
235 // having run it, which is the effectful class of
236 // archive/design/verification.md.
237 runner: "nix" | "command"
238
239 // The flake check attribute, for the nix runner.
240 attribute?: #Name
241
242 // The command line, for the command runner. One line, because a
243 // statement about it is written as lines and the form has no escapes.
244 command?: =~"^[^\\x00-\\x1f\\x7f]+$"
245
246 if runner == "nix" {
247 attribute: #Name
248 command?: _|_
249 // A pure check's evidence does not age, because time is not among
250 // its inputs. A window declared over one would be a window nothing
251 // reads, so it is refused rather than ignored — and the refusal is
252 // a sentinel rather than bottom, so `cue vet` names the field.
253 validity?: "INVALID: a pure check's evidence does not age; a validity window is for an effectful check"
254 }
255 if runner == "command" {
256 command: =~"^[^\\x00-\\x1f\\x7f]+$"
257 attribute?: _|_
258 }
259
260 // Δ: how long an observation of this check stays good once made
261 // (dcr-439b771). It exists for effectful checks alone. A pure check's
262 // evidence is content-addressed and does not age, because time is not
263 // among its inputs; an effectful check reads mutable world state,
264 // which has no digest, so the honest semantics are bounded staleness
265 // — an observation transfers while nothing relevant has changed under
266 // it and it is recent enough.
267 //
268 // Optional, and its absence is not a default. Absent a declared
269 // window the integrator re-demands the check: a window nobody wrote
270 // down is not a window a tool may invent. Like every other field
271 // here it composes across the two layers, so a group can floor a
272 // window and a project can only narrow it.
273 validity?: #Duration
274
275 // What running the check costs its runner. A "quick" check is cheap.
276 // A "slow" check takes minutes, or needs special hardware such as
277 // virtualisation support.
278 //
279 // The cost is a declared property. `valley checks` groups the required
280 // checks by it and can filter them by it. The integrator does not
281 // read it: approving runs every required check, and the integrator
282 // requires evidence for every required check, whatever its cost.
283 cost: *"quick" | "slow"
284}
285
286// A validity window: a whole number of seconds, minutes, hours or days.
287// Written as text rather than as a number so that the unit is in the
288// document a person reads, and bounded to units a policy has a use for —
289// a window in milliseconds is not a policy, and one in months is not a
290// window.
291#Duration: =~"^[1-9][0-9]*(s|m|h|d)$"
292
293// #Templates is the instance layer's set of project types. A project
294// selects one by name and then narrows it, so a project that is an
295// ordinary instance of its type needs almost no configuration of its own.
296#Templates: [#Name]: #Policy
297
298// Names are lowercase, dash-separated, and short. They are quoted in
299// integrator output and carried in attestation payloads, so they are held
300// to the same shape as project names in schema/valley.cue. The shape also
301// keeps every control character out of a class or check name, so each
302// prints whole on a line of its own.
303#Name: =~"^[a-z0-9][a-z0-9-]*$"
304
305// A path glob, relative to the repository root. A leading slash and a `..`
306// segment are both rejected: a pattern that reaches outside the tree is
307// describing something a diff cannot contain, and silently matching
308// nothing is the expensive way to find that out.
309//
310// A control character is rejected too. Git allows one in a path, but a
311// policy has no use for a pattern that matches one.
312#Pattern: =~"^[^/]" & !~"(^|/)\\.\\.(/|$)" & !~"[\\x00-\\x1f\\x7f]"
313
314// ----------------------------------------------------------------------
315// The composition.
316//
317// The two layers meet here. The schema cannot tell which file a field came
318// from, so the tool that composes a policy is what makes the floor a
319// floor: `floor` and `templates` must be read from the instance
320// repository's integrated tip, never from the project's tree. A project
321// that vendored a copy of the floor and edited the copy would unify
322// perfectly well — unification forces nothing on its own. What forces is
323// provenance: changing the floor is a change landing in the instance
324// repository, under that repository's own policy.
325//
326// The two halves resolve at different times, on purpose. The floor
327// resolves at head, because a floor a project could pin is not a floor.
328// Templates may be pinned, because they are defaults and a stale default
329// is harmless.
330
331// The instance layer, read from the instance repository's integrated tip.
332floor: #Policy
333templates: #Templates
334
335// The project type that adds nothing, so that a project which declares no
336// type still composes. It is defined here rather than left to each
337// instance because it is a property of the composition, not of any group.
338templates: none: {}
339
340// The project layer, read from the project's own tree. Neither field has
341// to be written: `type` defaults to the project type that adds nothing,
342// and an unwritten `project` is three empty sets. A project with no policy
343// document at all therefore composes — to the floor exactly, which is the
344// point of having one.
345type: #Name | *"none"
346project: #Policy
347
348// The effective policy for this project: what the integrator derives a
349// change's required checks from. Naming an unknown project type is an
350// evaluation error rather than a silent fallback. The `let` exists so the
351// cross-check below can read the composition without `policy` referring to
352// itself; every error still surfaces under `policy`, where a reader is
353// looking.
354let composed = floor & templates[type] & project
355policy: composed
356
357// Every check a class requires or follows, and every check `unclassified`
358// names, must exist in the composed catalogue. A name that does not lands in `checks`
359// as an incomplete #Check, so `cue vet -c` fails with an error naming the
360// missing check. Without this, a typo is not a vet error — it is a check
361// that silently stops being required. The cross-check runs against the
362// composition rather than either layer, because a floor may legitimately
363// require a check whose definition a template supplies.
364policy: {
365 for _, c in composed.classes for n, _ in c.requires {
366 checks: (n): _
367 }
368 for _, c in composed.classes if c.follows != _|_ for n, _ in c.follows {
369 checks: (n): _
370 }
371 for n, _ in composed.unclassified {
372 checks: (n): _
373 }
374}
375
376// The resolved carry mode of every class of the composed policy, keyed by
377// class name. A change's carry mode is the strictest of these among the
378// classes its paths match, and an unclassified path is `same-paths`.
379// `valley checks` reads this field and reports that mode. The field is
380// hidden because the sentinel below rejects every other top-level field.
381//
382// It reads `policy` rather than `composed`, because a document may refine
383// `policy` directly. A key written there is in `policy` and not in
384// `composed`, and the deriver matches paths and reads requirements from
385// `policy`. Reading `composed` here would miss a mode added that way, or a
386// whole class.
387_mode: {for n, c in policy.classes {(n): (#Resolve & {carry: c.carry}).mode}}
388
389// A file's top level cannot be closed (embedding a definition would open
390// it instead — embedding lifts closedness), so reject stray top-level
391// fields explicitly: anything but the composition's fields — a `class:`
392// typo, a host-side concern like `enable:` — conflicts with this sentinel
393// and fails vet with an error naming the field.
394[!="floor" & !="templates" & !="type" & !="project" & !="policy"]: "INVALID: unknown top-level field; only \"floor\", \"templates\", \"type\", \"project\" and \"policy\" are allowed"