gunk-dev/the-valley

identity-e2e.sh

sourced9767a27382dmain's published tip history
7.1 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}
30
31# The worked registry compiles to exactly the artifacts checked in beside
32# it. This is the whole claim of the compiler, and the artifacts it replaces
33# were written by hand — so the comparison is byte for byte.
34serve instance "$registry"
35compile --now 2026-08-09 2> first.log || exit 1
36diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
37diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1
38
39# Level-triggered: compiling again converges on the same bytes and touches
40# nothing.
41compile --now 2026-08-09 2> second.log || exit 1
42if ! grep -q "unchanged" second.log; then
43 echo "identity-e2e: a second compilation of an unchanged registry rewrote its artifacts" >&2
44 cat second.log >&2
45 exit 1
46fi
47
48# The registry is read from the integrated tip and from nowhere else, which
49# is the whole of the forcing: an edit on a branch governs nothing.
50git -C work checkout --quiet -b topic
51cp "$expired"/*.cue work/identity/registry.cue
52git -C work commit --quiet -am topic
53git -C work push --quiet "$TMPDIR/instance.git" topic
54compile --now 2026-08-09 || exit 1
55diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
56
57# The compiled verifier line is the one attest writes. The published keys
58# above pin the format against a fixed vector; this pins it against the
59# implementation that reads it, over a key generated here.
60ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/scratch"
61mkdir -p scratch-registry
62{
63 echo 'package identity'
64 echo 'boundaries: "registry": kind: "registry"'
65 echo 'genesis: "someone"'
66 echo 'principals: "someone": {'
67 echo ' kind: "human"'
68 echo ' keys: [{'
69 echo ' class: "ssh-ed25519"'
70 echo ' bound: "hardware"'
71 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/scratch.pub")"
72 echo ' signs: "someone/attestations"'
73 echo ' }]'
74 echo ' grants: govern: boundary: "registry"'
75 echo '}'
76} > scratch-registry/registry.cue
77serve instance scratch-registry
78compile --now 2026-08-09 || exit 1
79attest key --key "$TMPDIR/scratch" --name "someone/attestations" > expected-line
80if ! grep -qxFf expected-line "$TMPDIR/signers"; then
81 echo "identity-e2e: the compiled verifier key is not the line attest writes" >&2
82 cat expected-line "$TMPDIR/signers" >&2
83 exit 1
84fi
85
86# An entry past its expiry leaves both artifacts, says so, and costs the
87# rest of the registry nothing. The day before, it is still there — the
88# declared day is the first day the entry no longer counts.
89serve instance "$expired"
90compile --now 2026-09-30 || exit 1
91grep -q "stoned-flynn/attestations" "$TMPDIR/signers" || {
92 echo "identity-e2e: an unexpired entry was omitted" >&2
93 exit 1
94}
95compile --now 2026-11-01 2> expired.log || exit 1
96if grep -q "stoned-flynn" "$TMPDIR/signers" "$TMPDIR/keys"; then
97 echo "identity-e2e: an expired entry stayed in the compiled artifacts" >&2
98 cat "$TMPDIR/signers" "$TMPDIR/keys" >&2
99 exit 1
100fi
101if ! grep -q "stoned-flynn expired 2026-10-01" expired.log; then
102 echo "identity-e2e: an expired entry was dropped without saying so" >&2
103 cat expired.log >&2
104 exit 1
105fi
106grep -q "patrick" "$TMPDIR/keys" || {
107 echo "identity-e2e: one expired entry took the rest of the registry with it" >&2
108 exit 1
109}
110
111# The fail-safe. An invalid registry must cost the compilation and nothing
112# else: the last good artifacts are what the gates keep reading, because a
113# compiler bug that emptied them would lock the git user out of the host.
114cp "$TMPDIR/signers" last-good-signers
115cp "$TMPDIR/keys" last-good-keys
116mkdir -p invalid-registry
117cp "$invalid" invalid-registry/registry.cue
118serve instance invalid-registry
119if compile --now 2026-08-09 2> refused.log; then
120 echo "identity-e2e: an invalid registry compiled" >&2
121 exit 1
122fi
123if ! grep -q "externalGovernance" refused.log; then
124 echo "identity-e2e: the refusal did not name the field that failed" >&2
125 cat refused.log >&2
126 exit 1
127fi
128diff -u last-good-signers "$TMPDIR/signers" || exit 1
129diff -u last-good-keys "$TMPDIR/keys" || exit 1
130
131# Governance the clock orphans. This registry is valid as a document — the
132# genesis entry governs — and the only entry governing carries an expiry, so
133# whether it compiles depends on the day. The schema cannot see this; the
134# compiler can, and refuses the whole render rather than write artifacts
135# nobody could ever change again.
136mkdir -p orphan-registry
137{
138 echo 'package identity'
139 echo 'boundaries: {'
140 echo ' "push": kind: "git-push"'
141 echo ' "registry": kind: "registry"'
142 echo '}'
143 echo 'genesis: "founder"'
144 echo 'principals: {'
145 echo ' "founder": {'
146 echo ' kind: "human"'
147 echo ' keys: [{'
148 echo ' class: "ssh-ed25519"'
149 echo ' bound: "hardware"'
150 echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGCxVUxXoyFYV40QureqqSMSA17CvK9IrFB33BA6UOip"'
151 echo ' signs: "founder"'
152 echo ' }]'
153 echo ' grants: {'
154 echo ' push: boundary: "push"'
155 echo ' govern: boundary: "registry"'
156 echo ' }'
157 echo ' expires: "2026-10-01"'
158 echo ' }'
159 echo ' "runner": {'
160 echo ' kind: "machine"'
161 echo ' keys: [{'
162 echo ' class: "ssh-ed25519"'
163 echo ' bound: "host"'
164 echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAvpmbUKEDVsejgv2vxWaY/t4xl0JNnjFswb9SxcG9GG"'
165 echo ' }]'
166 echo ' grants: push: boundary: "push"'
167 echo ' expires: "2027-01-01"'
168 echo ' }'
169 echo '}'
170} > orphan-registry/registry.cue
171
172# The day before: an ordinary compilation.
173serve instance orphan-registry
174compile --now 2026-09-30 || exit 1
175grep -q "founder" "$TMPDIR/signers" || {
176 echo "identity-e2e: the governing entry was omitted while still unexpired" >&2
177 exit 1
178}
179cp "$TMPDIR/signers" last-good-signers
180cp "$TMPDIR/keys" last-good-keys
181
182# The day it expires, and a day well past it: the same document, refused. The
183# entry still pushing does not save it — a grant at another boundary is not
184# governance.
185for pinned in 2026-10-01 2026-12-01; do
186 if compile --now "$pinned" 2> orphaned.log; then
187 echo "identity-e2e: a registry nobody governs on $pinned compiled" >&2
188 exit 1
189 fi
190 if ! grep -q "governanceOrphaned" orphaned.log; then
191 echo "identity-e2e: the refusal on $pinned did not name governanceOrphaned" >&2
192 cat orphaned.log >&2
193 exit 1
194 fi
195 diff -u last-good-signers "$TMPDIR/signers" || exit 1
196 diff -u last-good-keys "$TMPDIR/keys" || exit 1
197done
198
199touch "$out"