gunk-dev/the-valley

identity-e2e.sh

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