| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" |
| 5 | export GIT_CONFIG_NOSYSTEM=1 |
| 6 | git config --global user.name valley-check |
| 7 | git config --global user.email valley-check@localhost |
| 8 | git config --global init.defaultBranch main |
| 9 | cd "$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. |
| 13 | serve() { |
| 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 | |
| 26 | compile() { |
| 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. |
| 35 | serve instance "$registry" |
| 36 | compile --now 2026-08-09 2> first.log || exit 1 |
| 37 | diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1 |
| 38 | diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1 |
| 39 | diff -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. |
| 46 | ssh-keygen -q -t ed25519 -N "" -C intruder -f "$TMPDIR/intruder" |
| 47 | cp "$registry/registry.cue" forged.cue |
| 48 | chmod 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 |
| 64 | landed="$(git -C instance.git rev-parse main:identity/registry.cue)" |
| 65 | forged="$(git -C instance.git hash-object -w "$TMPDIR/forged.cue")" |
| 66 | git -C instance.git update-ref "refs/replace/$landed" "$forged" |
| 67 | if ! 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 |
| 70 | fi |
| 71 | compile --now 2026-08-09 || exit 1 |
| 72 | diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1 |
| 73 | diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1 |
| 74 | diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1 |
| 75 | if 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 |
| 78 | fi |
| 79 | # The same holds when the caller's environment asks for replacement refs. |
| 80 | GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" compile --now 2026-08-09 || exit 1 |
| 81 | diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1 |
| 82 | git -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. |
| 90 | rendered="$(grep -o "/nix/store/[^ ]*-valley-protect-guarded" "$identityInitScriptPath" | head -n1)" |
| 91 | if ! 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 |
| 95 | fi |
| 96 | sed "s|/var/lib/valley-identity/grants|$TMPDIR/grants|" "$rendered" > hook |
| 97 | chmod +x hook |
| 98 | git init --quiet --bare guarded.git |
| 99 | ln -s "$TMPDIR/hook" guarded.git/hooks/pre-receive |
| 100 | request=refs/the-valley/integration-requests/main/topic |
| 101 | if 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 |
| 104 | fi |
| 105 | grep -q 'request grant' refused.err |
| 106 | VALLEY_PRINCIPAL=patrick git -C work push --quiet "$TMPDIR/guarded.git" "main:$request" |
| 107 | git -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. |
| 114 | compile --now 2026-08-09 2> second.log || exit 1 |
| 115 | if ! 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 |
| 119 | fi |
| 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. |
| 123 | git -C work checkout --quiet -b topic |
| 124 | cp "$expired"/*.cue work/identity/registry.cue |
| 125 | git -C work commit --quiet -am topic |
| 126 | git -C work push --quiet "$TMPDIR/instance.git" topic |
| 127 | compile --now 2026-08-09 || exit 1 |
| 128 | diff -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. |
| 133 | ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/scratch" |
| 134 | mkdir -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 |
| 150 | serve instance scratch-registry |
| 151 | compile --now 2026-08-09 || exit 1 |
| 152 | attest key --key "$TMPDIR/scratch" --name "someone/attestations" > expected-line |
| 153 | if ! 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 |
| 157 | fi |
| 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. |
| 162 | serve instance "$expired" |
| 163 | compile --now 2026-09-30 || exit 1 |
| 164 | grep -q "stoned-flynn/attestations" "$TMPDIR/signers" || { |
| 165 | echo "identity-e2e: an unexpired entry was omitted" >&2 |
| 166 | exit 1 |
| 167 | } |
| 168 | grep -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 | } |
| 172 | compile --now 2026-11-01 2> expired.log || exit 1 |
| 173 | if 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 |
| 177 | fi |
| 178 | if ! 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 |
| 182 | fi |
| 183 | grep -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. |
| 191 | cp "$TMPDIR/signers" last-good-signers |
| 192 | cp "$TMPDIR/keys" last-good-keys |
| 193 | cp "$TMPDIR/grants" last-good-grants |
| 194 | mkdir -p invalid-registry |
| 195 | cp "$invalid" invalid-registry/registry.cue |
| 196 | serve instance invalid-registry |
| 197 | if compile --now 2026-08-09 2> refused.log; then |
| 198 | echo "identity-e2e: an invalid registry compiled" >&2 |
| 199 | exit 1 |
| 200 | fi |
| 201 | if ! 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 |
| 205 | fi |
| 206 | diff -u last-good-signers "$TMPDIR/signers" || exit 1 |
| 207 | diff -u last-good-keys "$TMPDIR/keys" || exit 1 |
| 208 | diff -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. |
| 215 | mkdir -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. |
| 252 | serve instance orphan-registry |
| 253 | compile --now 2026-09-30 || exit 1 |
| 254 | grep -q "founder" "$TMPDIR/signers" || { |
| 255 | echo "identity-e2e: the governing entry was omitted while still unexpired" >&2 |
| 256 | exit 1 |
| 257 | } |
| 258 | cp "$TMPDIR/signers" last-good-signers |
| 259 | cp "$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. |
| 264 | for 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 |
| 276 | done |
| 277 | |
| 278 | touch "$out" |