gunk-dev/the-valley

valley.cue

sourcecaaa5bee5e11main's published tip history
15.6 KiB
1// Package valley is the canonical domain model of a valley host: what the
2// host serves, independent of any machine that serves it.
3//
4// This file is deliberately not Nix. The NixOS module in this repo is one
5// installer that consumes the model (`cue vet` + `cue export` at build
6// time); other installers and tools read the same file. Deployment concerns
7// — data directory, unix user, SSH keys — are machine integration and live
8// with the installer, never here.
9package valley
10
11import "strings"
12
13// #Host is a complete valley host declaration: the set of projects the
14// host serves, the groups they belong to, and the durability policy for
15// the data behind them.
16#Host: {
17 // projects maps a project name to its declaration. Names must be
18 // filesystem- and URL-safe because they become on-disk directory
19 // names ("<name>.git") and clone paths. A project's name is its
20 // identifier: its repository, its plan graph, its policy/read and its
21 // read grants all name it by it. Its web name, name, is the
22 // identifier unless the declaration gives another.
23 projects: [ID=#ProjectName]: #Project & {name: *ID | #ProjectName}
24
25 // groups maps each group's identifier to its declaration. The
26 // identifier never changes, and the projects of the group name it.
27 // The group's name and aliases are what its web URLs say, and they
28 // may change. Optional: a declaration without groups evaluates as it
29 // did before groups existed, and its projects have no web URLs.
30 groups: [#GroupID]: #Group
31
32 // The host's offsite-backup policy. Optional: a declaration without
33 // it is a host without offsite backup, and evaluates exactly as it
34 // did before this field existed.
35 backup?: #Backup
36}
37
38// #Project is the unit of declaration. A project *has* stores; git is the
39// only store type today, and it is nested rather than top-level on
40// purpose — the valley is not committed to being git-only.
41#Project: {
42 // The project's git store.
43 git: {
44 // Whether the host serves a git repository for this project.
45 // Disabling never deletes anything: an existing repository is
46 // left untouched on disk, merely unmanaged.
47 enable: bool | *true
48 }
49
50 // Publication-mirror URLs. Every push to the primary replicates main
51 // and the tags to each URL, force-updating them. Only those: a mirror
52 // publishes what has been integrated, and topic branches awaiting
53 // review are not published. The host changes nothing else on a
54 // mirror: the only deletions it makes there are tags deleted on the
55 // primary. So a repository that becomes a mirror keeps every ref it
56 // already holds; delete what must not be published when declaring it
57 // (docs/architecture.md shows how). Replication is best-effort: a dead
58 // mirror never rejects the primary push. Credentials are the host's
59 // concern (the installer documents how); they are not declared here.
60 mirrors: [...string] | *[]
61
62 // The project's write protection. Optional: a project without it has
63 // no protected refs, so its branches are open to every push. Every
64 // other rule of the push policy (#Protection describes them) applies
65 // to it unchanged.
66 protection?: #Protection
67
68 // Whether the project keeps every change record it holds. True, no
69 // push deletes a record, whatever grant the pusher holds, so a
70 // record's history shows whether a change stayed ready or was
71 // withdrawn or unreadied in between. Retained records count against
72 // the repository's bound on change records, and nothing removes them.
73 // False by default: a holder of the approve grant may delete a record.
74 retainChangeRecords: bool | *false
75
76 // Named grants, each opening ref patterns the push policy closes by
77 // default to the principals it names. A signed release tag pushed by a
78 // person is the case they exist for: tags take no push except through
79 // one. Empty by default, so a project opens nothing it does not name.
80 grants: [#GrantName]: #RefGrant
81
82 // The identifier of the group the project belongs to, which groups
83 // declares. A project's web URLs open with its group's name and its
84 // own web name: /<group>/<name>/…. Optional: a project in no group has
85 // no web URL, so the web publisher publishes nothing of it.
86 group?: #GroupID
87
88 // The project's web name: the second segment of its web URLs. It is
89 // the project's identifier unless the declaration gives another, and
90 // it may change while the identifier does not. A web name is unique
91 // within its group, ignoring case, and two groups may each hold a
92 // project named app. A project in a group has a #WebName, and a
93 // project in no group has no web name but its identifier.
94 name: #ProjectName
95 if group != _|_ {
96 name: #WebName
97 }
98
99 // The project's former web names within its group. A web URL under
100 // one, /<group>/<former>/…, answers with a permanent redirect to the
101 // same path under /<group>/<name>/…, so a project that is renamed
102 // keeps its former name here and its links keep working. They share
103 // the group's names with its projects' web names. Empty by default. A
104 // project with former names belongs to a group.
105 formerNames: [...#WebName] | *[]
106
107 // The project's short aliases. Each is a first segment of a web URL:
108 // /<alias>/… answers with a permanent redirect to the same path under
109 // /<group>/<name>/…. Empty by default. A project with aliases belongs
110 // to a group.
111 aliases: [...#WebName] | *[]
112}
113
114// #ProjectName names a project on the host.
115#ProjectName: =~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
116
117// #GroupID is a group's identifier: the name the declaration keys it by,
118// and its projects name it by. It never changes, and no URL shows it.
119#GroupID: #WebName
120
121// #Group is one group of projects. Its members and stewards are not
122// declared yet: a group is its name and its aliases.
123#Group: {
124 // The group's name: the first segment of its projects' web URLs.
125 name: #WebName
126
127 // Other first segments for the group. A web URL under an alias
128 // answers with a permanent redirect to the same path under name, so
129 // a group that is renamed keeps its former name here and its links
130 // keep working. Empty by default.
131 aliases: [...#WebName] | *[]
132}
133
134// #WebName is a name a web URL holds as one segment: a group's name or
135// alias, or a project's web name, former name or alias. It has the
136// project-name shape and at most
137// 64 characters, so it never starts with "-", and the web server's own
138// paths, under /-/, are never a name's.
139#WebName: =~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$" & strings.MaxRunes(64)
140
141// #RefGrant is one named grant: ref patterns, and the principals who may
142// write the refs they match. A grant only adds writers. A protected ref it
143// covers still takes a declared writer too.
144#RefGrant: {
145 // The refs opened, as full refnames with `*` matching any characters.
146 refs: [#GrantPattern, ...#GrantPattern]
147
148 // Who may write them. At least one: a grant that opens a pattern to
149 // nobody opens nothing.
150 writers: [#PrincipalName, ...#PrincipalName]
151}
152
153// #GrantName names a grant. It appears in what a refusal says.
154#GrantName: =~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
155
156// #GrantPattern is a pattern a grant may name. The namespaces whose rule
157// is fixed are refused: branches are open already, replacement refs and
158// notes are closed to every push, and the valley's own namespace has rules
159// of its own. A declaration that tries to open one fails rather than
160// reading as a grant the hook never applies.
161#GrantPattern: #RefPattern & !~"^refs/(heads|replace|notes|the-valley)/"
162
163// #Protection states which of a project's refs are protected: closed to
164// every push but a declared writer's. It is the declared half of the one
165// structural git invariant, which the pre-receive hook (valleyhook/)
166// enforces on every project a host serves, protected or not.
167//
168// The hook holds every push to an allowlist, and a write must pass every
169// rule that applies to its ref:
170//
171// - replacement refs (refs/replace/*), notes (refs/notes/*) and the
172// valley's own namespace (refs/the-valley/*, but for the four below)
173// take no push at all;
174// - a ref that is symbolic in the repository takes no push;
175// - topic branches (refs/heads/*) are open;
176// - attestation refs (refs/the-valley/attestations/<digest>/<key hash>)
177// may be created by anyone, never updated or deleted, and only
178// pointing at notes about that digest signed under that key hash;
179// - integration requests (refs/the-valley/integration-requests/*) take
180// writes only from a principal holding the approve grant, also
181// accepted as request (dcr-e544f20);
182// - change records (refs/the-valley/changes/main/<change ID>) move only
183// by the transitions the hook allows;
184// - the plan graph (refs/the-valley/knots, that name exactly) takes a
185// named principal's write of one commit on its tip that leaves the
186// graph valid, and is never deleted;
187// - every other ref, tags included, takes a named grant of the project
188// (#Project.grants) that opens it to the pusher;
189// - a protected ref also takes a declared writer.
190//
191// Only the protected set, its writers and the project's grants are
192// declared. The other rules are not choices: the namespaces they cover are
193// fixed by the contributor protocol (archive/design/contribute.md), and the closed
194// ones change what every reader of the repository sees. Protection only
195// adds a requirement — a writer of a pattern covering integration requests
196// still needs the approve grant. All policy beyond the invariant lives in
197// the integrator, never here (archive/design/architecture.md, _a pull-based
198// integrator_).
199#Protection: {
200 // The refs closed to everyone but a declared writer. Patterns are
201 // matched against the full refname, so they begin with "refs/" — a
202 // bare branch name would protect nothing — and `*` matches any
203 // characters, path separators included.
204 refs: [#RefPattern, ...#RefPattern] | *["refs/heads/main"]
205
206 // The principals allowed to push them. Empty by default: a protected
207 // ref admits no pushes at all. The integrator's local ref write is
208 // then the only path onto it, and that privilege is the machinery's
209 // — it is a write made on the host, which no push hook governs — so
210 // it is never declared here. A writers list is the deliberate
211 // exception that punches a named hole in the wall; the transition
212 // arrangement used one for the operator.
213 writers: [...#PrincipalName] | *[]
214}
215
216// #RefPattern is a full refname, optionally with `*` wildcards. The
217// characters git forbids in a refname — space, ~, ^, :, ?, [, \ — are
218// rejected here too, so a pattern that could never match a real ref fails
219// the declaration rather than sitting dead in a hook.
220#RefPattern: =~"^refs/[^ ~^:?\\\\[]+$"
221
222// #PrincipalName names a principal — a human, machine, or service the
223// instance grants something to — in the instance's identity registry
224// (dcr-b87f6e8). The registry's compilation (identity/) binds each name to
225// the keys that act as it, and on a host that compiles no registry the
226// installer does — the NixOS module in this repo maps each name to its
227// keys by hand. The shape is the project-name shape, for the same reason: a
228// name travels into files and command lines.
229#PrincipalName: =~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
230
231// #Backup is the durability policy for the host's data: that an offsite
232// backup exists, how often it runs, and how long snapshots are kept. It is
233// deliberately host-level — one data directory, one repository — because
234// that is the host's reality today; per-project backup is a possible
235// growth path (a `backup` on #Project), not a present need. Only policy
236// is declared here: the repository URL, credentials, and host-key pinning
237// are machine integration and live with the installer, never here.
238#Backup: {
239 // Whether offsite backup runs for the host's data. Disabling never
240 // deletes anything: an existing backup repository is left untouched,
241 // merely no longer written to or pruned.
242 enable: bool | *true
243
244 // The backup target kind. restic over sftp (dcr-d7952bc) is the
245 // only target today; an object-store target
246 // would widen this to a disjunction ("restic-s3") — deliberately not
247 // before an implementation exists.
248 target: "restic-sftp"
249
250 // How often a snapshot is taken. "nightly" is the only cadence
251 // today; its exact wall-clock rendering is the installer's choice.
252 cadence: "nightly"
253
254 // How many snapshots to keep per tier when pruning (restic
255 // --keep-daily/--keep-weekly/--keep-monthly semantics). Every tier
256 // keeps at least one snapshot — a zero tier would silently thin
257 // history, so it is rejected rather than rendered.
258 retention: {
259 daily: int & >0 | *7
260 weekly: int & >0 | *4
261 monthly: int & >0 | *6
262 }
263}
264
265// The top level of this package is itself a host declaration: a config
266// file evaluated together with this schema is validated against #Host —
267// unknown fields and unsafe project names are rejected.
268//
269// cue vet -c schema/valley.cue <config>.cue
270// cue export schema/valley.cue <config>.cue
271projects: #Host.projects
272
273// Mirrors #Host.groups.
274groups: #Host.groups
275
276// Every group a project names is declared, and a project with a web name
277// of its own, former names or aliases is in a group. A project that
278// breaks one of these lands in _groupless as a sentinel string against a
279// `bool`, and fails vet naming the project.
280_groupless: [string]: bool
281_groupless: {
282 for n, p in projects {
283 if p.group != _|_ if groups[p.group] == _|_ {
284 "\(n)": "INVALID: the project's group is not declared under groups"
285 }
286 if p.group == _|_ if p.name != n {
287 "\(n)": "INVALID: a project with a web name belongs to a group"
288 }
289 if p.group == _|_ if len(p.formerNames) > 0 {
290 "\(n)": "INVALID: a project with former names belongs to a group"
291 }
292 if p.group == _|_ if len(p.aliases) > 0 {
293 "\(n)": "INVALID: a project with aliases belongs to a group"
294 }
295 }
296}
297
298// The web names and former names of a group's projects share one
299// namespace: the second segment of the group's web URLs. Two of them that
300// differ at most in case collide, and so does a name given twice. Each
301// claim lands in _secondSegments under its group's identifier and its
302// lowercased name, with who makes it, and two claims of one name in one
303// group conflict and fail vet naming both. Projects in different groups
304// never collide.
305_secondSegments: [string]: [string]: string
306_secondSegments: {
307 for n, p in projects if p.group != _|_ {
308 (p.group): (strings.ToLower(p.name)): "the web name of project \(n)"
309 for i, f in p.formerNames {
310 (p.group): (strings.ToLower(f)): "former name \(i) of project \(n)"
311 }
312 }
313}
314
315// Group names, group aliases and projects' aliases share one namespace:
316// the first segment of a web URL. Two of them that differ at most in case
317// collide, and so does a name given twice. Each claim lands in
318// _firstSegments under its lowercased name, with who makes it, and two
319// claims of one name conflict and fail vet naming both.
320_firstSegments: [string]: string
321_firstSegments: {
322 for id, g in groups {
323 (strings.ToLower(g.name)): "the name of group \(id)"
324 for i, a in g.aliases {
325 (strings.ToLower(a)): "alias \(i) of group \(id)"
326 }
327 }
328 for n, p in projects for i, a in p.aliases {
329 (strings.ToLower(a)): "alias \(i) of project \(n)"
330 }
331}
332
333// Mirrors #Host.backup. CUE cannot reference an optional field, so the
334// constraint is restated against the same definition.
335backup?: #Backup
336
337// A file's top level cannot be closed (embedding #Host would open it
338// instead — embedding lifts closedness), so reject stray top-level fields
339// explicitly: anything but the #Host fields — a `project:` typo, a
340// deployment concern like `user:` — conflicts with this sentinel and
341// fails vet with an error naming the field.
342[!="projects" & !="groups" & !="backup"]: "INVALID: unknown top-level field; only \"projects\", \"groups\" and \"backup\" are allowed"