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