gunk-dev/the-valley

valley.cue

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