gunk-dev/the-valley

knots-e2e.sh

source8c4ff6fd0e3bmain's published tip history
15.5 KiB
1# shellcheck shell=bash
2# The plan graph through the real host hook: bare repositories wired with
3# the pre-receive hook the protected host's valley-init renders, written by
4# the real valley knot as each principal, and pushed crafted commits that
5# break each rule the hook holds a push to refs/the-valley/knots to. Every
6# refused push must leave the ref where it was.
7# Paths and fixtures come from the derivation environment.
8# shellcheck disable=SC2154
9set -euo pipefail
10
11w="$TMPDIR/knots-e2e"
12mkdir -p "$w"
13export HOME="$w"
14export XDG_CACHE_HOME="$w/cache"
15export GIT_CONFIG_NOSYSTEM=1
16git config --global user.name knots-e2e
17git config --global user.email knots-e2e@localhost
18git config --global init.defaultBranch main
19git config --global advice.detachedHead false
20
21fail() {
22 printf 'knots-e2e: %s\n' "$*" >&2
23 if [ -s "$w/err" ]; then cat "$w/err" >&2; fi
24 exit 1
25}
26
27# valley is bin/valley, run by bash, with the valley-go helper on PATH.
28mkdir -p "$w/bin"
29printf '#!%s\nexec %q %q "$@"\n' "$BASH" "$BASH" "$cliScript" > "$w/bin/valley"
30chmod +x "$w/bin/valley"
31export PATH="$w/bin:$PATH"
32
33# A bare repo wired as valley-init wires it, with the hook followed from
34# the rendered init script, as protect-e2e wires it. No verifier keys are
35# named: no attestation is accepted here.
36serve() {
37 local hook
38 hook="$(grep -o "/nix/store/[^ ]*-valley-protect-$1" "$initScriptPath" | head -n1)"
39 test -x "$hook"
40 git init --quiet --bare "$w/$1.git"
41 sed "s|/var/lib/valley-instance/known_signers|$w/no_such_keys|" "$hook" > "$w/$1.git/hooks/pre-receive"
42 chmod +x "$w/$1.git/hooks/pre-receive"
43}
44serve guarded
45serve open
46
47# Pushing as a principal is pushing with the tag on, as protect-e2e does.
48as() {
49 local who="$1"
50 shift
51 if [ "$who" = anonymous ]; then
52 env -u VALLEY_PRINCIPAL "$@"
53 else
54 VALLEY_PRINCIPAL="$who" "$@"
55 fi
56}
57tip() { git -C "$w/$1.git" rev-parse --verify --quiet refs/the-valley/knots || true; }
58
59git init --quiet "$w/work"
60cd "$w/work"
61git remote add origin "$w/guarded.git"
62git remote add open "$w/open.git"
63
64# ----------------------------------------------------------------------
65# The real CLI, as each principal: patrick creates the graph, klaus-env
66# adds to it. The hook admits every write.
67git config valley.principal patrick
68as patrick valley knot new outcome "Slice one lands" > "$w/out" 2> "$w/err" || fail "patrick could not create the graph"
69milestone="oc-$(printf '%s' slice-one-lands | sha256sum | cut -c1-7)"
70created="$(tip guarded)"
71[ -n "$created" ] || fail "the create did not land"
72as patrick valley knot new bug "The hook reads too much" --parent "$milestone" > "$w/out" 2> "$w/err" \
73 || fail "patrick could not update the graph"
74bug="bd-$(printf '%s' the-hook-reads-too-much | sha256sum | cut -c1-7)"
75as patrick valley knot comment "$bug" -m "Seen on a large graph." > "$w/out" 2> "$w/err" || fail "patrick could not comment"
76git config valley.principal klaus-env
77as klaus-env valley knot new outcome "Write the hook case" --parent "$milestone" --blocked-by "$bug" \
78 > "$w/out" 2> "$w/err" || fail "klaus-env could not add a knot"
79work="oc-$(printf '%s' write-the-hook-case | sha256sum | cut -c1-7)"
80as klaus-env valley knot claim "$work" --run 20261015-0058-feb0a926 > "$w/out" 2> "$w/err" || fail "klaus-env could not claim"
81as klaus-env valley knot set "$bug" status=closed > "$w/out" 2> "$w/err" || fail "klaus-env could not change patrick's knot"
82as klaus-env valley knot comment "$work" -m "Claimed." > "$w/out" 2> "$w/err" || fail "klaus-env could not comment"
83[ "$(git -C "$w/guarded.git" log --format=%cn refs/the-valley/knots | sort -u | tr '\n' ' ')" = "klaus-env patrick " ] \
84 || fail "the graph's commits are not both principals'"
85
86# The CLI, written as a principal its key does not push as, is refused by
87# the hook, and says what the hook said.
88git config valley.principal patrick
89as klaus-env valley knot comment "$bug" -m "Forged." > "$w/out" 2> "$w/err" && fail "a write by a principal other than the pusher landed"
90grep -q 'origin refused the write' "$w/err" || fail "the CLI did not report the hook's refusal"
91grep -q 'patrick as its committer, and klaus-env is pushing it' "$w/err" || fail "the refusal does not name the committer"
92
93# ----------------------------------------------------------------------
94# Crafted commits. Each is made on a scratch index from the current tip,
95# with the edits given, by the committer given, and pushed as a principal.
96git fetch --quiet origin refs/the-valley/knots
97good="$(tip guarded)"
98# blob writes a file's content, given as command substitution leaves it:
99# without the one newline every canonical file ends with.
100blob() { printf '%s\n' "$1" | git hash-object -w --stdin; }
101# craft <parent|-> <committer> <message|-> <edit…>, where each edit is
102# +PATH=CONTENT, -PATH, or MODE:PATH=OID.
103craft() {
104 local parent="$1" committer="$2" message="$3" edit tree
105 shift 3
106 export GIT_INDEX_FILE="$w/scratch.index"
107 rm -f "$GIT_INDEX_FILE"
108 if [ "$parent" = - ]; then git read-tree --empty; else git read-tree "$parent"; fi
109 for edit in "$@"; do
110 case "$edit" in
111 +*) git update-index --add --cacheinfo "100644,$(blob "${edit#*=}"),$(p="${edit#+}" && echo "${p%%=*}")" ;;
112 -*) git update-index --force-remove -- "${edit#-}" ;;
113 *:*) git update-index --add --cacheinfo "${edit%%:*},${edit#*=},$(p="${edit#*:}" && echo "${p%%=*}")" ;;
114 esac
115 done
116 tree="$(git write-tree)"
117 unset GIT_INDEX_FILE
118 if [ "$message" = - ]; then message="$(printf 'crafted\n\nValley-Op: %016x' "$RANDOM$RANDOM")"; fi
119 local parents=()
120 if [ "$parent" != - ]; then parents=(-p "$parent"); fi
121 GIT_COMMITTER_NAME="$committer" git commit-tree "$tree" "${parents[@]}" -m "$message"
122}
123# refused <what> <ERE> <who> <git push args…>: the push fails, saying
124# something matching the pattern, and the ref stays where it was.
125refused() {
126 local what="$1" says="$2" who="$3" before
127 shift 3
128 before="$(tip guarded)"
129 if as "$who" git push --quiet "$@" 2> "$w/err"; then fail "$what was admitted"; fi
130 grep -qE -- "$says" "$w/err" || fail "$what was refused without saying $says"
131 [ "$(tip guarded)" = "$before" ] || fail "$what moved the ref"
132}
133push_graph() { echo "$1:refs/the-valley/knots"; }
134
135knot_path() { git ls-tree -r --name-only "$good" | grep -m1 -- "/$1-"; }
136bug_path="$(knot_path "$bug")"
137work_path="$(knot_path "$work")"
138comment_path="$(git ls-tree -r --name-only "$good" -- "comments/$bug/" | head -n1)"
139bug_text="$(git cat-file blob "$good:$bug_path")"
140work_text="$(git cat-file blob "$good:$work_path")"
141stamp="20261015T120000Z"
142comment() { printf -- '---\nauthor: "%s"\n---\n\n%s\n' "$1" "$2"; }
143idea() { # idea <title> <slug> <creator> [body]
144 local id
145 id="ida-$(printf '%s' "$2" | sha256sum | cut -c1-7)"
146 printf -- '---\ntype: idea\nid: %s\nstatus: raw\ntitle: "%s"\ncreated: 2026-10-15\ncreated_by: "%s"\n---\n' "$id" "$1" "$3"
147 if [ -n "${4:-}" ]; then printf '\n%s\n' "$4"; fi
148}
149idea_path() { echo "ideas/ida-$(printf '%s' "$1" | sha256sum | cut -c1-7)-$1.md"; }
150
151# A well-formed next commit is admitted, so every refusal below is the
152# hook's answer to the one thing each breaks.
153next="$(craft "$good" patrick - "+$(idea_path a-crafted-idea)=$(idea "A crafted idea" a-crafted-idea patrick)")"
154cp /dev/null "$w/err"
155
156# Who: an untagged key is no principal.
157refused "an untagged key's write" 'untagged key.*named principal' anonymous origin "$(push_graph "$next")"
158
159# Shape: one commit whose only parent is the old tip.
160older="$(git rev-parse "$good~1")"
161rewind="$(craft "$older" patrick - "+$(idea_path rewound)=$(idea Rewound rewound patrick)")"
162refused "a non-fast-forward update" 'only parent is the tip it replaces' patrick --force origin "$(push_graph "$rewind")"
163side="$(craft "$older" patrick - "+$(idea_path side)=$(idea Side side patrick)")"
164merge="$(GIT_COMMITTER_NAME=patrick git commit-tree "$next^{tree}" -p "$good" -p "$side" -m "$(printf 'merge\n\nValley-Op: 0123456789abcdef')")"
165refused "a merge" 'only parent is the tip it replaces' patrick origin "$(push_graph "$merge")"
166two="$(craft "$next" patrick - "+$(idea_path second)=$(idea Second second patrick)")"
167refused "two commits at once" 'only parent is the tip it replaces' patrick origin "$(push_graph "$two")"
168git commit --quiet --allow-empty -m unrelated
169stranger="$(craft "$(git rev-parse HEAD)" patrick - "+$(idea_path stranger)=$(idea Stranger stranger patrick)")"
170refused "a parent that is not the old tip" 'only parent is the tip it replaces' patrick --force origin "$(push_graph "$stranger")"
171tagged="$(printf 'object %s\ntype commit\ntag t\ntagger t <t> 0 +0000\n\nt\n' "$next" | git mktag)"
172refused "a tag object as the tip" 'points at a tag' patrick origin "$tagged:refs/the-valley/knots"
173
174# Every commit: committed by the pusher, and carrying its operation.
175forged="$(craft "$good" klaus-env - "+$(idea_path forged)=$(idea Forged forged klaus-env)")"
176refused "a commit by another committer" 'names klaus-env as its committer, and patrick is pushing it' patrick origin "$(push_graph "$forged")"
177untrailed="$(craft "$good" patrick "no trailer" "+$(idea_path untrailed)=$(idea Untrailed untrailed patrick)")"
178refused "a commit without its trailer" 'Valley-Op' patrick origin "$(push_graph "$untrailed")"
179
180# Transitions: comments stay, knots stay, and what never changes does not.
181refused "a deleted comment" 'a comment is never deleted' patrick origin "$(push_graph "$(craft "$good" patrick - "-$comment_path")")"
182refused "a changed comment" 'stays exactly as it was written' patrick origin \
183 "$(push_graph "$(craft "$good" patrick - "+$comment_path=$(comment patrick 'Rewritten.')")")"
184refused "a deleted knot" 'a knot is never deleted' patrick origin "$(push_graph "$(craft "$good" patrick - "-$bug_path")")"
185refused "a changed id" 'filename says id' patrick origin \
186 "$(push_graph "$(craft "$good" patrick - "+$bug_path=${bug_text/id: $bug/id: bd-0000000}")")"
187refused "a changed type" "id is \"$bug\", and an outcome's id is oc-" patrick origin \
188 "$(push_graph "$(craft "$good" patrick - "+$bug_path=${bug_text/type: bug/type: outcome}")")"
189refused "a changed created_by" 'changes created_by from "patrick" to "klaus-env"' klaus-env origin \
190 "$(push_graph "$(craft "$good" klaus-env - "+$bug_path=${bug_text/created_by: \"patrick\"/created_by: \"klaus-env\"}")")"
191
192# Provenance: a new comment's author and a new knot's creator are the
193# pusher.
194refused "a comment with a forged author" 'names klaus-env as its author, and patrick is pushing it' patrick origin \
195 "$(push_graph "$(craft "$good" patrick - "+comments/$bug/$stamp-0123456789abcdef.md=$(comment klaus-env 'Not mine.')")")"
196refused "a knot with a forged creator" 'names klaus-env in created_by, and patrick is pushing it' patrick origin \
197 "$(push_graph "$(craft "$good" patrick - "+$(idea_path forged)=$(idea Forged forged klaus-env)")")"
198
199# Layout: plain files, in the layout, within the bounds.
200knot_blob="$(blob "$(idea Linked linked patrick)")"
201refused "a symlink" 'mode 120000' patrick origin "$(push_graph "$(craft "$good" patrick - "120000:$(idea_path linked)=$knot_blob")")"
202refused "an executable" 'mode 100755' patrick origin "$(push_graph "$(craft "$good" patrick - "100755:$(idea_path linked)=$knot_blob")")"
203refused "a file outside the layout" 'outside the graph.s layout' patrick origin "$(push_graph "$(craft "$good" patrick - "+README.md=hello")")"
204refused "a directory outside the layout" 'no directory a graph holds' patrick origin \
205 "$(push_graph "$(craft "$good" patrick - "+tasks/$(basename "$(idea_path tasked)")=$(idea Tasked tasked patrick)")")"
206refused "a non-canonical knot" 'not in canonical form' patrick origin \
207 "$(push_graph "$(craft "$good" patrick - "+$(idea_path loose)=$(idea Loose loose patrick | sed 's/^title: "Loose"/title: Loose/')")")"
208big="$(head -c 70000 /dev/zero | tr '\0' x)"
209refused "an oversized knot" 'more than the 65536' patrick origin \
210 "$(push_graph "$(craft "$good" patrick - "+$(idea_path big)=$(idea Big big patrick "$big")")")"
211
212# A tree that names one subtree many times over is refused at a bound,
213# before it is walked.
214leaf_lines=""
215for i in $(seq 1 64); do
216 leaf_lines+="$(printf '100644 blob %s\t%s-%016x.md' "$(blob "$(comment patrick x)")" "$stamp" "$i")"$'\n'
217done
218leaf="$(printf '%s' "$leaf_lines" | git mktree)"
219comments_tree="$(for i in $(seq 1 1000); do printf '040000 tree %s\toc-%07x\n' "$leaf" "$i"; done | git mktree)"
220root="$(git ls-tree "$good" | grep -v $'\tcomments$' | { cat; printf '040000 tree %s\tcomments\n' "$comments_tree"; } | git mktree)"
221bomb="$(GIT_COMMITTER_NAME=patrick git commit-tree "$root" -p "$good" -m "$(printf 'bomb\n\nValley-Op: 0123456789abcdef')")"
222start="$(date +%s)"
223refused "a shared-subtree bomb" 'entries in all' patrick origin "$(push_graph "$bomb")"
224[ $(($(date +%s) - start)) -lt 10 ] || fail "the bomb took more than 10 seconds to refuse"
225
226# Edges: every edge resolves, a parent is an outcome, and no edge forms a
227# cycle. A cycle is crafted, since the CLI refuses to write one.
228refused "a blocker that is no knot" 'is no knot in the graph' patrick origin \
229 "$(push_graph "$(craft "$good" patrick - "+$work_path=${work_text/blocked_by: \[$bug\]/blocked_by: [$bug, oc-0000000]}")")"
230loop_bug="${bug_text/blocked_by: \[\]/blocked_by: [$work]}"
231[ "$loop_bug" != "$bug_text" ] || fail "the bug's blocked_by could not be edited"
232refused "a cycle" 'blocked_by edges form a cycle' patrick origin "$(push_graph "$(craft "$good" patrick - "+$bug_path=$loop_bug")")"
233
234# The ref: never deleted, and matched exactly.
235refused "a deletion of the graph" 'never deleted' patrick origin --delete refs/the-valley/knots
236refused "a ref that only begins with the graph's name" 'written only by the machinery' patrick origin "$next:refs/the-valley/knots-x"
237refused "a ref below the graph's name" 'written only by the machinery' patrick origin "$next:refs/the-valley/knots/x"
238
239# Create: one commit with no parent, on a project that has no graph yet.
240first="$(craft - patrick - "+$(idea_path first)=$(idea First first patrick)")"
241child="$(craft "$first" patrick - "+$(idea_path second)=$(idea Second second patrick)")"
242if as patrick git push --quiet open "$(push_graph "$child")" 2> "$w/err"; then fail "a create with a parent was admitted"; fi
243grep -q 'creates the graph has no parent' "$w/err" || fail "a create with a parent was refused for another reason"
244[ -z "$(tip open)" ] || fail "a refused create made the ref"
245as patrick git push --quiet open "$(push_graph "$first")" 2> "$w/err" || fail "a parentless create was refused"
246as patrick git push --quiet open "$(push_graph "$child")" 2> "$w/err" || fail "the update after it was refused"
247[ "$(tip open)" = "$child" ] || fail "the open project's graph is not at the update"
248
249# A push that carries a valid graph update with an invalid write to
250# another namespace is refused whole, atomic or not: a change record that
251# is no record, an attestation no key here accepts, and an integration
252# request from a principal without the request grant.
253record="refs/the-valley/changes/main/$(printf 'k%.0s' $(seq 1 32))"
254attestation="refs/the-valley/attestations/$(printf '%064d' 0)/00000000"
255request="refs/the-valley/integration-requests/main/topic"
256for other in "$next:$record" "$next:$attestation" "HEAD:$request"; do
257 for mode in --atomic --no-atomic; do
258 refused "a graph update beside $other ($mode)" 'refs/the-valley/(changes|attestations|integration-requests)/' \
259 patrick "$mode" origin "$(push_graph "$next")" "$other"
260 if git -C "$w/guarded.git" rev-parse --verify --quiet "${other#*:}" > /dev/null; then fail "$other landed"; fi
261 done
262done
263
264# The well-formed commit itself is admitted.
265as patrick git push --quiet origin "$(push_graph "$next")" 2> "$w/err" || fail "a well-formed update was refused"
266[ "$(tip guarded)" = "$next" ] || fail "the well-formed update did not land"
267touch "$out"