gunk-dev/the-valley

web-push-e2e.sh

source262d8ca88ba9main's published tip history
18.3 KiB
1# End-to-end test script for the web push path (see web-push-e2e.nix).
2# Each background server is launched with start, which waits up to 30
3# seconds for its listening address, and the EXIT trap stops all running
4# servers when the script exits.
5set -euo pipefail
6
7export HOME="$TMPDIR/home"
8export XDG_CACHE_HOME="$TMPDIR/cache"
9export XDG_STATE_HOME="$TMPDIR/xdg-state"
10export GIT_CONFIG_NOSYSTEM=1
11export GIT_CONFIG_GLOBAL="$TMPDIR/gitconfig"
12printf '[user]\n\tname = operator\n\temail = operator@example.org\n[init]\n\tdefaultBranch = main\n' > "$GIT_CONFIG_GLOBAL"
13cd "$TMPDIR"
14
15fail() {
16 echo "web-push-e2e: $*" >&2
17 exit 1
18}
19
20# The servers still running, stopped on every exit.
21pids=()
22stop() {
23 local pid=$1 rest=() p
24 kill -TERM "$pid" 2> /dev/null || true
25 for _ in $(seq 150); do
26 kill -0 "$pid" 2> /dev/null || break
27 sleep 0.1
28 done
29 kill -KILL "$pid" 2> /dev/null || true
30 wait "$pid" 2> /dev/null || true
31 for p in "${pids[@]}"; do
32 if [ "$p" != "$pid" ]; then rest+=("$p"); fi
33 done
34 pids=("${rest[@]}")
35}
36cleanup() {
37 for pid in "${pids[@]}"; do stop "$pid"; done
38}
39trap cleanup EXIT
40
41# start NAME LOG COMMAND...: start a server that prints "NAME: listening
42# on ADDR" on stderr once it listens, and wait at most 30 seconds for it.
43# It sets pid and addr.
44start() {
45 local name=$1 log=$2
46 shift 2
47 "$@" 2> "$log" &
48 pid=$!
49 pids+=("$pid")
50 for _ in $(seq 300); do
51 addr=$(sed -n "s/^$name: listening on //p" "$log")
52 if [ -n "$addr" ]; then return; fi
53 kill -0 "$pid" 2> /dev/null || fail "$name exited before it listened: $(cat "$log")"
54 sleep 0.1
55 done
56 fail "$name did not say where it listens within 30 seconds"
57}
58
59# get PATH: valley-web's status and Location for a GET of PATH, with its
60# body in $TMPDIR/body. follow PATH: the same after following redirects.
61get() {
62 curl -sS --max-time 10 -o "$TMPDIR/body" -w '%{http_code} %header{location}' "http://$web$1"
63}
64follow() {
65 curl -sS --max-time 10 --max-redirs 3 -L -o "$TMPDIR/body" -w '%{http_code}' "http://$web$1"
66}
67# serves PATH TEXT: valley-web serves PATH, after its redirect to the
68# tree, with TEXT in the page. gone PATH: it answers 404.
69serves() {
70 local status
71 status=$(follow "$1")
72 [ "$status" = 200 ] || fail "valley-web answered $1 with $status, want 200"
73 grep -qF "$2" "$TMPDIR/body" || fail "valley-web's page for $1 does not show $2"
74}
75gone() {
76 local status
77 status=$(get "$1")
78 [ "$status" = "404 " ] || fail "valley-web answered $1 with $status, want 404"
79}
80
81# One pass of the publisher as the package wraps it, with its pinned git,
82# cue, valley CLI and schemas.
83publish() {
84 timeout -k 10 "${within:-300}" valley-web-publish --instance-repo "$TMPDIR/srv/valley.git" \
85 --instance valley-host --destination example.org --host "$TMPDIR/host.json" \
86 --state "$TMPDIR/publisher" --principals "$TMPDIR/principals.json" "$@"
87}
88
89commit() {
90 git -C "work/$1" add --all
91 git -C "work/$1" commit --quiet -m "$1: $2"
92 git -C "work/$1" push --quiet origin main
93}
94
95# The projects' publication identities, which their policy/read declares
96# and the registry's grants name.
97declare -A id=(
98 [valley]=f83927d5a4ed068cdd02a8cdaa09f26d
99 [app]=9da6809f038df92138c131cd16bed76d
100 [site]=273d5668702918ea7ae079de374db96b
101)
102read_policy() {
103 printf 'package read\n\nproject: "%s"\nidentity: "%s"\nreaders: {%s}\n' "$1" "${id[$1]}" "$2"
104}
105# The instance's registry: the operator governs it, and anyone may read
106# the projects of the scope given at the edge.
107registry() {
108 cat << EOF
109package identity
110
111boundaries: {
112 registry: kind: "registry"
113 web: {kind: "edge-read", destination: "example.org"}
114}
115genesis: "operator"
116principals: operator: {
117 kind: "human"
118 keys: [{class: "ssh-ed25519", bound: "hardware", public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyForARejectedFixtureAAAAAAAAAAA"}]
119 grants: {
120 govern: boundary: "registry"
121 read: {boundary: "web", projects: "all"}
122 }
123}
124anyone: grants: read: {boundary: "web", projects: $1}
125EOF
126}
127
128# 1. The instance and its two projects, each with its own README, and both
129# readable by anyone, in the group tools. app has the short alias a, and
130# site the former name homepage.
131for name in valley app site; do
132 git init --quiet --bare "srv/$name.git"
133 git init --quiet "work/$name"
134 git -C "work/$name" remote add origin "$TMPDIR/srv/$name.git"
135 git -C "work/$name" config valley.principal operator
136done
137mkdir -p work/valley/policy/instance work/valley/policy/read work/valley/identity
138echo 'package verification' > work/valley/policy/instance/floor.cue
139read_policy valley 'operator: true' > work/valley/policy/read/read.cue
140registry "{app: \"${id[app]}\", site: \"${id[site]}\"}" > work/valley/identity/registry.cue
141commit valley "the instance"
142projects=$(printf 'app\nsite\nvalley\n' | git -C srv/valley.git hash-object -w --stdin)
143git -C srv/valley.git update-ref refs/the-valley/projects "$projects"
144for name in app site; do
145 mkdir -p "work/$name/policy/read"
146 echo "$TMPDIR/srv/valley.git" > "work/$name/policy/valley"
147 printf '# %s\n\n%s-readme-text\n' "$name" "$name" > "work/$name/README.md"
148 read_policy "$name" 'anyone: true, operator: true' > "work/$name/policy/read/read.cue"
149 commit "$name" "the first commit"
150done
151knot=$(cd work/app && valley knot new outcome "Open the edge" --json | jq -r .knot.id)
152
153cat > host.cue << 'EOF'
154package valley
155
156groups: core: name: "tools"
157projects: app: {group: "core", aliases: ["a"]}
158projects: site: {group: "core", formerNames: ["homepage"]}
159projects: valley: {}
160EOF
161cue export --out json "$schema" host.cue > host.json
162echo '{"operator": "operator"}' > principals.json
163
164# 2. A pass to a directory makes the export key, whose verifier line the
165# receiver pins from its first start. Then the receiver and valley-web
166# start over one state directory, and a pass uploads.
167publish --out first > /dev/null
168test -s publisher/verifier || fail "the first pass made no verifier line"
169echo edge-token-0123456789abcdef0123456789 > token
170echo another-token-0123456789abcdef0123456 > wrong
171receive() {
172 start valley-web-receive receive.log valley-web-receive --state edge/store --listen 127.0.0.1:0 \
173 --instance valley-host --destination example.org --verifiers publisher/verifier --token-file token
174 receiver_pid=$pid receiver=http://$addr
175}
176serve() {
177 start valley-web web.log valley-web --state edge/store --listen 127.0.0.1:0
178 web_pid=$pid web=$addr
179}
180receive
181publish --receiver "$receiver" --receiver-token token > pass.out
182grep -q "the receiver stored authorization 2: anyone may read app, site" pass.out ||
183 fail "the pass did not say the receiver stored its authorization: $(cat pass.out)"
184grep -q "the receiver activated sequence" pass.out || fail "the pass did not say the receiver activated it: $(cat pass.out)"
185test -f edge/store && test -d edge/v2/generations && test -f edge/v2/authorization ||
186 fail "the receiver did not make the store's descriptor, generations and authorization"
187serve
188serves /tools/app app-readme-text
189serves /tools/site site-readme-text
190serves "/tools/app/knots/$knot" "Open the edge"
191[ "$(get "/a/knots/$knot?x=1")" = "308 /tools/app/knots/$knot?x=1" ] ||
192 fail "the alias a answered $(get "/a/knots/$knot?x=1")"
193[ "$(get /tools/homepage/source/)" = "308 /tools/site/source/" ] ||
194 fail "the former name homepage answered $(get /tools/homepage/source/)"
195
196# A reader downloads app's source from the link on its tree page: a
197# gzipped tar archive of main's tip that lists main's files and
198# directories, each under one directory named for the project and the
199# commit.
200serves /tools/app app-readme-text
201tip=$(git -C work/app rev-parse HEAD)
202grep -qF "href=\"/tools/app/source/archive/$tip.tar.gz\">Download .tar.gz<" "$TMPDIR/body" ||
203 fail "app's tree page does not link the archive of main's tip"
204status=$(curl -sS --max-time 10 -o app.tar.gz -w '%{http_code} %header{content-type}' "http://$web/tools/app/source/archive/$tip.tar.gz")
205[ "$status" = "200 application/gzip" ] || fail "valley-web answered app's archive with $status"
206prefix=app-${tip:0:12}/
207tar -tzf app.tar.gz | LC_ALL=C sort > listed
208{
209 echo "$prefix"
210 git -C work/app ls-tree -r -t --format='%(objecttype) %(path)' HEAD |
211 sed -e "s|^tree \(.*\)|$prefix\1/|" -e "s|^blob \(.*\)|$prefix\1|"
212} | LC_ALL=C sort > want
213cmp -s listed want || fail "app's archive lists $(cat listed), not $(cat want)"
214
215# main.tar.gz is one address for the archive of main's tip, wherever main
216# is: a temporary redirect to that archive, which curl -L follows to the
217# same bytes.
218[ "$(get /tools/app/source/archive/main.tar.gz)" = "302 /tools/app/source/archive/$tip.tar.gz" ] ||
219 fail "app's main.tar.gz answered $(get /tools/app/source/archive/main.tar.gz)"
220status=$(curl -sS --max-time 10 --max-redirs 3 -L -o main.tar.gz -w '%{http_code} %header{content-type}' "http://$web/tools/app/source/archive/main.tar.gz")
221[ "$status" = "200 application/gzip" ] || fail "valley-web answered app's main.tar.gz with $status"
222cmp -s main.tar.gz app.tar.gz || fail "app's main.tar.gz is not the archive of main's tip"
223
224# The pages say when the policy was last checked, apart from when the
225# plan was collected. A pass that finds nothing changed sends no content,
226# and still sends an authorization, signed again with a later sequence and
227# a later check, so the time the pages show advances while export.json
228# keeps its bytes.
229checked_of() { sed -n 's/^checked //p' "$1"; }
230sequence_of() { sed -n 's/^sequence //p' "$1"; }
231shown() { date -u -d "$1" '+%Y-%m-%d %H:%M UTC'; }
232serves / "Policy last checked $(shown "$(checked_of edge/v2/authorization)")"
233grep -qF "Collected " "$TMPDIR/body" || fail "the plan page does not say when the plan was collected"
234digest=$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)
235before=$(checked_of edge/v2/authorization)
236sleep 1.1
237publish --receiver "$receiver" --receiver-token token > pass.out
238grep -q "nothing has changed since; nothing is sent" pass.out || fail "an unchanged pass sent content: $(cat pass.out)"
239[ "$(sequence_of edge/v2/authorization)" = 3 ] || fail "an unchanged pass stored authorization $(sequence_of edge/v2/authorization), want 3"
240[ "$(checked_of edge/v2/authorization)" != "$before" ] || fail "an unchanged pass did not advance the policy check from $before"
241[ "$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)" = "$digest" ] ||
242 fail "an unchanged pass changed export.json"
243serves / "Policy last checked $(shown "$(checked_of edge/v2/authorization)")"
244
245# An authorization the receiver stored already is refused as replayed,
246# and one whose text was changed is refused for its signature, whatever
247# its sequence.
248post() {
249 curl -sS --max-time 10 -o "$TMPDIR/reply" -w '%{http_code}' -H "Authorization: Bearer $(cat token)" \
250 --data-binary "@$2" "$receiver$1"
251}
252[ "$(post /web-export/v2/authorization publisher/authorization)" = 409 ] && grep -q '"error":"replayed"' reply ||
253 fail "a replayed authorization was not refused as replayed: $(cat reply)"
254sed 's/^sequence .*/sequence 99/' publisher/authorization > forged
255[ "$(post /web-export/v2/authorization forged)" = 403 ] && grep -q '"error":"bad-signature"' reply ||
256 fail "an authorization whose text was changed was not refused for its signature: $(cat reply)"
257
258# A publisher of the first protocol is refused with a clear error, and
259# valley-web refuses the directory the first store layout took as its
260# state.
261[ "$(post /web-export/v1/manifest forged)" = 410 ] && grep -q '"error":"unsupported-version"' reply ||
262 fail "a manifest of the first protocol was not refused as unsupported-version: $(cat reply)"
263if valley-web --state edge --listen 127.0.0.1:0 2> old.log; then fail "valley-web served the store's directory"; fi
264grep -q "is a directory" old.log || fail "valley-web refused the store's directory for another reason: $(cat old.log)"
265
266# 3. A comment changes the export, so the next pass uploads. With another
267# token it is refused as unauthenticated, and the edge keeps serving what
268# it had.
269(cd work/app && valley knot comment "$knot" -m "edge-comment-text" > /dev/null)
270if publish --receiver "$receiver" --receiver-token wrong > pass.out 2> pass.err; then
271 fail "a pass with another token succeeded"
272fi
273grep -q unauthenticated pass.err || fail "a pass with another token failed for another reason: $(cat pass.err)"
274serves "/tools/app/knots/$knot" "Open the edge"
275if grep -qF edge-comment-text "$TMPDIR/body"; then fail "the refused pass's comment is served"; fi
276
277# 4. Withdrawing anyone's grant for app takes app off the edge on the
278# next pass, its alias with it, and site stays.
279registry "{site: \"${id[site]}\"}" > work/valley/identity/registry.cue
280commit valley "withdraw app"
281publish --receiver "$receiver" --receiver-token token > /dev/null
282gone /tools/app
283gone /a
284serves /tools/site site-readme-text
285
286# 4b. A withdrawal by authorization alone, while content work is
287# stalled. With app granted again, a new knot changes the export, and a
288# pass reads it, site included, then stalls at its upload: the test holds
289# the receiver's activation lock, as a commit that checks a large
290# repository would. Meanwhile site's grant is withdrawn, and a
291# --policy-only pass takes site off the edge at once: valley-web, not
292# restarted, answers the next request without it. Once the lock is
293# released, the stalled upload, whose content holds site and whose
294# sequence is later, activates and restores nothing. The receiver deletes
295# site's repository, and the active export.json keeps its signed bytes.
296registry "{app: \"${id[app]}\", site: \"${id[site]}\"}" > work/valley/identity/registry.cue
297commit valley "grant app again"
298publish --receiver "$receiver" --receiver-token token > /dev/null
299serves /tools/app app-readme-text
300(cd work/site && valley knot new outcome "Withdrawn while stalled" > /dev/null)
301flock -o edge/v2/lock sleep 300 &
302holder=$!
303pids+=("$holder")
304publish --receiver "$receiver" --receiver-token token > stalled.out 2> stalled.err &
305stalled=$!
306for _ in $(seq 600); do
307 grep -q "^valley-web-publish: public: app, site" stalled.out && break
308 kill -0 "$stalled" 2> /dev/null || fail "the stalled pass ended before its upload: $(cat stalled.out stalled.err)"
309 sleep 0.1
310done
311grep -q "^valley-web-publish: public: app, site" stalled.out || fail "the stalled pass did not read the valley within a minute"
312sleep 1
313registry "{app: \"${id[app]}\"}" > work/valley/identity/registry.cue
314commit valley "withdraw site"
315within=40 publish --receiver "$receiver" --receiver-token token --policy-only > policy.out ||
316 fail "a --policy-only pass failed while content was stalled"
317grep -q "anyone may read app$" policy.out || fail "the --policy-only pass did not withdraw site: $(cat policy.out)"
318if grep -q "the receiver activated" policy.out; then fail "the --policy-only pass sent content"; fi
319gone /tools/site
320gone /tools/homepage
321serves /tools/app app-readme-text
322kill -0 "$stalled" 2> /dev/null || fail "the stalled pass did not stall: $(cat stalled.out stalled.err)"
323stop "$holder"
324wait "$stalled" || fail "the stalled pass failed once its upload went on: $(cat stalled.err)"
325grep -q "the receiver activated sequence" stalled.out || fail "the stalled pass did not activate its upload: $(cat stalled.out)"
326grep -q '"project":"site"' edge/v2/current/export.json || fail "the stalled upload's export does not hold site"
327gone /tools/site
328digest=$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)
329for _ in $(seq 300); do
330 [ -e edge/v2/current/repos/site.git ] || break
331 sleep 0.1
332done
333[ ! -e edge/v2/current/repos/site.git ] || fail "the receiver kept the repository of site after its withdrawal"
334[ -e edge/v2/current/repos/app.git ] || fail "the receiver deleted the repository of app"
335[ "$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)" = "$digest" ] ||
336 fail "the withdrawal changed the signed export.json"
337
338# A store whose authorization is removed serves no project, though
339# valley-web has the export indexed already; once it is back, it serves.
340mv edge/v2/authorization authorization.kept
341[ "$(get /tools/app)" = "503 " ] || fail "with no authorization valley-web answered /tools/app with $(get /tools/app)"
342grep -qF "Nothing published yet" "$TMPDIR/body" || fail "with no authorization the page does not say nothing is published"
343if grep -qF app-readme-text "$TMPDIR/body"; then fail "with no authorization valley-web served app's README"; fi
344[ "$(get /)" = "503 " ] || fail "with no authorization valley-web answered / with $(get /)"
345mv authorization.kept edge/v2/authorization
346serves /tools/app app-readme-text
347
348# Granting site again, and withdrawing app as step 4 left it, serves site
349# once more: the receiver's reply says it no longer holds site's
350# repository, so the pass sends site's bundle again.
351registry "{site: \"${id[site]}\"}" > work/valley/identity/registry.cue
352commit valley "withdraw app again"
353publish --receiver "$receiver" --receiver-token token > pass.out
354grep -q "^valley-web-publish: sent .*site; the receiver activated" pass.out || fail "the pass did not send site's bundle again: $(cat pass.out)"
355serves /tools/site site-readme-text
356gone /tools/app
357
358# 5. Both servers restart onto the same content.
359stop "$receiver_pid"
360stop "$web_pid"
361receive
362serve
363gone /tools/app
364serves /tools/site site-readme-text
365
366# 6. With the receiver stopped, valley-web keeps serving. A pass that
367# withdraws site fails because it cannot send its authorization, not
368# because it failed earlier or hung until timeout ended it, and changes
369# nothing served. Once the receiver is back, the next pass checks the
370# policy again, signs a new authorization with a later sequence, and takes
371# site off the edge.
372stop "$receiver_pid"
373serves /tools/site site-readme-text
374read_policy site 'operator: true' > work/site/policy/read/read.cue
375commit site "withdraw it"
376status=0
377publish --receiver "$receiver" --receiver-token token > pass.out 2> pass.err || status=$?
378case $status in
379 0) fail "a pass to a stopped receiver succeeded" ;;
380 124 | 137) fail "a pass to a stopped receiver hung until timeout ended it (status $status)" ;;
381esac
382grep -q "sending authorization [0-9]* failed [0-9]* times: .*dial tcp ${receiver#http://}: connect: connection refused" pass.err ||
383 fail "a pass to a stopped receiver failed with status $status, but not because it could not connect: $(cat pass.err)"
384serves /tools/site site-readme-text
385unsent=$(sequence_of publisher/authorization)
386receive
387publish --receiver "$receiver" --receiver-token token > /dev/null
388[ "$(sequence_of edge/v2/authorization)" -gt "$unsent" ] ||
389 fail "the pass after the receiver came back stored authorization $(sequence_of edge/v2/authorization), not one signed after $unsent"
390gone /tools/site
391gone /tools/homepage
392gone /tools/app
393
394touch "$out"