| 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 directory of the |
| 87 | # compiled files, 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 -e "s|/var/lib/valley-identity/grants|$TMPDIR/grants|" \ |
| 97 | -e "s|/var/lib/valley-identity/known-signers|$TMPDIR/signers|" "$rendered" > hook |
| 98 | grep -qF -- "--known-signers $TMPDIR/signers" hook || { |
| 99 | echo "identity-e2e: the hook of a host compiling the registry does not check attestations against the compiled keys" >&2 |
| 100 | exit 1 |
| 101 | } |
| 102 | chmod +x hook |
| 103 | git init --quiet --bare guarded.git |
| 104 | ln -s "$TMPDIR/hook" guarded.git/hooks/pre-receive |
| 105 | request=refs/the-valley/integration-requests/main/topic |
| 106 | if VALLEY_PRINCIPAL=stoned-flynn git -C work push --quiet "$TMPDIR/guarded.git" "main:$request" 2> refused.err; then |
| 107 | echo "identity-e2e: a principal the registry grants push and not request filed a request" >&2 |
| 108 | exit 1 |
| 109 | fi |
| 110 | grep -q 'approve grant (also accepted as request)' refused.err |
| 111 | VALLEY_PRINCIPAL=patrick git -C work push --quiet "$TMPDIR/guarded.git" "main:$request" |
| 112 | git -C guarded.git rev-parse --verify --quiet "$request" > /dev/null || { |
| 113 | echo "identity-e2e: the principal the registry grants request could not file one" >&2 |
| 114 | exit 1 |
| 115 | } |
| 116 | VALLEY_PRINCIPAL=patrick git -C work push --quiet "$TMPDIR/guarded.git" --delete "$request" |
| 117 | |
| 118 | # The approve grant goes by two names, approve and request, and a registry |
| 119 | # may use either. A registry whose hook boundary is of kind approve, and one |
| 120 | # granting patrick at a boundary of each kind, compile exactly the artifacts |
| 121 | # the worked registry does, with patrick written once. |
| 122 | mkdir -p approve-registry both-registry |
| 123 | sed 's/kind: "request"/kind: "approve"/' "$registry/registry.cue" > approve-registry/registry.cue |
| 124 | sed -e 's/^\t"classic-laddie-request": kind: "request"$/&\n\t"classic-laddie-approve": kind: "approve"/' \ |
| 125 | -e 's/^\t\t\trequest: boundary: "classic-laddie-request"$/&\n\t\t\tapprove: boundary: "classic-laddie-approve"/' \ |
| 126 | "$registry/registry.cue" > both-registry/registry.cue |
| 127 | if grep -q 'kind: "request"' approve-registry/registry.cue || |
| 128 | [ "$(grep -c 'classic-laddie-approve' both-registry/registry.cue)" != 2 ]; then |
| 129 | echo "identity-e2e: the registries naming the grant approve were not written, so the checks below prove nothing" >&2 |
| 130 | exit 1 |
| 131 | fi |
| 132 | # hook_as <principal> <git push arguments…>: a push through the rendered |
| 133 | # hook, as the principal. |
| 134 | hook_as() { |
| 135 | local who="$1" |
| 136 | shift |
| 137 | VALLEY_PRINCIPAL="$who" git -C work push --quiet "$TMPDIR/guarded.git" "$@" |
| 138 | } |
| 139 | for variant in approve-registry both-registry; do |
| 140 | serve instance "$variant" |
| 141 | compile --now 2026-08-09 || exit 1 |
| 142 | diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1 |
| 143 | diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1 |
| 144 | diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1 |
| 145 | # And through the hook: patrick files and deletes a request, and |
| 146 | # stoned-flynn, holding push alone, does neither. |
| 147 | if hook_as stoned-flynn "main:$request" 2> refused.err; then |
| 148 | echo "identity-e2e: under $variant, a principal holding push alone filed a request" >&2 |
| 149 | exit 1 |
| 150 | fi |
| 151 | grep -q 'approve grant (also accepted as request)' refused.err |
| 152 | hook_as patrick "main:$request" |
| 153 | if hook_as stoned-flynn --delete "$request" 2> refused.err; then |
| 154 | echo "identity-e2e: under $variant, a principal holding push alone deleted a request" >&2 |
| 155 | exit 1 |
| 156 | fi |
| 157 | hook_as patrick --delete "$request" |
| 158 | if git -C guarded.git rev-parse --verify --quiet "$request" > /dev/null; then |
| 159 | echo "identity-e2e: under $variant, the approve holder could not delete its request" >&2 |
| 160 | exit 1 |
| 161 | fi |
| 162 | done |
| 163 | |
| 164 | # The hook reads a grants file naming the grant approve exactly as one |
| 165 | # naming it request. approver is named under approve, requester under |
| 166 | # request, both under each, and stoned-flynn under neither. Each holder |
| 167 | # files a request, replaces it with a head that does not descend from it, |
| 168 | # and deletes it, and stoned-flynn can do none of that in between. |
| 169 | cat > "$TMPDIR/grants" << 'EOF' |
| 170 | approve approver |
| 171 | request requester |
| 172 | approve both |
| 173 | request both |
| 174 | EOF |
| 175 | for holder in approver requester both; do |
| 176 | hook_as "$holder" "main:$request" |
| 177 | git -C work commit --quiet --allow-empty -m "replaced by $holder" |
| 178 | replacement="$(git -C work rev-parse HEAD)" |
| 179 | git -C work reset --quiet --hard HEAD~1 |
| 180 | hook_as "$holder" --force "$replacement:$request" |
| 181 | [ "$(git -C guarded.git rev-parse "$request")" = "$replacement" ] || { |
| 182 | echo "identity-e2e: $holder could not replace its request" >&2 |
| 183 | exit 1 |
| 184 | } |
| 185 | for refused in "main:$request" "--delete $request"; do |
| 186 | # shellcheck disable=SC2086 # the push arguments are two words, split on purpose |
| 187 | if hook_as stoned-flynn --force $refused 2> refused.err; then |
| 188 | echo "identity-e2e: a principal holding neither name of the approve grant wrote $holder's request" >&2 |
| 189 | exit 1 |
| 190 | fi |
| 191 | grep -q 'approve grant (also accepted as request)' refused.err |
| 192 | done |
| 193 | hook_as "$holder" --delete "$request" |
| 194 | if git -C guarded.git rev-parse --verify --quiet "$request" > /dev/null; then |
| 195 | echo "identity-e2e: $holder could not delete its request" >&2 |
| 196 | exit 1 |
| 197 | fi |
| 198 | done |
| 199 | |
| 200 | # The worked registry again, for what follows. |
| 201 | serve instance "$registry" |
| 202 | compile --now 2026-08-09 || exit 1 |
| 203 | |
| 204 | # Level-triggered: compiling again converges on the same bytes and touches |
| 205 | # nothing. |
| 206 | compile --now 2026-08-09 2> second.log || exit 1 |
| 207 | if ! grep -q "unchanged" second.log; then |
| 208 | echo "identity-e2e: a second compilation of an unchanged registry rewrote its artifacts" >&2 |
| 209 | cat second.log >&2 |
| 210 | exit 1 |
| 211 | fi |
| 212 | |
| 213 | # The registry is read from the integrated tip and from nowhere else, which |
| 214 | # is the whole of the forcing: an edit on a branch governs nothing. |
| 215 | git -C work checkout --quiet -b topic |
| 216 | cp "$expired"/*.cue work/identity/registry.cue |
| 217 | git -C work commit --quiet -am topic |
| 218 | git -C work push --quiet "$TMPDIR/instance.git" topic |
| 219 | compile --now 2026-08-09 || exit 1 |
| 220 | diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1 |
| 221 | |
| 222 | # The compiled verifier line is the one attest writes. The published keys |
| 223 | # above pin the format against a fixed vector; this pins it against the |
| 224 | # implementation that reads it, over a key generated here. |
| 225 | ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/scratch" |
| 226 | mkdir -p scratch-registry |
| 227 | { |
| 228 | echo 'package identity' |
| 229 | echo 'boundaries: "registry": kind: "registry"' |
| 230 | echo 'genesis: "someone"' |
| 231 | echo 'principals: "someone": {' |
| 232 | echo ' kind: "human"' |
| 233 | echo ' keys: [{' |
| 234 | echo ' class: "ssh-ed25519"' |
| 235 | echo ' bound: "hardware"' |
| 236 | printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/scratch.pub")" |
| 237 | echo ' signs: "someone/attestations"' |
| 238 | echo ' }]' |
| 239 | echo ' grants: govern: boundary: "registry"' |
| 240 | echo '}' |
| 241 | } > scratch-registry/registry.cue |
| 242 | serve instance scratch-registry |
| 243 | compile --now 2026-08-09 || exit 1 |
| 244 | attest key --key "$TMPDIR/scratch" --name "someone/attestations" > expected-line |
| 245 | if ! grep -qxFf expected-line "$TMPDIR/signers"; then |
| 246 | echo "identity-e2e: the compiled verifier key is not the line attest writes" >&2 |
| 247 | cat expected-line "$TMPDIR/signers" >&2 |
| 248 | exit 1 |
| 249 | fi |
| 250 | |
| 251 | # An entry past its expiry leaves both artifacts, says so, and costs the |
| 252 | # rest of the registry nothing. The day before, it is still there — the |
| 253 | # declared day is the first day the entry no longer counts. |
| 254 | serve instance "$expired" |
| 255 | compile --now 2026-09-30 || exit 1 |
| 256 | grep -q "stoned-flynn/attestations" "$TMPDIR/signers" || { |
| 257 | echo "identity-e2e: an unexpired entry was omitted" >&2 |
| 258 | exit 1 |
| 259 | } |
| 260 | grep -qx "request stoned-flynn" "$TMPDIR/grants" || { |
| 261 | echo "identity-e2e: an unexpired holder of request was omitted from the grants" >&2 |
| 262 | exit 1 |
| 263 | } |
| 264 | compile --now 2026-11-01 2> expired.log || exit 1 |
| 265 | if grep -q "stoned-flynn" "$TMPDIR/signers" "$TMPDIR/keys" "$TMPDIR/grants"; then |
| 266 | echo "identity-e2e: an expired entry stayed in the compiled artifacts" >&2 |
| 267 | cat "$TMPDIR/signers" "$TMPDIR/keys" "$TMPDIR/grants" >&2 |
| 268 | exit 1 |
| 269 | fi |
| 270 | if ! grep -q "stoned-flynn expired 2026-10-01" expired.log; then |
| 271 | echo "identity-e2e: an expired entry was dropped without saying so" >&2 |
| 272 | cat expired.log >&2 |
| 273 | exit 1 |
| 274 | fi |
| 275 | grep -q "patrick" "$TMPDIR/keys" || { |
| 276 | echo "identity-e2e: one expired entry took the rest of the registry with it" >&2 |
| 277 | exit 1 |
| 278 | } |
| 279 | |
| 280 | # The fail-safe. An invalid registry must cost the compilation and nothing |
| 281 | # else: the last good artifacts are what the gates keep reading, because a |
| 282 | # compiler bug that emptied them would lock the git user out of the host. |
| 283 | cp "$TMPDIR/signers" last-good-signers |
| 284 | cp "$TMPDIR/keys" last-good-keys |
| 285 | cp "$TMPDIR/grants" last-good-grants |
| 286 | mkdir -p invalid-registry |
| 287 | cp "$invalid" invalid-registry/registry.cue |
| 288 | serve instance invalid-registry |
| 289 | if compile --now 2026-08-09 2> refused.log; then |
| 290 | echo "identity-e2e: an invalid registry compiled" >&2 |
| 291 | exit 1 |
| 292 | fi |
| 293 | if ! grep -q "externalGovernance" refused.log; then |
| 294 | echo "identity-e2e: the refusal did not name the field that failed" >&2 |
| 295 | cat refused.log >&2 |
| 296 | exit 1 |
| 297 | fi |
| 298 | diff -u last-good-signers "$TMPDIR/signers" || exit 1 |
| 299 | diff -u last-good-keys "$TMPDIR/keys" || exit 1 |
| 300 | diff -u last-good-grants "$TMPDIR/grants" || exit 1 |
| 301 | |
| 302 | # Governance the clock orphans. This registry is valid as a document — the |
| 303 | # genesis entry governs — and the only entry governing carries an expiry, so |
| 304 | # whether it compiles depends on the day. The schema cannot see this; the |
| 305 | # compiler can, and refuses the whole render rather than write artifacts |
| 306 | # nobody could ever change again. |
| 307 | mkdir -p orphan-registry |
| 308 | { |
| 309 | echo 'package identity' |
| 310 | echo 'boundaries: {' |
| 311 | echo ' "push": kind: "git-push"' |
| 312 | echo ' "registry": kind: "registry"' |
| 313 | echo '}' |
| 314 | echo 'genesis: "founder"' |
| 315 | echo 'principals: {' |
| 316 | echo ' "founder": {' |
| 317 | echo ' kind: "human"' |
| 318 | echo ' keys: [{' |
| 319 | echo ' class: "ssh-ed25519"' |
| 320 | echo ' bound: "hardware"' |
| 321 | echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGCxVUxXoyFYV40QureqqSMSA17CvK9IrFB33BA6UOip"' |
| 322 | echo ' signs: "founder"' |
| 323 | echo ' }]' |
| 324 | echo ' grants: {' |
| 325 | echo ' push: boundary: "push"' |
| 326 | echo ' govern: boundary: "registry"' |
| 327 | echo ' }' |
| 328 | echo ' expires: "2026-10-01"' |
| 329 | echo ' }' |
| 330 | echo ' "runner": {' |
| 331 | echo ' kind: "machine"' |
| 332 | echo ' keys: [{' |
| 333 | echo ' class: "ssh-ed25519"' |
| 334 | echo ' bound: "host"' |
| 335 | echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAvpmbUKEDVsejgv2vxWaY/t4xl0JNnjFswb9SxcG9GG"' |
| 336 | echo ' }]' |
| 337 | echo ' grants: push: boundary: "push"' |
| 338 | echo ' expires: "2027-01-01"' |
| 339 | echo ' }' |
| 340 | echo '}' |
| 341 | } > orphan-registry/registry.cue |
| 342 | |
| 343 | # The day before: an ordinary compilation. |
| 344 | serve instance orphan-registry |
| 345 | compile --now 2026-09-30 || exit 1 |
| 346 | grep -q "founder" "$TMPDIR/signers" || { |
| 347 | echo "identity-e2e: the governing entry was omitted while still unexpired" >&2 |
| 348 | exit 1 |
| 349 | } |
| 350 | cp "$TMPDIR/signers" last-good-signers |
| 351 | cp "$TMPDIR/keys" last-good-keys |
| 352 | |
| 353 | # The day it expires, and a day well past it: the same document, refused. The |
| 354 | # entry still pushing does not save it — a grant at another boundary is not |
| 355 | # governance. |
| 356 | for pinned in 2026-10-01 2026-12-01; do |
| 357 | if compile --now "$pinned" 2> orphaned.log; then |
| 358 | echo "identity-e2e: a registry nobody governs on $pinned compiled" >&2 |
| 359 | exit 1 |
| 360 | fi |
| 361 | if ! grep -q "governanceOrphaned" orphaned.log; then |
| 362 | echo "identity-e2e: the refusal on $pinned did not name governanceOrphaned" >&2 |
| 363 | cat orphaned.log >&2 |
| 364 | exit 1 |
| 365 | fi |
| 366 | diff -u last-good-signers "$TMPDIR/signers" || exit 1 |
| 367 | diff -u last-good-keys "$TMPDIR/keys" || exit 1 |
| 368 | done |
| 369 | |
| 370 | # A key the host declares by hand under one tag, and the registry |
| 371 | # authorizes as another principal, is refused: sshd reads the declared |
| 372 | # entry first, so the registry's principal would never be the one pushing. |
| 373 | serve instance "$registry" |
| 374 | compile --now 2026-08-09 || exit 1 |
| 375 | cp "$TMPDIR/keys" last-good-keys |
| 376 | patrick_key="$(grep -m1 -o 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGCxVUxXoyFYV40QureqqSMSA17CvK9IrFB33BA6UOip' "$registry/registry.cue")" |
| 377 | for declared in "$patrick_key operator@laptop" "environment=\"VALLEY_PRINCIPAL=someone\" $patrick_key"; do |
| 378 | printf '%s\n' "$declared" > declared-keys |
| 379 | if compile --now 2026-08-09 --declared-keys declared-keys 2> declared.err; then |
| 380 | echo "identity-e2e: a registry key declared by hand under another tag compiled" >&2 |
| 381 | exit 1 |
| 382 | fi |
| 383 | grep -q 'declared on this host' declared.err || { |
| 384 | echo "identity-e2e: the refusal did not name the declared entry" >&2 |
| 385 | cat declared.err >&2 |
| 386 | exit 1 |
| 387 | } |
| 388 | diff -u last-good-keys "$TMPDIR/keys" || exit 1 |
| 389 | done |
| 390 | printf 'environment="VALLEY_PRINCIPAL=patrick" %s\n' "$patrick_key" > declared-keys |
| 391 | compile --now 2026-08-09 --declared-keys declared-keys || exit 1 |
| 392 | |
| 393 | # Attestation names. Under a registry with two signers, the hook checks |
| 394 | # each pushed attestation's signature against the compiled keys, so a name |
| 395 | # in the create-only namespace can only be taken by the evidence it is |
| 396 | # named for. The pusher is never the question: evidence is relayed. |
| 397 | for who in victim attacker stranger; do |
| 398 | ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$TMPDIR/$who" |
| 399 | done |
| 400 | mkdir -p signers-registry |
| 401 | { |
| 402 | echo 'package identity' |
| 403 | echo 'boundaries: {' |
| 404 | echo ' "push": kind: "git-push"' |
| 405 | echo ' "registry": kind: "registry"' |
| 406 | echo '}' |
| 407 | echo 'genesis: "victim"' |
| 408 | for who in victim attacker; do |
| 409 | printf 'principals: "%s": {\n' "$who" |
| 410 | echo ' kind: "human"' |
| 411 | echo ' keys: [{' |
| 412 | echo ' class: "ssh-ed25519"' |
| 413 | echo ' bound: "hardware"' |
| 414 | printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/$who.pub")" |
| 415 | printf '\t\tsigns: "%s/attestations"\n' "$who" |
| 416 | echo ' }]' |
| 417 | if [ "$who" = victim ]; then |
| 418 | echo ' grants: {push: boundary: "push", govern: boundary: "registry"}' |
| 419 | else |
| 420 | echo ' grants: push: boundary: "push"' |
| 421 | fi |
| 422 | echo '}' |
| 423 | done |
| 424 | } > signers-registry/registry.cue |
| 425 | serve instance signers-registry |
| 426 | compile --now 2026-08-09 || exit 1 |
| 427 | |
| 428 | git init --quiet --bare attested.git |
| 429 | ln -s "$TMPDIR/hook" attested.git/hooks/pre-receive |
| 430 | git init --quiet occ |
| 431 | git -C occ commit --quiet --allow-empty -m "a tree to attest" |
| 432 | attestation() { |
| 433 | attest run --repo occ --key "$TMPDIR/$1" --name "$1/attestations" --command ok=true | |
| 434 | sed -n 's/^stored \(refs[^ ]*\) -> .*/\1/p' |
| 435 | } |
| 436 | mine="$(attestation attacker)" |
| 437 | theirs="$(attestation victim)" |
| 438 | unknown="$(attestation stranger)" |
| 439 | digest="$(echo "$mine" | cut -d/ -f4)" |
| 440 | victim_hash="$(echo "$theirs" | cut -d/ -f5)" |
| 441 | squat="refs/the-valley/attestations/$digest/$victim_hash" |
| 442 | push_as() { VALLEY_PRINCIPAL="$1" git -C occ push --quiet "$TMPDIR/attested.git" "$2"; } |
| 443 | must_refuse() { |
| 444 | if push_as attacker "$1" 2> occupied.err; then |
| 445 | echo "identity-e2e: $2 took an attestation name" >&2 |
| 446 | exit 1 |
| 447 | fi |
| 448 | grep -qF -- "$3" occupied.err || { |
| 449 | echo "identity-e2e: the refusal of $2 did not say \"$3\"" >&2 |
| 450 | cat occupied.err >&2 |
| 451 | exit 1 |
| 452 | } |
| 453 | } |
| 454 | # The attacker's own evidence, under its own name: accepted. |
| 455 | push_as attacker "$mine:$mine" |
| 456 | # The attacker's note, filed under the victim's key hash. |
| 457 | must_refuse "$mine:$squat" "the attacker's note under the victim's name" \ |
| 458 | "no signature under the key hash $victim_hash by a key this host accepts" |
| 459 | # A note whose signature line claims the victim's key and carries the |
| 460 | # attacker's signature. |
| 461 | git -C occ cat-file blob "$mine:ok/statement.note" > mine.note |
| 462 | sig="$(sed -n 's/^— attacker\/attestations //p' mine.note)" |
| 463 | forged_sig="$( { printf "$(echo "$victim_hash" | sed 's/../\\x&/g')"; printf '%s' "$sig" | base64 -d | tail -c 64; } | base64 -w0)" |
| 464 | sed "s|^— attacker/attestations .*|— victim/attestations $forged_sig|" mine.note > forged.note |
| 465 | blob="$(git -C occ hash-object -w "$TMPDIR/forged.note")" |
| 466 | sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C occ mktree)" |
| 467 | top="$(printf '040000 tree %s\tok\n' "$sub" | git -C occ mktree)" |
| 468 | must_refuse "$top:$squat" "a forged signature" "does not check out" |
| 469 | # A key the registry does not hold, under its own name. |
| 470 | must_refuse "$unknown:$unknown" "an unknown signer's note" "does not open" |
| 471 | # The victim's genuine note, with a line appended that no verifier reads. |
| 472 | # Its signature still verifies, and the note is still one no verifier |
| 473 | # opens, so it may not take the victim's name. |
| 474 | git -C occ cat-file blob "$theirs:ok/statement.note" > theirs.note |
| 475 | { cat theirs.note; printf '— x\t AAAAAAA=\n'; } > poisoned.note |
| 476 | blob="$(git -C occ hash-object -w "$TMPDIR/poisoned.note")" |
| 477 | sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C occ mktree)" |
| 478 | top="$(printf '040000 tree %s\tok\n' "$sub" | git -C occ mktree)" |
| 479 | must_refuse "$top:$theirs" "a poisoned relay of the victim's note" "does not open" |
| 480 | # And the victim's own evidence, relayed by the attacker, takes the name |
| 481 | # that none of the above could. |
| 482 | push_as attacker "$theirs:$theirs" |
| 483 | git -C attested.git rev-parse --verify --quiet "$theirs" > /dev/null || { |
| 484 | echo "identity-e2e: relayed evidence could not take its own name" >&2 |
| 485 | exit 1 |
| 486 | } |
| 487 | |
| 488 | # The restack grant. A principal holding a grant at a boundary of kind |
| 489 | # restack is compiled into the grants as "restack <principal>", and the |
| 490 | # hook a host renders then lets it add a version to another principal's |
| 491 | # ready change and write that change's restack report. The same write from |
| 492 | # a principal the registry grants push alone is refused. |
| 493 | ssh-keygen -q -t ed25519 -N "" -C restacker -f "$TMPDIR/restacker" |
| 494 | mkdir -p restack-registry |
| 495 | { |
| 496 | echo 'package identity' |
| 497 | echo 'boundaries: {' |
| 498 | echo ' "push": kind: "git-push"' |
| 499 | echo ' "registry": kind: "registry"' |
| 500 | echo ' "restack": kind: "restack"' |
| 501 | echo '}' |
| 502 | echo 'genesis: "author"' |
| 503 | echo 'principals: "author": {' |
| 504 | echo ' kind: "human"' |
| 505 | echo ' keys: [{' |
| 506 | echo ' class: "ssh-ed25519"' |
| 507 | echo ' bound: "hardware"' |
| 508 | printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/victim.pub")" |
| 509 | echo ' }]' |
| 510 | echo ' grants: {push: boundary: "push", govern: boundary: "registry"}' |
| 511 | echo '}' |
| 512 | echo 'principals: "valley-restacker": {' |
| 513 | echo ' kind: "service"' |
| 514 | echo ' keys: [{' |
| 515 | echo ' class: "ssh-ed25519"' |
| 516 | echo ' bound: "host"' |
| 517 | printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/restacker.pub")" |
| 518 | echo ' }]' |
| 519 | echo ' grants: {push: boundary: "push", restack: boundary: "restack"}' |
| 520 | echo ' expires: "2027-01-01"' |
| 521 | echo '}' |
| 522 | } > restack-registry/registry.cue |
| 523 | serve instance restack-registry |
| 524 | compile --now 2026-08-09 || exit 1 |
| 525 | if [ "$(grep -v '^#' "$TMPDIR/grants")" != "restack valley-restacker" ]; then |
| 526 | echo "identity-e2e: the restack grant did not compile into the one line it holds" >&2 |
| 527 | cat "$TMPDIR/grants" >&2 |
| 528 | exit 1 |
| 529 | fi |
| 530 | |
| 531 | git init --quiet --bare restacked.git |
| 532 | ln -s "$TMPDIR/hook" restacked.git/hooks/pre-receive |
| 533 | git init --quiet rs |
| 534 | git -C rs commit --quiet --allow-empty -m "the change" |
| 535 | h1="$(git -C rs rev-parse HEAD)" |
| 536 | h2="$(git -C rs commit-tree "HEAD^{tree}" -p HEAD -m "the change, restacked")" |
| 537 | change_id=nxvpwlkmqtsrzyoukklnpmqrstuvwxyz |
| 538 | version() { printf '{"n": %s, "head": "git-sha1:%s", "title": "the change", "branch": "topic", "uploader": "%s"}' "$@"; } |
| 539 | # one_file <file> <json> <parent…>: a commit whose tree holds the JSON as |
| 540 | # the file. |
| 541 | one_file() { |
| 542 | local file="$1" json="$2" blob tree parent |
| 543 | local -a parents=() |
| 544 | shift 2 |
| 545 | for parent in "$@"; do parents+=(-p "$parent"); done |
| 546 | blob="$(printf '%s' "$json" | git -C rs hash-object -w --stdin)" |
| 547 | tree="$(printf '100644 blob %s\t%s\n' "$blob" "$file" | git -C rs mktree)" |
| 548 | git -C rs commit-tree "$tree" "${parents[@]}" -m "$file" |
| 549 | } |
| 550 | rpush() { VALLEY_PRINCIPAL="$1" git -C rs push --quiet "$TMPDIR/restacked.git" "$2"; } |
| 551 | record_ref="refs/the-valley/changes/main/$change_id" |
| 552 | created="$(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")" |
| 553 | rpush author "$created:$record_ref" |
| 554 | appended="$(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")" |
| 555 | if rpush stoned-flynn "$appended:$record_ref" 2> restack.err; then |
| 556 | echo "identity-e2e: a principal the registry does not grant restack added a version to another's change" >&2 |
| 557 | exit 1 |
| 558 | fi |
| 559 | grep -q "only the change's owner, author" restack.err |
| 560 | rpush valley-restacker "$appended:$record_ref" |
| 561 | [ "$(git -C restacked.git rev-parse "$record_ref")" = "$appended" ] || { |
| 562 | echo "identity-e2e: the principal the registry grants restack could not add a version" >&2 |
| 563 | exit 1 |
| 564 | } |
| 565 | report_ref="refs/the-valley/restack/main/$change_id" |
| 566 | report="$(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}")" |
| 567 | if rpush author "$report:$report_ref" 2> report.err; then |
| 568 | echo "identity-e2e: a principal the registry does not grant restack wrote a restack report" >&2 |
| 569 | exit 1 |
| 570 | fi |
| 571 | grep -q 'restack grant' report.err |
| 572 | rpush valley-restacker "$report:$report_ref" |
| 573 | [ "$(git -C restacked.git rev-parse "$report_ref")" = "$report" ] || { |
| 574 | echo "identity-e2e: the principal the registry grants restack could not write a restack report" >&2 |
| 575 | exit 1 |
| 576 | } |
| 577 | |
| 578 | touch "$out" |