| 1 | # shellcheck shell=bash |
| 2 | # valley ready and valley withdraw, end to end and unmocked: every record |
| 3 | # they write is pushed for real to a bare repository wired with the real |
| 4 | # rendered pre-receive hook, as each principal, and what that repository |
| 5 | # then holds is read back. What sshd would set from a key's |
| 6 | # authorized_keys entry, these pushes set in the environment directly, as |
| 7 | # protect-e2e's do. |
| 8 | # Paths and fixtures come from the derivation environment. |
| 9 | # shellcheck disable=SC2154 |
| 10 | set -euo pipefail |
| 11 | |
| 12 | w="$TMPDIR/valley-ready" |
| 13 | mkdir -p "$w" |
| 14 | export HOME="$w" |
| 15 | export GIT_CONFIG_NOSYSTEM=1 |
| 16 | git config --global user.name valley-ready-check |
| 17 | git config --global user.email valley-ready-check@localhost |
| 18 | git config --global init.defaultBranch main |
| 19 | git config --global advice.detachedHead false |
| 20 | |
| 21 | fail() { |
| 22 | printf 'valley-ready: %s\n' "$*" >&2 |
| 23 | for f in "$w"/out "$w"/err; do |
| 24 | if [ -s "$f" ]; then cat "$f" >&2; fi |
| 25 | done |
| 26 | exit 1 |
| 27 | } |
| 28 | |
| 29 | # The project "open" of the protected host: it protects nothing, and the |
| 30 | # host grants request to the principal named requester. Its hook is the |
| 31 | # one valley-init installs, followed from the rendered init script. |
| 32 | origin="$w/project.git" |
| 33 | hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$initScriptPath" | head -n1)" |
| 34 | test -x "$hook" |
| 35 | git init --quiet --bare "$origin" |
| 36 | cp "$hook" "$origin/hooks/pre-receive" |
| 37 | chmod +x "$origin/hooks/pre-receive" |
| 38 | |
| 39 | repo="$w/project" |
| 40 | git init --quiet "$repo" |
| 41 | cd "$repo" |
| 42 | git remote add origin "$origin" |
| 43 | echo base > readme |
| 44 | git add readme |
| 45 | git commit --quiet -m base |
| 46 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 47 | |
| 48 | # as <principal> <command…>: run a command as a principal. The record names |
| 49 | # the principal git config valley.principal does, and the hook reads the |
| 50 | # one the push carries; an honest client has the two agree. anonymous is a |
| 51 | # key that names no principal. |
| 52 | as() { |
| 53 | local who="$1" |
| 54 | shift |
| 55 | if [ "$who" = anonymous ]; then |
| 56 | git config --unset valley.principal || true |
| 57 | env -u VALLEY_PRINCIPAL "$@" |
| 58 | else |
| 59 | git config valley.principal "$who" |
| 60 | VALLEY_PRINCIPAL="$who" "$@" |
| 61 | fi |
| 62 | } |
| 63 | |
| 64 | # ok <command…>: the command succeeds, and its output is kept. |
| 65 | ok() { |
| 66 | "$@" > "$w/out" 2> "$w/err" || fail "$* failed" |
| 67 | } |
| 68 | |
| 69 | # refused <phrase> <command…>: the command fails, saying the phrase. |
| 70 | refused() { |
| 71 | local says="$1" |
| 72 | shift |
| 73 | if "$@" > "$w/out" 2> "$w/err"; then |
| 74 | fail "$* succeeded where it had to be refused" |
| 75 | fi |
| 76 | grep -qF -- "$says" "$w/err" || fail "$* was refused without saying: $says" |
| 77 | } |
| 78 | |
| 79 | # record <id> <jq condition…>: origin's record of the change holds each. |
| 80 | record() { |
| 81 | local id="$1" cond |
| 82 | shift |
| 83 | git -C "$origin" cat-file blob "refs/the-valley/changes/main/$id:record.json" > "$w/record.json" \ |
| 84 | || fail "origin holds no record of $id" |
| 85 | for cond in "$@"; do |
| 86 | jq -e "$cond" "$w/record.json" > /dev/null || fail "the record of $id: $cond does not hold" |
| 87 | done |
| 88 | } |
| 89 | |
| 90 | # unchanged <id> <command…>: the command leaves origin's record as it was. |
| 91 | unchanged() { |
| 92 | local id="$1" before |
| 93 | shift |
| 94 | before="$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$id")" |
| 95 | "$@" |
| 96 | [ "$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$id")" = "$before" ] \ |
| 97 | || fail "the record of $id moved" |
| 98 | } |
| 99 | |
| 100 | no_records() { |
| 101 | [ -z "$(git -C "$origin" for-each-ref refs/the-valley/changes)" ] || fail "$1" |
| 102 | } |
| 103 | |
| 104 | # commit <subject> [<trailer>]: a new commit on the current branch, adding |
| 105 | # a file of its own, so that any two branches merge cleanly. |
| 106 | commit() { |
| 107 | echo "$1" > "$1.txt" |
| 108 | git add "$1.txt" |
| 109 | if [ $# -gt 1 ]; then |
| 110 | git commit --quiet -m "$1" -m "$2" |
| 111 | else |
| 112 | git commit --quiet -m "$1" |
| 113 | fi |
| 114 | } |
| 115 | |
| 116 | # with_header <id>: HEAD rewritten to carry a change-id header, the way jj |
| 117 | # writes one, after the committer. |
| 118 | with_header() { |
| 119 | local amended |
| 120 | amended="$(git cat-file commit HEAD | sed "/^committer /a change-id $1" | |
| 121 | git hash-object -t commit -w --stdin)" |
| 122 | git update-ref refs/heads/"$(git symbolic-ref --short HEAD)" "$amended" |
| 123 | } |
| 124 | |
| 125 | # ---------------------------------------------------------------------- |
| 126 | # A head marked ready for the first time is v1 of a new change. Its head |
| 127 | # carries no change ID, so ready makes one and prints it. |
| 128 | git checkout --quiet -b topic |
| 129 | commit one |
| 130 | first="$(git rev-parse HEAD)" |
| 131 | refused 'push it there first' as contributor valley ready |
| 132 | no_records 'a head not on origin was recorded' |
| 133 | as contributor git push --quiet origin topic |
| 134 | ok as contributor valley ready |
| 135 | grep -qE '^project [k-z]{4} v1 ready$' "$w/out" || fail 'ready did not print the handle and v1' |
| 136 | id="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 137 | [ "${#id}" = 32 ] || fail 'ready did not print the change ID it made' |
| 138 | handle="${id:0:4}" |
| 139 | grep -qx "project $handle v1 ready" "$w/out" || fail 'the handle is not the ID'"'"'s first four letters' |
| 140 | record "$id" '.schema == "valley-change-record/v1"' ".change_id == \"$id\"" '.target == "main"' \ |
| 141 | '.owner == "contributor"' '.state == "ready"' '.seq == 1' '.active == 1' \ |
| 142 | ".versions == [{n: 1, head: \"git-sha1:$first\", title: \"one\", branch: \"topic\", uploader: \"contributor\"}]" |
| 143 | # The record commit holds on to the head it records. |
| 144 | [ "$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$id^1")" = "$first" ] \ |
| 145 | || fail 'the record commit does not have the head as its parent' |
| 146 | |
| 147 | # A second ready at a new head is v2, named by the ID it printed. |
| 148 | commit two |
| 149 | second="$(git rev-parse HEAD)" |
| 150 | as contributor git push --quiet origin topic |
| 151 | ok as contributor valley ready --change "$id" |
| 152 | grep -qx "project $handle v2 ready" "$w/out" || fail 'the second ready was not v2' |
| 153 | record "$id" '.seq == 2' '.active == 2' '.state == "ready"' '.versions | length == 2' \ |
| 154 | ".versions[0].head == \"git-sha1:$first\"" ".versions[1] == {n: 2, head: \"git-sha1:$second\", title: \"two\", branch: \"topic\", uploader: \"contributor\"}" |
| 155 | |
| 156 | # The same head again is no new version. |
| 157 | unchanged "$id" refused 'a new version needs a new head' as contributor valley ready --change "$id" |
| 158 | |
| 159 | # Withdrawn, the change comes back only by a new version. |
| 160 | ok as contributor valley withdraw "$handle" |
| 161 | grep -qx "project $handle v2 withdrawn" "$w/out" || fail 'withdraw did not say what it withdrew' |
| 162 | record "$id" '.state == "withdrawn"' '.seq == 3' '.active == 2' '.versions | length == 2' |
| 163 | unchanged "$id" refused 'withdrawn already' as contributor valley withdraw "$handle" |
| 164 | unchanged "$id" refused 'a new version needs a new head' as contributor valley ready --change "$id" |
| 165 | commit three |
| 166 | third="$(git rev-parse HEAD)" |
| 167 | as contributor git push --quiet origin topic |
| 168 | ok as contributor valley ready --change "$id" |
| 169 | grep -qx "project $handle v3 ready" "$w/out" || fail 'the revived change was not v3' |
| 170 | record "$id" '.state == "ready"' '.seq == 4' '.active == 3' ".versions[2].head == \"git-sha1:$third\"" |
| 171 | |
| 172 | # ---------------------------------------------------------------------- |
| 173 | # Who may write the record. A principal that neither owns the change nor |
| 174 | # holds the request grant is refused by the hook, whatever the command |
| 175 | # writes, and so is a key that names no principal. |
| 176 | commit four |
| 177 | fourth="$(git rev-parse HEAD)" |
| 178 | as contributor git push --quiet origin topic |
| 179 | unchanged "$id" refused "only the change's owner, contributor" as integrator valley ready --change "$id" |
| 180 | unchanged "$id" refused "only the change's owner, contributor" as integrator valley withdraw "$handle" |
| 181 | unchanged "$id" refused 'a key that names no principal' env -u VALLEY_PRINCIPAL valley withdraw "$handle" |
| 182 | refused 'git config valley.principal names none' as anonymous valley ready --change "$id" |
| 183 | # The operator, who holds the request grant, may add a version and |
| 184 | # withdraw, and the owner stays the owner. |
| 185 | ok as requester valley ready --head "$fourth" --change "$id" |
| 186 | grep -qx "project $handle v4 ready" "$w/out" || fail "the operator's version was not v4" |
| 187 | record "$id" '.owner == "contributor"' '.versions[3].uploader == "requester"' '.seq == 5' |
| 188 | ok as requester valley withdraw "$id" |
| 189 | record "$id" '.state == "withdrawn"' '.seq == 6' '.owner == "contributor"' |
| 190 | |
| 191 | # --head marks exactly the head given, which must be on origin. An older |
| 192 | # head of the branch is on origin, and a commit nobody pushed is not. |
| 193 | commit five |
| 194 | unpushed="$(git rev-parse HEAD)" |
| 195 | unchanged "$id" refused 'push it there first' as contributor valley ready --head "$unpushed" --change "$id" |
| 196 | git reset --quiet --hard "$fourth" |
| 197 | |
| 198 | # ---------------------------------------------------------------------- |
| 199 | # Change identity. A jj change-id header in the head commit names the |
| 200 | # change; so does a Change-Id trailer in its message. The two disagreeing |
| 201 | # is refused, and so is a value that is no change ID. |
| 202 | header_id=zyxwvutsrqponmlkzyxwvutsrqponmlk |
| 203 | trailer_id=kkkkllllmmmmnnnnooooppppqqqqrrrr |
| 204 | git checkout --quiet -b from-header main |
| 205 | commit header |
| 206 | with_header "$header_id" |
| 207 | as contributor git push --quiet origin from-header |
| 208 | ok as contributor valley ready |
| 209 | grep -qx "project zyxw v1 ready" "$w/out" || fail 'the header did not name the change' |
| 210 | grep -q 'is new' "$w/out" && fail 'a change ID read from the header was reported as made' |
| 211 | record "$header_id" '.versions[0].branch == "from-header"' |
| 212 | |
| 213 | git checkout --quiet -b from-trailer main |
| 214 | commit trailer "Change-Id: $trailer_id" |
| 215 | as contributor git push --quiet origin from-trailer |
| 216 | ok as contributor valley ready |
| 217 | grep -qx "project kkkk v1 ready" "$w/out" || fail 'the trailer did not name the change' |
| 218 | record "$trailer_id" '.versions[0].branch == "from-trailer"' |
| 219 | |
| 220 | git checkout --quiet -b disagreeing main |
| 221 | commit disagreeing "Change-Id: $trailer_id" |
| 222 | with_header "$header_id" |
| 223 | as contributor git push --quiet origin disagreeing |
| 224 | unchanged "$trailer_id" unchanged "$header_id" \ |
| 225 | refused 'they must agree' as contributor valley ready |
| 226 | refused 'they must agree' as contributor valley ready --change "$id" |
| 227 | |
| 228 | git checkout --quiet -b malformed main |
| 229 | commit malformed "Change-Id: I0123456789abcdef0123456789abcdef01234567" |
| 230 | as contributor git push --quiet origin malformed |
| 231 | refused 'which is not a change ID' as contributor valley ready |
| 232 | |
| 233 | # A change is a line of commits from its merge base with main. A merge |
| 234 | # in that range is refused. |
| 235 | git checkout --quiet -b merged main |
| 236 | commit merged |
| 237 | git merge --quiet --no-ff -m merge from-trailer |
| 238 | as contributor git push --quiet origin merged |
| 239 | refused 'include the merge' as contributor valley ready |
| 240 | |
| 241 | # ---------------------------------------------------------------------- |
| 242 | # A change is named by its handle wherever a change is taken: status |
| 243 | # reads the branch its active version names, and withdraw takes a branch |
| 244 | # name too. |
| 245 | ok valley status kkkk |
| 246 | grep -q '^from-trailer: not approved' "$w/out" || fail 'status did not read the change by its handle' |
| 247 | ok as contributor valley withdraw from-trailer |
| 248 | grep -qx 'project kkkk v1 withdrawn' "$w/out" || fail 'withdraw did not take a branch name' |
| 249 | |
| 250 | # --stamp carries a new change's ID in an unpushed head as a trailer, |
| 251 | # pushes the branch, and marks it ready. It will not rewrite a head that |
| 252 | # is on origin in a change that is not the user's. |
| 253 | git checkout --quiet -b stamped main |
| 254 | commit stamped |
| 255 | ok as contributor valley ready --stamp |
| 256 | stamped_id="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")" |
| 257 | [ "${#stamped_id}" = 32 ] || fail '--stamp made no change ID' |
| 258 | [ "$(git log -1 --format='%(trailers:key=Change-Id,valueonly)' | head -n1)" = "$stamped_id" ] \ |
| 259 | || fail '--stamp did not carry the ID in the head' |
| 260 | [ "$(git rev-parse HEAD)" = "$(git -C "$origin" rev-parse refs/heads/stamped)" ] \ |
| 261 | || fail '--stamp did not push the stamped head' |
| 262 | record "$stamped_id" ".versions[0].head == \"git-sha1:$(git rev-parse HEAD)\"" |
| 263 | git checkout --quiet -b pushed main |
| 264 | commit pushed |
| 265 | as contributor git push --quiet origin pushed |
| 266 | refused 'will not rewrite it' as integrator valley ready --stamp |
| 267 | # A change that has a record already is never stamped, whoever |
| 268 | # valley.principal says is asking: neither the local branch nor origin's |
| 269 | # moves. |
| 270 | git checkout --quiet stamped |
| 271 | commit restamped |
| 272 | restamped="$(git rev-parse HEAD)" |
| 273 | unchanged "$stamped_id" refused 'has a record on origin already' \ |
| 274 | as contributor valley ready --stamp --change "$stamped_id" |
| 275 | [ "$(git rev-parse HEAD)" = "$restamped" ] || fail '--stamp rewrote the head of a change with a record' |
| 276 | [ "$(git -C "$origin" rev-parse refs/heads/stamped)" != "$restamped" ] \ |
| 277 | || fail '--stamp pushed for a change with a record' |
| 278 | |
| 279 | # ---------------------------------------------------------------------- |
| 280 | # A change named by its handle is approved, reviewed and rebased at the |
| 281 | # head its record names, never at its branch's tip. The record's branch is |
| 282 | # only where that head is expected to be, and whoever can push the branch |
| 283 | # can move it, in the same push as the record if they like. |
| 284 | # |
| 285 | # approve derives what the change owes from the owning valley's floor, |
| 286 | # named at policy/valley on main. This floor owes nothing, so approving |
| 287 | # files a request with no evidence, and no signing key is needed. |
| 288 | valley="$w/valley.git" |
| 289 | git init --quiet --bare "$valley" |
| 290 | git init --quiet "$w/valley-work" |
| 291 | mkdir -p "$w/valley-work/policy/instance" |
| 292 | cat > "$w/valley-work/policy/instance/floor.cue" << 'CUE' |
| 293 | package verification |
| 294 | |
| 295 | floor: { |
| 296 | checks: {} |
| 297 | classes: {} |
| 298 | unclassified: {} |
| 299 | } |
| 300 | CUE |
| 301 | git -C "$w/valley-work" add -A |
| 302 | git -C "$w/valley-work" commit --quiet -m floor |
| 303 | git -C "$w/valley-work" push --quiet "$valley" HEAD:main |
| 304 | git checkout --quiet main |
| 305 | mkdir -p policy |
| 306 | echo "$valley" > policy/valley |
| 307 | git add policy/valley |
| 308 | git commit --quiet -m 'name the owning valley' |
| 309 | env -u VALLEY_PRINCIPAL git push --quiet origin main |
| 310 | |
| 311 | # write_record <json> <parent…>: a record commit holding the JSON, written |
| 312 | # by hand, the way anyone holding a key could write one. |
| 313 | write_record() { |
| 314 | local json="$1" blob tree parent |
| 315 | local -a parents=() |
| 316 | shift |
| 317 | for parent in "$@"; do parents+=(-p "$parent"); done |
| 318 | blob="$(printf '%s\n' "$json" | git hash-object -w --stdin)" |
| 319 | tree="$(printf '100644 blob %s\trecord.json\n' "$blob" | git mktree)" |
| 320 | git commit-tree "$tree" "${parents[@]}" -m 'a change record' |
| 321 | } |
| 322 | request=refs/the-valley/integration-requests/main/victim |
| 323 | no_request() { |
| 324 | if git -C "$origin" rev-parse --verify --quiet "$request" > /dev/null; then |
| 325 | fail "$1" |
| 326 | fi |
| 327 | } |
| 328 | |
| 329 | # The record names H on victim, and the same push puts B on victim. |
| 330 | target_id=ppppqqqqrrrrssssttttuuuuvvvvwwww |
| 331 | git checkout --quiet -b victim main |
| 332 | commit honest |
| 333 | honest="$(git rev-parse HEAD)" |
| 334 | commit substituted |
| 335 | substituted="$(git rev-parse HEAD)" |
| 336 | v1="$(jq -cn --arg id "$target_id" --arg head "git-sha1:$honest" '{schema: "valley-change-record/v1", |
| 337 | change_id: $id, target: "main", owner: "contributor", state: "ready", seq: 1, |
| 338 | versions: [{n: 1, head: $head, title: "honest", branch: "victim", uploader: "contributor"}], active: 1}')" |
| 339 | r1="$(write_record "$v1" "$honest")" |
| 340 | as contributor git push --quiet origin "$r1:refs/the-valley/changes/main/$target_id" \ |
| 341 | "$substituted:refs/heads/victim" |
| 342 | [ "$(git -C "$origin" rev-parse refs/heads/victim)" = "$substituted" ] || fail 'the branch did not move with the record' |
| 343 | |
| 344 | refused "records the head $honest, and origin/victim is at $substituted" as requester valley approve pppp |
| 345 | no_request 'approve filed a request for a branch tip no version records' |
| 346 | refused "origin/victim is at $substituted; nothing rebased" as requester valley rebase pppp |
| 347 | [ "$(git -C "$origin" rev-parse refs/heads/victim)" = "$substituted" ] || fail 'rebase moved the branch' |
| 348 | # review shows the recorded head, and none of what the branch added. |
| 349 | printf 's\n' | valley review pppp > "$w/out" 2> "$w/err" || fail 'review of a handle failed' |
| 350 | grep -q '^change pppp v1 at' "$w/out" || fail 'review did not say which version it shows' |
| 351 | grep -q ' honest$' "$w/out" || fail 'review did not show the recorded head' |
| 352 | if grep -q 'substituted' "$w/out"; then fail 'review showed the branch tip, not the recorded head'; fi |
| 353 | # Rejecting it there deletes nothing while the branch is elsewhere: the |
| 354 | # refusal names both commits, and the next answer goes to the prompt. |
| 355 | printf 'r\nyes\ns\n' | as requester valley review pppp > "$w/out" 2> "$w/err" \ |
| 356 | || fail 'review of a handle failed' |
| 357 | grep -qF "records the head $honest, and origin/victim is at $substituted; nothing deleted" "$w/err" \ |
| 358 | || fail 'reject did not refuse a branch whose tip is not the recorded head' |
| 359 | [ "$(git -C "$origin" rev-parse refs/heads/victim)" = "$substituted" ] \ |
| 360 | || fail 'reject deleted a branch whose tip is not the recorded head' |
| 361 | |
| 362 | # With the branch at the recorded head, approve files exactly that head. |
| 363 | as contributor git push --quiet --force origin "$honest:refs/heads/victim" |
| 364 | ok as requester valley approve pppp |
| 365 | [ "$(git -C "$origin" rev-parse "$request")" = "$honest" ] || fail 'approve did not file the recorded head' |
| 366 | |
| 367 | # A second agent acting as the same principal adds v2 at a head that is |
| 368 | # not on victim, while its record says victim. approve refuses, and the |
| 369 | # request stays where it was. |
| 370 | git checkout --quiet -b elsewhere main |
| 371 | commit elsewhere |
| 372 | elsewhere="$(git rev-parse HEAD)" |
| 373 | as contributor git push --quiet origin elsewhere |
| 374 | v2="$(jq -c --arg head "git-sha1:$elsewhere" '.versions += [{n: 2, head: $head, title: "elsewhere", |
| 375 | branch: "victim", uploader: "contributor"}] | .active = 2 | .seq = 2' <<< "$v1")" |
| 376 | r2="$(write_record "$v2" "$r1" "$elsewhere")" |
| 377 | as contributor git push --quiet origin "$r2:refs/the-valley/changes/main/$target_id" |
| 378 | refused "records the head $elsewhere, and origin/victim is at $honest" as requester valley approve pppp |
| 379 | [ "$(git -C "$origin" rev-parse "$request")" = "$honest" ] || fail 'a redirected version moved the request' |
| 380 | # With the branch at the recorded head, rejecting it in review deletes it. |
| 381 | as contributor git push --quiet --force origin "$elsewhere:refs/heads/victim" |
| 382 | printf 'r\nyes\n' | as requester valley review pppp > "$w/out" 2> "$w/err" \ |
| 383 | || fail 'rejecting a handle at its recorded head failed' |
| 384 | grep -qF 'rejected victim: deleted from origin' "$w/out" || fail 'reject did not say it deleted the branch' |
| 385 | if git -C "$origin" rev-parse --verify --quiet refs/heads/victim > /dev/null; then |
| 386 | fail 'reject kept a branch at the recorded head' |
| 387 | fi |
| 388 | |
| 389 | echo 'valley-ready: change records through real pushes passed' |
| 390 | touch "$out" |