| 1 | # The push path end to end, over the shipped binaries (web-push-e2e.nix). |
| 2 | # Every server is started through start, which waits a bounded time for |
| 3 | # it to listen, and the trap stops every one still running on any exit. |
| 4 | set -euo pipefail |
| 5 | |
| 6 | export HOME="$TMPDIR/home" |
| 7 | export XDG_CACHE_HOME="$TMPDIR/cache" |
| 8 | export XDG_STATE_HOME="$TMPDIR/xdg-state" |
| 9 | export GIT_CONFIG_NOSYSTEM=1 |
| 10 | export GIT_CONFIG_GLOBAL="$TMPDIR/gitconfig" |
| 11 | printf '[user]\n\tname = operator\n\temail = operator@example.org\n[init]\n\tdefaultBranch = main\n' > "$GIT_CONFIG_GLOBAL" |
| 12 | cd "$TMPDIR" |
| 13 | |
| 14 | fail() { |
| 15 | echo "web-push-e2e: $*" >&2 |
| 16 | exit 1 |
| 17 | } |
| 18 | |
| 19 | # The servers still running, stopped on every exit. |
| 20 | pids=() |
| 21 | stop() { |
| 22 | local pid=$1 rest=() p |
| 23 | kill -TERM "$pid" 2> /dev/null || true |
| 24 | for _ in $(seq 150); do |
| 25 | kill -0 "$pid" 2> /dev/null || break |
| 26 | sleep 0.1 |
| 27 | done |
| 28 | kill -KILL "$pid" 2> /dev/null || true |
| 29 | wait "$pid" 2> /dev/null || true |
| 30 | for p in "${pids[@]}"; do |
| 31 | if [ "$p" != "$pid" ]; then rest+=("$p"); fi |
| 32 | done |
| 33 | pids=("${rest[@]}") |
| 34 | } |
| 35 | cleanup() { |
| 36 | for pid in "${pids[@]}"; do stop "$pid"; done |
| 37 | } |
| 38 | trap cleanup EXIT |
| 39 | |
| 40 | # start NAME LOG COMMAND...: start a server that prints "NAME: listening |
| 41 | # on ADDR" on stderr once it listens, and wait at most 30 seconds for it. |
| 42 | # It sets pid and addr. |
| 43 | start() { |
| 44 | local name=$1 log=$2 |
| 45 | shift 2 |
| 46 | "$@" 2> "$log" & |
| 47 | pid=$! |
| 48 | pids+=("$pid") |
| 49 | for _ in $(seq 300); do |
| 50 | addr=$(sed -n "s/^$name: listening on //p" "$log") |
| 51 | if [ -n "$addr" ]; then return; fi |
| 52 | kill -0 "$pid" 2> /dev/null || fail "$name exited before it listened: $(cat "$log")" |
| 53 | sleep 0.1 |
| 54 | done |
| 55 | fail "$name did not say where it listens within 30 seconds" |
| 56 | } |
| 57 | |
| 58 | # get PATH: valley-web's status and Location for a GET of PATH, with its |
| 59 | # body in $TMPDIR/body. follow PATH: the same after following redirects. |
| 60 | get() { |
| 61 | curl -sS --max-time 10 -o "$TMPDIR/body" -w '%{http_code} %header{location}' "http://$web$1" |
| 62 | } |
| 63 | follow() { |
| 64 | curl -sS --max-time 10 --max-redirs 3 -L -o "$TMPDIR/body" -w '%{http_code}' "http://$web$1" |
| 65 | } |
| 66 | # serves PATH TEXT: valley-web serves PATH, after its redirect to the |
| 67 | # tree, with TEXT in the page. gone PATH: it answers 404. |
| 68 | serves() { |
| 69 | local status |
| 70 | status=$(follow "$1") |
| 71 | [ "$status" = 200 ] || fail "valley-web answered $1 with $status, want 200" |
| 72 | grep -qF "$2" "$TMPDIR/body" || fail "valley-web's page for $1 does not show $2" |
| 73 | } |
| 74 | gone() { |
| 75 | local status |
| 76 | status=$(get "$1") |
| 77 | [ "$status" = "404 " ] || fail "valley-web answered $1 with $status, want 404" |
| 78 | } |
| 79 | |
| 80 | # One pass of the publisher as the package wraps it, with its pinned git, |
| 81 | # cue, valley CLI and schemas. |
| 82 | publish() { |
| 83 | timeout -k 10 300 valley-web-publish --instance-repo "$TMPDIR/srv/valley.git" \ |
| 84 | --instance valley-host --destination example.org --host "$TMPDIR/host.json" \ |
| 85 | --state "$TMPDIR/publisher" --principals "$TMPDIR/principals.json" "$@" |
| 86 | } |
| 87 | |
| 88 | commit() { |
| 89 | git -C "work/$1" add --all |
| 90 | git -C "work/$1" commit --quiet -m "$1: $2" |
| 91 | git -C "work/$1" push --quiet origin main |
| 92 | } |
| 93 | |
| 94 | # The projects' publication identities, which their policy/read declares |
| 95 | # and the registry's grants name. |
| 96 | declare -A id=( |
| 97 | [valley]=f83927d5a4ed068cdd02a8cdaa09f26d |
| 98 | [app]=9da6809f038df92138c131cd16bed76d |
| 99 | [site]=273d5668702918ea7ae079de374db96b |
| 100 | ) |
| 101 | read_policy() { |
| 102 | printf 'package read\n\nproject: "%s"\nidentity: "%s"\nreaders: {%s}\n' "$1" "${id[$1]}" "$2" |
| 103 | } |
| 104 | # The instance's registry: the operator governs it, and anyone may read |
| 105 | # the projects of the scope given at the edge. |
| 106 | registry() { |
| 107 | cat << EOF |
| 108 | package identity |
| 109 | |
| 110 | boundaries: { |
| 111 | registry: kind: "registry" |
| 112 | web: {kind: "edge-read", destination: "example.org"} |
| 113 | } |
| 114 | genesis: "operator" |
| 115 | principals: operator: { |
| 116 | kind: "human" |
| 117 | keys: [{class: "ssh-ed25519", bound: "hardware", public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyForARejectedFixtureAAAAAAAAAAA"}] |
| 118 | grants: { |
| 119 | govern: boundary: "registry" |
| 120 | read: {boundary: "web", projects: "all"} |
| 121 | } |
| 122 | } |
| 123 | anyone: grants: read: {boundary: "web", projects: $1} |
| 124 | EOF |
| 125 | } |
| 126 | |
| 127 | # 1. The instance and its two projects, each with its own README, and both |
| 128 | # readable by anyone, in the group tools. app has the short alias a, and |
| 129 | # site the former name homepage. |
| 130 | for name in valley app site; do |
| 131 | git init --quiet --bare "srv/$name.git" |
| 132 | git init --quiet "work/$name" |
| 133 | git -C "work/$name" remote add origin "$TMPDIR/srv/$name.git" |
| 134 | git -C "work/$name" config valley.principal operator |
| 135 | done |
| 136 | mkdir -p work/valley/policy/instance work/valley/policy/read work/valley/identity |
| 137 | echo 'package verification' > work/valley/policy/instance/floor.cue |
| 138 | read_policy valley 'operator: true' > work/valley/policy/read/read.cue |
| 139 | registry "{app: \"${id[app]}\", site: \"${id[site]}\"}" > work/valley/identity/registry.cue |
| 140 | commit valley "the instance" |
| 141 | projects=$(printf 'app\nsite\nvalley\n' | git -C srv/valley.git hash-object -w --stdin) |
| 142 | git -C srv/valley.git update-ref refs/the-valley/projects "$projects" |
| 143 | for name in app site; do |
| 144 | mkdir -p "work/$name/policy/read" |
| 145 | echo "$TMPDIR/srv/valley.git" > "work/$name/policy/valley" |
| 146 | printf '# %s\n\n%s-readme-text\n' "$name" "$name" > "work/$name/README.md" |
| 147 | read_policy "$name" 'anyone: true, operator: true' > "work/$name/policy/read/read.cue" |
| 148 | commit "$name" "the first commit" |
| 149 | done |
| 150 | knot=$(cd work/app && valley knot new outcome "Open the edge" --json | jq -r .knot.id) |
| 151 | |
| 152 | cat > host.cue << 'EOF' |
| 153 | package valley |
| 154 | |
| 155 | groups: core: name: "tools" |
| 156 | projects: app: {group: "core", aliases: ["a"]} |
| 157 | projects: site: {group: "core", formerNames: ["homepage"]} |
| 158 | projects: valley: {} |
| 159 | EOF |
| 160 | cue export --out json "$schema" host.cue > host.json |
| 161 | echo '{"operator": "operator"}' > principals.json |
| 162 | |
| 163 | # 2. A pass to a directory makes the export key, whose verifier line the |
| 164 | # receiver pins from its first start. Then the receiver and valley-web |
| 165 | # start over one state directory, and a pass uploads. |
| 166 | publish --out first > /dev/null |
| 167 | test -s publisher/verifier || fail "the first pass made no verifier line" |
| 168 | echo edge-token-0123456789abcdef0123456789 > token |
| 169 | echo another-token-0123456789abcdef0123456 > wrong |
| 170 | receive() { |
| 171 | start valley-web-receive receive.log valley-web-receive --state edge --listen 127.0.0.1:0 \ |
| 172 | --instance valley-host --destination example.org --verifiers publisher/verifier --token-file token |
| 173 | receiver_pid=$pid receiver=http://$addr |
| 174 | } |
| 175 | serve() { |
| 176 | start valley-web web.log valley-web --state edge --listen 127.0.0.1:0 |
| 177 | web_pid=$pid web=$addr |
| 178 | } |
| 179 | receive |
| 180 | publish --receiver "$receiver" --receiver-token token > pass.out |
| 181 | grep -q "the receiver activated sequence" pass.out || fail "the pass did not say the receiver activated it: $(cat pass.out)" |
| 182 | serve |
| 183 | serves /tools/app app-readme-text |
| 184 | serves /tools/site site-readme-text |
| 185 | serves "/tools/app/knots/$knot" "Open the edge" |
| 186 | [ "$(get "/a/knots/$knot?x=1")" = "308 /tools/app/knots/$knot?x=1" ] || |
| 187 | fail "the alias a answered $(get "/a/knots/$knot?x=1")" |
| 188 | [ "$(get /tools/homepage/source/)" = "308 /tools/site/source/" ] || |
| 189 | fail "the former name homepage answered $(get /tools/homepage/source/)" |
| 190 | |
| 191 | # 3. A comment changes the export, so the next pass uploads. With another |
| 192 | # token it is refused as unauthenticated, and the edge keeps serving what |
| 193 | # it had. |
| 194 | (cd work/app && valley knot comment "$knot" -m "edge-comment-text" > /dev/null) |
| 195 | if publish --receiver "$receiver" --receiver-token wrong > pass.out 2> pass.err; then |
| 196 | fail "a pass with another token succeeded" |
| 197 | fi |
| 198 | grep -q unauthenticated pass.err || fail "a pass with another token failed for another reason: $(cat pass.err)" |
| 199 | serves "/tools/app/knots/$knot" "Open the edge" |
| 200 | if grep -qF edge-comment-text "$TMPDIR/body"; then fail "the refused pass's comment is served"; fi |
| 201 | |
| 202 | # 4. Withdrawing anyone's grant for app takes app off the edge on the |
| 203 | # next pass, its alias with it, and site stays. |
| 204 | registry "{site: \"${id[site]}\"}" > work/valley/identity/registry.cue |
| 205 | commit valley "withdraw app" |
| 206 | publish --receiver "$receiver" --receiver-token token > /dev/null |
| 207 | gone /tools/app |
| 208 | gone /a |
| 209 | serves /tools/site site-readme-text |
| 210 | |
| 211 | # 5. Both servers restart onto the same content. |
| 212 | stop "$receiver_pid" |
| 213 | stop "$web_pid" |
| 214 | receive |
| 215 | serve |
| 216 | gone /tools/app |
| 217 | serves /tools/site site-readme-text |
| 218 | |
| 219 | # 6. With the receiver stopped, valley-web keeps serving. A pass that |
| 220 | # withdraws site fails because its upload cannot connect, not because it |
| 221 | # failed earlier or hung until timeout ended it, and changes nothing |
| 222 | # served. Once the receiver is back, the next pass takes site off the edge. |
| 223 | stop "$receiver_pid" |
| 224 | serves /tools/site site-readme-text |
| 225 | read_policy site 'operator: true' > work/site/policy/read/read.cue |
| 226 | commit site "withdraw it" |
| 227 | status=0 |
| 228 | publish --receiver "$receiver" --receiver-token token > pass.out 2> pass.err || status=$? |
| 229 | case $status in |
| 230 | 0) fail "a pass to a stopped receiver succeeded" ;; |
| 231 | 124 | 137) fail "a pass to a stopped receiver hung until timeout ended it (status $status)" ;; |
| 232 | esac |
| 233 | grep -q "the upload failed [0-9]* times: .*dial tcp ${receiver#http://}: connect: connection refused" pass.err || |
| 234 | fail "a pass to a stopped receiver failed with status $status, but not because its upload could not connect: $(cat pass.err)" |
| 235 | serves /tools/site site-readme-text |
| 236 | receive |
| 237 | publish --receiver "$receiver" --receiver-token token > /dev/null |
| 238 | gone /tools/site |
| 239 | gone /tools/homepage |
| 240 | |
| 241 | touch "$out" |