gunk-dev/the-valley

verification.cue

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