gunk-dev/the-valley

web-push-e2e.sh

sourcefe48b06af1c9main's published tip history
31.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" --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)
112read_policy() {
113 printf 'package read\n\nproject: "%s"\nidentity: "%s"\nreaders: {%s}\n' "$1" "${id[$1]}" "$2"
114}
115# The instance's registry: the operator governs it, and anyone may read
116# the projects of the scope given at the edge.
117registry() {
118 cat << EOF
119package identity
120
121boundaries: {
122 registry: kind: "registry"
123 web: {kind: "edge-read", destination: "example.org"}
124}
125genesis: "operator"
126principals: operator: {
127 kind: "human"
128 keys: [{class: "ssh-ed25519", bound: "hardware", public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyForARejectedFixtureAAAAAAAAAAA"}]
129 grants: {
130 govern: boundary: "registry"
131 read: {boundary: "web", projects: "all"}
132 }
133}
134principals: alice: {kind: "human", keys: [], grants: read: {boundary: "web", projects: "all"}}
135anyone: grants: read: {boundary: "web", projects: $1}
136EOF
137}
138
139# 1. The instance and its two projects, each with its own Markdown README,
140# which links its read policy, and both readable by anyone, in the group
141# tools. app has the short alias a, and site the former name homepage.
142for name in valley app site; do
143 git init --quiet --bare "srv/$name.git"
144 git init --quiet "work/$name"
145 git -C "work/$name" remote add origin "$TMPDIR/srv/$name.git"
146 git -C "work/$name" config valley.principal operator
147done
148mkdir -p work/valley/policy/instance work/valley/policy/read work/valley/identity
149echo 'package verification' > work/valley/policy/instance/floor.cue
150read_policy valley 'operator: true' > work/valley/policy/read/read.cue
151registry "{app: \"${id[app]}\", site: \"${id[site]}\"}" > work/valley/identity/registry.cue
152commit valley "the instance"
153projects=$(printf 'app\nsite\nvalley\n' | git -C srv/valley.git hash-object -w --stdin)
154git -C srv/valley.git update-ref refs/the-valley/projects "$projects"
155for name in app site; do
156 mkdir -p "work/$name/policy/read"
157 echo "$TMPDIR/srv/valley.git" > "work/$name/policy/valley"
158 printf '# %s\n\n%s-readme-text, under [its read policy](policy/read/read.cue).\n' "$name" "$name" > "work/$name/README.md"
159 read_policy "$name" 'anyone: true, operator: true, alice: true' > "work/$name/policy/read/read.cue"
160 commit "$name" "the first commit"
161done
162knot=$(cd work/app && valley knot new outcome "Open the edge" --json | jq -r .knot.id)
163
164cat > host.cue << 'EOF'
165package valley
166
167groups: core: name: "tools"
168projects: app: {group: "core", aliases: ["a"]}
169projects: site: {group: "core", formerNames: ["homepage"]}
170projects: valley: {}
171EOF
172cue export --out json "$schema" host.cue > host.json
173echo '{"operator": "operator"}' > principals.json
174
175# 2. A pass to a directory makes the export key, whose verifier line the
176# receiver pins from its first start. Then the receiver and valley-web
177# start over one state directory, and a pass uploads.
178publish --out first > /dev/null
179test -s publisher/verifier || fail "the first pass made no verifier line"
180echo edge-token-0123456789abcdef0123456789 > token
181echo another-token-0123456789abcdef0123456 > wrong
182receive() {
183 start valley-web-receive receive.log valley-web-receive --state edge/store --listen 127.0.0.1:0 \
184 --instance valley-host --destination example.org --verifiers publisher/verifier --token-file token
185 receiver_pid=$pid receiver=http://$addr
186}
187serve() {
188 start valley-web web.log valley-web --state edge/store --listen 127.0.0.1:0
189 web_pid=$pid web=$addr
190}
191receive
192publish --receiver "$receiver" --receiver-token token > pass.out
193grep -q "the receiver stored authorization 2: anyone may read app, site" pass.out ||
194 fail "the pass did not say the receiver stored its authorization: $(cat pass.out)"
195grep -q "the receiver activated sequence" pass.out || fail "the pass did not say the receiver activated it: $(cat pass.out)"
196test -f edge/store && test -d edge/v2/generations && test -f edge/v2/authorization ||
197 fail "the receiver did not make the store's descriptor, generations and authorization"
198serve
199serves /tools/app app-readme-text
200# The README renders as Markdown: its heading has an id, and its relative
201# link goes to the file's page at the same commit.
202app_tip=$(git -C srv/app.git rev-parse main)
203serves "/tools/app/source/tree/$app_tip/" '<h1 id="app">app <a class="anchor" href="#app">#</a></h1>'
204grep -qF "<a href=\"/tools/app/source/blob/$app_tip/policy/read/read.cue\">its read policy</a>" "$TMPDIR/body" ||
205 fail "app's README does not link its read policy at $app_tip: $(grep -F readme-text "$TMPDIR/body")"
206serves /tools/site site-readme-text
207serves "/tools/app/knots/$knot" "Open the edge"
208[ "$(get "/a/knots/$knot?x=1")" = "308 /tools/app/knots/$knot?x=1" ] ||
209 fail "the alias a answered $(get "/a/knots/$knot?x=1")"
210[ "$(get /tools/homepage/source/)" = "308 /tools/site/source/" ] ||
211 fail "the former name homepage answered $(get /tools/homepage/source/)"
212
213# A reader downloads app's source from the link on its tree page: a
214# gzipped tar archive of main's tip that lists main's files and
215# directories, each under one directory named for the project and the
216# commit.
217serves /tools/app app-readme-text
218tip=$(git -C work/app rev-parse HEAD)
219grep -qF "href=\"/tools/app/source/archive/$tip.tar.gz\">Download .tar.gz<" "$TMPDIR/body" ||
220 fail "app's tree page does not link the archive of main's tip"
221status=$(curl -sS --max-time 10 -o app.tar.gz -w '%{http_code} %header{content-type}' "http://$web/tools/app/source/archive/$tip.tar.gz")
222[ "$status" = "200 application/gzip" ] || fail "valley-web answered app's archive with $status"
223prefix=app-${tip:0:12}/
224tar -tzf app.tar.gz | LC_ALL=C sort > listed
225{
226 echo "$prefix"
227 git -C work/app ls-tree -r -t --format='%(objecttype) %(path)' HEAD |
228 sed -e "s|^tree \(.*\)|$prefix\1/|" -e "s|^blob \(.*\)|$prefix\1|"
229} | LC_ALL=C sort > want
230cmp -s listed want || fail "app's archive lists $(cat listed), not $(cat want)"
231
232# main.tar.gz is one address for the archive of main's tip, wherever main
233# is: a temporary redirect to that archive, which curl -L follows to the
234# same bytes.
235[ "$(get /tools/app/source/archive/main.tar.gz)" = "302 /tools/app/source/archive/$tip.tar.gz" ] ||
236 fail "app's main.tar.gz answered $(get /tools/app/source/archive/main.tar.gz)"
237status=$(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")
238[ "$status" = "200 application/gzip" ] || fail "valley-web answered app's main.tar.gz with $status"
239cmp -s main.tar.gz app.tar.gz || fail "app's main.tar.gz is not the archive of main's tip"
240
241# The pages say when the policy was last checked, apart from when the
242# plan was collected. A pass that finds nothing changed sends no content,
243# and still sends an authorization, signed again with a later sequence and
244# a later check, so the time the pages show advances while export.json
245# keeps its bytes.
246checked_of() { sed -n 's/^checked //p' "$1"; }
247sequence_of() { sed -n 's/^sequence //p' "$1"; }
248shown() { date -u -d "$1" '+%Y-%m-%d %H:%M UTC'; }
249serves / "Policy last checked $(shown "$(checked_of edge/v2/authorization)")"
250grep -qF "Collected " "$TMPDIR/body" || fail "the plan page does not say when the plan was collected"
251digest=$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)
252before=$(checked_of edge/v2/authorization)
253sleep 1.1
254publish --receiver "$receiver" --receiver-token token > pass.out
255grep -q "nothing has changed since; nothing is sent" pass.out || fail "an unchanged pass sent content: $(cat pass.out)"
256[ "$(sequence_of edge/v2/authorization)" = 3 ] || fail "an unchanged pass stored authorization $(sequence_of edge/v2/authorization), want 3"
257[ "$(checked_of edge/v2/authorization)" != "$before" ] || fail "an unchanged pass did not advance the policy check from $before"
258[ "$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)" = "$digest" ] ||
259 fail "an unchanged pass changed export.json"
260serves / "Policy last checked $(shown "$(checked_of edge/v2/authorization)")"
261
262# A read link: issued only to a principal the registry grants an edge
263# read that a project consents to, here alice, who holds no key. The host
264# keeps its verifier, and the next pass carries it to the edge.
265mkdir -m 750 links
266if issue_link --principal bob > bob.out 2> bob.err; then fail "a link was issued to bob, whom the registry does not name"; fi
267grep -q "grants bob no edge read" bob.err || fail "issuing to bob failed for another reason: $(cat bob.err)"
268url=$(issue_link --principal alice --note laptop 2> issue.err)
269[[ $url =~ ^https://example\.org/-/redeem#(lk-[0-9a-f]{16})\.([A-Za-z0-9_-]{43})$ ]] ||
270 fail "issue printed ${#url} characters, not one link: $(cat issue.err)"
271lk=${BASH_REMATCH[1]} secret=${BASH_REMATCH[2]}
272link list > list.out
273grep -Eq "^$lk +alice +active +pending" list.out || fail "an undelivered link is not pending: $(cat list.out)"
274publish --receiver "$receiver" --receiver-token token > pass.out
275verifier=$(printf '%s=' "$secret" | tr '_-' '/+' | base64 -d | sha256sum | cut -d' ' -f1)
276grep -Eqx "binding $lk alice $verifier" edge/v2/authorization ||
277 fail "the stored authorization does not bind $lk to alice by the secret's SHA-256: $(grep binding edge/v2/authorization)"
278link list > list.out
279grep -Eq "^$lk +alice +active +enforced +[0-9TZ:-]+ +laptop$" list.out || fail "a delivered link is not enforced: $(cat list.out)"
280if grep -rqF "$secret" publisher edge links first ./*.out ./*.err ./*.log; then
281 fail "the link's secret is in the publisher's state, the edge, the credential file, an export or a log"
282fi
283
284# An authorization the receiver stored already is refused as replayed,
285# and one whose text was changed is refused for its signature, whatever
286# its sequence.
287post() {
288 curl -sS --max-time 10 -o "$TMPDIR/reply" -w '%{http_code}' -H "Authorization: Bearer $(cat token)" \
289 --data-binary "@$2" "$receiver$1"
290}
291[ "$(post /web-export/v2/authorization publisher/authorization)" = 409 ] && grep -q '"error":"replayed"' reply ||
292 fail "a replayed authorization was not refused as replayed: $(cat reply)"
293sed 's/^sequence .*/sequence 99/' publisher/authorization > forged
294[ "$(post /web-export/v2/authorization forged)" = 403 ] && grep -q '"error":"bad-signature"' reply ||
295 fail "an authorization whose text was changed was not refused for its signature: $(cat reply)"
296
297# A publisher of the first protocol is refused with a clear error, and
298# valley-web refuses the directory the first store layout took as its
299# state.
300[ "$(post /web-export/v1/manifest forged)" = 410 ] && grep -q '"error":"unsupported-version"' reply ||
301 fail "a manifest of the first protocol was not refused as unsupported-version: $(cat reply)"
302if valley-web --state edge --listen 127.0.0.1:0 2> old.log; then fail "valley-web served the store's directory"; fi
303grep -q "is a directory" old.log || fail "valley-web refused the store's directory for another reason: $(cat old.log)"
304
305# 3. A comment changes the export, so the next pass uploads. With another
306# token it is refused as unauthenticated, and the edge keeps serving what
307# it had.
308(cd work/app && valley knot comment "$knot" -m "edge-comment-text" > /dev/null)
309if publish --receiver "$receiver" --receiver-token wrong > pass.out 2> pass.err; then
310 fail "a pass with another token succeeded"
311fi
312grep -q unauthenticated pass.err || fail "a pass with another token failed for another reason: $(cat pass.err)"
313serves "/tools/app/knots/$knot" "Open the edge"
314if grep -qF edge-comment-text "$TMPDIR/body"; then fail "the refused pass's comment is served"; fi
315
316# 4. Withdrawing anyone's grant for app takes app off the edge on the
317# next pass, its alias with it, and site stays.
318registry "{site: \"${id[site]}\"}" > work/valley/identity/registry.cue
319commit valley "withdraw app"
320publish --receiver "$receiver" --receiver-token token > /dev/null
321gone /tools/app
322gone /a
323serves /tools/site site-readme-text
324
325# 4b. A withdrawal by authorization alone, while content work is
326# stalled. With app granted again, a new knot changes the export, and a
327# pass reads it, site included, then stalls at its upload: the test holds
328# the receiver's activation lock, as a commit that checks a large
329# repository would. Meanwhile site's grant is withdrawn, and a
330# --policy-only pass takes site off the edge at once: valley-web, not
331# restarted, answers the next request without it. Once the lock is
332# released, the stalled upload, whose content holds site and whose
333# sequence is later, activates and restores nothing. The receiver deletes
334# site's repository, and the active export.json keeps its signed bytes.
335registry "{app: \"${id[app]}\", site: \"${id[site]}\"}" > work/valley/identity/registry.cue
336commit valley "grant app again"
337publish --receiver "$receiver" --receiver-token token > /dev/null
338serves /tools/app app-readme-text
339(cd work/site && valley knot new outcome "Withdrawn while stalled" > /dev/null)
340flock -o edge/v2/lock sleep 300 &
341holder=$!
342pids+=("$holder")
343publish --receiver "$receiver" --receiver-token token > stalled.out 2> stalled.err &
344stalled=$!
345for _ in $(seq 600); do
346 grep -q "^valley-web-publish: public: app, site" stalled.out && break
347 kill -0 "$stalled" 2> /dev/null || fail "the stalled pass ended before its upload: $(cat stalled.out stalled.err)"
348 sleep 0.1
349done
350grep -q "^valley-web-publish: public: app, site" stalled.out || fail "the stalled pass did not read the valley within a minute"
351sleep 1
352registry "{app: \"${id[app]}\"}" > work/valley/identity/registry.cue
353commit valley "withdraw site"
354link revoke "$lk" > /dev/null
355if link list --check "$lk" > /dev/null 2> check.err; then fail "an undelivered revocation is not pending"; fi
356grep -q "$lk: revoked here; the edge may accept it until a pass delivers the revocation" check.err ||
357 fail "list --check did not say the revocation is pending: $(cat check.err)"
358within=40 publish --receiver "$receiver" --receiver-token token --policy-only > policy.out ||
359 fail "a --policy-only pass failed while content was stalled"
360grep -q "anyone may read app$" policy.out || fail "the --policy-only pass did not withdraw site: $(cat policy.out)"
361if grep -q "the receiver activated" policy.out; then fail "the --policy-only pass sent content"; fi
362gone /tools/site
363gone /tools/homepage
364serves /tools/app app-readme-text
365if grep -q "^binding $lk " edge/v2/authorization; then fail "the --policy-only pass did not revoke $lk"; fi
366link list --check "$lk" > list.out || fail "a delivered revocation is still pending"
367grep -Eq "^$lk +alice +revoked +enforced" list.out || fail "a delivered revocation is not enforced: $(cat list.out)"
368kill -0 "$stalled" 2> /dev/null || fail "the stalled pass did not stall: $(cat stalled.out stalled.err)"
369stop "$holder"
370wait "$stalled" || fail "the stalled pass failed once its upload went on: $(cat stalled.err)"
371grep -q "the receiver activated sequence" stalled.out || fail "the stalled pass did not activate its upload: $(cat stalled.out)"
372grep -q '"project":"site"' edge/v2/current/export.json || fail "the stalled upload's export does not hold site"
373gone /tools/site
374digest=$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)
375for _ in $(seq 300); do
376 [ -e edge/v2/current/repos/site.git ] || break
377 sleep 0.1
378done
379[ ! -e edge/v2/current/repos/site.git ] || fail "the receiver kept the repository of site after its withdrawal"
380[ -e edge/v2/current/repos/app.git ] || fail "the receiver deleted the repository of app"
381[ "$(sha256sum edge/v2/current/export.json | cut -d' ' -f1)" = "$digest" ] ||
382 fail "the withdrawal changed the signed export.json"
383
384# Revoking a principal's links, and then every link, reaches the edge
385# through a policy pass the same way.
386issued alice > /dev/null
387issued alice > /dev/null
388kept=$(issued operator)
389publish --receiver "$receiver" --receiver-token token --policy-only > /dev/null
390[ "$(bindings)" = 3 ] || fail "the authorization carries $(bindings) bindings, want 3"
391link revoke --principal alice > /dev/null
392publish --receiver "$receiver" --receiver-token token --policy-only > /dev/null
393[ "$(bindings)" = 1 ] && grep -Eq "^binding $kept operator " edge/v2/authorization ||
394 fail "revoking alice's links left $(grep binding edge/v2/authorization)"
395link revoke --all > /dev/null
396publish --receiver "$receiver" --receiver-token token --policy-only > /dev/null
397[ "$(bindings)" = 0 ] || fail "revoking every link left $(bindings) bindings"
398link list --check > /dev/null || fail "a delivered revocation of every link is still pending"
399
400policy() { publish --receiver "$receiver" --receiver-token token --policy-only "$@"; }
401
402# A pass holds the links lock, the file lock beside the credential file,
403# from just before it reads the file until it has signed. Every change to
404# the file holds the same lock. Here a pass pauses holding it: the
405# credential file is a FIFO, which the pass waits to read. A revoke
406# meanwhile waits, and writes nothing, until the pass has signed, so the
407# paused pass signs the link as active. The revocation is pending until
408# the next pass, the one the wrapper starts after a revoke, signs no
409# binding for the link and the receiver acknowledges it.
410paused_lk=$(issued alice)
411policy > /dev/null
412grep -q "^binding $paused_lk " edge/v2/authorization || fail "the pass did not deliver $paused_lk"
413# A revoke while a pass is checking the policy does not wait for the
414# check: the pass holds only the authorization lock then.
415mkdir pause
416mkfifo pause/read.cue
417VALLEY_READ_SCHEMA="$TMPDIR/pause/read.cue" policy > checking.out 2> checking.err &
418checking=$!
419for _ in $(seq 300); do
420 flock -n publisher/authorization.lock true || break
421 kill -0 "$checking" 2> /dev/null || fail "the pass ended before its policy check: $(cat checking.out checking.err)"
422 sleep 0.1
423done
424if flock -n publisher/authorization.lock true; then fail "the pass did not take the authorization lock"; fi
425early=$(issued alice)
426link revoke "$early" > early.out 2> early.err || fail "a revoke while a pass checks the policy failed: $(cat early.err)"
427if grep -q waiting early.err; then fail "a revoke waited for a pass that was checking the policy: $(cat early.err)"; fi
428cp "$readSchema" pause/read.regular
429timeout 30 bash -c 'exec 3> pause/read.cue && mv -f pause/read.regular pause/read.cue && cat "$1" >&3' _ "$readSchema" ||
430 fail "the pass checking the policy did not read its schema"
431wait "$checking" || fail "the pass checking the policy failed: $(cat checking.err)"
432if grep -q "^binding $early " edge/v2/authorization; then fail "a pass signed $early, revoked while it checked the policy"; fi
433# Now the pause inside the links lock. The FIFO is linked at the credential
434# file's name, and keeps a second name for the test to write it through.
435cp links/links pause/links.regular
436cp links/links pause/links.content
437mkfifo pause/links.fifo
438ln -f pause/links.fifo links/links
439policy > paused.out 2> paused.err &
440paused=$!
441for _ in $(seq 600); do
442 flock -n links/lock true || break
443 kill -0 "$paused" 2> /dev/null || fail "the paused pass ended before it read the credential file: $(cat paused.out paused.err)"
444 sleep 0.1
445done
446if flock -n links/lock true; then fail "the paused pass did not take the links lock"; fi
447link revoke "$paused_lk" > revoke.out 2> revoke.err &
448revoking=$!
449for _ in $(seq 300); do
450 grep -q "waiting up to 1m0s for the policy pass or the change that holds the links lock" revoke.err && break
451 kill -0 "$revoking" 2> /dev/null || fail "the revoke did not wait for the paused pass: $(cat revoke.out revoke.err)"
452 sleep 0.1
453done
454grep -q "waiting up to 1m0s for the policy pass or the change that holds the links lock" revoke.err ||
455 fail "the revoke did not say it waits for the paused pass: $(cat revoke.err)"
456sleep 1
457kill -0 "$revoking" 2> /dev/null || fail "the revoke ended while a pass held the links lock: $(cat revoke.out revoke.err)"
458[ -p links/links ] || fail "the revoke wrote the credential file while a pass held the links lock"
459# Let the pass go on: once it has opened the FIFO, the regular file takes
460# its place for the revoke, and the pass reads the file through the FIFO.
461timeout 30 bash -c 'exec 3> pause/links.fifo && mv -f pause/links.regular links/links && cat pause/links.content >&3' ||
462 fail "the paused pass did not read the credential file"
463wait "$paused" || fail "the paused pass failed: $(cat paused.err)"
464wait "$revoking" || fail "the revoke failed: $(cat revoke.err)"
465grep -q "^revoked $paused_lk " revoke.out || fail "the revoke did not revoke $paused_lk: $(cat revoke.out)"
466grep -q "^binding $paused_lk " edge/v2/authorization || fail "the paused pass did not sign $paused_lk, active as it signed"
467if 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
468grep -Eq "^$paused_lk +alice +revoked +pending" list.out || fail "a revocation no pass has delivered is not pending: $(cat list.out)"
469policy > /dev/null
470if grep -q "^binding $paused_lk " edge/v2/authorization; then fail "the pass after the revoke signed the revoked $paused_lk"; fi
471link list --check "$paused_lk" > list.out || fail "a delivered revocation is still pending: $(cat list.out)"
472grep -Eq "^$paused_lk +alice +revoked +enforced" list.out || fail "a delivered revocation is not enforced: $(cat list.out)"
473
474# A lost acknowledgement: the receiver stored an authorization carrying a
475# link, and the publisher never recorded it. A revocation after that is
476# pending, never enforced, until a pass delivers it.
477cp publisher/links-delivery delivery.before
478cp publisher/authorization authorization.before
479lost=$(issued alice)
480policy > /dev/null
481grep -q "^binding $lost " edge/v2/authorization || fail "the pass did not deliver $lost"
482cp delivery.before publisher/links-delivery
483cp authorization.before publisher/authorization
484link revoke "$lost" > /dev/null
485if link list --check "$lost" > list.out 2> check.err; then fail "a revocation after a lost acknowledgement is not pending: $(cat list.out)"; fi
486grep -Eq "^$lost +alice +revoked +pending" list.out || fail "a revocation after a lost acknowledgement is not pending: $(cat list.out)"
487policy > /dev/null
488link list --check "$lost" > list.out || fail "a delivered revocation is still pending: $(cat list.out)"
489
490# A missing credential file holds no links, and the pass succeeds. A
491# corrupt one sends an authorization with no bindings, and fails the pass.
492kept=$(issued alice)
493mv links/links links.kept
494policy > /dev/null || fail "a pass with no credential file failed"
495[ "$(bindings)" = 0 ] || fail "a pass with no credential file sent $(bindings) bindings"
496echo '{' > links/links
497if policy > /dev/null 2> corrupt.err; then fail "a pass with a corrupt credential file succeeded"; fi
498grep -q "is not a credential file" corrupt.err || fail "a pass with a corrupt credential file failed for another reason: $(cat corrupt.err)"
499[ "$(bindings)" = 0 ] || fail "a corrupt credential file sent $(bindings) bindings"
500mv links.kept links/links
501policy > /dev/null
502grep -q "^binding $kept " edge/v2/authorization || fail "the credential file put back did not deliver $kept"
503
504# A credential file restored from an older copy holds a link revoked
505# since. A pass signs the file as it is, so the link is active at the edge
506# again. The next change writes past every revision a pass signed from,
507# and revoke --all cuts every link off.
508cp links/links links.older
509link revoke "$kept" > /dev/null
510policy > /dev/null
511signed=$(jq .signed publisher/links-delivery)
512cp links.older links/links
513policy > /dev/null 2> older.err || fail "a pass from an older credential file failed: $(cat older.err)"
514grep -q "^binding $kept " edge/v2/authorization || fail "a pass from an older credential file did not deliver the $kept it holds"
515link revoke --all > /dev/null
516[ "$(jq .revision links/links)" = $((signed + 1)) ] ||
517 fail "revoke --all on an older credential file wrote revision $(jq .revision links/links), not $((signed + 1))"
518policy > /dev/null
519[ "$(bindings)" = 0 ] || fail "revoke --all on an older credential file left $(bindings) bindings"
520link list --check > /dev/null || fail "after revoke --all a link is pending"
521
522# A host restore: the credential file and the publisher's state come back
523# from one older backup, after a pass delivered a revocation. The pass
524# after the restore signs above the authorization the receiver holds, and
525# the links are as they were at the backup: the revoked link is active
526# again. revoke --all then cuts every link off.
527restored=$(issued alice)
528policy > /dev/null
529cp -a publisher backup.publisher
530cp links/links backup.links
531link revoke "$restored" > /dev/null
532policy > /dev/null
533if grep -q "^binding $restored " edge/v2/authorization; then fail "the pass did not revoke $restored"; fi
534rm -rf publisher
535cp -a backup.publisher publisher
536cp backup.links links/links
537policy > restored.out 2> restored.err || fail "a pass after a host restore failed: $(cat restored.err)"
538grep -q "refused [0-9]* as replayed" restored.err || fail "the restored counter was not refused as replayed: $(cat restored.err)"
539grep -q "^binding $restored " edge/v2/authorization || fail "a host restore did not restore $restored as it was at the backup"
540serves /tools/app app-readme-text
541link list --check "$restored" > list.out || fail "the restored link is pending after a pass: $(cat list.out)"
542link revoke --all > /dev/null
543policy > /dev/null
544[ "$(bindings)" = 0 ] || fail "revoke --all after a host restore left $(bindings) bindings"
545link list --check > /dev/null || fail "after revoke --all a link is pending"
546
547# A store whose authorization is removed serves no project, though
548# valley-web has the export indexed already; once it is back, it serves.
549mv edge/v2/authorization authorization.kept
550[ "$(get /tools/app)" = "503 " ] || fail "with no authorization valley-web answered /tools/app with $(get /tools/app)"
551grep -qF "Nothing published yet" "$TMPDIR/body" || fail "with no authorization the page does not say nothing is published"
552if grep -qF app-readme-text "$TMPDIR/body"; then fail "with no authorization valley-web served app's README"; fi
553[ "$(get /)" = "503 " ] || fail "with no authorization valley-web answered / with $(get /)"
554mv authorization.kept edge/v2/authorization
555serves /tools/app app-readme-text
556
557# Granting site again, and withdrawing app as step 4 left it, serves site
558# once more: the receiver's reply says it no longer holds site's
559# repository, so the pass sends site's bundle again.
560registry "{site: \"${id[site]}\"}" > work/valley/identity/registry.cue
561commit valley "withdraw app again"
562publish --receiver "$receiver" --receiver-token token > pass.out
563grep -q "^valley-web-publish: sent .*site; the receiver activated" pass.out || fail "the pass did not send site's bundle again: $(cat pass.out)"
564serves /tools/site site-readme-text
565gone /tools/app
566
567# 5. Both servers restart onto the same content.
568stop "$receiver_pid"
569stop "$web_pid"
570receive
571serve
572gone /tools/app
573serves /tools/site site-readme-text
574
575# 6. With the receiver stopped, valley-web keeps serving. A pass that
576# withdraws site fails because it cannot send its authorization, not
577# because it failed earlier or hung until timeout ended it, and changes
578# nothing served. Once the receiver is back, the next pass checks the
579# policy again, signs a new authorization with a later sequence, and takes
580# site off the edge.
581stop "$receiver_pid"
582serves /tools/site site-readme-text
583read_policy site 'operator: true' > work/site/policy/read/read.cue
584commit site "withdraw it"
585status=0
586publish --receiver "$receiver" --receiver-token token > pass.out 2> pass.err || status=$?
587case $status in
588 0) fail "a pass to a stopped receiver succeeded" ;;
589 124 | 137) fail "a pass to a stopped receiver hung until timeout ended it (status $status)" ;;
590esac
591grep -q "sending authorization [0-9]* failed [0-9]* times: .*dial tcp ${receiver#http://}: connect: connection refused" pass.err ||
592 fail "a pass to a stopped receiver failed with status $status, but not because it could not connect: $(cat pass.err)"
593serves /tools/site site-readme-text
594unsent=$(sequence_of publisher/authorization)
595receive
596publish --receiver "$receiver" --receiver-token token > /dev/null
597[ "$(sequence_of edge/v2/authorization)" -gt "$unsent" ] ||
598 fail "the pass after the receiver came back stored authorization $(sequence_of edge/v2/authorization), not one signed after $unsent"
599gone /tools/site
600gone /tools/homepage
601gone /tools/app
602
603touch "$out"