gunk-dev/the-valley

identity-e2e.sh

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