gunk-dev/the-valley

identity-e2e.sh

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