gunk-dev/the-valley

identity-e2e.sh

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