gunk-dev/the-valley

identity-e2e.sh

sourceef0e6a83672emain's published tip history
10.8 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export GIT_CONFIG_NOSYSTEM=1
6git config --global user.name valley-check
7git config --global user.email valley-check@localhost
8git config --global init.defaultBranch main
9cd "$TMPDIR" || exit 1
10
11# A bare instance repository, holding one registry at its tip — what a host
12# serves and what the compiler reads.
13serve() {
14 local name="$1" source="$2"
15 rm -rf work "$name.git"
16 git init --quiet --bare "$name.git"
17 git init --quiet work
18 mkdir -p work/identity
19 cp "$source"/*.cue work/identity/
20 chmod u+w work/identity/*.cue
21 git -C work add -A
22 git -C work commit --quiet -m "$name"
23 git -C work push --quiet "$TMPDIR/$name.git" main
24}
25
26compile() {
27 identity compile --repo "$TMPDIR/instance.git" \
28 --known-signers "$TMPDIR/signers" --authorized-keys "$TMPDIR/keys" \
29 --grants "$TMPDIR/grants" "$@"
30}
31
32# The worked registry compiles to exactly the artifacts checked in beside
33# it. This is the whole claim of the compiler, and the artifacts it replaces
34# were written by hand — so the comparison is byte for byte.
35serve instance "$registry"
36compile --now 2026-08-09 2> first.log || exit 1
37diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
38diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1
39diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1
40
41# A replacement ref planted in the instance repository changes nothing the
42# compiler reads. refs/replace/<blob> makes git read another object wherever
43# it looks that blob up, so a reader that leaves replacement refs on would
44# compile a registry nobody landed — here, one granting a key push and
45# request — while main stays exactly where it was.
46ssh-keygen -q -t ed25519 -N "" -C intruder -f "$TMPDIR/intruder"
47cp "$registry/registry.cue" forged.cue
48chmod u+w forged.cue
49{
50 echo
51 echo 'principals: "intruder": {'
52 echo ' kind: "human"'
53 echo ' keys: [{'
54 echo ' class: "ssh-ed25519"'
55 echo ' bound: "hardware"'
56 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/intruder.pub")"
57 echo ' }]'
58 echo ' grants: {'
59 echo ' push: boundary: "classic-laddie-push"'
60 echo ' request: boundary: "classic-laddie-request"'
61 echo ' }'
62 echo '}'
63} >> forged.cue
64landed="$(git -C instance.git rev-parse main:identity/registry.cue)"
65forged="$(git -C instance.git hash-object -w "$TMPDIR/forged.cue")"
66git -C instance.git update-ref "refs/replace/$landed" "$forged"
67if ! git -C instance.git show main:identity/registry.cue | grep intruder > /dev/null; then
68 echo "identity-e2e: the planted replacement did not take, so the check below proves nothing" >&2
69 exit 1
70fi
71compile --now 2026-08-09 || exit 1
72diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
73diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1
74diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1
75if grep -q intruder "$TMPDIR/keys" "$TMPDIR/grants"; then
76 echo "identity-e2e: a replacement ref put a principal nobody landed into the compiled artifacts" >&2
77 exit 1
78fi
79# The same holds when the caller's environment asks for replacement refs.
80GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" compile --now 2026-08-09 || exit 1
81diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1
82git -C instance.git update-ref -d "refs/replace/$landed"
83
84# What the hook does with the compiled grants. A bare repository is wired
85# with the pre-receive hook a host compiling this registry renders,
86# followed from its init script; the one edit is the path of the compiled
87# grants file, which a host keeps under /var/lib and this sandbox cannot.
88# Pushing as a principal is pushing with the tag on, the substitution for
89# sshd the hook checks make everywhere.
90rendered="$(grep -o "/nix/store/[^ ]*-valley-protect-guarded" "$identityInitScriptPath" | head -n1)"
91if ! grep -qF -- "--grants /var/lib/valley-identity/grants" "$rendered"; then
92 echo "identity-e2e: the hook of a host compiling the registry does not read the compiled grants" >&2
93 cat "$rendered" >&2
94 exit 1
95fi
96sed "s|/var/lib/valley-identity/grants|$TMPDIR/grants|" "$rendered" > hook
97chmod +x hook
98git init --quiet --bare guarded.git
99ln -s "$TMPDIR/hook" guarded.git/hooks/pre-receive
100request=refs/the-valley/integration-requests/main/topic
101if VALLEY_PRINCIPAL=stoned-flynn git -C work push --quiet "$TMPDIR/guarded.git" "main:$request" 2> refused.err; then
102 echo "identity-e2e: a principal the registry grants push and not request filed a request" >&2
103 exit 1
104fi
105grep -q 'request grant' refused.err
106VALLEY_PRINCIPAL=patrick git -C work push --quiet "$TMPDIR/guarded.git" "main:$request"
107git -C guarded.git rev-parse --verify --quiet "$request" > /dev/null || {
108 echo "identity-e2e: the principal the registry grants request could not file one" >&2
109 exit 1
110}
111
112# Level-triggered: compiling again converges on the same bytes and touches
113# nothing.
114compile --now 2026-08-09 2> second.log || exit 1
115if ! grep -q "unchanged" second.log; then
116 echo "identity-e2e: a second compilation of an unchanged registry rewrote its artifacts" >&2
117 cat second.log >&2
118 exit 1
119fi
120
121# The registry is read from the integrated tip and from nowhere else, which
122# is the whole of the forcing: an edit on a branch governs nothing.
123git -C work checkout --quiet -b topic
124cp "$expired"/*.cue work/identity/registry.cue
125git -C work commit --quiet -am topic
126git -C work push --quiet "$TMPDIR/instance.git" topic
127compile --now 2026-08-09 || exit 1
128diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1
129
130# The compiled verifier line is the one attest writes. The published keys
131# above pin the format against a fixed vector; this pins it against the
132# implementation that reads it, over a key generated here.
133ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/scratch"
134mkdir -p scratch-registry
135{
136 echo 'package identity'
137 echo 'boundaries: "registry": kind: "registry"'
138 echo 'genesis: "someone"'
139 echo 'principals: "someone": {'
140 echo ' kind: "human"'
141 echo ' keys: [{'
142 echo ' class: "ssh-ed25519"'
143 echo ' bound: "hardware"'
144 printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/scratch.pub")"
145 echo ' signs: "someone/attestations"'
146 echo ' }]'
147 echo ' grants: govern: boundary: "registry"'
148 echo '}'
149} > scratch-registry/registry.cue
150serve instance scratch-registry
151compile --now 2026-08-09 || exit 1
152attest key --key "$TMPDIR/scratch" --name "someone/attestations" > expected-line
153if ! grep -qxFf expected-line "$TMPDIR/signers"; then
154 echo "identity-e2e: the compiled verifier key is not the line attest writes" >&2
155 cat expected-line "$TMPDIR/signers" >&2
156 exit 1
157fi
158
159# An entry past its expiry leaves both artifacts, says so, and costs the
160# rest of the registry nothing. The day before, it is still there — the
161# declared day is the first day the entry no longer counts.
162serve instance "$expired"
163compile --now 2026-09-30 || exit 1
164grep -q "stoned-flynn/attestations" "$TMPDIR/signers" || {
165 echo "identity-e2e: an unexpired entry was omitted" >&2
166 exit 1
167}
168grep -qx "request stoned-flynn" "$TMPDIR/grants" || {
169 echo "identity-e2e: an unexpired holder of request was omitted from the grants" >&2
170 exit 1
171}
172compile --now 2026-11-01 2> expired.log || exit 1
173if grep -q "stoned-flynn" "$TMPDIR/signers" "$TMPDIR/keys" "$TMPDIR/grants"; then
174 echo "identity-e2e: an expired entry stayed in the compiled artifacts" >&2
175 cat "$TMPDIR/signers" "$TMPDIR/keys" "$TMPDIR/grants" >&2
176 exit 1
177fi
178if ! grep -q "stoned-flynn expired 2026-10-01" expired.log; then
179 echo "identity-e2e: an expired entry was dropped without saying so" >&2
180 cat expired.log >&2
181 exit 1
182fi
183grep -q "patrick" "$TMPDIR/keys" || {
184 echo "identity-e2e: one expired entry took the rest of the registry with it" >&2
185 exit 1
186}
187
188# The fail-safe. An invalid registry must cost the compilation and nothing
189# else: the last good artifacts are what the gates keep reading, because a
190# compiler bug that emptied them would lock the git user out of the host.
191cp "$TMPDIR/signers" last-good-signers
192cp "$TMPDIR/keys" last-good-keys
193cp "$TMPDIR/grants" last-good-grants
194mkdir -p invalid-registry
195cp "$invalid" invalid-registry/registry.cue
196serve instance invalid-registry
197if compile --now 2026-08-09 2> refused.log; then
198 echo "identity-e2e: an invalid registry compiled" >&2
199 exit 1
200fi
201if ! grep -q "externalGovernance" refused.log; then
202 echo "identity-e2e: the refusal did not name the field that failed" >&2
203 cat refused.log >&2
204 exit 1
205fi
206diff -u last-good-signers "$TMPDIR/signers" || exit 1
207diff -u last-good-keys "$TMPDIR/keys" || exit 1
208diff -u last-good-grants "$TMPDIR/grants" || exit 1
209
210# Governance the clock orphans. This registry is valid as a document — the
211# genesis entry governs — and the only entry governing carries an expiry, so
212# whether it compiles depends on the day. The schema cannot see this; the
213# compiler can, and refuses the whole render rather than write artifacts
214# nobody could ever change again.
215mkdir -p orphan-registry
216{
217 echo 'package identity'
218 echo 'boundaries: {'
219 echo ' "push": kind: "git-push"'
220 echo ' "registry": kind: "registry"'
221 echo '}'
222 echo 'genesis: "founder"'
223 echo 'principals: {'
224 echo ' "founder": {'
225 echo ' kind: "human"'
226 echo ' keys: [{'
227 echo ' class: "ssh-ed25519"'
228 echo ' bound: "hardware"'
229 echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGCxVUxXoyFYV40QureqqSMSA17CvK9IrFB33BA6UOip"'
230 echo ' signs: "founder"'
231 echo ' }]'
232 echo ' grants: {'
233 echo ' push: boundary: "push"'
234 echo ' govern: boundary: "registry"'
235 echo ' }'
236 echo ' expires: "2026-10-01"'
237 echo ' }'
238 echo ' "runner": {'
239 echo ' kind: "machine"'
240 echo ' keys: [{'
241 echo ' class: "ssh-ed25519"'
242 echo ' bound: "host"'
243 echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAvpmbUKEDVsejgv2vxWaY/t4xl0JNnjFswb9SxcG9GG"'
244 echo ' }]'
245 echo ' grants: push: boundary: "push"'
246 echo ' expires: "2027-01-01"'
247 echo ' }'
248 echo '}'
249} > orphan-registry/registry.cue
250
251# The day before: an ordinary compilation.
252serve instance orphan-registry
253compile --now 2026-09-30 || exit 1
254grep -q "founder" "$TMPDIR/signers" || {
255 echo "identity-e2e: the governing entry was omitted while still unexpired" >&2
256 exit 1
257}
258cp "$TMPDIR/signers" last-good-signers
259cp "$TMPDIR/keys" last-good-keys
260
261# The day it expires, and a day well past it: the same document, refused. The
262# entry still pushing does not save it — a grant at another boundary is not
263# governance.
264for pinned in 2026-10-01 2026-12-01; do
265 if compile --now "$pinned" 2> orphaned.log; then
266 echo "identity-e2e: a registry nobody governs on $pinned compiled" >&2
267 exit 1
268 fi
269 if ! grep -q "governanceOrphaned" orphaned.log; then
270 echo "identity-e2e: the refusal on $pinned did not name governanceOrphaned" >&2
271 cat orphaned.log >&2
272 exit 1
273 fi
274 diff -u last-good-signers "$TMPDIR/signers" || exit 1
275 diff -u last-good-keys "$TMPDIR/keys" || exit 1
276done
277
278touch "$out"