gunk-dev/the-valley

web-push-e2e.sh

sourcef961a59e125emain's published tip history
54.4 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" --links "$TMPDIR/links/links" "$@"
87}
88# The link command, with the flags each verb reads; issued PRINCIPAL
89# issues a link and prints its ID alone.
90link() {
91 valley-web-publish link "$@" --links "$TMPDIR/links/links" --state "$TMPDIR/publisher"
92}
93issue_link() {
94 link issue "$@" --instance-repo "$TMPDIR/srv/valley.git" --destination example.org --host "$TMPDIR/host.json"
95}
96issued() { issue_link --principal "$1" 2> /dev/null | sed 's/.*#\(lk-[0-9a-f]*\)\..*/\1/'; }
97bindings() { grep -c '^binding ' edge/v2/authorization || true; }
98
99commit() {
100 git -C "work/$1" add --all
101 git -C "work/$1" commit --quiet -m "$1: $2"
102 git -C "work/$1" push --quiet origin main
103}
104
105# The projects' publication identities, which their policy/read declares
106# and the registry's grants name.
107declare -A id=(
108 [valley]=f83927d5a4ed068cdd02a8cdaa09f26d
109 [app]=9da6809f038df92138c131cd16bed76d
110 [site]=273d5668702918ea7ae079de374db96b
111 [docs]=5b1e0c7a9f3d2e4c6a8b0d1f3e5c7a92
112 [notes]=8e3b6f1d2a4c7e9b0d5f3a1c6e8b2d47
113)
114read_policy() {
115 printf 'package read\n\nproject: "%s"\nidentity: "%s"\nreaders: {%s}\n' "$1" "${id[$1]}" "$2"
116}
117# The instance's registry: the operator governs it. At the edge, anyone
118# may read the projects of the first scope given; alice, who holds no key,
119# those of the second; and the operator those of the third. alice and the
120# operator may read every project when their scope is not given.
121registry() {
122 local alice=${2:-'"all"'} operator=${3:-'"all"'}
123 cat << EOF
124package identity
125
126boundaries: {
127 registry: kind: "registry"
128 web: {kind: "edge-read", destination: "example.org"}
129}
130genesis: "operator"
131principals: operator: {
132 kind: "human"
133 keys: [{class: "ssh-ed25519", bound: "hardware", public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyForARejectedFixtureAAAAAAAAAAA"}]
134 grants: {
135 govern: boundary: "registry"
136 read: {boundary: "web", projects: $operator}
137 }
138}
139principals: alice: {kind: "human", keys: [], grants: read: {boundary: "web", projects: $alice}}
140anyone: grants: read: {boundary: "web", projects: $1}
141EOF
142}
143
144# 1. The instance and four projects in the group tools, each with its own
145# Markdown README, which links its read policy. app and site are public:
146# anyone may read them, and app's policy/read lets alice and the operator
147# read it too. docs is private: its policy/read lets alice and the
148# operator alone read it. notes is private, and has no effective reader:
149# its policy/read lets bob alone read it, and no grant names bob. app has
150# the short alias a, site the former name homepage, and docs the short
151# alias d.
152for name in valley app site docs notes; do
153 git init --quiet --bare "srv/$name.git"
154 git init --quiet "work/$name"
155 git -C "work/$name" remote add origin "$TMPDIR/srv/$name.git"
156 git -C "work/$name" config valley.principal operator
157done
158mkdir -p work/valley/policy/instance work/valley/policy/read work/valley/identity
159echo 'package verification' > work/valley/policy/instance/floor.cue
160read_policy valley 'operator: true' > work/valley/policy/read/read.cue
161registry "{app: \"${id[app]}\", site: \"${id[site]}\"}" > work/valley/identity/registry.cue
162commit valley "the instance"
163projects=$(printf 'app\ndocs\nnotes\nsite\nvalley\n' | git -C srv/valley.git hash-object -w --stdin)
164git -C srv/valley.git update-ref refs/the-valley/projects "$projects"
165declare -A readers=(
166 [app]='anyone: true, operator: true, alice: true'
167 [site]='anyone: true'
168 [docs]='alice: true, operator: true'
169 [notes]='bob: true'
170)
171for name in app site docs notes; do
172 mkdir -p "work/$name/policy/read"
173 echo "$TMPDIR/srv/valley.git" > "work/$name/policy/valley"
174 printf '# %s\n\n%s-readme-text, under [its read policy](policy/read/read.cue).\n' "$name" "$name" > "work/$name/README.md"
175 read_policy "$name" "${readers[$name]}" > "work/$name/policy/read/read.cue"
176 commit "$name" "the first commit"
177done
178knot=$(cd work/app && valley knot new outcome "Open the edge" --json | jq -r .knot.id)
179declare -A private_knot
180for name in docs notes; do
181 private_knot[$name]=$(cd "work/$name" && valley knot new outcome "$name-knot-text" --json | jq -r .knot.id)
182done
183
184cat > host.cue << 'EOF'
185package valley
186
187groups: core: name: "tools"
188projects: app: {group: "core", aliases: ["a"]}
189projects: site: {group: "core", formerNames: ["homepage"]}
190projects: docs: {group: "core", aliases: ["d"]}
191projects: notes: {group: "core"}
192projects: valley: {}
193EOF
194cue export --out json "$schema" host.cue > host.json
195echo '{"operator": "operator"}' > principals.json
196
197# 2. A pass to a directory makes the export key, whose verifier line the
198# receiver pins from its first start. Then the receiver and valley-web
199# start over one state directory, and a pass uploads.
200publish --out first > /dev/null
201test -s publisher/verifier || fail "the first pass made no verifier line"
202echo edge-token-0123456789abcdef0123456789 > token
203echo another-token-0123456789abcdef0123456 > wrong
204# valley-web signs readers in at the origin https://example.org, and keeps
205# their sessions in edge/browser, which the deployment makes for it alone.
206# Before the receiver makes its store, valley-web cannot open it and
207# exits, for its service to start it again.
208mkdir -p edge
209mkdir -m 700 edge/browser
210if valley-web --state edge/store --listen 127.0.0.1:0 --origin https://example.org 2> early.log; then
211 fail "valley-web started before the receiver made its store"
212fi
213receive() {
214 start valley-web-receive receive.log valley-web-receive --state edge/store --listen 127.0.0.1:0 \
215 --instance valley-host --destination example.org --verifiers publisher/verifier --token-file token
216 receiver_pid=$pid receiver=http://$addr
217}
218serve() {
219 start valley-web web.log valley-web --state edge/store --listen 127.0.0.1:0 --origin https://example.org
220 web_pid=$pid web=$addr
221 cat web.log >> web.logs
222}
223receive
224publish --receiver "$receiver" --receiver-token token > pass.out
225grep -q "the receiver stored authorization 2: app to alice, anyone, operator; docs to alice, operator; site to anyone$" pass.out ||
226 fail "the pass did not say the receiver stored its authorization: $(cat pass.out)"
227grep -q "the receiver activated sequence" pass.out || fail "the pass did not say the receiver activated it: $(cat pass.out)"
228test -f edge/store && test -d edge/v2/generations && test -f edge/v2/authorization ||
229 fail "the receiver did not make the store's descriptor, generations and authorization"
230serve
231serves /tools/app app-readme-text
232# valley-web made its browser state when it started, under authorization
233# 2, which binds no link, and the state holds no link yet.
234[ "$(cat edge/browser/state)" = the-valley/web-reader-state/v1 ] ||
235 fail "valley-web did not make an empty browser state: $(cat edge/browser/state)"
236[ "$(stat -c %a edge/browser/state)" = 600 ] || fail "the browser state is mode $(stat -c %a edge/browser/state)"
237# The README renders as Markdown: its heading has an id, and its relative
238# link goes to the file's page at the same commit.
239app_tip=$(git -C srv/app.git rev-parse main)
240serves "/tools/app/source/tree/$app_tip/" '<h1 id="app">app <a class="anchor" href="#app">#</a></h1>'
241grep -qF "<a href=\"/tools/app/source/blob/$app_tip/policy/read/read.cue\">its read policy</a>" "$TMPDIR/body" ||
242 fail "app's README does not link its read policy at $app_tip: $(grep -F readme-text "$TMPDIR/body")"
243serves /tools/site site-readme-text
244# / lands on the source of the first project published, app, at its tip.
245[ "$(get /)" = "302 /tools/app/source/tree/$app_tip/" ] || fail "/ answered $(get /)"
246serves "/tools/app/knots/$knot" "Open the edge"
247[ "$(get "/a/knots/$knot?x=1")" = "308 /tools/app/knots/$knot?x=1" ] ||
248 fail "the alias a answered $(get "/a/knots/$knot?x=1")"
249[ "$(get /tools/homepage/source/)" = "308 /tools/site/source/" ] ||
250 fail "the former name homepage answered $(get /tools/homepage/source/)"
251
252# docs has two effective readers, so it reaches the edge with both in its
253# authorization: the store holds its repository and its knots. notes has
254# none, so nothing of it reaches the edge: no authorization names it, the
255# store holds no repository of it, and no export holds its name or its
256# knots.
257for reader in alice operator; do
258 grep -qx "reader docs $reader" edge/v2/authorization || fail "the authorization does not let $reader read docs"
259done
260grep -qx "project docs ${id[docs]}" edge/v2/authorization && ! grep -q "^reader docs anyone" edge/v2/authorization ||
261 fail "the authorization does not name docs for its named readers alone: $(cat edge/v2/authorization)"
262[ -d edge/v2/current/repos/docs.git ] || fail "the edge holds no repository of docs"
263grep -qF docs-knot-text edge/v2/current/export.json || fail "the edge's export.json does not hold docs' knot"
264if grep -q "notes" edge/v2/authorization; then fail "the authorization names notes, which has no effective reader"; fi
265[ ! -e edge/v2/current/repos/notes.git ] || fail "the edge holds a repository of notes"
266if grep -rqF -e notes-knot-text -e '"project":"notes"' edge; then fail "the edge holds the name or a knot of notes"; fi
267# An anonymous request gets 404 for both private projects on every route:
268# a project page, its source tree, blob, history and archives, a knot, and
269# docs' short alias.
270for name in docs notes; do
271 ptip=$(git -C "srv/$name.git" rev-parse main)
272 for path in "/tools/$name" "/tools/$name/" "/tools/$name/source/" "/tools/$name/source/tree/$ptip/" \
273 "/tools/$name/source/blob/$ptip/README.md" "/tools/$name/source/log/$ptip" \
274 "/tools/$name/source/archive/$ptip.tar.gz" "/tools/$name/source/archive/main.tar.gz" \
275 "/tools/$name/knots/${private_knot[$name]}" /tools/$name/knots/oc-0000000; do
276 gone "$path"
277 done
278done
279gone /d
280gone "/d/knots/${private_knot[docs]}"
281serves /-/knots "Open the edge"
282if grep -qF -e docs-knot-text -e notes-knot-text "$TMPDIR/body"; then fail "the knots page shows anyone a private knot"; fi
283
284# A reader downloads app's source from the link on its tree page: a
285# gzipped tar archive of main's tip that lists main's files and
286# directories, each under one directory named for the project and the
287# commit.
288serves /tools/app app-readme-text
289tip=$(git -C work/app rev-parse HEAD)
290grep -qF "href=\"/tools/app/source/archive/$tip.tar.gz\">Download .tar.gz<" "$TMPDIR/body" ||
291 fail "app's tree page does not link the archive of main's tip"
292status=$(curl -sS --max-time 10 -o app.tar.gz -w '%{http_code} %header{content-type}' "http://$web/tools/app/source/archive/$tip.tar.gz")
293[ "$status" = "200 application/gzip" ] || fail "valley-web answered app's archive with $status"
294prefix=app-${tip:0:12}/
295tar -tzf app.tar.gz | LC_ALL=C sort > listed
296{
297 echo "$prefix"
298 git -C work/app ls-tree -r -t --format='%(objecttype) %(path)' HEAD |
299 sed -e "s|^tree \(.*\)|$prefix\1/|" -e "s|^blob \(.*\)|$prefix\1|"
300} | LC_ALL=C sort > want
301cmp -s listed want || fail "app's archive lists $(cat listed), not $(cat want)"
302
303# main.tar.gz is one address for the archive of main's tip, wherever main
304# is: a temporary redirect to that archive, which curl -L follows to the
305# same bytes.
306[ "$(get /tools/app/source/archive/main.tar.gz)" = "302 /tools/app/source/archive/$tip.tar.gz" ] ||
307 fail "app's main.tar.gz answered $(get /tools/app/source/archive/main.tar.gz)"
308status=$(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")
309[ "$status" = "200 application/gzip" ] || fail "valley-web answered app's main.tar.gz with $status"
310cmp -s main.tar.gz app.tar.gz || fail "app's main.tar.gz is not the archive of main's tip"
311
312# Every response is private and never stored: a page, a redirect, an
313# archive, a 404 and a HEAD alike, even one that asks to be answered 304.
314# None carries an ETag. The knots page's policy admits its one script by
315# its hash; every other route's admits none. No policy lets a script
316# connect anywhere.
317headers() { curl -sS --max-time 10 -o /dev/null -D - -H 'If-None-Match: *' "$@" | tr -d '\r'; }
318for path in / /-/knots "/tools/app/source/tree/$app_tip/" "/tools/app/source/archive/$tip.tar.gz" \
319 /tools/app/source/archive/main.tar.gz "/a/knots/$knot" /tools/nosuch/ /-/nosuch; do
320 for method in GET HEAD; do
321 if [ "$method" = HEAD ]; then got=$(headers -I "http://$web$path"); else got=$(headers "http://$web$path"); fi
322 grep -qx 'Cache-Control: private, no-store' <<< "$got" || fail "$method $path is not private and unstored: $got"
323 grep -qx 'Vary: Cookie' <<< "$got" || fail "$method $path does not vary by cookie: $got"
324 if grep -qi '^etag:' <<< "$got" || grep -q '^HTTP/[0-9.]* 304' <<< "$got"; then
325 fail "$method $path answered with an ETag or 304: $got"
326 fi
327 policy=$(sed -n 's/^Content-Security-Policy: //p' <<< "$got")
328 case $path in
329 /-/knots) script="script-src 'sha256-" ;;
330 *) script="script-src 'none';" ;;
331 esac
332 [[ $policy == *"$script"* && $policy == *"connect-src 'none'"* ]] ||
333 fail "$method $path answered with the policy $policy"
334 done
335done
336
337# The pages say when the policy was last checked, apart from when the
338# plan was collected. A pass that finds nothing changed sends no content,
339# and still sends an authorization, signed again with a later sequence and
340# a later check, so the time the pages show advances while export.json
341# keeps its bytes.
342checked_of() { sed -n 's/^checked //p' "$1"; }
343sequence_of() { sed -n 's/^sequence //p' "$1"; }
344shown() { date -u -d "$1" '+%Y-%m-%d %H:%M UTC'; }
345serves /-/knots "Policy last checked $(shown "$(checked_of edge/v2/authorization)")"
346grep -qF "Collected " "$TMPDIR/body" || fail "the knots page does not say when the plan was collected"
347digest=$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)
348before=$(checked_of edge/v2/authorization)
349sleep 1.1
350publish --receiver "$receiver" --receiver-token token > pass.out
351grep -q "nothing has changed since; nothing is sent" pass.out || fail "an unchanged pass sent content: $(cat pass.out)"
352[ "$(sequence_of edge/v2/authorization)" = 3 ] || fail "an unchanged pass stored authorization $(sequence_of edge/v2/authorization), want 3"
353[ "$(checked_of edge/v2/authorization)" != "$before" ] || fail "an unchanged pass did not advance the policy check from $before"
354[ "$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)" = "$digest" ] ||
355 fail "an unchanged pass changed export.json"
356serves /-/knots "Policy last checked $(shown "$(checked_of edge/v2/authorization)")"
357
358# A read link: issued only to a principal the registry grants an edge
359# read that a project consents to, here alice, who holds no key. The host
360# keeps its verifier, and the next pass carries it to the edge.
361mkdir -m 750 links
362if issue_link --principal bob > bob.out 2> bob.err; then fail "a link was issued to bob, whom the registry does not name"; fi
363grep -q "grants bob no edge read" bob.err || fail "issuing to bob failed for another reason: $(cat bob.err)"
364url=$(issue_link --principal alice --note laptop 2> issue.err)
365[[ $url =~ ^https://example\.org/-/redeem#(lk-[0-9a-f]{16})\.([A-Za-z0-9_-]{43})$ ]] ||
366 fail "issue printed ${#url} characters, not one link: $(cat issue.err)"
367lk=${BASH_REMATCH[1]} secret=${BASH_REMATCH[2]}
368link list > list.out
369grep -Eq "^$lk +alice +active +pending" list.out || fail "an undelivered link is not pending: $(cat list.out)"
370publish --receiver "$receiver" --receiver-token token > pass.out
371verifier=$(printf '%s=' "$secret" | tr '_-' '/+' | base64 -d | sha256sum | cut -d' ' -f1)
372grep -Eqx "binding $lk alice $verifier" edge/v2/authorization ||
373 fail "the stored authorization does not bind $lk to alice by the secret's SHA-256: $(grep binding edge/v2/authorization)"
374link list > list.out
375grep -Eq "^$lk +alice +active +enforced +[0-9TZ:-]+ +laptop$" list.out || fail "a delivered link is not enforced: $(cat list.out)"
376if grep -rqF "$secret" publisher edge links first ./*.out ./*.err ./*.log; then
377 fail "the link's secret is in the publisher's state, the edge, the credential file, an export or a log"
378fi
379
380# An authorization the receiver stored already is refused as replayed,
381# and one whose text was changed is refused for its signature, whatever
382# its sequence.
383post() {
384 curl -sS --max-time 10 -o "$TMPDIR/reply" -w '%{http_code}' -H "Authorization: Bearer $(cat token)" \
385 --data-binary "@$2" "$receiver$1"
386}
387[ "$(post /web-export/v2/authorization publisher/authorization)" = 409 ] && grep -q '"error":"replayed"' reply ||
388 fail "a replayed authorization was not refused as replayed: $(cat reply)"
389sed 's/^sequence .*/sequence 99/' publisher/authorization > forged
390[ "$(post /web-export/v2/authorization forged)" = 403 ] && grep -q '"error":"bad-signature"' reply ||
391 fail "an authorization whose text was changed was not refused for its signature: $(cat reply)"
392
393# A publisher of the first protocol is refused with a clear error, and
394# valley-web refuses the directory the first store layout took as its
395# state.
396[ "$(post /web-export/v1/manifest forged)" = 410 ] && grep -q '"error":"unsupported-version"' reply ||
397 fail "a manifest of the first protocol was not refused as unsupported-version: $(cat reply)"
398if valley-web --state edge --listen 127.0.0.1:0 2> old.log; then fail "valley-web served the store's directory"; fi
399grep -q "is a directory" old.log || fail "valley-web refused the store's directory for another reason: $(cat old.log)"
400
401# 3. A comment changes the export, so the next pass uploads. With another
402# token it is refused as unauthenticated, and the edge keeps serving what
403# it had.
404(cd work/app && valley knot comment "$knot" -m "edge-comment-text" > /dev/null)
405if publish --receiver "$receiver" --receiver-token wrong > pass.out 2> pass.err; then
406 fail "a pass with another token succeeded"
407fi
408grep -q unauthenticated pass.err || fail "a pass with another token failed for another reason: $(cat pass.err)"
409serves "/tools/app/knots/$knot" "Open the edge"
410if grep -qF edge-comment-text "$TMPDIR/body"; then fail "the refused pass's comment is served"; fi
411
412# 3b. A policy check that cannot read docs keeps, of the readers the
413# stored authorization gave docs, only those a grant still covers, and
414# adds none. Here alice's grant stops covering docs while docs' repository
415# cannot be read: the pass sends an authorization that keeps docs for the
416# operator alone, on the basis of the authorization the receiver holds,
417# and fails as incomplete.
418registry "{app: \"${id[app]}\", site: \"${id[site]}\"}" "{app: \"${id[app]}\"}" > work/valley/identity/registry.cue
419commit valley "alice reads app alone"
420held=$(sequence_of edge/v2/authorization)
421mv srv/docs.git docs.moved
422if publish --receiver "$receiver" --receiver-token token --policy-only > kept.out 2> kept.err; then
423 fail "a pass that could not read docs succeeded"
424fi
425mv docs.moved srv/docs.git
426grep -q "the policy check is incomplete" kept.err || fail "the pass that could not read docs failed for another reason: $(cat kept.err)"
427grep -qx "basis $held" edge/v2/authorization && grep -qx "reader docs operator" edge/v2/authorization ||
428 fail "the incomplete check did not keep docs for the operator on basis $held: $(cat edge/v2/authorization)"
429if grep -q "^reader docs alice" edge/v2/authorization; then fail "the incomplete check kept alice, whose grant no longer covers docs"; fi
430
431# Removing one of docs' two readers keeps docs on the edge for the other.
432# Removing the last withdraws it: the authorization no longer names it,
433# and the receiver deletes its repository, while the signed export.json
434# keeps its bytes.
435publish --receiver "$receiver" --receiver-token token --policy-only > /dev/null
436grep -qx "reader docs operator" edge/v2/authorization && ! grep -q "^reader docs alice" edge/v2/authorization ||
437 fail "with alice's grant removed the authorization does not let the operator alone read docs: $(cat edge/v2/authorization)"
438[ -d edge/v2/current/repos/docs.git ] || fail "removing one of docs' readers deleted its repository"
439digest=$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)
440registry "{app: \"${id[app]}\", site: \"${id[site]}\"}" "{app: \"${id[app]}\"}" "{app: \"${id[app]}\"}" > work/valley/identity/registry.cue
441commit valley "no one reads docs"
442publish --receiver "$receiver" --receiver-token token --policy-only > /dev/null
443if grep -q "^project docs " edge/v2/authorization; then fail "the authorization names docs once it has no reader"; fi
444for _ in $(seq 300); do
445 [ -e edge/v2/current/repos/docs.git ] || break
446 sleep 0.1
447done
448[ ! -e edge/v2/current/repos/docs.git ] || fail "the receiver kept the repository of docs once it had no reader"
449[ "$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)" = "$digest" ] ||
450 fail "withdrawing docs changed the signed export.json"
451gone /d
452# Granting both readers again sends docs' bundle again.
453registry "{app: \"${id[app]}\", site: \"${id[site]}\"}" > work/valley/identity/registry.cue
454commit valley "alice and the operator read every project"
455publish --receiver "$receiver" --receiver-token token > pass.out
456grep -q "^valley-web-publish: sent .*docs.*; the receiver activated" pass.out || fail "the pass did not send docs' bundle again: $(cat pass.out)"
457[ -d edge/v2/current/repos/docs.git ] || fail "the edge holds no repository of docs once it is granted again"
458
459# 4. Withdrawing anyone's grant for app takes app off the edge for
460# anonymous requests on the next pass, its alias with it, and site stays.
461# alice and the operator may still read app, so the edge keeps it for
462# them.
463registry "{site: \"${id[site]}\"}" > work/valley/identity/registry.cue
464commit valley "withdraw app"
465publish --receiver "$receiver" --receiver-token token > /dev/null
466gone /tools/app
467gone /a
468serves /tools/site site-readme-text
469grep -qx "project app ${id[app]}" edge/v2/authorization && ! grep -q "^reader app anyone" edge/v2/authorization ||
470 fail "withdrawing anyone's grant for app did not keep it for its named readers alone: $(cat edge/v2/authorization)"
471[ -d edge/v2/current/repos/app.git ] || fail "withdrawing anyone's grant for app deleted its repository, which its named readers read"
472
473# 4a. Growth never blocks a restriction. One commit to the registry
474# withdraws anyone's grant for site, grants app to anyone again, and
475# grants 1800 new principals, whom app's and docs' policy/read name, and
476# a link is issued to alice. The authorization that gives all of it is
477# over the 256 KiB a receiver reads, so the pass sends its bounded
478# fallback in its place: the stored authorization less every access the
479# check no longer gives. site leaves the edge, its former name with it,
480# and nothing is added: app stays off the edge for anonymous requests,
481# and the new link is pending. The pass fails, naming the overflow. A
482# policy/read file holds at most 64 KiB, so each project's new readers
483# are in three files of 600.
484for name in app docs; do
485 for part in 0 1 2; do
486 {
487 printf 'package read\n\nreaders: {\n'
488 for i in $(seq $((part * 600 + 1)) $((part * 600 + 600))); do printf '\tr%063d: true\n' "$i"; done
489 printf '}\n'
490 } > "work/$name/policy/read/more-$part.cue"
491 done
492 commit "$name" "1800 more readers"
493done
494{
495 registry "{app: \"${id[app]}\"}"
496 for i in $(seq 1800); do
497 printf 'principals: r%063d: {kind: "human", keys: [], grants: read: {boundary: "web", projects: "all"}}\n' "$i"
498 done
499} > work/valley/identity/registry.cue
500commit valley "withdraw site, and grant app and 1800 principals"
501grown=$(issued alice)
502held=$(sequence_of edge/v2/authorization)
503if publish --receiver "$receiver" --receiver-token token > grown.out 2> grown.err; then
504 fail "a pass whose authorization is over the limit succeeded"
505fi
506grep -q "the authorization is too large to send: authorization [0-9]* would be [0-9]* bytes signed" grown.err ||
507 fail "the pass over the limit failed for another reason: $(cat grown.err)"
508grep -q "from authorization $held, the last the receiver acknowledged" grown.err ||
509 fail "the fallback is not signed from authorization $held: $(cat grown.err)"
510grep -q "^valley-web-publish: pending: app to 1801 readers; docs to 1800 readers; 1 read links$" grown.out ||
511 fail "the pass does not say what is pending: $(cat grown.out)"
512[ "$(sequence_of edge/v2/authorization)" -gt "$held" ] || fail "the edge holds no newer authorization than $held"
513[ "$(wc -c < edge/v2/authorization)" -le 262144 ] || fail "the edge stored an authorization over 256 KiB"
514if grep -q "^project site \|^reader app anyone\|^reader [a-z]* r0" edge/v2/authorization; then
515 fail "the fallback gives access the stored authorization did not, or keeps site: $(cat edge/v2/authorization)"
516fi
517grep -qx "reader app operator" edge/v2/authorization && grep -qx "reader docs alice" edge/v2/authorization ||
518 fail "the fallback dropped access the check still gives: $(cat edge/v2/authorization)"
519gone /tools/site
520gone /homepage
521gone /tools/app
522if link list --check "$grown" > list.out 2> check.err; then fail "a link the fallback does not carry is not pending: $(cat list.out)"; fi
523grep -Eq "^$grown +alice +active +pending" list.out || fail "the link issued with the overflow is not pending: $(cat list.out)"
524link revoke "$grown" > /dev/null
525for name in app docs; do
526 rm "work/$name"/policy/read/more-*.cue
527 commit "$name" "the readers again"
528done
529
530# 4b. A withdrawal by authorization alone, while content work is
531# stalled. With app granted again, a new knot changes the export, and a
532# pass reads it, site and docs included, then stalls at its upload: the
533# test holds the receiver's activation lock, as a commit that checks a
534# large repository would. Meanwhile anyone's grant for site is withdrawn,
535# and so are both grants that cover docs, and a --policy-only pass takes
536# both off the edge at once: valley-web, not restarted, answers the next
537# request without site. Once the lock is released, the stalled upload,
538# whose content holds site and docs and whose sequence is later, activates
539# and restores neither's authorization. The receiver deletes both
540# repositories, and the active export.json keeps its signed bytes.
541registry "{app: \"${id[app]}\", site: \"${id[site]}\"}" > work/valley/identity/registry.cue
542commit valley "grant app again"
543publish --receiver "$receiver" --receiver-token token > /dev/null
544serves /tools/app app-readme-text
545(cd work/site && valley knot new outcome "Withdrawn while stalled" > /dev/null)
546flock -o edge/v2/lock sleep 300 &
547holder=$!
548pids+=("$holder")
549publish --receiver "$receiver" --receiver-token token > stalled.out 2> stalled.err &
550stalled=$!
551for _ in $(seq 600); do
552 grep -q "^valley-web-publish: exported: app, docs, site;" stalled.out && break
553 kill -0 "$stalled" 2> /dev/null || fail "the stalled pass ended before its upload: $(cat stalled.out stalled.err)"
554 sleep 0.1
555done
556grep -q "^valley-web-publish: exported: app, docs, site;" stalled.out || fail "the stalled pass did not read the valley within a minute"
557sleep 1
558registry "{app: \"${id[app]}\"}" "{app: \"${id[app]}\"}" "{app: \"${id[app]}\"}" > work/valley/identity/registry.cue
559commit valley "withdraw site and docs"
560link revoke "$lk" > /dev/null
561if link list --check "$lk" > /dev/null 2> check.err; then fail "an undelivered revocation is not pending"; fi
562grep -q "$lk: revoked here; the edge may accept it until a pass delivers the revocation" check.err ||
563 fail "list --check did not say the revocation is pending: $(cat check.err)"
564within=40 publish --receiver "$receiver" --receiver-token token --policy-only > policy.out ||
565 fail "a --policy-only pass failed while content was stalled"
566grep -q "the receiver stored authorization [0-9]*: app to alice, anyone, operator$" policy.out ||
567 fail "the --policy-only pass did not withdraw site and docs: $(cat policy.out)"
568if grep -q "the receiver activated" policy.out; then fail "the --policy-only pass sent content"; fi
569gone /tools/site
570gone /tools/homepage
571serves /tools/app app-readme-text
572if grep -q "^binding $lk " edge/v2/authorization; then fail "the --policy-only pass did not revoke $lk"; fi
573link list --check "$lk" > list.out || fail "a delivered revocation is still pending"
574grep -Eq "^$lk +alice +revoked +enforced" list.out || fail "a delivered revocation is not enforced: $(cat list.out)"
575kill -0 "$stalled" 2> /dev/null || fail "the stalled pass did not stall: $(cat stalled.out stalled.err)"
576stop "$holder"
577wait "$stalled" || fail "the stalled pass failed once its upload went on: $(cat stalled.err)"
578grep -q "the receiver activated sequence" stalled.out || fail "the stalled pass did not activate its upload: $(cat stalled.out)"
579for name in site docs; do
580 grep -q "\"project\":\"$name\"" edge/v2/current/export.json || fail "the stalled upload's export does not hold $name"
581 if grep -q "^project $name " edge/v2/authorization; then fail "the stalled upload restored the authorization of $name"; fi
582done
583gone /tools/site
584digest=$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)
585for _ in $(seq 300); do
586 [ -e edge/v2/current/repos/site.git ] || [ -e edge/v2/current/repos/docs.git ] || break
587 sleep 0.1
588done
589for name in site docs; do
590 [ ! -e "edge/v2/current/repos/$name.git" ] || fail "the receiver kept the repository of $name after its withdrawal"
591done
592[ -e edge/v2/current/repos/app.git ] || fail "the receiver deleted the repository of app"
593[ "$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)" = "$digest" ] ||
594 fail "the withdrawal changed the signed export.json"
595
596# Revoking a principal's links, and then every link, reaches the edge
597# through a policy pass the same way.
598issued alice > /dev/null
599issued alice > /dev/null
600kept=$(issued operator)
601publish --receiver "$receiver" --receiver-token token --policy-only > /dev/null
602[ "$(bindings)" = 3 ] || fail "the authorization carries $(bindings) bindings, want 3"
603link revoke --principal alice > /dev/null
604publish --receiver "$receiver" --receiver-token token --policy-only > /dev/null
605[ "$(bindings)" = 1 ] && grep -Eq "^binding $kept operator " edge/v2/authorization ||
606 fail "revoking alice's links left $(grep binding edge/v2/authorization)"
607link revoke --all > /dev/null
608publish --receiver "$receiver" --receiver-token token --policy-only > /dev/null
609[ "$(bindings)" = 0 ] || fail "revoking every link left $(bindings) bindings"
610link list --check > /dev/null || fail "a delivered revocation of every link is still pending"
611
612policy() { publish --receiver "$receiver" --receiver-token token --policy-only "$@"; }
613
614# A pass holds the links lock, the file lock beside the credential file,
615# from just before it reads the file until it has signed. Every change to
616# the file holds the same lock. Here a pass pauses holding it: the
617# credential file is a FIFO, which the pass waits to read. A revoke
618# meanwhile waits, and writes nothing, until the pass has signed, so the
619# paused pass signs the link as active. The revocation is pending until
620# the next pass, the one the wrapper starts after a revoke, signs no
621# binding for the link and the receiver acknowledges it.
622paused_lk=$(issued alice)
623policy > /dev/null
624grep -q "^binding $paused_lk " edge/v2/authorization || fail "the pass did not deliver $paused_lk"
625# A revoke while a pass is checking the policy does not wait for the
626# check: the pass holds only the authorization lock then.
627mkdir pause
628mkfifo pause/read.cue
629VALLEY_READ_SCHEMA="$TMPDIR/pause/read.cue" policy > checking.out 2> checking.err &
630checking=$!
631for _ in $(seq 300); do
632 flock -n publisher/authorization.lock true || break
633 kill -0 "$checking" 2> /dev/null || fail "the pass ended before its policy check: $(cat checking.out checking.err)"
634 sleep 0.1
635done
636if flock -n publisher/authorization.lock true; then fail "the pass did not take the authorization lock"; fi
637early=$(issued alice)
638link revoke "$early" > early.out 2> early.err || fail "a revoke while a pass checks the policy failed: $(cat early.err)"
639if grep -q waiting early.err; then fail "a revoke waited for a pass that was checking the policy: $(cat early.err)"; fi
640cp "$readSchema" pause/read.regular
641timeout 30 bash -c 'exec 3> pause/read.cue && mv -f pause/read.regular pause/read.cue && cat "$1" >&3' _ "$readSchema" ||
642 fail "the pass checking the policy did not read its schema"
643wait "$checking" || fail "the pass checking the policy failed: $(cat checking.err)"
644if grep -q "^binding $early " edge/v2/authorization; then fail "a pass signed $early, revoked while it checked the policy"; fi
645# Now the pause inside the links lock. The FIFO is linked at the credential
646# file's name, and keeps a second name for the test to write it through.
647cp links/links pause/links.regular
648cp links/links pause/links.content
649mkfifo pause/links.fifo
650ln -f pause/links.fifo links/links
651policy > paused.out 2> paused.err &
652paused=$!
653for _ in $(seq 600); do
654 flock -n links/lock true || break
655 kill -0 "$paused" 2> /dev/null || fail "the paused pass ended before it read the credential file: $(cat paused.out paused.err)"
656 sleep 0.1
657done
658if flock -n links/lock true; then fail "the paused pass did not take the links lock"; fi
659link revoke "$paused_lk" > revoke.out 2> revoke.err &
660revoking=$!
661for _ in $(seq 300); do
662 grep -q "waiting up to 1m0s for the policy pass or the change that holds the links lock" revoke.err && break
663 kill -0 "$revoking" 2> /dev/null || fail "the revoke did not wait for the paused pass: $(cat revoke.out revoke.err)"
664 sleep 0.1
665done
666grep -q "waiting up to 1m0s for the policy pass or the change that holds the links lock" revoke.err ||
667 fail "the revoke did not say it waits for the paused pass: $(cat revoke.err)"
668sleep 1
669kill -0 "$revoking" 2> /dev/null || fail "the revoke ended while a pass held the links lock: $(cat revoke.out revoke.err)"
670[ -p links/links ] || fail "the revoke wrote the credential file while a pass held the links lock"
671# Let the pass go on: once it has opened the FIFO, the regular file takes
672# its place for the revoke, and the pass reads the file through the FIFO.
673timeout 30 bash -c 'exec 3> pause/links.fifo && mv -f pause/links.regular links/links && cat pause/links.content >&3' ||
674 fail "the paused pass did not read the credential file"
675wait "$paused" || fail "the paused pass failed: $(cat paused.err)"
676wait "$revoking" || fail "the revoke failed: $(cat revoke.err)"
677grep -q "^revoked $paused_lk " revoke.out || fail "the revoke did not revoke $paused_lk: $(cat revoke.out)"
678grep -q "^binding $paused_lk " edge/v2/authorization || fail "the paused pass did not sign $paused_lk, active as it signed"
679if link list --check "$paused_lk" > list.out 2> check.err; then fail "a revocation no pass has delivered is not pending: $(cat list.out)"; fi
680grep -Eq "^$paused_lk +alice +revoked +pending" list.out || fail "a revocation no pass has delivered is not pending: $(cat list.out)"
681policy > /dev/null
682if grep -q "^binding $paused_lk " edge/v2/authorization; then fail "the pass after the revoke signed the revoked $paused_lk"; fi
683link list --check "$paused_lk" > list.out || fail "a delivered revocation is still pending: $(cat list.out)"
684grep -Eq "^$paused_lk +alice +revoked +enforced" list.out || fail "a delivered revocation is not enforced: $(cat list.out)"
685
686# A lost acknowledgement: the receiver stored an authorization carrying a
687# link, and the publisher never recorded it. A revocation after that is
688# pending, never enforced, until a pass delivers it.
689cp publisher/links-delivery delivery.before
690cp publisher/authorization authorization.before
691lost=$(issued alice)
692policy > /dev/null
693grep -q "^binding $lost " edge/v2/authorization || fail "the pass did not deliver $lost"
694cp delivery.before publisher/links-delivery
695cp authorization.before publisher/authorization
696link revoke "$lost" > /dev/null
697if link list --check "$lost" > list.out 2> check.err; then fail "a revocation after a lost acknowledgement is not pending: $(cat list.out)"; fi
698grep -Eq "^$lost +alice +revoked +pending" list.out || fail "a revocation after a lost acknowledgement is not pending: $(cat list.out)"
699policy > /dev/null
700link list --check "$lost" > list.out || fail "a delivered revocation is still pending: $(cat list.out)"
701
702# A missing credential file holds no links, and the pass succeeds. A
703# corrupt one sends an authorization with no bindings, and fails the pass.
704kept=$(issued alice)
705mv links/links links.kept
706policy > /dev/null || fail "a pass with no credential file failed"
707[ "$(bindings)" = 0 ] || fail "a pass with no credential file sent $(bindings) bindings"
708echo '{' > links/links
709if policy > /dev/null 2> corrupt.err; then fail "a pass with a corrupt credential file succeeded"; fi
710grep -q "is not a credential file" corrupt.err || fail "a pass with a corrupt credential file failed for another reason: $(cat corrupt.err)"
711[ "$(bindings)" = 0 ] || fail "a corrupt credential file sent $(bindings) bindings"
712mv links.kept links/links
713policy > /dev/null
714grep -q "^binding $kept " edge/v2/authorization || fail "the credential file put back did not deliver $kept"
715
716# A credential file restored from an older copy holds a link revoked
717# since. A pass signs the file as it is, so the link is active at the edge
718# again. The next change writes past every revision a pass signed from,
719# and revoke --all cuts every link off.
720cp links/links links.older
721link revoke "$kept" > /dev/null
722policy > /dev/null
723signed=$(jq .signed publisher/links-delivery)
724cp links.older links/links
725policy > /dev/null 2> older.err || fail "a pass from an older credential file failed: $(cat older.err)"
726grep -q "^binding $kept " edge/v2/authorization || fail "a pass from an older credential file did not deliver the $kept it holds"
727link revoke --all > /dev/null
728[ "$(jq .revision links/links)" = $((signed + 1)) ] ||
729 fail "revoke --all on an older credential file wrote revision $(jq .revision links/links), not $((signed + 1))"
730policy > /dev/null
731[ "$(bindings)" = 0 ] || fail "revoke --all on an older credential file left $(bindings) bindings"
732link list --check > /dev/null || fail "after revoke --all a link is pending"
733
734# A host restore: the credential file and the publisher's state come back
735# from one older backup, after a pass delivered a revocation. The pass
736# after the restore signs above the authorization the receiver holds, and
737# the links are as they were at the backup: the revoked link is active
738# again. revoke --all then cuts every link off.
739restored=$(issued alice)
740policy > /dev/null
741cp -a publisher backup.publisher
742cp links/links backup.links
743link revoke "$restored" > /dev/null
744policy > /dev/null
745if grep -q "^binding $restored " edge/v2/authorization; then fail "the pass did not revoke $restored"; fi
746rm -rf publisher
747cp -a backup.publisher publisher
748cp backup.links links/links
749policy > restored.out 2> restored.err || fail "a pass after a host restore failed: $(cat restored.err)"
750grep -q "refused [0-9]* as replayed" restored.err || fail "the restored counter was not refused as replayed: $(cat restored.err)"
751grep -q "^binding $restored " edge/v2/authorization || fail "a host restore did not restore $restored as it was at the backup"
752serves /tools/app app-readme-text
753link list --check "$restored" > list.out || fail "the restored link is pending after a pass: $(cat list.out)"
754link revoke --all > /dev/null
755policy > /dev/null
756[ "$(bindings)" = 0 ] || fail "revoke --all after a host restore left $(bindings) bindings"
757link list --check > /dev/null || fail "after revoke --all a link is pending"
758
759# Readers sign in, as a reader page's script asks: each request is a
760# POST from the origin, of JSON, with X-Valley-Reader: 1. A link the host
761# issues redeems once, into a session whose cookie each response renews,
762# an archive's too. Of eight concurrent redemptions of one link, one wins.
763# A session survives both servers' restarts, ends at logout, and ends at
764# the pass that delivers its link's revocation. A second valley-web over
765# the same browser state refuses to start. Neither a link's secret nor a
766# session's token reaches a log or the browser state.
767reader_post() {
768 curl -sS --max-time 10 -o "$TMPDIR/reply" -D "$TMPDIR/reply.headers" -w '%{http_code}' \
769 -H 'Origin: https://example.org' -H 'Content-Type: application/json' -H 'X-Valley-Reader: 1' \
770 -H "Cookie: __Host-valley-reader=${3:-}" --data-binary "$2" "http://$web$1"
771}
772redeem() { reader_post /-/redeem "{\"credential\":\"$1\"}" "${2:-}"; }
773token_of() { tr -d '\r' < "$TMPDIR/reply.headers" | sed -n 's/^Set-Cookie: __Host-valley-reader=\([^;]*\);.*/\1/p'; }
774as_reader() {
775 curl -sS --max-time 10 -o "$TMPDIR/body" -D "$TMPDIR/headers" -w '%{http_code}' \
776 -H "Cookie: __Host-valley-reader=$1" "http://$web$2"
777}
778renewed() {
779 tr -d '\r' < "$TMPDIR/headers" |
780 grep -qx "Set-Cookie: __Host-valley-reader=$1; Secure; HttpOnly; Path=/; SameSite=Lax; Max-Age=2592000"
781}
782# redact FILE: the file for a failure's message, with each credential and
783# session token in it cut to its credential ID.
784redact() { sed -E 's/(lk-[0-9a-f]{16})\.[A-Za-z0-9_-]{43}/\1.<redacted>/g' "$1"; }
785# new_link PRINCIPAL: a link issued and delivered; its credential.
786new_link() {
787 local url
788 url=$(issue_link --principal "$1" 2> /dev/null)
789 policy > /dev/null
790 echo "${url#*#}" | tee -a secrets
791}
792cred=$(new_link alice)
793[ "$(redeem "$cred")" = 204 ] || fail "a host-issued link did not redeem: $(cat reply)"
794token=$(token_of)
795echo "$token" >> secrets
796[[ $token =~ ^${cred%%.*}\.[A-Za-z0-9_-]{43}$ ]] || fail "the redemption set no session token: $(redact reply.headers)"
797[ "$(redeem "$cred")" = 403 ] && [ "$(cat reply)" = "This link cannot be used. Request another link." ] ||
798 fail "a redeemed link redeemed again: $(cat reply)"
799[ "$(as_reader "$token" /tools/app)" = 302 ] && renewed "$token" || fail "a session was not renewed on a redirect"
800[ "$(as_reader "$token" /tools/nosuch/)" = 404 ] && renewed "$token" || fail "a session was not renewed on a 404"
801[ "$(as_reader "$token" /-/knots)" = 200 ] && grep -qF '<a href="/-/logout">Sign out</a>' body ||
802 fail "a session's knots page does not link to Sign out"
803if get /-/knots > /dev/null && grep -qF 'Sign out' body; then fail "anyone's knots page links to Sign out"; fi
804archive=$(get /tools/app/source/archive/main.tar.gz)
805archive=${archive#302 }
806curl -sS --max-time 10 -o anonymous.tar.gz "http://$web$archive"
807[ "$(as_reader "$token" "$archive")" = 200 ] && renewed "$token" && cmp -s body anonymous.tar.gz ||
808 fail "a session's download of app's archive differs, or was not renewed"
809tr -d '\r' < headers | grep -qx 'Cache-Control: private, no-store' && tr -d '\r' < headers | grep -qx 'Content-Type: application/gzip' &&
810 tr -d '\r' < headers | grep -q '^Content-Disposition: attachment' || fail "a session's archive lost its headers: $(redact headers)"
811race=$(new_link alice)
812racers=()
813for i in $(seq 8); do
814 curl -sS --max-time 10 -o /dev/null -w '%{http_code}\n' -H 'Origin: https://example.org' \
815 -H 'Content-Type: application/json' -H 'X-Valley-Reader: 1' --data-binary "{\"credential\":\"$race\"}" \
816 "http://$web/-/redeem" > "race.$i" &
817 racers+=("$!")
818done
819wait "${racers[@]}"
820[ "$(cat race.* | grep -c '^204$')" = 1 ] && [ "$(cat race.* | grep -c '^403$')" = 7 ] ||
821 fail "eight concurrent redemptions of one link answered $(cat race.* | tr '\n' ' ')"
822stop "$web_pid"
823stop "$receiver_pid"
824receive
825serve
826[ "$(as_reader "$token" /tools/app)" = 302 ] && renewed "$token" || fail "a session did not survive both servers' restarts"
827if valley-web --state edge/store --listen 127.0.0.1:0 --origin https://example.org 2> second.log; then
828 fail "a second valley-web started over the same browser state"
829fi
830grep -q "another process owns the browser state" second.log || fail "a second valley-web failed for another reason: $(cat second.log)"
831[ "$(reader_post /-/logout '{}' "$token")" = 204 ] &&
832 tr -d '\r' < reply.headers | grep -qx 'Set-Cookie: __Host-valley-reader=; Secure; HttpOnly; Path=/; SameSite=Lax; Max-Age=0' ||
833 fail "logout did not clear the cookie: $(redact reply.headers)"
834[ "$(as_reader "$token" /tools/app)" = 302 ] && ! renewed "$token" || fail "a signed-out session is still renewed"
835[ "$(reader_post /-/logout '{}' "$token")" = 204 ] || fail "a second logout failed"
836revoked=$(new_link alice)
837[ "$(redeem "$revoked")" = 204 ] || fail "a third link did not redeem"
838revoked_token=$(token_of)
839echo "$revoked_token" >> secrets
840link revoke "${revoked%%.*}" > /dev/null
841policy > /dev/null
842[ "$(as_reader "$revoked_token" /tools/app)" = 302 ] && ! renewed "$revoked_token" ||
843 fail "the session of a revoked link is still renewed"
844
845# A signed-in reader reads a project that only named readers may read.
846# Step 4b took docs off the edge, so a commit grants the operator docs
847# again, and a pass sends docs' bundle back. A session of a link issued to
848# the operator reads docs' tree page with its README, docs' knot, its
849# archive, and the knots page, which lists docs' knot. Anonymous requests
850# for the same pages get 404, and anyone's knots page shows no knot of
851# docs. Once a pass delivers the link's revocation, the session gets 404
852# for docs' tree page, while the operator may still read docs.
853registry "{app: \"${id[app]}\"}" "{app: \"${id[app]}\"}" "{app: \"${id[app]}\", docs: \"${id[docs]}\"}" \
854 > work/valley/identity/registry.cue
855commit valley "the operator reads docs"
856publish --receiver "$receiver" --receiver-token token > pass.out
857grep -q "^valley-web-publish: sent .*docs.*; the receiver activated" pass.out || fail "the pass did not send docs' bundle again: $(cat pass.out)"
858docs_link=$(new_link operator)
859docs_id=${docs_link%%.*}
860grep -qx "reader docs operator" edge/v2/authorization && ! grep -q "^reader docs \(alice\|anyone\)$" edge/v2/authorization &&
861 grep -q "^binding $docs_id operator " edge/v2/authorization ||
862 fail "the authorization does not let the operator alone read docs, or does not bind $docs_id to the operator: $(cat edge/v2/authorization)"
863[ "$(redeem "$docs_link")" = 204 ] || fail "the operator's link did not redeem: $(redact reply)"
864docs_token=$(token_of)
865echo "$docs_token" >> secrets
866docs_tip=$(git -C srv/docs.git rev-parse main)
867docs_tree=/tools/docs/source/tree/$docs_tip/
868docs_knot=/tools/docs/knots/${private_knot[docs]}
869docs_archive=/tools/docs/source/archive/$docs_tip.tar.gz
870[ "$(as_reader "$docs_token" "$docs_tree")" = 200 ] && grep -qF docs-readme-text body ||
871 fail "the operator's session was not served docs' tree page with its README"
872[ "$(as_reader "$docs_token" "$docs_knot")" = 200 ] && grep -qF docs-knot-text body ||
873 fail "the operator's session was not served docs' knot"
874[ "$(as_reader "$docs_token" "$docs_archive")" = 200 ] || fail "the operator's session was not served docs' archive"
875tar -tzf body > docs.listed || fail "the operator's session's archive of docs is not a gzipped tar archive"
876grep -qx "docs-${docs_tip:0:12}/README.md" docs.listed || fail "the operator's session's archive of docs does not hold its README"
877[ "$(as_reader "$docs_token" /-/knots)" = 200 ] && grep -qF docs-knot-text body ||
878 fail "the operator's session's knots page does not list docs' knot"
879for path in "$docs_tree" "$docs_knot" "$docs_archive"; do gone "$path"; done
880[ "$(get /-/knots)" = "200 " ] || fail "anyone's knots page answered $(get /-/knots)"
881if grep -qF docs-knot-text body; then fail "anyone's knots page lists docs' knot"; fi
882link revoke "$docs_id" > /dev/null
883policy > /dev/null
884grep -qx "reader docs operator" edge/v2/authorization && ! grep -q "^binding $docs_id " edge/v2/authorization ||
885 fail "the pass did not revoke $docs_id while the operator still reads docs: $(cat edge/v2/authorization)"
886[ "$(as_reader "$docs_token" "$docs_tree")" = 404 ] || fail "the session of a revoked link was served docs' tree page"
887
888# A lost browser state is never made again under an authorization that
889# binds a link: redemption and requests with a cookie answer 503, and
890# anyone is served. The guide's recovery revokes every link, delivers an
891# authorization that binds none, which the next request makes the state
892# under, and issues new links.
893pending=$(new_link alice)
894stop "$web_pid"
895mv edge/browser/state lost.state
896serve
897[ "$(redeem "$pending")" = 503 ] || fail "with its browser state lost, a redemption answered $(cat reply)"
898[ "$(as_reader "$revoked_token" /tools/app)" = 503 ] || fail "with its browser state lost, a request with a cookie was served"
899[ "$(get /tools/app | cut -d' ' -f1)" = 302 ] || fail "with its browser state lost, anyone was not served"
900[ ! -e edge/browser/state ] || fail "valley-web made a browser state under an authorization that binds a link"
901link revoke --all > /dev/null
902policy > /dev/null
903get /-/knots > /dev/null
904[ "$(cat edge/browser/state)" = the-valley/web-reader-state/v1 ] || fail "no empty browser state was made once no link was bound"
905[ "$(redeem "$(new_link alice)")" = 204 ] || fail "a new link did not redeem after the recovery: $(cat reply)"
906recovered=$(token_of)
907echo "$recovered" >> secrets
908# A browser state that cannot be read is never replaced: anyone is served,
909# and a request with a cookie answers 503, until the operator delivers an
910# authorization that binds no link, removes the state while valley-web is
911# stopped, and starts it.
912stop "$web_pid"
913echo garbage > edge/browser/state
914serve
915grep -q "the browser state is unavailable" web.log || fail "valley-web did not say its browser state is unavailable: $(cat web.log)"
916[ "$(as_reader "$recovered" /tools/app)" = 503 ] || fail "with its browser state unreadable, a request with a cookie was served"
917[ "$(get /-/knots | cut -c1-3)" = 200 ] || fail "with its browser state unreadable, anyone was not served"
918[ "$(cat edge/browser/state)" = garbage ] || fail "valley-web replaced a browser state it could not read"
919link revoke --all > /dev/null
920policy > /dev/null
921stop "$web_pid"
922rm edge/browser/state
923serve
924[ "$(cat edge/browser/state)" = the-valley/web-reader-state/v1 ] || fail "valley-web did not make the browser state again"
925if grep -rqFf secrets --exclude=secrets --exclude=reply.headers --exclude=headers --exclude='race.*' \
926 ./*.log web.logs edge/browser links publisher; then
927 fail "a link's secret or a session's token is in a log, the browser state, the credential file or the publisher's state"
928fi
929
930# A store whose authorization is removed serves no project, though
931# valley-web has the export indexed already; once it is back, it serves.
932mv edge/v2/authorization authorization.kept
933[ "$(get /tools/app)" = "503 " ] || fail "with no authorization valley-web answered /tools/app with $(get /tools/app)"
934grep -qF "Nothing published yet" "$TMPDIR/body" || fail "with no authorization the page does not say nothing is published"
935if grep -qF app-readme-text "$TMPDIR/body"; then fail "with no authorization valley-web served app's README"; fi
936[ "$(get /)" = "503 " ] || fail "with no authorization valley-web answered / with $(get /)"
937mv authorization.kept edge/v2/authorization
938serves /tools/app app-readme-text
939
940# Granting site again, and withdrawing app as step 4 left it, serves site
941# once more: the receiver's reply says it no longer holds site's
942# repository, so the pass sends site's bundle again.
943registry "{site: \"${id[site]}\"}" > work/valley/identity/registry.cue
944commit valley "withdraw app again"
945publish --receiver "$receiver" --receiver-token token > pass.out
946grep -q "^valley-web-publish: sent .*site; the receiver activated" pass.out || fail "the pass did not send site's bundle again: $(cat pass.out)"
947serves /tools/site site-readme-text
948gone /tools/app
949
950# 5. Both servers restart onto the same content.
951stop "$receiver_pid"
952stop "$web_pid"
953receive
954serve
955gone /tools/app
956serves /tools/site site-readme-text
957
958# 6. With the receiver stopped, valley-web keeps serving. A pass that
959# withdraws site fails because it cannot send its authorization, not
960# because it failed earlier or hung until timeout ended it, and changes
961# nothing served. Once the receiver is back, the next pass checks the
962# policy again, signs a new authorization with a later sequence, and takes
963# site off the edge.
964stop "$receiver_pid"
965serves /tools/site site-readme-text
966read_policy site '' > work/site/policy/read/read.cue
967commit site "withdraw it"
968status=0
969publish --receiver "$receiver" --receiver-token token > pass.out 2> pass.err || status=$?
970case $status in
971 0) fail "a pass to a stopped receiver succeeded" ;;
972 124 | 137) fail "a pass to a stopped receiver hung until timeout ended it (status $status)" ;;
973esac
974grep -q "sending authorization [0-9]* failed [0-9]* times: .*dial tcp ${receiver#http://}: connect: connection refused" pass.err ||
975 fail "a pass to a stopped receiver failed with status $status, but not because it could not connect: $(cat pass.err)"
976serves /tools/site site-readme-text
977unsent=$(sequence_of publisher/authorization)
978receive
979publish --receiver "$receiver" --receiver-token token > /dev/null
980[ "$(sequence_of edge/v2/authorization)" -gt "$unsent" ] ||
981 fail "the pass after the receiver came back stored authorization $(sequence_of edge/v2/authorization), not one signed after $unsent"
982gone /tools/site
983gone /tools/homepage
984gone /tools/app
985
986touch "$out"