gunk-dev/the-valley

web-push-e2e.sh

source57064e55f18fmain's published tip history
16.6 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# The pages say when the policy was last checked, apart from when the
197# plan was collected. A pass that finds nothing changed sends no content,
198# and still sends an authorization, signed again with a later sequence and
199# a later check, so the time the pages show advances while export.json
200# keeps its bytes.
201checked_of() { sed -n 's/^checked //p' "$1"; }
202sequence_of() { sed -n 's/^sequence //p' "$1"; }
203shown() { date -u -d "$1" '+%Y-%m-%d %H:%M UTC'; }
204serves / "Policy last checked $(shown "$(checked_of edge/v2/authorization)")"
205grep -qF "Collected " "$TMPDIR/body" || fail "the plan page does not say when the plan was collected"
206digest=$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)
207before=$(checked_of edge/v2/authorization)
208sleep 1.1
209publish --receiver "$receiver" --receiver-token token > pass.out
210grep -q "nothing has changed since; nothing is sent" pass.out || fail "an unchanged pass sent content: $(cat pass.out)"
211[ "$(sequence_of edge/v2/authorization)" = 3 ] || fail "an unchanged pass stored authorization $(sequence_of edge/v2/authorization), want 3"
212[ "$(checked_of edge/v2/authorization)" != "$before" ] || fail "an unchanged pass did not advance the policy check from $before"
213[ "$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)" = "$digest" ] ||
214 fail "an unchanged pass changed export.json"
215serves / "Policy last checked $(shown "$(checked_of edge/v2/authorization)")"
216
217# An authorization the receiver stored already is refused as replayed,
218# and one whose text was changed is refused for its signature, whatever
219# its sequence.
220post() {
221 curl -sS --max-time 10 -o "$TMPDIR/reply" -w '%{http_code}' -H "Authorization: Bearer $(cat token)" \
222 --data-binary "@$2" "$receiver$1"
223}
224[ "$(post /web-export/v2/authorization publisher/authorization)" = 409 ] && grep -q '"error":"replayed"' reply ||
225 fail "a replayed authorization was not refused as replayed: $(cat reply)"
226sed 's/^sequence .*/sequence 99/' publisher/authorization > forged
227[ "$(post /web-export/v2/authorization forged)" = 403 ] && grep -q '"error":"bad-signature"' reply ||
228 fail "an authorization whose text was changed was not refused for its signature: $(cat reply)"
229
230# A publisher of the first protocol is refused with a clear error, and
231# valley-web refuses the directory the first store layout took as its
232# state.
233[ "$(post /web-export/v1/manifest forged)" = 410 ] && grep -q '"error":"unsupported-version"' reply ||
234 fail "a manifest of the first protocol was not refused as unsupported-version: $(cat reply)"
235if valley-web --state edge --listen 127.0.0.1:0 2> old.log; then fail "valley-web served the store's directory"; fi
236grep -q "is a directory" old.log || fail "valley-web refused the store's directory for another reason: $(cat old.log)"
237
238# 3. A comment changes the export, so the next pass uploads. With another
239# token it is refused as unauthenticated, and the edge keeps serving what
240# it had.
241(cd work/app && valley knot comment "$knot" -m "edge-comment-text" > /dev/null)
242if publish --receiver "$receiver" --receiver-token wrong > pass.out 2> pass.err; then
243 fail "a pass with another token succeeded"
244fi
245grep -q unauthenticated pass.err || fail "a pass with another token failed for another reason: $(cat pass.err)"
246serves "/tools/app/knots/$knot" "Open the edge"
247if grep -qF edge-comment-text "$TMPDIR/body"; then fail "the refused pass's comment is served"; fi
248
249# 4. Withdrawing anyone's grant for app takes app off the edge on the
250# next pass, its alias with it, and site stays.
251registry "{site: \"${id[site]}\"}" > work/valley/identity/registry.cue
252commit valley "withdraw app"
253publish --receiver "$receiver" --receiver-token token > /dev/null
254gone /tools/app
255gone /a
256serves /tools/site site-readme-text
257
258# 4b. A withdrawal by authorization alone, while content work is
259# stalled. With app granted again, a new knot changes the export, and a
260# pass reads it, site included, then stalls at its upload: the test holds
261# the receiver's activation lock, as a commit that checks a large
262# repository would. Meanwhile site's grant is withdrawn, and a
263# --policy-only pass takes site off the edge at once: valley-web, not
264# restarted, answers the next request without it. Once the lock is
265# released, the stalled upload, whose content holds site and whose
266# sequence is later, activates and restores nothing. The receiver deletes
267# site's repository, and the active export.json keeps its signed bytes.
268registry "{app: \"${id[app]}\", site: \"${id[site]}\"}" > work/valley/identity/registry.cue
269commit valley "grant app again"
270publish --receiver "$receiver" --receiver-token token > /dev/null
271serves /tools/app app-readme-text
272(cd work/site && valley knot new outcome "Withdrawn while stalled" > /dev/null)
273flock -o edge/v2/lock sleep 300 &
274holder=$!
275pids+=("$holder")
276publish --receiver "$receiver" --receiver-token token > stalled.out 2> stalled.err &
277stalled=$!
278for _ in $(seq 600); do
279 grep -q "^valley-web-publish: public: app, site" stalled.out && break
280 kill -0 "$stalled" 2> /dev/null || fail "the stalled pass ended before its upload: $(cat stalled.out stalled.err)"
281 sleep 0.1
282done
283grep -q "^valley-web-publish: public: app, site" stalled.out || fail "the stalled pass did not read the valley within a minute"
284sleep 1
285registry "{app: \"${id[app]}\"}" > work/valley/identity/registry.cue
286commit valley "withdraw site"
287within=40 publish --receiver "$receiver" --receiver-token token --policy-only > policy.out ||
288 fail "a --policy-only pass failed while content was stalled"
289grep -q "anyone may read app$" policy.out || fail "the --policy-only pass did not withdraw site: $(cat policy.out)"
290if grep -q "the receiver activated" policy.out; then fail "the --policy-only pass sent content"; fi
291gone /tools/site
292gone /tools/homepage
293serves /tools/app app-readme-text
294kill -0 "$stalled" 2> /dev/null || fail "the stalled pass did not stall: $(cat stalled.out stalled.err)"
295stop "$holder"
296wait "$stalled" || fail "the stalled pass failed once its upload went on: $(cat stalled.err)"
297grep -q "the receiver activated sequence" stalled.out || fail "the stalled pass did not activate its upload: $(cat stalled.out)"
298grep -q '"project":"site"' edge/v2/current/export.json || fail "the stalled upload's export does not hold site"
299gone /tools/site
300digest=$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)
301for _ in $(seq 300); do
302 [ -e edge/v2/current/repos/site.git ] || break
303 sleep 0.1
304done
305[ ! -e edge/v2/current/repos/site.git ] || fail "the receiver kept the repository of site after its withdrawal"
306[ -e edge/v2/current/repos/app.git ] || fail "the receiver deleted the repository of app"
307[ "$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)" = "$digest" ] ||
308 fail "the withdrawal changed the signed export.json"
309
310# A store whose authorization is removed serves no project, though
311# valley-web has the export indexed already; once it is back, it serves.
312mv edge/v2/authorization authorization.kept
313[ "$(get /tools/app)" = "503 " ] || fail "with no authorization valley-web answered /tools/app with $(get /tools/app)"
314grep -qF "Nothing published yet" "$TMPDIR/body" || fail "with no authorization the page does not say nothing is published"
315if grep -qF app-readme-text "$TMPDIR/body"; then fail "with no authorization valley-web served app's README"; fi
316[ "$(get /)" = "503 " ] || fail "with no authorization valley-web answered / with $(get /)"
317mv authorization.kept edge/v2/authorization
318serves /tools/app app-readme-text
319
320# Granting site again, and withdrawing app as step 4 left it, serves site
321# once more: the receiver's reply says it no longer holds site's
322# repository, so the pass sends site's bundle again.
323registry "{site: \"${id[site]}\"}" > work/valley/identity/registry.cue
324commit valley "withdraw app again"
325publish --receiver "$receiver" --receiver-token token > pass.out
326grep -q "^valley-web-publish: sent .*site; the receiver activated" pass.out || fail "the pass did not send site's bundle again: $(cat pass.out)"
327serves /tools/site site-readme-text
328gone /tools/app
329
330# 5. Both servers restart onto the same content.
331stop "$receiver_pid"
332stop "$web_pid"
333receive
334serve
335gone /tools/app
336serves /tools/site site-readme-text
337
338# 6. With the receiver stopped, valley-web keeps serving. A pass that
339# withdraws site fails because it cannot send its authorization, not
340# because it failed earlier or hung until timeout ended it, and changes
341# nothing served. Once the receiver is back, the next pass checks the
342# policy again, signs a new authorization with a later sequence, and takes
343# site off the edge.
344stop "$receiver_pid"
345serves /tools/site site-readme-text
346read_policy site 'operator: true' > work/site/policy/read/read.cue
347commit site "withdraw it"
348status=0
349publish --receiver "$receiver" --receiver-token token > pass.out 2> pass.err || status=$?
350case $status in
351 0) fail "a pass to a stopped receiver succeeded" ;;
352 124 | 137) fail "a pass to a stopped receiver hung until timeout ended it (status $status)" ;;
353esac
354grep -q "sending authorization [0-9]* failed [0-9]* times: .*dial tcp ${receiver#http://}: connect: connection refused" pass.err ||
355 fail "a pass to a stopped receiver failed with status $status, but not because it could not connect: $(cat pass.err)"
356serves /tools/site site-readme-text
357unsent=$(sequence_of publisher/authorization)
358receive
359publish --receiver "$receiver" --receiver-token token > /dev/null
360[ "$(sequence_of edge/v2/authorization)" -gt "$unsent" ] ||
361 fail "the pass after the receiver came back stored authorization $(sequence_of edge/v2/authorization), not one signed after $unsent"
362gone /tools/site
363gone /tools/homepage
364gone /tools/app
365
366touch "$out"