Files
ianitor/deploy/test-access.sh
T

120 lines
4.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# SPDX-License-Identifier: MPL-2.0
#
# After every server change (CLAUDE.md): the hosts still answer as they should, protected hosts
# still send strangers to Authelia, and Ianitor's control is not reachable from outside.
# Run it from another machine on the LAN, not from the server (loopback would hide exactly the
# exposure this looks for). Read-only: it sends GET requests and nothing else.
#
# deploy/test-access.sh --server 192.168.100.166 \
# --public flowers.stage.marek-novak.cz --public esp32.stage.marek-novak.cz \
# --protected wiki.example --auth-host auth.example \
# --via-ianitor flowers.stage.marek-novak.cz --via-ianitor esp32.stage.marek-novak.cz
#
# --server ADDR the server's LAN address (requests go there, whatever DNS says)
# --public HOST must answer 2xx or 3xx (not a sign-in redirect)
# --protected HOST without a session, must redirect to --auth-host (302/303) or answer 401
# --auth-host HOST Authelia's portal name, for --protected
# --via-ianitor HOST must answer 2xx/3xx, and the answer must not come from Caddy alone
# (Ianitor adds no Via; Caddy adds "Via: 1.1 Caddy" once, so exactly one)
# --control-port N control's port (default 7878): must NOT answer from outside
# --insecure accept a certificate the client does not trust (staging: tls internal)
#
# Exit status: 0 when every check passes, 1 otherwise. Each check prints one line.
set -u
server=""
auth_host=""
control_port=7878
insecure=()
public=()
protected=()
via=()
while [ $# -gt 0 ]; do
case "$1" in
--server) server="$2"; shift 2 ;;
--public) public+=("$2"); shift 2 ;;
--protected) protected+=("$2"); shift 2 ;;
--auth-host) auth_host="$2"; shift 2 ;;
--via-ianitor) via+=("$2"); shift 2 ;;
--control-port) control_port="$2"; shift 2 ;;
--insecure) insecure=(-k); shift ;;
-h|--help) sed -n '2,26p' "$0"; exit 0 ;;
*) echo "unknown argument: $1" >&2; exit 2 ;;
esac
done
if [ -z "$server" ]; then
echo "--server is required" >&2
exit 2
fi
if [ ${#protected[@]} -gt 0 ] && [ -z "$auth_host" ]; then
echo "--protected needs --auth-host" >&2
exit 2
fi
failures=0
pass() { printf 'ok %s\n' "$1"; }
fail() { printf 'FAIL %s\n' "$1"; failures=$((failures + 1)); }
# status, location and the number of Via headers for https://HOST/ sent to the server
probe() {
curl -sS -o /dev/null -D - --max-time 15 "${insecure[@]}" \
--resolve "$1:443:$server" "https://$1/" 2>/dev/null | tr -d '\r'
}
for h in "${public[@]}"; do
head=$(probe "$h")
code=$(printf '%s\n' "$head" | awk 'NR==1 {print $2}')
loc=$(printf '%s\n' "$head" | awk 'tolower($1)=="location:" {print $2}')
if [[ "$code" =~ ^[23][0-9][0-9]$ ]] && { [ -z "$auth_host" ] || [[ "$loc" != *"$auth_host"* ]]; }; then
pass "public $h answers $code"
else
fail "public $h answers '${code:-nothing}'${loc:+ -> $loc}"
fi
done
for h in "${protected[@]}"; do
head=$(probe "$h")
code=$(printf '%s\n' "$head" | awk 'NR==1 {print $2}')
loc=$(printf '%s\n' "$head" | awk 'tolower($1)=="location:" {print $2}')
if { [[ "$code" =~ ^30[23]$ ]] && [[ "$loc" == *"$auth_host"* ]]; } || [ "$code" = "401" ]; then
pass "protected $h sends strangers to sign-in ($code)"
else
fail "protected $h answers '${code:-nothing}'${loc:+ -> $loc} without a session"
fi
done
for h in "${via[@]}"; do
head=$(probe "$h")
code=$(printf '%s\n' "$head" | awk 'NR==1 {print $2}')
vias=$(printf '%s\n' "$head" | grep -ci '^via:' || true)
if [[ "$code" =~ ^[23][0-9][0-9]$ ]]; then
pass "through Ianitor $h answers $code (Via headers: $vias)"
else
fail "through Ianitor $h answers '${code:-nothing}'"
fi
done
# Control must not answer from outside loopback, on any address of the server.
if curl -sS -o /dev/null --max-time 5 "http://$server:$control_port/api/v1/overview" 2>/dev/null; then
fail "control answers on $server:$control_port from outside loopback"
else
pass "control is not reachable on $server:$control_port"
fi
# Nor Ianitor edge's loopback listeners in the beside-Caddy setup.
for p in 18080 18443; do
if curl -sS -o /dev/null --max-time 5 -k "https://$server:$p/" 2>/dev/null ||
curl -sS -o /dev/null --max-time 5 "http://$server:$p/" 2>/dev/null; then
fail "Ianitor edge's loopback port $p answers from outside"
else
pass "port $p is not reachable from outside"
fi
done
if [ "$failures" -gt 0 ]; then
echo "$failures check(s) failed"
exit 1
fi
echo "all checks passed"