gunk-dev/the-valley

identity-e2e.sh

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