|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# shellcheck disable=SC1091 |
| 4 | +. .tests/utils.sh |
| 5 | + |
| 6 | +echo "ℹ️ Starting Authentik tests ..." |
| 7 | + |
| 8 | +# Create working directory |
| 9 | +if [ -d /tmp/bunkerweb-plugins ] ; then |
| 10 | + do_and_check_cmd sudo rm -rf /tmp/bunkerweb-plugins |
| 11 | +fi |
| 12 | +do_and_check_cmd mkdir -p /tmp/bunkerweb-plugins/authentik/bw-data/plugins |
| 13 | +do_and_check_cmd cp -r ./authentik /tmp/bunkerweb-plugins/authentik/bw-data/plugins |
| 14 | +do_and_check_cmd sudo chown -R 101:101 /tmp/bunkerweb-plugins/authentik/bw-data |
| 15 | + |
| 16 | +# Copy compose + mock outpost config |
| 17 | +do_and_check_cmd cp .tests/authentik/docker-compose.yml /tmp/bunkerweb-plugins/authentik |
| 18 | +do_and_check_cmd cp .tests/authentik/mock-outpost.conf /tmp/bunkerweb-plugins/authentik |
| 19 | + |
| 20 | +# Edit compose to use the locally built :tests images |
| 21 | +do_and_check_cmd sed -i "s@bunkerity/bunkerweb:.*\$@bunkerweb:tests@g" /tmp/bunkerweb-plugins/authentik/docker-compose.yml |
| 22 | +do_and_check_cmd sed -i "s@bunkerity/bunkerweb-scheduler:.*\$@bunkerweb-scheduler:tests@g" /tmp/bunkerweb-plugins/authentik/docker-compose.yml |
| 23 | + |
| 24 | +# Do the tests |
| 25 | +cd /tmp/bunkerweb-plugins/authentik || exit 1 |
| 26 | +echo "ℹ️ Running compose ..." |
| 27 | +do_and_check_cmd docker compose up --build -d |
| 28 | + |
| 29 | +# Wait until the plugin is LIVE: while BunkerWeb is still applying config it serves a |
| 30 | +# 200 "Generating..." page and the plugin is inactive. An unauthenticated request only |
| 31 | +# becomes a 302 (gated) once config is applied -> use that as the readiness signal. |
| 32 | +echo "ℹ️ Waiting for BW (plugin live) ..." |
| 33 | +success="ko" |
| 34 | +retry=0 |
| 35 | +while [ $retry -lt 120 ] ; do |
| 36 | + code="$(curl -s -o /dev/null -w "%{http_code}" -H "Host: app.example.com" http://localhost 2>/dev/null)" |
| 37 | + if [ "$code" = "302" ] ; then |
| 38 | + success="ok" |
| 39 | + break |
| 40 | + fi |
| 41 | + retry=$((retry + 1)) |
| 42 | + sleep 2 |
| 43 | +done |
| 44 | +if [ "$success" = "ko" ] ; then |
| 45 | + docker compose logs |
| 46 | + docker compose down -v |
| 47 | + echo "❌ Error: BunkerWeb / authentik plugin never became active" |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +fail=0 |
| 52 | + |
| 53 | +# T1: unauthenticated -> 302 to the outpost sign-in |
| 54 | +echo "ℹ️ T1: unauthenticated request is redirected to the outpost ..." |
| 55 | +loc="$(curl -s -D - -o /dev/null -H "Host: app.example.com" http://localhost | tr -d '\r' | awk -F': ' 'tolower($1)=="location"{print $2}')" |
| 56 | +if echo "$loc" | grep -q "/outpost.goauthentik.io/start?rd=" ; then |
| 57 | + echo "✔️ T1 ok ($loc)" |
| 58 | +else |
| 59 | + echo "❌ T1 failed (Location: $loc)" ; fail=1 |
| 60 | +fi |
| 61 | + |
| 62 | +# T2: authenticated -> 200 and identity header forwarded upstream (PASS=yes) |
| 63 | +echo "ℹ️ T2: authenticated request reaches upstream with identity header ..." |
| 64 | +body="$(curl -s -H "Host: app.example.com" -b "mock_session=valid" http://localhost)" |
| 65 | +if echo "$body" | grep -qi '"x-authentik-username": *"alice"' ; then |
| 66 | + echo "✔️ T2 ok" |
| 67 | +else |
| 68 | + echo "❌ T2 failed" ; fail=1 |
| 69 | +fi |
| 70 | + |
| 71 | +# T3: spoofed X-authentik-* are stripped (security); only Authentik's values survive |
| 72 | +echo "ℹ️ T3: spoofed identity headers are stripped ..." |
| 73 | +body="$(curl -s -H "Host: app.example.com" -b "mock_session=valid" -H "X-authentik-username: hacker" -H "X-authentik-uid: 0" http://localhost)" |
| 74 | +if echo "$body" | grep -qi '"x-authentik-username": *"alice"' \ |
| 75 | + && ! echo "$body" | grep -qi '"x-authentik-username": *"hacker"' \ |
| 76 | + && ! echo "$body" | grep -qi '"x-authentik-uid"' ; then |
| 77 | + echo "✔️ T3 ok (spoof stripped)" |
| 78 | +else |
| 79 | + echo "❌ T3 failed (spoof not stripped)" ; fail=1 |
| 80 | +fi |
| 81 | + |
| 82 | +# T4: PASS=no site strips spoofed identity headers and forwards none |
| 83 | +echo "ℹ️ T4: PASS=no site forwards no identity header ..." |
| 84 | +body="$(curl -s -H "Host: noheaders.example.com" -b "mock_session=valid" -H "X-authentik-username: hacker" http://localhost)" |
| 85 | +if ! echo "$body" | grep -qi "x-authentik-username" ; then |
| 86 | + echo "✔️ T4 ok" |
| 87 | +else |
| 88 | + echo "❌ T4 failed (identity header reached upstream)" ; fail=1 |
| 89 | +fi |
| 90 | + |
| 91 | +# T5: outpost path is proxied (not gated) |
| 92 | +echo "ℹ️ T5: outpost path is proxied to the outpost ..." |
| 93 | +body="$(curl -s -H "Host: app.example.com" http://localhost/outpost.goauthentik.io/start)" |
| 94 | +if echo "$body" | grep -q "MOCK AUTHENTIK OUTPOST" ; then |
| 95 | + echo "✔️ T5 ok" |
| 96 | +else |
| 97 | + echo "❌ T5 failed" ; fail=1 |
| 98 | +fi |
| 99 | + |
| 100 | +# T6: trailing-slash AUTHENTIK_URL still proxies the outpost (rstrip fix) |
| 101 | +echo "ℹ️ T6: trailing-slash AUTHENTIK_URL still proxies ..." |
| 102 | +body="$(curl -s -H "Host: noheaders.example.com" http://localhost/outpost.goauthentik.io/start)" |
| 103 | +if echo "$body" | grep -q "MOCK AUTHENTIK OUTPOST" ; then |
| 104 | + echo "✔️ T6 ok" |
| 105 | +else |
| 106 | + echo "❌ T6 failed (trailing-slash URL broke the outpost proxy)" ; fail=1 |
| 107 | +fi |
| 108 | + |
| 109 | +if [ "$fail" -ne 0 ] ; then |
| 110 | + docker compose logs |
| 111 | + docker compose down -v |
| 112 | + echo "❌ Authentik tests failed" |
| 113 | + exit 1 |
| 114 | +fi |
| 115 | + |
| 116 | +if [ "$1" = "verbose" ] ; then |
| 117 | + docker compose logs |
| 118 | +fi |
| 119 | + |
| 120 | +docker compose down -v |
| 121 | +echo "✔️ Authentik tests succeeded" |
0 commit comments