gunk-dev/the-valley

identity-e2e.sh

source4f0fdda3f4famain's published tip history
16.2 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export GIT_CONFIG_NOSYSTEM=1
6git config --global user.name valley-check
7git config --global user.email valley-check@localhost
8git config --global init.defaultBranch main
9cd "$TMPDIR" || exit 1
10
11# A bare instance repository, holding one registry at its tip — what a host
12# serves and what the compiler reads.
13serve() {
14 local name="$1" source="$2"
15 rm -rf work "$name.git"
16 git init --quiet --bare "$name.git"
17 git init --quiet work
18 mkdir -p work/identity
19 cp "$source"/*.cue work/identity/
20 chmod u+w work/identity/*.cue
21 git -C work add -A
22 git -C work commit --quiet -m "$name"
23 git -C work push --quiet "$TMPDIR/$name.git" main
24}
25
26compile() {
27 identity compile --repo "$TMPDIR/instance.git" \
28 --known-signers "$TMPDIR/signers" --authorized-keys "$TMPDIR/keys" \
29 --grants "$TMPDIR/grants" "$@"
30}
31
32# The worked registry compiles to exactly the artifacts checked in beside
33# it. This is the whole claim of the compiler, and the artifacts it replaces
34# were written by hand — so the comparison is byte for byte.
35serve instance "$registry"
36compile --now 2026-08-09 2> first.log || exit 1
37diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
38diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1
39diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1
40
41# A replacement ref planted in the instance repository changes nothing the
42# compiler reads. refs/replace/<blob> makes git read another object wherever
43# it looks that blob up, so a reader that leaves replacement refs on would
44# compile a registry nobody landed — here, one granting a key push and
45# request — while main stays exactly where it was.
46ssh-keygen -q -t ed25519 -N "" -C intruder -f "$TMPDIR/intruder"
47cp "$registry/registry.cue" forged.cue
48chmod u+w forged.cue
49{
50 echo
51 echo 'principals: "intruder": {'
52 echo ' kind: "human"'
53 echo ' keys: [{'
54 echo ' class: "ssh-ed25519"'
55 echo ' bound: "hardware"'
56 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/intruder.pub")"
57 echo ' }]'
58 echo ' grants: {'
59 echo ' push: boundary: "classic-laddie-push"'
60 echo ' request: boundary: "classic-laddie-request"'
61 echo ' }'
62 echo '}'
63} >> forged.cue
64landed="$(git -C instance.git rev-parse main:identity/registry.cue)"
65forged="$(git -C instance.git hash-object -w "$TMPDIR/forged.cue")"
66git -C instance.git update-ref "refs/replace/$landed" "$forged"
67if ! git -C instance.git show main:identity/registry.cue | grep intruder > /dev/null; then
68 echo "identity-e2e: the planted replacement did not take, so the check below proves nothing" >&2
69 exit 1
70fi
71compile --now 2026-08-09 || exit 1
72diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
73diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1
74diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1
75if grep -q intruder "$TMPDIR/keys" "$TMPDIR/grants"; then
76 echo "identity-e2e: a replacement ref put a principal nobody landed into the compiled artifacts" >&2
77 exit 1
78fi
79# The same holds when the caller's environment asks for replacement refs.
80GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" compile --now 2026-08-09 || exit 1
81diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1
82git -C instance.git update-ref -d "refs/replace/$landed"
83
84# What the hook does with the compiled grants. A bare repository is wired
85# with the pre-receive hook a host compiling this registry renders,
86# followed from its init script; the one edit is the directory of the
87# compiled files, which a host keeps under /var/lib and this sandbox cannot.
88# Pushing as a principal is pushing with the tag on, the substitution for
89# sshd the hook checks make everywhere.
90rendered="$(grep -o "/nix/store/[^ ]*-valley-protect-guarded" "$identityInitScriptPath" | head -n1)"
91if ! grep -qF -- "--grants /var/lib/valley-identity/grants" "$rendered"; then
92 echo "identity-e2e: the hook of a host compiling the registry does not read the compiled grants" >&2
93 cat "$rendered" >&2
94 exit 1
95fi
96sed -e "s|/var/lib/valley-identity/grants|$TMPDIR/grants|" \
97 -e "s|/var/lib/valley-identity/known-signers|$TMPDIR/signers|" "$rendered" > hook
98grep -qF -- "--known-signers $TMPDIR/signers" hook || {
99 echo "identity-e2e: the hook of a host compiling the registry does not check attestations against the compiled keys" >&2
100 exit 1
101}
102chmod +x hook
103git init --quiet --bare guarded.git
104ln -s "$TMPDIR/hook" guarded.git/hooks/pre-receive
105request=refs/the-valley/integration-requests/main/topic
106if VALLEY_PRINCIPAL=stoned-flynn git -C work push --quiet "$TMPDIR/guarded.git" "main:$request" 2> refused.err; then
107 echo "identity-e2e: a principal the registry grants push and not request filed a request" >&2
108 exit 1
109fi
110grep -q 'request grant' refused.err
111VALLEY_PRINCIPAL=patrick git -C work push --quiet "$TMPDIR/guarded.git" "main:$request"
112git -C guarded.git rev-parse --verify --quiet "$request" > /dev/null || {
113 echo "identity-e2e: the principal the registry grants request could not file one" >&2
114 exit 1
115}
116
117# Level-triggered: compiling again converges on the same bytes and touches
118# nothing.
119compile --now 2026-08-09 2> second.log || exit 1
120if ! grep -q "unchanged" second.log; then
121 echo "identity-e2e: a second compilation of an unchanged registry rewrote its artifacts" >&2
122 cat second.log >&2
123 exit 1
124fi
125
126# The registry is read from the integrated tip and from nowhere else, which
127# is the whole of the forcing: an edit on a branch governs nothing.
128git -C work checkout --quiet -b topic
129cp "$expired"/*.cue work/identity/registry.cue
130git -C work commit --quiet -am topic
131git -C work push --quiet "$TMPDIR/instance.git" topic
132compile --now 2026-08-09 || exit 1
133diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
134
135# The compiled verifier line is the one attest writes. The published keys
136# above pin the format against a fixed vector; this pins it against the
137# implementation that reads it, over a key generated here.
138ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/scratch"
139mkdir -p scratch-registry
140{
141 echo 'package identity'
142 echo 'boundaries: "registry": kind: "registry"'
143 echo 'genesis: "someone"'
144 echo 'principals: "someone": {'
145 echo ' kind: "human"'
146 echo ' keys: [{'
147 echo ' class: "ssh-ed25519"'
148 echo ' bound: "hardware"'
149 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/scratch.pub")"
150 echo ' signs: "someone/attestations"'
151 echo ' }]'
152 echo ' grants: govern: boundary: "registry"'
153 echo '}'
154} > scratch-registry/registry.cue
155serve instance scratch-registry
156compile --now 2026-08-09 || exit 1
157attest key --key "$TMPDIR/scratch" --name "someone/attestations" > expected-line
158if ! grep -qxFf expected-line "$TMPDIR/signers"; then
159 echo "identity-e2e: the compiled verifier key is not the line attest writes" >&2
160 cat expected-line "$TMPDIR/signers" >&2
161 exit 1
162fi
163
164# An entry past its expiry leaves both artifacts, says so, and costs the
165# rest of the registry nothing. The day before, it is still there — the
166# declared day is the first day the entry no longer counts.
167serve instance "$expired"
168compile --now 2026-09-30 || exit 1
169grep -q "stoned-flynn/attestations" "$TMPDIR/signers" || {
170 echo "identity-e2e: an unexpired entry was omitted" >&2
171 exit 1
172}
173grep -qx "request stoned-flynn" "$TMPDIR/grants" || {
174 echo "identity-e2e: an unexpired holder of request was omitted from the grants" >&2
175 exit 1
176}
177compile --now 2026-11-01 2> expired.log || exit 1
178if grep -q "stoned-flynn" "$TMPDIR/signers" "$TMPDIR/keys" "$TMPDIR/grants"; then
179 echo "identity-e2e: an expired entry stayed in the compiled artifacts" >&2
180 cat "$TMPDIR/signers" "$TMPDIR/keys" "$TMPDIR/grants" >&2
181 exit 1
182fi
183if ! grep -q "stoned-flynn expired 2026-10-01" expired.log; then
184 echo "identity-e2e: an expired entry was dropped without saying so" >&2
185 cat expired.log >&2
186 exit 1
187fi
188grep -q "patrick" "$TMPDIR/keys" || {
189 echo "identity-e2e: one expired entry took the rest of the registry with it" >&2
190 exit 1
191}
192
193# The fail-safe. An invalid registry must cost the compilation and nothing
194# else: the last good artifacts are what the gates keep reading, because a
195# compiler bug that emptied them would lock the git user out of the host.
196cp "$TMPDIR/signers" last-good-signers
197cp "$TMPDIR/keys" last-good-keys
198cp "$TMPDIR/grants" last-good-grants
199mkdir -p invalid-registry
200cp "$invalid" invalid-registry/registry.cue
201serve instance invalid-registry
202if compile --now 2026-08-09 2> refused.log; then
203 echo "identity-e2e: an invalid registry compiled" >&2
204 exit 1
205fi
206if ! grep -q "externalGovernance" refused.log; then
207 echo "identity-e2e: the refusal did not name the field that failed" >&2
208 cat refused.log >&2
209 exit 1
210fi
211diff -u last-good-signers "$TMPDIR/signers" || exit 1
212diff -u last-good-keys "$TMPDIR/keys" || exit 1
213diff -u last-good-grants "$TMPDIR/grants" || exit 1
214
215# Governance the clock orphans. This registry is valid as a document — the
216# genesis entry governs — and the only entry governing carries an expiry, so
217# whether it compiles depends on the day. The schema cannot see this; the
218# compiler can, and refuses the whole render rather than write artifacts
219# nobody could ever change again.
220mkdir -p orphan-registry
221{
222 echo 'package identity'
223 echo 'boundaries: {'
224 echo ' "push": kind: "git-push"'
225 echo ' "registry": kind: "registry"'
226 echo '}'
227 echo 'genesis: "founder"'
228 echo 'principals: {'
229 echo ' "founder": {'
230 echo ' kind: "human"'
231 echo ' keys: [{'
232 echo ' class: "ssh-ed25519"'
233 echo ' bound: "hardware"'
234 echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGCxVUxXoyFYV40QureqqSMSA17CvK9IrFB33BA6UOip"'
235 echo ' signs: "founder"'
236 echo ' }]'
237 echo ' grants: {'
238 echo ' push: boundary: "push"'
239 echo ' govern: boundary: "registry"'
240 echo ' }'
241 echo ' expires: "2026-10-01"'
242 echo ' }'
243 echo ' "runner": {'
244 echo ' kind: "machine"'
245 echo ' keys: [{'
246 echo ' class: "ssh-ed25519"'
247 echo ' bound: "host"'
248 echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAvpmbUKEDVsejgv2vxWaY/t4xl0JNnjFswb9SxcG9GG"'
249 echo ' }]'
250 echo ' grants: push: boundary: "push"'
251 echo ' expires: "2027-01-01"'
252 echo ' }'
253 echo '}'
254} > orphan-registry/registry.cue
255
256# The day before: an ordinary compilation.
257serve instance orphan-registry
258compile --now 2026-09-30 || exit 1
259grep -q "founder" "$TMPDIR/signers" || {
260 echo "identity-e2e: the governing entry was omitted while still unexpired" >&2
261 exit 1
262}
263cp "$TMPDIR/signers" last-good-signers
264cp "$TMPDIR/keys" last-good-keys
265
266# The day it expires, and a day well past it: the same document, refused. The
267# entry still pushing does not save it — a grant at another boundary is not
268# governance.
269for pinned in 2026-10-01 2026-12-01; do
270 if compile --now "$pinned" 2> orphaned.log; then
271 echo "identity-e2e: a registry nobody governs on $pinned compiled" >&2
272 exit 1
273 fi
274 if ! grep -q "governanceOrphaned" orphaned.log; then
275 echo "identity-e2e: the refusal on $pinned did not name governanceOrphaned" >&2
276 cat orphaned.log >&2
277 exit 1
278 fi
279 diff -u last-good-signers "$TMPDIR/signers" || exit 1
280 diff -u last-good-keys "$TMPDIR/keys" || exit 1
281done
282
283# A key the host declares by hand under one tag, and the registry
284# authorizes as another principal, is refused: sshd reads the declared
285# entry first, so the registry's principal would never be the one pushing.
286serve instance "$registry"
287compile --now 2026-08-09 || exit 1
288cp "$TMPDIR/keys" last-good-keys
289patrick_key="$(grep -m1 -o 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGCxVUxXoyFYV40QureqqSMSA17CvK9IrFB33BA6UOip' "$registry/registry.cue")"
290for declared in "$patrick_key operator@laptop" "environment=\"VALLEY_PRINCIPAL=someone\" $patrick_key"; do
291 printf '%s\n' "$declared" > declared-keys
292 if compile --now 2026-08-09 --declared-keys declared-keys 2> declared.err; then
293 echo "identity-e2e: a registry key declared by hand under another tag compiled" >&2
294 exit 1
295 fi
296 grep -q 'declared on this host' declared.err || {
297 echo "identity-e2e: the refusal did not name the declared entry" >&2
298 cat declared.err >&2
299 exit 1
300 }
301 diff -u last-good-keys "$TMPDIR/keys" || exit 1
302done
303printf 'environment="VALLEY_PRINCIPAL=patrick" %s\n' "$patrick_key" > declared-keys
304compile --now 2026-08-09 --declared-keys declared-keys || exit 1
305
306# Attestation names. Under a registry with two signers, the hook checks
307# each pushed attestation's signature against the compiled keys, so a name
308# in the create-only namespace can only be taken by the evidence it is
309# named for. The pusher is never the question: evidence is relayed.
310for who in victim attacker stranger; do
311 ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$TMPDIR/$who"
312done
313mkdir -p signers-registry
314{
315 echo 'package identity'
316 echo 'boundaries: {'
317 echo ' "push": kind: "git-push"'
318 echo ' "registry": kind: "registry"'
319 echo '}'
320 echo 'genesis: "victim"'
321 for who in victim attacker; do
322 printf 'principals: "%s": {\n' "$who"
323 echo ' kind: "human"'
324 echo ' keys: [{'
325 echo ' class: "ssh-ed25519"'
326 echo ' bound: "hardware"'
327 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/$who.pub")"
328 printf '\t\tsigns: "%s/attestations"\n' "$who"
329 echo ' }]'
330 if [ "$who" = victim ]; then
331 echo ' grants: {push: boundary: "push", govern: boundary: "registry"}'
332 else
333 echo ' grants: push: boundary: "push"'
334 fi
335 echo '}'
336 done
337} > signers-registry/registry.cue
338serve instance signers-registry
339compile --now 2026-08-09 || exit 1
340
341git init --quiet --bare attested.git
342ln -s "$TMPDIR/hook" attested.git/hooks/pre-receive
343git init --quiet occ
344git -C occ commit --quiet --allow-empty -m "a tree to attest"
345attestation() {
346 attest run --repo occ --key "$TMPDIR/$1" --name "$1/attestations" --command ok=true |
347 sed -n 's/^stored \(refs[^ ]*\) -> .*/\1/p'
348}
349mine="$(attestation attacker)"
350theirs="$(attestation victim)"
351unknown="$(attestation stranger)"
352digest="$(echo "$mine" | cut -d/ -f4)"
353victim_hash="$(echo "$theirs" | cut -d/ -f5)"
354squat="refs/the-valley/attestations/$digest/$victim_hash"
355push_as() { VALLEY_PRINCIPAL="$1" git -C occ push --quiet "$TMPDIR/attested.git" "$2"; }
356must_refuse() {
357 if push_as attacker "$1" 2> occupied.err; then
358 echo "identity-e2e: $2 took an attestation name" >&2
359 exit 1
360 fi
361 grep -qF -- "$3" occupied.err || {
362 echo "identity-e2e: the refusal of $2 did not say \"$3\"" >&2
363 cat occupied.err >&2
364 exit 1
365 }
366}
367# The attacker's own evidence, under its own name: accepted.
368push_as attacker "$mine:$mine"
369# The attacker's note, filed under the victim's key hash.
370must_refuse "$mine:$squat" "the attacker's note under the victim's name" \
371 "no signature under the key hash $victim_hash by a key this host accepts"
372# A note whose signature line claims the victim's key and carries the
373# attacker's signature.
374git -C occ cat-file blob "$mine:ok/statement.note" > mine.note
375sig="$(sed -n 's/^— attacker\/attestations //p' mine.note)"
376forged_sig="$( { printf "$(echo "$victim_hash" | sed 's/../\\x&/g')"; printf '%s' "$sig" | base64 -d | tail -c 64; } | base64 -w0)"
377sed "s|^— attacker/attestations .*|— victim/attestations $forged_sig|" mine.note > forged.note
378blob="$(git -C occ hash-object -w "$TMPDIR/forged.note")"
379sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C occ mktree)"
380top="$(printf '040000 tree %s\tok\n' "$sub" | git -C occ mktree)"
381must_refuse "$top:$squat" "a forged signature" "does not check out"
382# A key the registry does not hold, under its own name.
383must_refuse "$unknown:$unknown" "an unknown signer's note" "does not open"
384# The victim's genuine note, with a line appended that no verifier reads.
385# Its signature still verifies, and the note is still one no verifier
386# opens, so it may not take the victim's name.
387git -C occ cat-file blob "$theirs:ok/statement.note" > theirs.note
388{ cat theirs.note; printf '— x\t AAAAAAA=\n'; } > poisoned.note
389blob="$(git -C occ hash-object -w "$TMPDIR/poisoned.note")"
390sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C occ mktree)"
391top="$(printf '040000 tree %s\tok\n' "$sub" | git -C occ mktree)"
392must_refuse "$top:$theirs" "a poisoned relay of the victim's note" "does not open"
393# And the victim's own evidence, relayed by the attacker, takes the name
394# that none of the above could.
395push_as attacker "$theirs:$theirs"
396git -C attested.git rev-parse --verify --quiet "$theirs" > /dev/null || {
397 echo "identity-e2e: relayed evidence could not take its own name" >&2
398 exit 1
399}
400
401touch "$out"