gunk-dev/the-valley

valley.cue

sourcedba476a7feedmain's published tip history
9.8 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
11// #Host is a complete valley host declaration: the set of projects the
12// host serves, and the durability policy for the data behind them.
13#Host: {
14 // projects maps a project name to its declaration. Names must be
15 // filesystem- and URL-safe because they become on-disk directory
16 // names ("<name>.git") and clone paths.
17 projects: [=~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$"]: #Project
18
19 // The host's offsite-backup policy. Optional: a declaration without
20 // it is a host without offsite backup, and evaluates exactly as it
21 // did before this field existed.
22 backup?: #Backup
23}
24
25// #Project is the unit of declaration. A project *has* stores; git is the
26// only store type today, and it is nested rather than top-level on
27// purpose — the valley is not committed to being git-only.
28#Project: {
29 // The project's git store.
30 git: {
31 // Whether the host serves a git repository for this project.
32 // Disabling never deletes anything: an existing repository is
33 // left untouched on disk, merely unmanaged.
34 enable: bool | *true
35 }
36
37 // Publication-mirror URLs. Every push to the primary replicates main
38 // and the tags to each URL, force-updating them. Only those: a mirror
39 // publishes what has been integrated, and topic branches awaiting
40 // review are not published. The host changes nothing else on a
41 // mirror: the only deletions it makes there are tags deleted on the
42 // primary. So a repository that becomes a mirror keeps every ref it
43 // already holds; delete what must not be published when declaring it
44 // (docs/architecture.md shows how). Replication is best-effort: a dead
45 // mirror never rejects the primary push. Credentials are the host's
46 // concern (the installer documents how); they are not declared here.
47 mirrors: [...string] | *[]
48
49 // The project's write protection. Optional: a project without it has
50 // no protected refs, so its branches are open to every push. Every
51 // other rule of the push policy (#Protection describes them) applies
52 // to it unchanged.
53 protection?: #Protection
54
55 // Named grants, each opening ref patterns the push policy closes by
56 // default to the principals it names. A signed release tag pushed by a
57 // person is the case they exist for: tags take no push except through
58 // one. Empty by default, so a project opens nothing it does not name.
59 grants: [#GrantName]: #RefGrant
60}
61
62// #RefGrant is one named grant: ref patterns, and the principals who may
63// write the refs they match. A grant only adds writers. A protected ref it
64// covers still takes a declared writer too.
65#RefGrant: {
66 // The refs opened, as full refnames with `*` matching any characters.
67 refs: [#GrantPattern, ...#GrantPattern]
68
69 // Who may write them. At least one: a grant that opens a pattern to
70 // nobody opens nothing.
71 writers: [#PrincipalName, ...#PrincipalName]
72}
73
74// #GrantName names a grant. It appears in what a refusal says.
75#GrantName: =~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
76
77// #GrantPattern is a pattern a grant may name. The namespaces whose rule
78// is fixed are refused: branches are open already, replacement refs and
79// notes are closed to every push, and the valley's own namespace has rules
80// of its own. A declaration that tries to open one fails rather than
81// reading as a grant the hook never applies.
82#GrantPattern: #RefPattern & !~"^refs/(heads|replace|notes|the-valley)/"
83
84// #Protection states which of a project's refs are protected: closed to
85// every push but a declared writer's. It is the declared half of the one
86// structural git invariant, which the pre-receive hook (valleyhook/)
87// enforces on every project a host serves, protected or not.
88//
89// The hook holds every push to an allowlist, and a write must pass every
90// rule that applies to its ref:
91//
92// - replacement refs (refs/replace/*), notes (refs/notes/*) and the
93// valley's own namespace (refs/the-valley/*, but for the four below)
94// take no push at all;
95// - a ref that is symbolic in the repository takes no push;
96// - topic branches (refs/heads/*) are open;
97// - attestation refs (refs/the-valley/attestations/<digest>/<key hash>)
98// may be created by anyone, never updated or deleted, and only
99// pointing at notes about that digest signed under that key hash;
100// - integration requests (refs/the-valley/integration-requests/*) take
101// writes only from a principal holding the request grant (dcr-e544f20);
102// - change records (refs/the-valley/changes/main/<change ID>) move only
103// by the transitions the hook allows;
104// - the plan graph (refs/the-valley/knots, that name exactly) takes a
105// named principal's write of one commit on its tip that leaves the
106// graph valid, and is never deleted;
107// - every other ref, tags included, takes a named grant of the project
108// (#Project.grants) that opens it to the pusher;
109// - a protected ref also takes a declared writer.
110//
111// Only the protected set, its writers and the project's grants are
112// declared. The other rules are not choices: the namespaces they cover are
113// fixed by the contributor protocol (archive/design/contribute.md), and the closed
114// ones change what every reader of the repository sees. Protection only
115// adds a requirement — a writer of a pattern covering integration requests
116// still needs the request grant. All policy beyond the invariant lives in
117// the integrator, never here (archive/design/architecture.md, _a pull-based
118// integrator_).
119#Protection: {
120 // The refs closed to everyone but a declared writer. Patterns are
121 // matched against the full refname, so they begin with "refs/" — a
122 // bare branch name would protect nothing — and `*` matches any
123 // characters, path separators included.
124 refs: [#RefPattern, ...#RefPattern] | *["refs/heads/main"]
125
126 // The principals allowed to push them. Empty by default: a protected
127 // ref admits no pushes at all. The integrator's local ref write is
128 // then the only path onto it, and that privilege is the machinery's
129 // — it is a write made on the host, which no push hook governs — so
130 // it is never declared here. A writers list is the deliberate
131 // exception that punches a named hole in the wall; the transition
132 // arrangement used one for the operator.
133 writers: [...#PrincipalName] | *[]
134}
135
136// #RefPattern is a full refname, optionally with `*` wildcards. The
137// characters git forbids in a refname — space, ~, ^, :, ?, [, \ — are
138// rejected here too, so a pattern that could never match a real ref fails
139// the declaration rather than sitting dead in a hook.
140#RefPattern: =~"^refs/[^ ~^:?\\\\[]+$"
141
142// #PrincipalName names a principal — a human, machine, or service the
143// instance grants something to — in the instance's identity registry
144// (dcr-b87f6e8). The registry's compilation (identity/) binds each name to
145// the keys that act as it, and on a host that compiles no registry the
146// installer does — the NixOS module in this repo maps each name to its
147// keys by hand. The shape is the project-name shape, for the same reason: a
148// name travels into files and command lines.
149#PrincipalName: =~"^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
150
151// #Backup is the durability policy for the host's data: that an offsite
152// backup exists, how often it runs, and how long snapshots are kept. It is
153// deliberately host-level — one data directory, one repository — because
154// that is the host's reality today; per-project backup is a possible
155// growth path (a `backup` on #Project), not a present need. Only policy
156// is declared here: the repository URL, credentials, and host-key pinning
157// are machine integration and live with the installer, never here.
158#Backup: {
159 // Whether offsite backup runs for the host's data. Disabling never
160 // deletes anything: an existing backup repository is left untouched,
161 // merely no longer written to or pruned.
162 enable: bool | *true
163
164 // The backup target kind. restic over sftp (a Hetzner Storage Box,
165 // dcr-d7952bc) is the only target today; an object-store target
166 // would widen this to a disjunction ("restic-s3") — deliberately not
167 // before an implementation exists.
168 target: "restic-sftp"
169
170 // How often a snapshot is taken. "nightly" is the only cadence
171 // today; its exact wall-clock rendering is the installer's choice.
172 cadence: "nightly"
173
174 // How many snapshots to keep per tier when pruning (restic
175 // --keep-daily/--keep-weekly/--keep-monthly semantics). Every tier
176 // keeps at least one snapshot — a zero tier would silently thin
177 // history, so it is rejected rather than rendered.
178 retention: {
179 daily: int & >0 | *7
180 weekly: int & >0 | *4
181 monthly: int & >0 | *6
182 }
183}
184
185// The top level of this package is itself a host declaration: a config
186// file evaluated together with this schema is validated against #Host —
187// unknown fields and unsafe project names are rejected.
188//
189// cue vet -c schema/valley.cue <config>.cue
190// cue export schema/valley.cue <config>.cue
191projects: #Host.projects
192
193// Mirrors #Host.backup. CUE cannot reference an optional field, so the
194// constraint is restated against the same definition.
195backup?: #Backup
196
197// A file's top level cannot be closed (embedding #Host would open it
198// instead — embedding lifts closedness), so reject stray top-level fields
199// explicitly: anything but the #Host fields — a `project:` typo, a
200// deployment concern like `user:` — conflicts with this sentinel and
201// fails vet with an error naming the field.
202[!="projects" & !="backup"]: "INVALID: unknown top-level field; only \"projects\" and \"backup\" are allowed"