| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- #!/bin/sh
- # unbound-sec test suite.
- #
- # Runs fully inside Docker: this script is executed in a docker:cli container
- # with the Docker socket mounted; every assertion targets containers.
- #
- # Usage:
- # ./test/run.sh (host wrapper; recommended)
- # or directly in a docker:cli container with the socket + repo mounted
- # at the same absolute path as on the host (REPO_PATH set).
- set -u
- IMAGE="unbound-sec:test"
- C1="ubsec-t1" # main hardened container
- C2="ubsec-t2" # valid drop-in container
- C3="ubsec-t3" # broken drop-in container
- C4="ubsec-t4" # identity control container
- NET="ubsec-testnet"
- # Fixtures must use HOST paths (bind mounts are resolved by the daemon).
- # run.sh sets REPO_PATH to the host repo path and pre-creates the fixtures.
- CFG1="${REPO_PATH:-$(pwd)}/test/fixture-config-ok"
- CFG2="${REPO_PATH:-$(pwd)}/test/fixture-config-bad"
- PASS=0
- FAIL=0
- say() { printf '%s\n' "$*"; }
- ok() { PASS=$((PASS+1)); say " [PASS] $1"; }
- bad() { FAIL=$((FAIL+1)); say " [FAIL] $1"; }
- cleanup() {
- docker rm -f "$C1" "$C2" "$C3" "$C4" >/dev/null 2>&1
- docker network rm "$NET" >/dev/null 2>&1
- rm -rf "$(pwd)/test/fixture-identity" 2>/dev/null
- }
- trap cleanup EXIT
- say "== 0. cleanup old artifacts =="
- cleanup
- say "== 1. build image =="
- if docker build -t "$IMAGE" . >/dev/null 2>&1; then
- ok "docker build"
- else
- bad "docker build"
- say "ABORT: build failed"; exit 1
- fi
- say "== 2. start hardened container =="
- docker network create "$NET" >/dev/null
- docker run -d --name "$C1" --network "$NET" \
- -p 5053:5053/udp -p 5053:5053/tcp \
- --read-only \
- --tmpfs /tmp --tmpfs "/run/unbound:uid=1500,gid=1500" \
- --security-opt no-new-privileges:true \
- --cap-drop ALL \
- "$IMAGE" >/dev/null
- sleep 3
- if [ "$(docker inspect -f '{{.State.Running}}' "$C1" 2>/dev/null)" = "true" ]; then
- ok "container running"
- else
- bad "container running"; docker logs "$C1" 2>&1 | tail -5
- fi
- say "== 3. healthcheck =="
- healthy="no"
- i=0
- while [ $i -lt 12 ]; do
- h="$(docker inspect -f '{{.State.Health.Status}}' "$C1" 2>/dev/null)"
- [ "$h" = "healthy" ] && healthy="yes" && break
- sleep 6
- i=$((i+1))
- done
- if [ "$healthy" = "yes" ]; then
- ok "healthcheck healthy"
- else
- bad "healthcheck healthy (status=$h)"
- fi
- say "== 4. version =="
- v="$(docker exec "$C1" unbound -V 2>/dev/null | head -1)"
- case "$v" in
- *1.26.2*) ok "unbound version 1.26.2 ($v)";;
- *) bad "unbound version 1.26.2 (got: $v)";;
- esac
- say "== 5. DNS resolution =="
- if docker exec "$C1" drill -p 5053 example.com @127.0.0.1 2>/dev/null | grep -q "example.com"; then
- ok "drill resolves example.com"
- else
- bad "drill resolves example.com"
- fi
- say "== 6. DNSSEC (AD bit, DO query) =="
- probe_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/dns-check.py:/probe.py:ro" python:alpine python /probe.py "$C1" 5053 2>&1)"
- probe_rc=$?
- if [ $probe_rc -ne 0 ] || printf '%s' "$probe_out" | grep -q "FAIL"; then
- bad "DNSSEC AD-bit probe"
- printf '%s\n' "$probe_out" | sed 's/^/ /'
- else
- ok "DNSSEC AD-bit probe"
- fi
- say "== 7. non-root user =="
- uid="$(docker exec "$C1" sh -c 'id -u' 2>/dev/null)"
- if [ "$uid" = "1500" ]; then
- ok "runs as uid 1500"
- else
- bad "runs as uid 1500 (got: $uid)"
- fi
- say "== 8. capabilities =="
- capeff="$(docker exec "$C1" sh -c 'grep ^CapEff /proc/self/status' 2>/dev/null | awk '{print $2}')"
- if [ "$capeff" = "0000000000000000" ]; then
- ok "CapEff = 0"
- else
- bad "CapEff = 0 (got: $capeff)"
- fi
- say "== 9. no SUID binaries =="
- suid="$(docker exec "$C1" sh -c 'find / -perm -4000 -type f 2>/dev/null' 2>/dev/null)"
- if [ -z "$suid" ]; then
- ok "no SUID-root binaries"
- else
- bad "no SUID-root binaries (found: $suid)"
- fi
- say "== 10. read-only root filesystem =="
- if docker exec "$C1" sh -c 'touch /usr/writetest 2>/dev/null'; then
- bad "root fs is read-only (write to /usr succeeded)"
- else
- ok "root fs is read-only"
- fi
- if docker exec "$C1" sh -c 'touch /run/unbound/writetest && rm /run/unbound/writetest' 2>/dev/null; then
- ok "/run/unbound writable (tmpfs)"
- else
- bad "/run/unbound writable (tmpfs)"
- fi
- say "== 11. no-new-privileges + seccomp =="
- nnp="$(docker exec "$C1" sh -c 'grep ^NoNewPrivs /proc/self/status' 2>/dev/null | awk '{print $2}')"
- if [ "$nnp" = "1" ]; then
- ok "NoNewPrivs = 1"
- else
- bad "NoNewPrivs = 1 (got: $nnp)"
- fi
- sc="$(docker exec "$C1" sh -c 'grep ^Seccomp: /proc/self/status' 2>/dev/null | awk '{print $2}')"
- if [ "$sc" = "2" ]; then
- ok "seccomp filter active"
- else
- bad "seccomp filter active (got: $sc)"
- fi
- say "== 12. hide-identity / hide-version =="
- id_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/chaos-check.py:/probe.py:ro" python:alpine python /probe.py "$C1" 5053 id.server. empty 2>&1)"
- id_rc=$?
- if [ $id_rc -ne 0 ] || printf '%s' "$id_out" | grep -q "FAIL"; then
- bad "hide-identity: id.server. answer is empty"
- printf '%s\n' "$id_out" | sed 's/^/ /'
- else
- ok "hide-identity: id.server. answer is empty"
- fi
- # Control: same image with hide-identity switched off must reveal the identity.
- CFG_ID="${REPO_PATH:-$(pwd)}/test/fixture-identity"
- mkdir -p "$CFG_ID"
- printf 'server:\n hide-identity: no\n' > "$CFG_ID/00-identity.conf"
- docker run -d --name "$C4" --network "$NET" -v "$CFG_ID:/config:ro" "$IMAGE" >/dev/null
- sleep 3
- id2_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/chaos-check.py:/probe.py:ro" python:alpine python /probe.py "$C4" 5053 id.server. unbound-sec 2>&1)"
- id2_rc=$?
- if [ $id2_rc -ne 0 ] || printf '%s' "$id2_out" | grep -q "FAIL"; then
- bad "identity control: hide-identity:no reveals identity"
- printf '%s\n' "$id2_out" | sed 's/^/ /'
- else
- ok "identity control: hide-identity:no reveals identity"
- fi
- say "== 13. drop-in config include =="
- # Fixtures are created on the host by test/run.sh (host paths required).
- if [ ! -f "$CFG1/99-test.conf" ] || [ ! -f "$CFG2/99-broken.conf" ]; then
- bad "test fixtures missing (run via test/run.sh)"
- fi
- docker run -d --name "$C2" --network "$NET" -v "$CFG1:/config:ro" "$IMAGE" >/dev/null
- sleep 3
- if [ "$(docker inspect -f '{{.State.Running}}' "$C2" 2>/dev/null)" = "true" ]; then
- ok "valid drop-in config accepted (include works)"
- else
- bad "valid drop-in config accepted"
- fi
- docker run -d --name "$C3" --network "$NET" -v "$CFG2:/config:ro" "$IMAGE" >/dev/null
- sleep 3
- if [ "$(docker inspect -f '{{.State.Running}}' "$C3" 2>/dev/null)" = "false" ]; then
- ok "broken drop-in config rejects start (checkconf)"
- else
- bad "broken drop-in config rejects start"
- fi
- say "== 14. package attack surface =="
- has_curl="$(docker exec "$C1" sh -c 'test -x /usr/bin/curl && echo yes' 2>/dev/null)"
- if [ "$has_curl" = "yes" ]; then
- bad "curl not in runtime image"
- else
- ok "no curl in runtime image"
- fi
- has_shadow="$(docker exec "$C1" sh -c 'test -x /usr/bin/chsh && echo yes' 2>/dev/null)"
- if [ "$has_shadow" = "yes" ]; then
- bad "shadow package not removed"
- else
- ok "shadow package removed"
- fi
- say ""
- say "=================================================="
- say "RESULT: $PASS passed, $FAIL failed"
- say "=================================================="
- [ $FAIL -eq 0 ]
|