gunk-dev/the-valley

identity-e2e.sh

source6021d207164dmain's published tip history
24.8 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export GIT_CONFIG_NOSYSTEM=1
6git config --global user.name valley-check
7git config --global user.email valley-check@localhost
8git config --global init.defaultBranch main
9cd "$TMPDIR" || exit 1
10
11# A bare instance repository, holding one registry at its tip — what a host
12# serves and what the compiler reads.
13serve() {
14 local name="$1" source="$2"
15 rm -rf work "$name.git"
16 git init --quiet --bare "$name.git"
17 git init --quiet work
18 mkdir -p work/identity
19 cp "$source"/*.cue work/identity/
20 chmod u+w work/identity/*.cue
21 git -C work add -A
22 git -C work commit --quiet -m "$name"
23 git -C work push --quiet "$TMPDIR/$name.git" main
24}
25
26compile() {
27 identity compile --repo "$TMPDIR/instance.git" \
28 --known-signers "$TMPDIR/signers" --authorized-keys "$TMPDIR/keys" \
29 --grants "$TMPDIR/grants" "$@"
30}
31
32# The worked registry compiles to exactly the artifacts checked in beside
33# it. This is the whole claim of the compiler, and the artifacts it replaces
34# were written by hand — so the comparison is byte for byte.
35serve instance "$registry"
36compile --now 2026-08-09 2> first.log || exit 1
37diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
38diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1
39diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1
40
41# A replacement ref planted in the instance repository changes nothing the
42# compiler reads. refs/replace/<blob> makes git read another object wherever
43# it looks that blob up, so a reader that leaves replacement refs on would
44# compile a registry nobody landed — here, one granting a key push and
45# request — while main stays exactly where it was.
46ssh-keygen -q -t ed25519 -N "" -C intruder -f "$TMPDIR/intruder"
47cp "$registry/registry.cue" forged.cue
48chmod u+w forged.cue
49{
50 echo
51 echo 'principals: "intruder": {'
52 echo ' kind: "human"'
53 echo ' keys: [{'
54 echo ' class: "ssh-ed25519"'
55 echo ' bound: "hardware"'
56 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/intruder.pub")"
57 echo ' }]'
58 echo ' grants: {'
59 echo ' push: boundary: "classic-laddie-push"'
60 echo ' request: boundary: "classic-laddie-request"'
61 echo ' }'
62 echo '}'
63} >> forged.cue
64landed="$(git -C instance.git rev-parse main:identity/registry.cue)"
65forged="$(git -C instance.git hash-object -w "$TMPDIR/forged.cue")"
66git -C instance.git update-ref "refs/replace/$landed" "$forged"
67if ! git -C instance.git show main:identity/registry.cue | grep intruder > /dev/null; then
68 echo "identity-e2e: the planted replacement did not take, so the check below proves nothing" >&2
69 exit 1
70fi
71compile --now 2026-08-09 || exit 1
72diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
73diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1
74diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1
75if grep -q intruder "$TMPDIR/keys" "$TMPDIR/grants"; then
76 echo "identity-e2e: a replacement ref put a principal nobody landed into the compiled artifacts" >&2
77 exit 1
78fi
79# The same holds when the caller's environment asks for replacement refs.
80GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" compile --now 2026-08-09 || exit 1
81diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1
82git -C instance.git update-ref -d "refs/replace/$landed"
83
84# 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. Each holder
167# files a request, replaces it with a head that does not descend from it,
168# and deletes it, and stoned-flynn can do none of that in between.
169cat > "$TMPDIR/grants" << 'EOF'
170approve approver
171request requester
172approve both
173request both
174EOF
175for holder in approver requester both; do
176 hook_as "$holder" "main:$request"
177 git -C work commit --quiet --allow-empty -m "replaced by $holder"
178 replacement="$(git -C work rev-parse HEAD)"
179 git -C work reset --quiet --hard HEAD~1
180 hook_as "$holder" --force "$replacement:$request"
181 [ "$(git -C guarded.git rev-parse "$request")" = "$replacement" ] || {
182 echo "identity-e2e: $holder could not replace its request" >&2
183 exit 1
184 }
185 for refused in "main:$request" "--delete $request"; do
186 # shellcheck disable=SC2086 # the push arguments are two words, split on purpose
187 if hook_as stoned-flynn --force $refused 2> refused.err; then
188 echo "identity-e2e: a principal holding neither name of the approve grant wrote $holder's request" >&2
189 exit 1
190 fi
191 grep -q 'approve grant (also accepted as request)' refused.err
192 done
193 hook_as "$holder" --delete "$request"
194 if git -C guarded.git rev-parse --verify --quiet "$request" > /dev/null; then
195 echo "identity-e2e: $holder could not delete its request" >&2
196 exit 1
197 fi
198done
199
200# The worked registry again, for what follows.
201serve instance "$registry"
202compile --now 2026-08-09 || exit 1
203
204# Level-triggered: compiling again converges on the same bytes and touches
205# nothing.
206compile --now 2026-08-09 2> second.log || exit 1
207if ! grep -q "unchanged" second.log; then
208 echo "identity-e2e: a second compilation of an unchanged registry rewrote its artifacts" >&2
209 cat second.log >&2
210 exit 1
211fi
212
213# The registry is read from the integrated tip and from nowhere else, which
214# is the whole of the forcing: an edit on a branch governs nothing.
215git -C work checkout --quiet -b topic
216cp "$expired"/*.cue work/identity/registry.cue
217git -C work commit --quiet -am topic
218git -C work push --quiet "$TMPDIR/instance.git" topic
219compile --now 2026-08-09 || exit 1
220diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
221
222# The compiled verifier line is the one attest writes. The published keys
223# above pin the format against a fixed vector; this pins it against the
224# implementation that reads it, over a key generated here.
225ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/scratch"
226mkdir -p scratch-registry
227{
228 echo 'package identity'
229 echo 'boundaries: "registry": kind: "registry"'
230 echo 'genesis: "someone"'
231 echo 'principals: "someone": {'
232 echo ' kind: "human"'
233 echo ' keys: [{'
234 echo ' class: "ssh-ed25519"'
235 echo ' bound: "hardware"'
236 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/scratch.pub")"
237 echo ' signs: "someone/attestations"'
238 echo ' }]'
239 echo ' grants: govern: boundary: "registry"'
240 echo '}'
241} > scratch-registry/registry.cue
242serve instance scratch-registry
243compile --now 2026-08-09 || exit 1
244attest key --key "$TMPDIR/scratch" --name "someone/attestations" > expected-line
245if ! grep -qxFf expected-line "$TMPDIR/signers"; then
246 echo "identity-e2e: the compiled verifier key is not the line attest writes" >&2
247 cat expected-line "$TMPDIR/signers" >&2
248 exit 1
249fi
250
251# An entry past its expiry leaves both artifacts, says so, and costs the
252# rest of the registry nothing. The day before, it is still there — the
253# declared day is the first day the entry no longer counts.
254serve instance "$expired"
255compile --now 2026-09-30 || exit 1
256grep -q "stoned-flynn/attestations" "$TMPDIR/signers" || {
257 echo "identity-e2e: an unexpired entry was omitted" >&2
258 exit 1
259}
260grep -qx "request stoned-flynn" "$TMPDIR/grants" || {
261 echo "identity-e2e: an unexpired holder of request was omitted from the grants" >&2
262 exit 1
263}
264compile --now 2026-11-01 2> expired.log || exit 1
265if grep -q "stoned-flynn" "$TMPDIR/signers" "$TMPDIR/keys" "$TMPDIR/grants"; then
266 echo "identity-e2e: an expired entry stayed in the compiled artifacts" >&2
267 cat "$TMPDIR/signers" "$TMPDIR/keys" "$TMPDIR/grants" >&2
268 exit 1
269fi
270if ! grep -q "stoned-flynn expired 2026-10-01" expired.log; then
271 echo "identity-e2e: an expired entry was dropped without saying so" >&2
272 cat expired.log >&2
273 exit 1
274fi
275grep -q "patrick" "$TMPDIR/keys" || {
276 echo "identity-e2e: one expired entry took the rest of the registry with it" >&2
277 exit 1
278}
279
280# The fail-safe. An invalid registry must cost the compilation and nothing
281# else: the last good artifacts are what the gates keep reading, because a
282# compiler bug that emptied them would lock the git user out of the host.
283cp "$TMPDIR/signers" last-good-signers
284cp "$TMPDIR/keys" last-good-keys
285cp "$TMPDIR/grants" last-good-grants
286mkdir -p invalid-registry
287cp "$invalid" invalid-registry/registry.cue
288serve instance invalid-registry
289if compile --now 2026-08-09 2> refused.log; then
290 echo "identity-e2e: an invalid registry compiled" >&2
291 exit 1
292fi
293if ! grep -q "externalGovernance" refused.log; then
294 echo "identity-e2e: the refusal did not name the field that failed" >&2
295 cat refused.log >&2
296 exit 1
297fi
298diff -u last-good-signers "$TMPDIR/signers" || exit 1
299diff -u last-good-keys "$TMPDIR/keys" || exit 1
300diff -u last-good-grants "$TMPDIR/grants" || exit 1
301
302# Governance the clock orphans. This registry is valid as a document — the
303# genesis entry governs — and the only entry governing carries an expiry, so
304# whether it compiles depends on the day. The schema cannot see this; the
305# compiler can, and refuses the whole render rather than write artifacts
306# nobody could ever change again.
307mkdir -p orphan-registry
308{
309 echo 'package identity'
310 echo 'boundaries: {'
311 echo ' "push": kind: "git-push"'
312 echo ' "registry": kind: "registry"'
313 echo '}'
314 echo 'genesis: "founder"'
315 echo 'principals: {'
316 echo ' "founder": {'
317 echo ' kind: "human"'
318 echo ' keys: [{'
319 echo ' class: "ssh-ed25519"'
320 echo ' bound: "hardware"'
321 echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGCxVUxXoyFYV40QureqqSMSA17CvK9IrFB33BA6UOip"'
322 echo ' signs: "founder"'
323 echo ' }]'
324 echo ' grants: {'
325 echo ' push: boundary: "push"'
326 echo ' govern: boundary: "registry"'
327 echo ' }'
328 echo ' expires: "2026-10-01"'
329 echo ' }'
330 echo ' "runner": {'
331 echo ' kind: "machine"'
332 echo ' keys: [{'
333 echo ' class: "ssh-ed25519"'
334 echo ' bound: "host"'
335 echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAvpmbUKEDVsejgv2vxWaY/t4xl0JNnjFswb9SxcG9GG"'
336 echo ' }]'
337 echo ' grants: push: boundary: "push"'
338 echo ' expires: "2027-01-01"'
339 echo ' }'
340 echo '}'
341} > orphan-registry/registry.cue
342
343# The day before: an ordinary compilation.
344serve instance orphan-registry
345compile --now 2026-09-30 || exit 1
346grep -q "founder" "$TMPDIR/signers" || {
347 echo "identity-e2e: the governing entry was omitted while still unexpired" >&2
348 exit 1
349}
350cp "$TMPDIR/signers" last-good-signers
351cp "$TMPDIR/keys" last-good-keys
352
353# The day it expires, and a day well past it: the same document, refused. The
354# entry still pushing does not save it — a grant at another boundary is not
355# governance.
356for pinned in 2026-10-01 2026-12-01; do
357 if compile --now "$pinned" 2> orphaned.log; then
358 echo "identity-e2e: a registry nobody governs on $pinned compiled" >&2
359 exit 1
360 fi
361 if ! grep -q "governanceOrphaned" orphaned.log; then
362 echo "identity-e2e: the refusal on $pinned did not name governanceOrphaned" >&2
363 cat orphaned.log >&2
364 exit 1
365 fi
366 diff -u last-good-signers "$TMPDIR/signers" || exit 1
367 diff -u last-good-keys "$TMPDIR/keys" || exit 1
368done
369
370# A key the host declares by hand under one tag, and the registry
371# authorizes as another principal, is refused: sshd reads the declared
372# entry first, so the registry's principal would never be the one pushing.
373serve instance "$registry"
374compile --now 2026-08-09 || exit 1
375cp "$TMPDIR/keys" last-good-keys
376patrick_key="$(grep -m1 -o 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGCxVUxXoyFYV40QureqqSMSA17CvK9IrFB33BA6UOip' "$registry/registry.cue")"
377for declared in "$patrick_key operator@laptop" "environment=\"VALLEY_PRINCIPAL=someone\" $patrick_key"; do
378 printf '%s\n' "$declared" > declared-keys
379 if compile --now 2026-08-09 --declared-keys declared-keys 2> declared.err; then
380 echo "identity-e2e: a registry key declared by hand under another tag compiled" >&2
381 exit 1
382 fi
383 grep -q 'declared on this host' declared.err || {
384 echo "identity-e2e: the refusal did not name the declared entry" >&2
385 cat declared.err >&2
386 exit 1
387 }
388 diff -u last-good-keys "$TMPDIR/keys" || exit 1
389done
390printf 'environment="VALLEY_PRINCIPAL=patrick" %s\n' "$patrick_key" > declared-keys
391compile --now 2026-08-09 --declared-keys declared-keys || exit 1
392
393# Attestation names. Under a registry with two signers, the hook checks
394# each pushed attestation's signature against the compiled keys, so a name
395# in the create-only namespace can only be taken by the evidence it is
396# named for. The pusher is never the question: evidence is relayed.
397for who in victim attacker stranger; do
398 ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$TMPDIR/$who"
399done
400mkdir -p signers-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: "victim"'
408 for who in victim attacker; do
409 printf 'principals: "%s": {\n' "$who"
410 echo ' kind: "human"'
411 echo ' keys: [{'
412 echo ' class: "ssh-ed25519"'
413 echo ' bound: "hardware"'
414 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/$who.pub")"
415 printf '\t\tsigns: "%s/attestations"\n' "$who"
416 echo ' }]'
417 if [ "$who" = victim ]; then
418 echo ' grants: {push: boundary: "push", govern: boundary: "registry"}'
419 else
420 echo ' grants: push: boundary: "push"'
421 fi
422 echo '}'
423 done
424} > signers-registry/registry.cue
425serve instance signers-registry
426compile --now 2026-08-09 || exit 1
427
428git init --quiet --bare attested.git
429ln -s "$TMPDIR/hook" attested.git/hooks/pre-receive
430git init --quiet occ
431git -C occ commit --quiet --allow-empty -m "a tree to attest"
432attestation() {
433 attest run --repo occ --key "$TMPDIR/$1" --name "$1/attestations" --command ok=true |
434 sed -n 's/^stored \(refs[^ ]*\) -> .*/\1/p'
435}
436mine="$(attestation attacker)"
437theirs="$(attestation victim)"
438unknown="$(attestation stranger)"
439digest="$(echo "$mine" | cut -d/ -f4)"
440victim_hash="$(echo "$theirs" | cut -d/ -f5)"
441squat="refs/the-valley/attestations/$digest/$victim_hash"
442push_as() { VALLEY_PRINCIPAL="$1" git -C occ push --quiet "$TMPDIR/attested.git" "$2"; }
443must_refuse() {
444 if push_as attacker "$1" 2> occupied.err; then
445 echo "identity-e2e: $2 took an attestation name" >&2
446 exit 1
447 fi
448 grep -qF -- "$3" occupied.err || {
449 echo "identity-e2e: the refusal of $2 did not say \"$3\"" >&2
450 cat occupied.err >&2
451 exit 1
452 }
453}
454# The attacker's own evidence, under its own name: accepted.
455push_as attacker "$mine:$mine"
456# The attacker's note, filed under the victim's key hash.
457must_refuse "$mine:$squat" "the attacker's note under the victim's name" \
458 "no signature under the key hash $victim_hash by a key this host accepts"
459# A note whose signature line claims the victim's key and carries the
460# attacker's signature.
461git -C occ cat-file blob "$mine:ok/statement.note" > mine.note
462sig="$(sed -n 's/^— attacker\/attestations //p' mine.note)"
463forged_sig="$( { printf "$(echo "$victim_hash" | sed 's/../\\x&/g')"; printf '%s' "$sig" | base64 -d | tail -c 64; } | base64 -w0)"
464sed "s|^— attacker/attestations .*|— victim/attestations $forged_sig|" mine.note > forged.note
465blob="$(git -C occ hash-object -w "$TMPDIR/forged.note")"
466sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C occ mktree)"
467top="$(printf '040000 tree %s\tok\n' "$sub" | git -C occ mktree)"
468must_refuse "$top:$squat" "a forged signature" "does not check out"
469# A key the registry does not hold, under its own name.
470must_refuse "$unknown:$unknown" "an unknown signer's note" "does not open"
471# The victim's genuine note, with a line appended that no verifier reads.
472# Its signature still verifies, and the note is still one no verifier
473# opens, so it may not take the victim's name.
474git -C occ cat-file blob "$theirs:ok/statement.note" > theirs.note
475{ cat theirs.note; printf '— x\t AAAAAAA=\n'; } > poisoned.note
476blob="$(git -C occ hash-object -w "$TMPDIR/poisoned.note")"
477sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C occ mktree)"
478top="$(printf '040000 tree %s\tok\n' "$sub" | git -C occ mktree)"
479must_refuse "$top:$theirs" "a poisoned relay of the victim's note" "does not open"
480# And the victim's own evidence, relayed by the attacker, takes the name
481# that none of the above could.
482push_as attacker "$theirs:$theirs"
483git -C attested.git rev-parse --verify --quiet "$theirs" > /dev/null || {
484 echo "identity-e2e: relayed evidence could not take its own name" >&2
485 exit 1
486}
487
488# The restack grant. A principal holding a grant at a boundary of kind
489# restack is compiled into the grants as "restack <principal>", and the
490# hook a host renders then lets it add a version to another principal's
491# ready change and write that change's restack report. The same write from
492# a principal the registry grants push alone is refused.
493ssh-keygen -q -t ed25519 -N "" -C restacker -f "$TMPDIR/restacker"
494mkdir -p restack-registry
495{
496 echo 'package identity'
497 echo 'boundaries: {'
498 echo ' "push": kind: "git-push"'
499 echo ' "registry": kind: "registry"'
500 echo ' "restack": kind: "restack"'
501 echo '}'
502 echo 'genesis: "author"'
503 echo 'principals: "author": {'
504 echo ' kind: "human"'
505 echo ' keys: [{'
506 echo ' class: "ssh-ed25519"'
507 echo ' bound: "hardware"'
508 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/victim.pub")"
509 echo ' }]'
510 echo ' grants: {push: boundary: "push", govern: boundary: "registry"}'
511 echo '}'
512 echo 'principals: "valley-restacker": {'
513 echo ' kind: "service"'
514 echo ' keys: [{'
515 echo ' class: "ssh-ed25519"'
516 echo ' bound: "host"'
517 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/restacker.pub")"
518 echo ' }]'
519 echo ' grants: {push: boundary: "push", restack: boundary: "restack"}'
520 echo ' expires: "2027-01-01"'
521 echo '}'
522} > restack-registry/registry.cue
523serve instance restack-registry
524compile --now 2026-08-09 || exit 1
525if [ "$(grep -v '^#' "$TMPDIR/grants")" != "restack valley-restacker" ]; then
526 echo "identity-e2e: the restack grant did not compile into the one line it holds" >&2
527 cat "$TMPDIR/grants" >&2
528 exit 1
529fi
530
531git init --quiet --bare restacked.git
532ln -s "$TMPDIR/hook" restacked.git/hooks/pre-receive
533git init --quiet rs
534git -C rs commit --quiet --allow-empty -m "the change"
535h1="$(git -C rs rev-parse HEAD)"
536h2="$(git -C rs commit-tree "HEAD^{tree}" -p HEAD -m "the change, restacked")"
537change_id=nxvpwlkmqtsrzyoukklnpmqrstuvwxyz
538version() { printf '{"n": %s, "head": "git-sha1:%s", "title": "the change", "branch": "topic", "uploader": "%s"}' "$@"; }
539# one_file <file> <json> <parent…>: a commit whose tree holds the JSON as
540# the file.
541one_file() {
542 local file="$1" json="$2" blob tree parent
543 local -a parents=()
544 shift 2
545 for parent in "$@"; do parents+=(-p "$parent"); done
546 blob="$(printf '%s' "$json" | git -C rs hash-object -w --stdin)"
547 tree="$(printf '100644 blob %s\t%s\n' "$blob" "$file" | git -C rs mktree)"
548 git -C rs commit-tree "$tree" "${parents[@]}" -m "$file"
549}
550rpush() { VALLEY_PRINCIPAL="$1" git -C rs push --quiet "$TMPDIR/restacked.git" "$2"; }
551record_ref="refs/the-valley/changes/main/$change_id"
552created="$(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")"
553rpush author "$created:$record_ref"
554appended="$(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")"
555if rpush stoned-flynn "$appended:$record_ref" 2> restack.err; then
556 echo "identity-e2e: a principal the registry does not grant restack added a version to another's change" >&2
557 exit 1
558fi
559grep -q "only the change's owner, author" restack.err
560rpush valley-restacker "$appended:$record_ref"
561[ "$(git -C restacked.git rev-parse "$record_ref")" = "$appended" ] || {
562 echo "identity-e2e: the principal the registry grants restack could not add a version" >&2
563 exit 1
564}
565report_ref="refs/the-valley/restack/main/$change_id"
566report="$(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}")"
567if rpush author "$report:$report_ref" 2> report.err; then
568 echo "identity-e2e: a principal the registry does not grant restack wrote a restack report" >&2
569 exit 1
570fi
571grep -q 'restack grant' report.err
572rpush valley-restacker "$report:$report_ref"
573[ "$(git -C restacked.git rev-parse "$report_ref")" = "$report" ] || {
574 echo "identity-e2e: the principal the registry grants restack could not write a restack report" >&2
575 exit 1
576}
577
578touch "$out"