gunk-dev/the-valley

identity-e2e.sh

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