gunk-dev/the-valley

valley.cue

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