gunk-dev/the-valley

valley.cue

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