gunk-dev/the-valley

architecture.md

source813c6679e1c2main's published tip history
20.9 KiB · Rendered
1# Architecture as deployed
2
3This document describes what the-valley runs today, read from the code. Each section cites the files
4it describes. Each guarantee names the flake check (`checks.x86_64-linux.<name>`) or test that
5demonstrates it. Security and rollouts are in [security.md](./security.md), planned work is in
6[roadmap.md](./roadmap.md), and purpose and principles are in [purpose.md](./purpose.md).
7
8## Overview
9
10A valley host keeps bare git repositories and accepts changes to them over ssh. Anyone with a key
11may push a topic branch. A protected branch such as `main` moves only when the integrator lands an
12integration request. The integrator lands a request only when signed evidence covers every check the
13project's policy requires.
14
15The parts are:
16
17- the host module, which installs everything below (`nix/valley-host.nix`);
18- the push hook, which decides what a push may write (`valleyhook/`);
19- the integrator, one controller per protected project (`integrator/`);
20- `attest`, which runs checks and signs the evidence (`attest/`, `note/`);
21- the identity compiler, which turns a registry into keys and grants (`identity/`);
22- the `valley` command line tool (`bin/valley`);
23- `sigverify`, a hardware-key signature verifier that nothing calls yet (`sigverify/`).
24
25## The host module
26
27The NixOS module `services.valley` reads one CUE file, `services.valley.config`. The build vets it
28against `schema/valley.cue` and fails on any error (`nix/valley-host.nix`). The file declares the
29projects, their mirrors, their protection and the backup policy. Checked by `module-eval` and
30`cue-vet`.
31
32### Repositories and the git user
33
34Each project is a bare repository at `/srv/git/<name>.git`. All ssh git traffic logs in as one Unix
35user, `git`. The git user's login shell runs `valleyhook shell` (`nix/valley-host.nix`,
36`valleyhook/shell.go`). For each session, `valleyhook shell` does three things:
37
381. For a push only, it refuses the session when the hold file `/srv/git/.valley-hold` exists, or
39 when the converged record `/srv/git/.valley-converged` does not match the running configuration.
40 Fetches stay open.
412. It drops every `GIT_*` variable and any `VALLEY_PRINCIPAL` the client sent. It sets
42 `VALLEY_PRINCIPAL` from the tag on the authorized-keys entry of the key that logged in. An
43 untagged or ambiguous key gets no principal.
443. It runs `git-shell` with the same arguments. `git-shell` limits the session to git's own
45 commands.
46
47The sshd `Match User git` block turns off forwarding and tunnels, and sets `ExposeAuthInfo` so the
48shell can see which key logged in. Build-time assertions refuse an sshd config that would let a
49client set `VALLEY_PRINCIPAL` or `GIT_*` (`nix/valley-host.nix`). Checked by `ssh-e2e` (a real sshd
50in a VM), `module-eval` and `valleyhook-unit`.
51
52### valley-init and the push hold
53
54`valley-init` is a oneshot service that runs as git (`nix/valley-host.nix`). It first deletes the
55converged record, so pushes are refused while it runs. It then creates any missing repository and
56links each repository's hooks. A foreign `pre-receive` hook or any `core.hooksPath` setting is a
57conflict. When there is no conflict, it writes the converged record. The record is the first 32 hex
58characters of a SHA-256 of the init script. So pushes resume only when every repository runs this
59configuration's hooks. Checked by `init-e2e` and `ssh-e2e`.
60
61The hold file belongs to the operator, who creates it to pause all pushes. `valley-init` never
62touches it.
63
64## What a push may write
65
66Every repository's `pre-receive` hook runs `valleyhook pre-receive` (`valleyhook/main.go`,
67`valleyhook/policy.go`). The hook is default-deny. If any one ref update is refused, the whole push
68is refused. Checked by `protect-e2e`, `ssh-e2e` and `valleyhook-unit`.
69
70| Ref namespace | Who may push | Rule |
71| --------------------------------------------------- | ------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
72| `refs/heads/*` (topic branches) | Any key | Create, update and delete. |
73| Protected refs (default `refs/heads/main`) | Principals in `protection.writers` (default empty) | The integrator moves them on disk. It never pushes. |
74| `refs/the-valley/attestations/<tree>/<key-hash>` | Any key | Create only. The ref must name a tree of notes within size bounds, and each note must verify under a known signer and be about that tree (`valleyhook/attestation.go`). |
75| `refs/the-valley/integration-requests/<target>/<c>` | Principals with the `request` grant | Create, update and delete. |
76| Every other `refs/the-valley/*` | Nobody | This covers the integrator's outcome records. |
77| `refs/replace/*`, `refs/notes/*` | Nobody | A replace ref would let git show other content under a watched name. |
78| Symbolic refs | Nobody | |
79| Tags and every other ref | Principals named in a project grant: `grants.<name>: {refs, writers}` (`schema/valley.cue`) | Grant patterns cannot name `heads`, `replace`, `notes` or `the-valley`. |
80
81The `request` grant comes from two places: the host's declared `services.valley.grants.request` and
82the identity compiler's `grants` file. Why replace refs are closed is recorded in
83[bd-75c8721](../archive/.the-valley/bugs/bd-75c8721-pushes-could-write-any-namespace.md).
84
85## After a push: events and mirrors
86
87Each repository's `post-receive` hook runs every program in `hooks/post-receive.d/`
88(`nix/valley-host.nix`). There are two:
89
90- **The bus publisher**, when the bus is on. It publishes one `ref-updated` event for every ref the
91 push updated, on the subject `valley.git.<repo>.ref-updated`. Checked by `bus-e2e`.
92- **The mirror pusher**, when the project declares mirrors. Mirrors are a list of URLs
93 (`schema/valley.cue`). The pusher runs `git push --prune` of `main` and all tags to each URL, then
94 deletes every other branch on the mirror. So a mirror carries only integrated history. The push
95 uses the git user's own ssh identity, which the host provides. Failures go to the log only.
96 Checked by `mirror-e2e`.
97
98Git runs `post-receive` only for a push. The integrator moves refs with `git update-ref`, so it
99writes each landing to a queue directory in the repository instead (`integrator/bus.go`). A systemd
100path unit, `valley-publish@<project>`, starts a service as git when the queue is not empty. The
101service feeds the queue to the bus publisher, deletes the queue files, and runs the mirror pusher.
102Checked by `publish-e2e`.
103
104The bus is NATS with JetStream, listening on `127.0.0.1:4222`. `valley-bus-init` creates one stream,
105`valley`, over the subjects `valley.>`, with file storage and NATS defaults. The events are defined
106in `schema/events.cue`: `ref-updated`, `integration-succeeded` and `request-stale`. The bus has no
107authentication, so nothing treats an event as authoritative
108([bd-d853d9c](../archive/.the-valley/bugs/bd-d853d9c-bus-unauthenticated.md)). The integrator reads
109git directly.
110
111## The integrator
112
113Each protected project has one controller, `valley-integrator@<project>`, running as the
114`valley-integrator` user in group git (`nix/valley-host.nix`). It is the single writer of the
115project's protected refs. Every 15 seconds it reconciles each open request (`integrator/main.go`,
116`integrator/refs.go`).
117
1181. **Find requests.** A request is a ref `refs/the-valley/integration-requests/<target>/<change>`
119 that points at the change's head commit.
1202. **Skip what is decided.** The outcome record
121 `refs/the-valley/integration-outcomes/<target>/<change>` holds the head, the target tip, a hash
122 of all attestation refs, and the outcome. If the first three still match, the request is skipped.
123 A `failed` outcome is retried after 10 minutes.
1243. **Build the candidate.** The base is `git merge-base` of the tip and the head. If the base is the
125 tip, the candidate is the head. Otherwise `git merge-tree --write-tree` builds the tree, and the
126 integrator commits it with the tip as the only parent. The commit keeps the head's author. Its
127 committer is the integrator at `integrator@the-valley.invalid`. A conflict makes the request
128 stale.
1294. **Derive the required checks** (see Policy below).
1305. **Judge the evidence** (see Evidence below). The outcome is `land`, `stale` or `reject`. A stale
131 request publishes `request-stale`.
1326. **Land.** The integrator signs a transfer statement that records the policy digest and the checks
133 it relied on. Then it moves the target with compare-and-swap
134 (`git update-ref <target> <new> <tip>`). That is the commit point. Then it queues the landing for
135 the publisher, stores its countersigned evidence, deletes the request with compare-and-swap,
136 publishes `integration-succeeded`, and writes the outcome record.
137
138Each step is a separate `git update-ref` call. A crash between steps can leave a landing without its
139evidence or outcome record. Checked by `integrator-e2e` (scenarios for fast-forward, merge,
140conflict, stale evidence, tampered notes, retries and a moved target) and `integrator-unit`.
141
142## Policy
143
144A project's required checks come from three CUE layers, unified as
145`floor & templates[type] & project` (`schema/verification.cue`):
146
147- **The schema**, `schema/verification.cue`, ships inside the `valley` and integrator packages
148 (`nix/packages.nix`).
149- **The floor and templates** come from the owning valley's instance repository, at
150 `policy/instance/*.cue` on its `main`. `policy/valley` names that repository. A value the floor
151 writes as `true` is mandatory. A template value written `bool | *true` is a default a project may
152 turn off.
153- **The project layer** is `policy/project/*.cue`, read at the tip of the target branch.
154
155Both layers are read from integrated tips, never from the change being judged
156(`integrator/policy.go`). A path class maps path globs to checks. A change owes the union of the
157checks of every class one of its paths matches. A path no class matches owes the `unclassified`
158checks. The integrator gets this answer by running `valley checks`, which diffs with renames counted
159on both sides. Checked by `policy-deriver` and `cue-vet`.
160
161The reasons for CUE here, and for the mandatory-versus-default encoding, are in
162[dcr-0f5d9b1](../archive/.the-valley/decisions/dcr-0f5d9b1-cue-config-host-module.md) and
163[dcr-f41f718](../archive/.the-valley/decisions/dcr-f41f718-declared-verification-policy.md).
164
165## Evidence: attest and notes
166
167`attest run` runs checks against a commit exported with `git archive` (`attest/run.go`,
168`attest/check.go`). A `nix` check builds the flake check's derivation. A `command` check runs a
169shell command in the exported tree. If every check passes, `attest` writes a statement per check,
170signs it, and stores it under `refs/the-valley/attestations/<tree>/<key-hash>`.
171
172A statement is line-based text that opens with `the-valley/attestation/v1` (`attest/text.go`,
173`schema/attestation.cue`). Its subject is the tree digest. A pure statement records the derivation,
174its inputs digest and the output. An effectful statement records the environment and the time it was
175observed. The envelope is a signed note: the `sumdb/note` format with Ed25519, implemented in
176`note/note.go`. Checked by `attest-e2e`, `attest-conformance`, `attest-schema` and `note-unit`.
177
178The integrator reads evidence at the head's tree and verifies each note with `attest verify`
179(`integrator/evidence.go`). A required check is met when its note passed and one of these holds
180(`integrator/verdict/verdict.go`):
181
182- **A pure check, unmoved tree.** The landed tree is the attested tree.
183- **A pure check, same inputs.** `attest inputs` on the candidate gives the same inputs digest and
184 derivation hash, computed without a build.
185- **An effectful check.** No class that requires it changed between the base and the tip, and the
186 evidence is younger than the check's `validity` window.
187
188Checked by `integrator-unit` (the verdict tests) and `integrator-e2e`.
189
190Check definitions come from the change being judged. `attest` builds the flake checks of the
191exported tree, so a change can edit the check it is judged by
192([bd-eaefe82](../archive/.the-valley/bugs/bd-eaefe82-check-definitions-come-from-the-branch.md),
193[ida-a9e274c](../archive/.the-valley/ideas/ida-a9e274c-mandated-checks-come-from-the-instance.md)).
194the-valley's own Go, Nix and CUE paths match no class, so they owe only the floor's `unclassified`
195checks.
196
197## Identity
198
199The identity registry is CUE under `identity/` in the instance repository, read at its `main`
200(`identity/registry.go`, `schema/identity.cue`). It declares principals and boundaries.
201
202- A principal has a kind (`human`, `machine` or `service`), one or more `ssh-ed25519` keys, and
203 grants. A key may also sign attestations, under a name it declares.
204- A grant names a boundary. A boundary has a kind: `git-push`, `registry` or `request`.
205- A machine, service or external principal must declare `expires`. The principal is dropped from
206 that day on.
207
208`valley-identity` compiles the registry every 5 minutes, as git (`nix/valley-host.nix`,
209`identity/compile.go`, `identity/render.go`). It writes three files under
210`/var/lib/valley-identity/`:
211
212- `authorized_keys.git`: one line per key of a principal with a `git-push` grant, tagged
213 `environment="VALLEY_PRINCIPAL=<principal>"`. sshd reads it after the host's declared keys.
214- `known-signers`: the keys that may sign attestations, for the hook and the integrator.
215- `grants`: one `request <principal>` line per holder of the `request` grant.
216
217The compiler refuses a registry where no principal holds a registry grant, where one key belongs to
218two principals, or where a key matches a host-declared key under a different tag or none. It
219computes everything before it writes anything, so a refused registry leaves the last good files in
220place. Checked by `identity-e2e` and `identity-schema`, and by the Go tests in
221`identity/identity_test.go`. Why identity is a governed registry, and why machine keys expire, is in
222[dcr-b87f6e8](../archive/.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) and
223[bd-8a591dc](../archive/.the-valley/bugs/bd-8a591dc-machine-credentials-never-expire.md).
224
225## sigverify
226
227`sigverify` verifies OpenSSH SSHSIG signatures and signed git tags against an `allowed_signers` file
228(`sigverify/`). It exists because `ssh-keygen -Y verify` and `git verify-tag` never check the
229hardware key's flags. A key file edited with `ssh-keygen -p -O no-touch-required` signs without a
230touch, and those tools accept the signature.
231
232- The default class, `fido-sk`, takes only security-key signatures. It requires the user-presence
233 bit. It also requires user verification under `--require-uv` or a `verify-required` signer line.
234- The `tkey-signer` class takes a marked `ssh-ed25519` key from a Tillitis TKey.
235- Plain software keys pass only with `--allow-non-sk` or `--allow-class software`.
236
237Checked by `sigverify-unit` and `sigverify-e2e`, including a touchless-signature negative test.
238Nothing in the-valley or cosmo calls `sigverify` yet.
239
240## The valley command
241
242`bin/valley` is a shell script, also packaged with its schema (`nix/packages.nix`).
243
244| Command | What it does |
245| --------------------- | ------------------------------------------------------------------------------------------------- |
246| `valley pending` | Lists branches not merged into `main`. |
247| `valley review <b>` | Shows a branch and offers `[a]sk`, `[r]eject` and `[s]kip`, plus `[b]ase` when it needs a rebase. |
248| `valley checks` | Reports the checks a change owes. It runs nothing. |
249| `valley status <b>` | Reads the request and outcome record for a branch. |
250| `valley tail [subj]` | Subscribes to bus events. |
251| `valley replay [dir]` | Publishes one `ref-updated` event per current ref. |
252
253`[a]sk` runs the owed checks with `attest` in a worktree of the branch head. Then one atomic push
254carries the evidence refs and the request ref, under a lease. `[b]ase` rebases the branch in a
255temporary worktree and pushes it with a lease. `[r]eject` deletes the remote branch after a prompt.
256Checked by `valley-cli`, `valley-request`, `valley-status` and `review-notes`.
257
258## The life of a change
259
2601. An author pushes a topic branch. The hook accepts it, the bus gets `ref-updated`, and the mirrors
261 prune it.
2622. The operator runs `valley review <branch>` and chooses `[a]sk`. The owed checks run, and one push
263 files the evidence and the request. The hook verifies each note and the `request` grant.
2643. Within 15 seconds the integrator builds the candidate, derives the policy, judges the evidence,
265 and lands or refuses.
2664. On a landing, the publisher sends `ref-updated` to the bus and pushes `main` and tags to the
267 mirrors. `valley status <branch>` shows the outcome.
268
269## How cosmo deploys it
270
271cosmo's `hosts/classic-laddie/default.nix` imports `the-valley.nixosModules.valley-host` and points
272`services.valley.config` at `hosts/classic-laddie/valley.cue`. It turns on the bus, the integrator
273and the identity compiler, with the private repository `qinling` as the instance project. The host
274declares a small set of static keys beside the compiled registry keys.
275
276`valley.cue` declares two projects, `the-valley` and `qinling`. Both protect `main`. `the-valley`
277mirrors to GitHub at `gunk-dev/the-valley`, and `qinling` has no mirror. The mirror push uses the
278git user's ssh key, from an agenix secret. Backups are a nightly restic run of `/srv/git` to an
279offsite Storage Box over sftp, kept 7 daily, 4 weekly and 6 monthly. The backup target still needs
280provisioning, so the run logs a failure until it is set up.
281
282## Not deployed or not wired yet
283
284| What | State |
285| ------------------------------------------- | -------------------------------------------------------------------------------- |
286| `sigverify` | Landed and tested. Nothing calls it. |
287| Hardware-backed keys in the registry | `schema/identity.cue` accepts `ssh-ed25519` keys only. |
288| SSHSIG evidence | Attestations use the signed-note format. |
289| Approvals | The integrator checks evidence only. No approval is required for any path class. |
290| A crash-safe landing ledger | Landing is several separate ref updates. |
291| Policy and registry snapshot in the outcome | The outcome record holds the head, the tip and an evidence hash. |
292| Registry compile on a qinling landing | The compiler runs on its 5-minute timer only. |
293| Bus authentication | The bus listens on localhost with no authentication. |
294| Other projects on the valley | cosmo and klaus are not hosted on the valley. |