gunk-dev/the-valley

identity-e2e.sh

sourced67699705b80main's published tip history
31.8 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# A push landing while the compiler reads cannot mix two registries. The
85# compiler resolves the ref to a commit once and reads every document from
86# that commit. Here a git standing in front of the real one moves main to a
87# commit holding the forged registry the moment the compiler has resolved
88# it. A compiler reading through the ref would compile the forged registry
89# and name the first commit in its journal line. The compiler runs
90# unwrapped, because its wrapper puts its own git first on PATH.
91resolved="$(git -C instance.git rev-parse main)"
92cp forged.cue work/identity/registry.cue
93git -C work commit --quiet -am forged
94moved="$(git -C work rev-parse HEAD)"
95git -C work push --quiet "$TMPDIR/instance.git" "$moved:refs/heads/forged"
96realgit="$(command -v git)"
97mkdir -p racing
98cat > racing/git <<RACING
99#!/bin/sh
100"$realgit" "\$@" || exit
101case " \$* " in
102*" rev-parse "*) "$realgit" -C "$TMPDIR/instance.git" update-ref refs/heads/main "$moved" ;;
103esac
104RACING
105chmod +x racing/git
106PATH="$TMPDIR/racing:$PATH" "$identityUnwrapped" compile --repo "$TMPDIR/instance.git" \
107 --schema "$identitySchema" --known-signers "$TMPDIR/signers" \
108 --authorized-keys "$TMPDIR/keys" --grants "$TMPDIR/grants" --now 2026-08-09 2> raced.log || {
109 cat raced.log >&2
110 exit 1
111}
112if [ "$(git -C instance.git rev-parse main)" != "$moved" ]; then
113 echo "identity-e2e: main did not move mid-read, so the check below proves nothing" >&2
114 exit 1
115fi
116grep -qF "at $resolved:" raced.log || {
117 echo "identity-e2e: the compilation did not name the commit it resolved" >&2
118 cat raced.log >&2
119 exit 1
120}
121diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
122diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1
123diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1
124# The next compilation resolves main where it now is, and compiles the
125# forged registry: the commit the race moved main to compiles differently.
126compile --now 2026-08-09 || exit 1
127grep -q intruder "$TMPDIR/keys" || {
128 echo "identity-e2e: the commit main moved to compiles the same, so the race check proves nothing" >&2
129 exit 1
130}
131git -C instance.git update-ref refs/heads/main "$resolved"
132compile --now 2026-08-09 || exit 1
133diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1
134
135# What the hook does with the compiled grants. A bare repository is wired
136# with the pre-receive hook a host compiling this registry renders,
137# followed from its init script; the one edit is the directory of the
138# compiled files, which a host keeps under /var/lib and this sandbox cannot.
139# Pushing as a principal is pushing with the tag on, the substitution for
140# sshd the hook checks make everywhere.
141rendered="$(grep -o "/nix/store/[^ ]*-valley-protect-guarded" "$identityInitScriptPath" | head -n1)"
142if ! grep -qF -- "--grants /var/lib/valley-identity/grants" "$rendered"; then
143 echo "identity-e2e: the hook of a host compiling the registry does not read the compiled grants" >&2
144 cat "$rendered" >&2
145 exit 1
146fi
147sed -e "s|/var/lib/valley-identity/grants|$TMPDIR/grants|" \
148 -e "s|/var/lib/valley-identity/known-signers|$TMPDIR/signers|" "$rendered" > hook
149grep -qF -- "--known-signers $TMPDIR/signers" hook || {
150 echo "identity-e2e: the hook of a host compiling the registry does not check attestations against the compiled keys" >&2
151 exit 1
152}
153chmod +x hook
154git init --quiet --bare guarded.git
155ln -s "$TMPDIR/hook" guarded.git/hooks/pre-receive
156request=refs/the-valley/integration-requests/main/topic
157if VALLEY_PRINCIPAL=stoned-flynn git -C work push --quiet "$TMPDIR/guarded.git" "main:$request" 2> refused.err; then
158 echo "identity-e2e: a principal the registry grants push and not request filed a request" >&2
159 exit 1
160fi
161grep -q 'approve grant (also accepted as request)' refused.err
162VALLEY_PRINCIPAL=patrick git -C work push --quiet "$TMPDIR/guarded.git" "main:$request"
163git -C guarded.git rev-parse --verify --quiet "$request" > /dev/null || {
164 echo "identity-e2e: the principal the registry grants request could not file one" >&2
165 exit 1
166}
167VALLEY_PRINCIPAL=patrick git -C work push --quiet "$TMPDIR/guarded.git" --delete "$request"
168
169# The approve grant goes by two names, approve and request, and a registry
170# may use either. A registry whose hook boundary is of kind approve, and one
171# granting patrick at a boundary of each kind, compile exactly the artifacts
172# the worked registry does, with patrick written once.
173mkdir -p approve-registry both-registry
174sed 's/kind: "request"/kind: "approve"/' "$registry/registry.cue" > approve-registry/registry.cue
175sed -e 's/^\t"classic-laddie-request": kind: "request"$/&\n\t"classic-laddie-approve": kind: "approve"/' \
176 -e 's/^\t\t\trequest: boundary: "classic-laddie-request"$/&\n\t\t\tapprove: boundary: "classic-laddie-approve"/' \
177 "$registry/registry.cue" > both-registry/registry.cue
178if grep -q 'kind: "request"' approve-registry/registry.cue ||
179 [ "$(grep -c 'classic-laddie-approve' both-registry/registry.cue)" != 2 ]; then
180 echo "identity-e2e: the registries naming the grant approve were not written, so the checks below prove nothing" >&2
181 exit 1
182fi
183# hook_as <principal> <git push arguments…>: a push through the rendered
184# hook, as the principal.
185hook_as() {
186 local who="$1"
187 shift
188 VALLEY_PRINCIPAL="$who" git -C work push --quiet "$TMPDIR/guarded.git" "$@"
189}
190for variant in approve-registry both-registry; do
191 serve instance "$variant"
192 compile --now 2026-08-09 || exit 1
193 diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
194 diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1
195 diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1
196 # And through the hook: patrick files and deletes a request, and
197 # stoned-flynn, holding push alone, does neither.
198 if hook_as stoned-flynn "main:$request" 2> refused.err; then
199 echo "identity-e2e: under $variant, a principal holding push alone filed a request" >&2
200 exit 1
201 fi
202 grep -q 'approve grant (also accepted as request)' refused.err
203 hook_as patrick "main:$request"
204 if hook_as stoned-flynn --delete "$request" 2> refused.err; then
205 echo "identity-e2e: under $variant, a principal holding push alone deleted a request" >&2
206 exit 1
207 fi
208 hook_as patrick --delete "$request"
209 if git -C guarded.git rev-parse --verify --quiet "$request" > /dev/null; then
210 echo "identity-e2e: under $variant, the approve holder could not delete its request" >&2
211 exit 1
212 fi
213done
214
215# The hook reads a grants file naming the grant approve exactly as one
216# naming it request. approver is named under approve, requester under
217# request, both under each, and stoned-flynn under neither. A principal
218# named approve is in the file too, holding only push, a grant the hook
219# does not check: a principal's name is not a grant. Each holder files a
220# request, replaces it with a head that does not descend from it, and
221# deletes it. stoned-flynn and approve can do none of that in between.
222cat > "$TMPDIR/grants" << 'EOF'
223approve approver
224request requester
225approve both
226request both
227push approve
228EOF
229for holder in approver requester both; do
230 hook_as "$holder" "main:$request"
231 git -C work commit --quiet --allow-empty -m "replaced by $holder"
232 replacement="$(git -C work rev-parse HEAD)"
233 git -C work reset --quiet --hard HEAD~1
234 hook_as "$holder" --force "$replacement:$request"
235 [ "$(git -C guarded.git rev-parse "$request")" = "$replacement" ] || {
236 echo "identity-e2e: $holder could not replace its request" >&2
237 exit 1
238 }
239 for refused in "main:$request" "--delete $request"; do
240 for lacking in stoned-flynn approve; do
241 # shellcheck disable=SC2086 # the push arguments are two words, split on purpose
242 if hook_as "$lacking" --force $refused 2> refused.err; then
243 echo "identity-e2e: $lacking, holding neither name of the approve grant, wrote $holder's request" >&2
244 exit 1
245 fi
246 grep -q 'approve grant (also accepted as request)' refused.err
247 done
248 done
249 hook_as "$holder" --delete "$request"
250 if git -C guarded.git rev-parse --verify --quiet "$request" > /dev/null; then
251 echo "identity-e2e: $holder could not delete its request" >&2
252 exit 1
253 fi
254done
255
256# Change records, through the same hook and the same grants file. patrick
257# owns the change and holds nothing in this file. A holder of the approve
258# grant under either name, or both, may update another principal's change
259# record, here by withdrawing the change, and may delete the record.
260# stoned-flynn and approve may do neither, and each refusal leaves the
261# record where it was. The record is created afresh for each holder.
262owned_ref=refs/the-valley/changes/main/klmnopqrstuvwxyzklmnopqrstuvwxyz
263# owned_record <state> <seq> <parent>: a record commit of patrick's change,
264# at one version whose head is owned_head.
265owned_record() {
266 local blob tree
267 blob="$(printf '{"schema": "valley-change-record/v1", "change_id": "%s", "target": "main", "owner": "patrick", "state": "%s", "seq": %s, "versions": [{"n": 1, "head": "git-sha1:%s", "title": "the change", "branch": "topic", "uploader": "patrick"}], "active": 1}\n' \
268 "${owned_ref##*/}" "$1" "$2" "$owned_head" | git -C work hash-object -w --stdin)"
269 tree="$(printf '100644 blob %s\trecord.json\n' "$blob" | git -C work mktree)"
270 git -C work commit-tree "$tree" -p "$3" -m "a change record"
271}
272# owned_at <commit> <what>: the record is at the commit, or gone for "".
273owned_at() {
274 if [ "$(git -C guarded.git rev-parse --verify --quiet "$owned_ref")" != "$1" ]; then
275 echo "identity-e2e: $2" >&2
276 exit 1
277 fi
278}
279# owned_refused <principal> <commit>: the principal may neither move the
280# record to the commit nor delete it, for holding neither name of the
281# approve grant.
282owned_refused() {
283 local who="$1" before
284 before="$(git -C guarded.git rev-parse "$owned_ref")"
285 if hook_as "$who" "$2:$owned_ref" 2> refused.err; then
286 echo "identity-e2e: $who, holding neither name of the approve grant, updated another principal's change record" >&2
287 exit 1
288 fi
289 grep -q "only the change's owner, patrick, or a principal holding the approve grant (also accepted as request) may update its record" refused.err
290 if hook_as "$who" --delete "$owned_ref" 2> refused.err; then
291 echo "identity-e2e: $who, holding neither name of the approve grant, deleted another principal's change record" >&2
292 exit 1
293 fi
294 grep -q "only a principal holding the approve grant (also accepted as request) may delete a change record, and $who holds none" refused.err
295 owned_at "$before" "a refused write by $who moved the change record"
296}
297# owned_commits: patrick's change, and the two records of it pushed below,
298# written into work. filed creates the change, and retired withdraws it.
299# serve replaces work, so they are written again after it.
300owned_commits() {
301 owned_head="$(git -C work commit-tree "main^{tree}" -p main -m "patrick's change")"
302 filed="$(owned_record ready 1 "$owned_head")"
303 retired="$(owned_record withdrawn 2 "$filed")"
304}
305owned_commits
306for holder in approver requester both; do
307 hook_as patrick "$filed:$owned_ref"
308 owned_at "$filed" "patrick could not create a change record"
309 owned_refused stoned-flynn "$retired"
310 owned_refused approve "$retired"
311 hook_as "$holder" "$retired:$owned_ref"
312 owned_at "$retired" "$holder could not withdraw another principal's change"
313 hook_as "$holder" --delete "$owned_ref"
314 owned_at "" "$holder could not delete another principal's change record"
315done
316
317# A grant's name is not its kind. A registry granting stoned-flynn a grant
318# named approve at a boundary of another kind compiles exactly the grants
319# the worked registry does, so the hook refuses stoned-flynn the approve
320# grant's writes.
321for boundary in classic-laddie-push registry; do
322 mkdir -p "mislabelled-$boundary"
323 sed "s/^\t\tgrants: push: boundary: \"classic-laddie-push\"$/\t\tgrants: {push: boundary: \"classic-laddie-push\", approve: boundary: \"$boundary\"}/" \
324 "$registry/registry.cue" > "mislabelled-$boundary/registry.cue"
325 if ! grep -q "approve: boundary: \"$boundary\"" "mislabelled-$boundary/registry.cue"; then
326 echo "identity-e2e: the registry granting approve at $boundary was not written, so the check below proves nothing" >&2
327 exit 1
328 fi
329 serve instance "mislabelled-$boundary"
330 compile --now 2026-08-09 || exit 1
331 diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1
332 owned_commits
333 if hook_as stoned-flynn "main:$request" 2> refused.err; then
334 echo "identity-e2e: a grant named approve at $boundary let its holder file a request" >&2
335 exit 1
336 fi
337 grep -q 'approve grant (also accepted as request)' refused.err
338 hook_as patrick "$filed:$owned_ref"
339 owned_refused stoned-flynn "$retired"
340 hook_as patrick --delete "$owned_ref"
341 owned_at "" "patrick could not delete its own change record, holding the approve grant"
342done
343
344# The worked registry again, for what follows.
345serve instance "$registry"
346compile --now 2026-08-09 || exit 1
347
348# Level-triggered: compiling again converges on the same bytes and touches
349# nothing.
350compile --now 2026-08-09 2> second.log || exit 1
351if ! grep -q "unchanged" second.log; then
352 echo "identity-e2e: a second compilation of an unchanged registry rewrote its artifacts" >&2
353 cat second.log >&2
354 exit 1
355fi
356
357# The registry is read from the integrated tip and from nowhere else, which
358# is the whole of the forcing: an edit on a branch governs nothing.
359git -C work checkout --quiet -b topic
360cp "$expired"/*.cue work/identity/registry.cue
361git -C work commit --quiet -am topic
362git -C work push --quiet "$TMPDIR/instance.git" topic
363compile --now 2026-08-09 || exit 1
364diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
365
366# The compiled verifier line is the one attest writes. The published keys
367# above pin the format against a fixed vector; this pins it against the
368# implementation that reads it, over a key generated here.
369ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/scratch"
370mkdir -p scratch-registry
371{
372 echo 'package identity'
373 echo 'boundaries: "registry": kind: "registry"'
374 echo 'genesis: "someone"'
375 echo 'principals: "someone": {'
376 echo ' kind: "human"'
377 echo ' keys: [{'
378 echo ' class: "ssh-ed25519"'
379 echo ' bound: "hardware"'
380 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/scratch.pub")"
381 echo ' signs: "someone/attestations"'
382 echo ' }]'
383 echo ' grants: govern: boundary: "registry"'
384 echo '}'
385} > scratch-registry/registry.cue
386serve instance scratch-registry
387compile --now 2026-08-09 || exit 1
388attest key --key "$TMPDIR/scratch" --name "someone/attestations" > expected-line
389if ! grep -qxFf expected-line "$TMPDIR/signers"; then
390 echo "identity-e2e: the compiled verifier key is not the line attest writes" >&2
391 cat expected-line "$TMPDIR/signers" >&2
392 exit 1
393fi
394
395# An entry past its expiry leaves both artifacts, says so, and costs the
396# rest of the registry nothing. The day before, it is still there — the
397# declared day is the first day the entry no longer counts.
398serve instance "$expired"
399compile --now 2026-09-30 || exit 1
400grep -q "stoned-flynn/attestations" "$TMPDIR/signers" || {
401 echo "identity-e2e: an unexpired entry was omitted" >&2
402 exit 1
403}
404grep -qx "request stoned-flynn" "$TMPDIR/grants" || {
405 echo "identity-e2e: an unexpired holder of request was omitted from the grants" >&2
406 exit 1
407}
408compile --now 2026-11-01 2> expired.log || exit 1
409if grep -q "stoned-flynn" "$TMPDIR/signers" "$TMPDIR/keys" "$TMPDIR/grants"; then
410 echo "identity-e2e: an expired entry stayed in the compiled artifacts" >&2
411 cat "$TMPDIR/signers" "$TMPDIR/keys" "$TMPDIR/grants" >&2
412 exit 1
413fi
414if ! grep -q "stoned-flynn expired 2026-10-01" expired.log; then
415 echo "identity-e2e: an expired entry was dropped without saying so" >&2
416 cat expired.log >&2
417 exit 1
418fi
419grep -q "patrick" "$TMPDIR/keys" || {
420 echo "identity-e2e: one expired entry took the rest of the registry with it" >&2
421 exit 1
422}
423
424# The fail-safe. An invalid registry must cost the compilation and nothing
425# else: the last good artifacts are what the gates keep reading, because a
426# compiler bug that emptied them would lock the git user out of the host.
427cp "$TMPDIR/signers" last-good-signers
428cp "$TMPDIR/keys" last-good-keys
429cp "$TMPDIR/grants" last-good-grants
430mkdir -p invalid-registry
431cp "$invalid" invalid-registry/registry.cue
432serve instance invalid-registry
433if compile --now 2026-08-09 2> refused.log; then
434 echo "identity-e2e: an invalid registry compiled" >&2
435 exit 1
436fi
437if ! grep -q "externalGovernance" refused.log; then
438 echo "identity-e2e: the refusal did not name the field that failed" >&2
439 cat refused.log >&2
440 exit 1
441fi
442diff -u last-good-signers "$TMPDIR/signers" || exit 1
443diff -u last-good-keys "$TMPDIR/keys" || exit 1
444diff -u last-good-grants "$TMPDIR/grants" || exit 1
445
446# Governance the clock orphans. This registry is valid as a document — the
447# genesis entry governs — and the only entry governing carries an expiry, so
448# whether it compiles depends on the day. The schema cannot see this; the
449# compiler can, and refuses the whole render rather than write artifacts
450# nobody could ever change again.
451mkdir -p orphan-registry
452{
453 echo 'package identity'
454 echo 'boundaries: {'
455 echo ' "push": kind: "git-push"'
456 echo ' "registry": kind: "registry"'
457 echo '}'
458 echo 'genesis: "founder"'
459 echo 'principals: {'
460 echo ' "founder": {'
461 echo ' kind: "human"'
462 echo ' keys: [{'
463 echo ' class: "ssh-ed25519"'
464 echo ' bound: "hardware"'
465 echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGCxVUxXoyFYV40QureqqSMSA17CvK9IrFB33BA6UOip"'
466 echo ' signs: "founder"'
467 echo ' }]'
468 echo ' grants: {'
469 echo ' push: boundary: "push"'
470 echo ' govern: boundary: "registry"'
471 echo ' }'
472 echo ' expires: "2026-10-01"'
473 echo ' }'
474 echo ' "runner": {'
475 echo ' kind: "machine"'
476 echo ' keys: [{'
477 echo ' class: "ssh-ed25519"'
478 echo ' bound: "host"'
479 echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAvpmbUKEDVsejgv2vxWaY/t4xl0JNnjFswb9SxcG9GG"'
480 echo ' }]'
481 echo ' grants: push: boundary: "push"'
482 echo ' expires: "2027-01-01"'
483 echo ' }'
484 echo '}'
485} > orphan-registry/registry.cue
486
487# The day before: an ordinary compilation.
488serve instance orphan-registry
489compile --now 2026-09-30 || exit 1
490grep -q "founder" "$TMPDIR/signers" || {
491 echo "identity-e2e: the governing entry was omitted while still unexpired" >&2
492 exit 1
493}
494cp "$TMPDIR/signers" last-good-signers
495cp "$TMPDIR/keys" last-good-keys
496
497# The day it expires, and a day well past it: the same document, refused. The
498# entry still pushing does not save it — a grant at another boundary is not
499# governance.
500for pinned in 2026-10-01 2026-12-01; do
501 if compile --now "$pinned" 2> orphaned.log; then
502 echo "identity-e2e: a registry nobody governs on $pinned compiled" >&2
503 exit 1
504 fi
505 if ! grep -q "governanceOrphaned" orphaned.log; then
506 echo "identity-e2e: the refusal on $pinned did not name governanceOrphaned" >&2
507 cat orphaned.log >&2
508 exit 1
509 fi
510 diff -u last-good-signers "$TMPDIR/signers" || exit 1
511 diff -u last-good-keys "$TMPDIR/keys" || exit 1
512done
513
514# A key the host declares by hand under one tag, and the registry
515# authorizes as another principal, is refused: sshd reads the declared
516# entry first, so the registry's principal would never be the one pushing.
517serve instance "$registry"
518compile --now 2026-08-09 || exit 1
519cp "$TMPDIR/keys" last-good-keys
520patrick_key="$(grep -m1 -o 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGCxVUxXoyFYV40QureqqSMSA17CvK9IrFB33BA6UOip' "$registry/registry.cue")"
521for declared in "$patrick_key operator@laptop" "environment=\"VALLEY_PRINCIPAL=someone\" $patrick_key"; do
522 printf '%s\n' "$declared" > declared-keys
523 if compile --now 2026-08-09 --declared-keys declared-keys 2> declared.err; then
524 echo "identity-e2e: a registry key declared by hand under another tag compiled" >&2
525 exit 1
526 fi
527 grep -q 'declared on this host' declared.err || {
528 echo "identity-e2e: the refusal did not name the declared entry" >&2
529 cat declared.err >&2
530 exit 1
531 }
532 diff -u last-good-keys "$TMPDIR/keys" || exit 1
533done
534printf 'environment="VALLEY_PRINCIPAL=patrick" %s\n' "$patrick_key" > declared-keys
535compile --now 2026-08-09 --declared-keys declared-keys || exit 1
536
537# Attestation names. Under a registry with two signers, the hook checks
538# each pushed attestation's signature against the compiled keys, so a name
539# in the create-only namespace can only be taken by the evidence it is
540# named for. The pusher is never the question: evidence is relayed.
541for who in victim attacker stranger; do
542 ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$TMPDIR/$who"
543done
544mkdir -p signers-registry
545{
546 echo 'package identity'
547 echo 'boundaries: {'
548 echo ' "push": kind: "git-push"'
549 echo ' "registry": kind: "registry"'
550 echo '}'
551 echo 'genesis: "victim"'
552 for who in victim attacker; do
553 printf 'principals: "%s": {\n' "$who"
554 echo ' kind: "human"'
555 echo ' keys: [{'
556 echo ' class: "ssh-ed25519"'
557 echo ' bound: "hardware"'
558 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/$who.pub")"
559 printf '\t\tsigns: "%s/attestations"\n' "$who"
560 echo ' }]'
561 if [ "$who" = victim ]; then
562 echo ' grants: {push: boundary: "push", govern: boundary: "registry"}'
563 else
564 echo ' grants: push: boundary: "push"'
565 fi
566 echo '}'
567 done
568} > signers-registry/registry.cue
569serve instance signers-registry
570compile --now 2026-08-09 || exit 1
571
572git init --quiet --bare attested.git
573ln -s "$TMPDIR/hook" attested.git/hooks/pre-receive
574git init --quiet occ
575git -C occ commit --quiet --allow-empty -m "a tree to attest"
576attestation() {
577 attest run --repo occ --key "$TMPDIR/$1" --name "$1/attestations" --command ok=true |
578 sed -n 's/^stored \(refs[^ ]*\) -> .*/\1/p'
579}
580mine="$(attestation attacker)"
581theirs="$(attestation victim)"
582unknown="$(attestation stranger)"
583digest="$(echo "$mine" | cut -d/ -f4)"
584victim_hash="$(echo "$theirs" | cut -d/ -f5)"
585squat="refs/the-valley/attestations/$digest/$victim_hash"
586push_as() { VALLEY_PRINCIPAL="$1" git -C occ push --quiet "$TMPDIR/attested.git" "$2"; }
587must_refuse() {
588 if push_as attacker "$1" 2> occupied.err; then
589 echo "identity-e2e: $2 took an attestation name" >&2
590 exit 1
591 fi
592 grep -qF -- "$3" occupied.err || {
593 echo "identity-e2e: the refusal of $2 did not say \"$3\"" >&2
594 cat occupied.err >&2
595 exit 1
596 }
597}
598# The attacker's own evidence, under its own name: accepted.
599push_as attacker "$mine:$mine"
600# The attacker's note, filed under the victim's key hash.
601must_refuse "$mine:$squat" "the attacker's note under the victim's name" \
602 "no signature under the key hash $victim_hash by a key this host accepts"
603# A note whose signature line claims the victim's key and carries the
604# attacker's signature.
605git -C occ cat-file blob "$mine:ok/statement.note" > mine.note
606sig="$(sed -n 's/^— attacker\/attestations //p' mine.note)"
607forged_sig="$( { printf "$(echo "$victim_hash" | sed 's/../\\x&/g')"; printf '%s' "$sig" | base64 -d | tail -c 64; } | base64 -w0)"
608sed "s|^— attacker/attestations .*|— victim/attestations $forged_sig|" mine.note > forged.note
609blob="$(git -C occ hash-object -w "$TMPDIR/forged.note")"
610sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C occ mktree)"
611top="$(printf '040000 tree %s\tok\n' "$sub" | git -C occ mktree)"
612must_refuse "$top:$squat" "a forged signature" "does not check out"
613# A key the registry does not hold, under its own name.
614must_refuse "$unknown:$unknown" "an unknown signer's note" "does not open"
615# The victim's genuine note, with a line appended that no verifier reads.
616# Its signature still verifies, and the note is still one no verifier
617# opens, so it may not take the victim's name.
618git -C occ cat-file blob "$theirs:ok/statement.note" > theirs.note
619{ cat theirs.note; printf '— x\t AAAAAAA=\n'; } > poisoned.note
620blob="$(git -C occ hash-object -w "$TMPDIR/poisoned.note")"
621sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C occ mktree)"
622top="$(printf '040000 tree %s\tok\n' "$sub" | git -C occ mktree)"
623must_refuse "$top:$theirs" "a poisoned relay of the victim's note" "does not open"
624# And the victim's own evidence, relayed by the attacker, takes the name
625# that none of the above could.
626push_as attacker "$theirs:$theirs"
627git -C attested.git rev-parse --verify --quiet "$theirs" > /dev/null || {
628 echo "identity-e2e: relayed evidence could not take its own name" >&2
629 exit 1
630}
631
632# The restack grant. A principal holding a grant at a boundary of kind
633# restack is compiled into the grants as "restack <principal>", and the
634# hook a host renders then lets it add a version to another principal's
635# ready change and write that change's restack report. The same write from
636# a principal the registry grants push alone is refused.
637ssh-keygen -q -t ed25519 -N "" -C restacker -f "$TMPDIR/restacker"
638mkdir -p restack-registry
639{
640 echo 'package identity'
641 echo 'boundaries: {'
642 echo ' "push": kind: "git-push"'
643 echo ' "registry": kind: "registry"'
644 echo ' "restack": kind: "restack"'
645 echo '}'
646 echo 'genesis: "author"'
647 echo 'principals: "author": {'
648 echo ' kind: "human"'
649 echo ' keys: [{'
650 echo ' class: "ssh-ed25519"'
651 echo ' bound: "hardware"'
652 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/victim.pub")"
653 echo ' }]'
654 echo ' grants: {push: boundary: "push", govern: boundary: "registry"}'
655 echo '}'
656 echo 'principals: "valley-restacker": {'
657 echo ' kind: "service"'
658 echo ' keys: [{'
659 echo ' class: "ssh-ed25519"'
660 echo ' bound: "host"'
661 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/restacker.pub")"
662 echo ' }]'
663 echo ' grants: {push: boundary: "push", restack: boundary: "restack"}'
664 echo ' expires: "2027-01-01"'
665 echo '}'
666} > restack-registry/registry.cue
667serve instance restack-registry
668compile --now 2026-08-09 || exit 1
669if [ "$(grep -v '^#' "$TMPDIR/grants")" != "restack valley-restacker" ]; then
670 echo "identity-e2e: the restack grant did not compile into the one line it holds" >&2
671 cat "$TMPDIR/grants" >&2
672 exit 1
673fi
674
675git init --quiet --bare restacked.git
676ln -s "$TMPDIR/hook" restacked.git/hooks/pre-receive
677git init --quiet rs
678git -C rs commit --quiet --allow-empty -m "the change"
679h1="$(git -C rs rev-parse HEAD)"
680h2="$(git -C rs commit-tree "HEAD^{tree}" -p HEAD -m "the change, restacked")"
681change_id=nxvpwlkmqtsrzyoukklnpmqrstuvwxyz
682version() { printf '{"n": %s, "head": "git-sha1:%s", "title": "the change", "branch": "topic", "uploader": "%s"}' "$@"; }
683# one_file <file> <json> <parent…>: a commit whose tree holds the JSON as
684# the file.
685one_file() {
686 local file="$1" json="$2" blob tree parent
687 local -a parents=()
688 shift 2
689 for parent in "$@"; do parents+=(-p "$parent"); done
690 blob="$(printf '%s' "$json" | git -C rs hash-object -w --stdin)"
691 tree="$(printf '100644 blob %s\t%s\n' "$blob" "$file" | git -C rs mktree)"
692 git -C rs commit-tree "$tree" "${parents[@]}" -m "$file"
693}
694rpush() { VALLEY_PRINCIPAL="$1" git -C rs push --quiet "$TMPDIR/restacked.git" "$2"; }
695record_ref="refs/the-valley/changes/main/$change_id"
696created="$(one_file record.json "{\"schema\": \"valley-change-record/v1\", \"change_id\": \"$change_id\", \"target\": \"main\", \"owner\": \"author\", \"state\": \"ready\", \"seq\": 1, \"versions\": [$(version 1 "$h1" author)], \"active\": 1}" "$h1")"
697rpush author "$created:$record_ref"
698appended="$(one_file record.json "{\"schema\": \"valley-change-record/v1\", \"change_id\": \"$change_id\", \"target\": \"main\", \"owner\": \"author\", \"state\": \"ready\", \"seq\": 2, \"versions\": [$(version 1 "$h1" author), $(version 2 "$h2" valley-restacker)], \"active\": 2}" "$created" "$h2")"
699if rpush stoned-flynn "$appended:$record_ref" 2> restack.err; then
700 echo "identity-e2e: a principal the registry does not grant restack added a version to another's change" >&2
701 exit 1
702fi
703grep -q "only the change's owner, author" restack.err
704rpush valley-restacker "$appended:$record_ref"
705[ "$(git -C restacked.git rev-parse "$record_ref")" = "$appended" ] || {
706 echo "identity-e2e: the principal the registry grants restack could not add a version" >&2
707 exit 1
708}
709report_ref="refs/the-valley/restack/main/$change_id"
710report="$(one_file report.json "{\"schema\": \"valley-restack-report/v1\", \"change_id\": \"$change_id\", \"target\": \"main\", \"target_tip\": \"git-sha1:$h1\", \"operation\": \"op-1\", \"result\": \"complete\", \"layers\": [{\"change_id\": \"$change_id\", \"record\": \"git-sha1:$created\", \"version\": 1, \"head\": \"git-sha1:$h1\", \"branch\": \"topic\", \"branch_tip\": \"git-sha1:$h1\", \"published\": {\"head\": \"git-sha1:$h2\", \"version\": 2, \"record\": \"git-sha1:$appended\"}}], \"failing_layer\": null, \"conflict_paths\": [], \"blocking_requests\": [], \"refusal\": null}")"
711if rpush author "$report:$report_ref" 2> report.err; then
712 echo "identity-e2e: a principal the registry does not grant restack wrote a restack report" >&2
713 exit 1
714fi
715grep -q 'restack grant' report.err
716rpush valley-restacker "$report:$report_ref"
717[ "$(git -C restacked.git rev-parse "$report_ref")" = "$report" ] || {
718 echo "identity-e2e: the principal the registry grants restack could not write a restack report" >&2
719 exit 1
720}
721
722touch "$out"