run-tests.sh 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. #!/bin/sh
  2. # unbound-sec test suite.
  3. #
  4. # Runs fully inside Docker: this script is executed in a docker:cli container
  5. # with the Docker socket mounted; every assertion targets containers.
  6. #
  7. # Usage:
  8. # ./test/run.sh (host wrapper; recommended)
  9. # or directly in a docker:cli container with the socket + repo mounted
  10. # at the same absolute path as on the host (REPO_PATH set).
  11. set -u
  12. IMAGE="unbound-sec:test"
  13. C1="ubsec-t1" # main hardened container
  14. C2="ubsec-t2" # valid drop-in container
  15. C3="ubsec-t3" # broken drop-in container
  16. C4="ubsec-t4" # identity control container
  17. C5="ubsec-t5" # anchor persistence container
  18. C6="ubsec-t6" # negative-test: valid zone + trust anchor (expect AD)
  19. C7="ubsec-t7" # negative-test: broken signature + trust anchor (expect SERVFAIL)
  20. C8="ubsec-t8" # negative-test: broken zone, validator off (counter-test, expect NOERROR)
  21. RESP="ubsec-t6-resp" # static DNS responder, VALID zone (port 5353)
  22. RESP_BROKEN="ubsec-t7-resp" # static DNS responder, BROKEN zone (port 5354)
  23. NET="ubsec-testnet"
  24. # The compose template is tested under a DEDICATED project name so the test
  25. # never touches a production deployment of the same template on this daemon
  26. # (its own container, network, and volume unbound-sec-test_unbound_anchor).
  27. PROJ_TEST="unbound-sec-test"
  28. C6_CONT="${PROJ_TEST}-unbound-1" # compose auto-name for the test project
  29. # Fixtures must use HOST paths (bind mounts are resolved by the daemon).
  30. # run.sh sets REPO_PATH to the host repo path and pre-creates the fixtures.
  31. CFG1="${REPO_PATH:-$(pwd)}/test/fixture-config-ok"
  32. CFG2="${REPO_PATH:-$(pwd)}/test/fixture-config-bad"
  33. ANCHOR_DIR="${REPO_PATH:-$(pwd)}/test/fixture-anchor"
  34. PASS=0
  35. FAIL=0
  36. NOTVERIFIED=0 # checks that could not be run (infrastructure, e.g. timeout)
  37. say() { printf '%s\n' "$*"; }
  38. ok() { PASS=$((PASS+1)); say " [PASS] $1"; }
  39. bad() { FAIL=$((FAIL+1)); say " [FAIL] $1"; }
  40. # A security probe that could not be executed (timeout / network) — NOT a
  41. # content finding, but must be visible in the result.
  42. nv() { NOTVERIFIED=$((NOTVERIFIED+1)); say " [NOT VERIFIED] $1"; }
  43. # Resolve a container's IP on $NET so probes can address it directly
  44. # (avoids depending on Docker's embedded DNS inside the probe container).
  45. ip_of() { docker inspect -f '{{(index .NetworkSettings.Networks "'"$NET"').IPAddress}}' "$1" 2>/dev/null; }
  46. # Test-only compose invocation (dedicated project + test-image override;
  47. # see test/compose.test.yml). A function (not an array) for POSIX sh.
  48. compose_test() {
  49. (cd "${REPO_PATH:-$(pwd)}" && docker compose -p "$PROJ_TEST" \
  50. -f docker-compose.yml -f test/compose.test.yml "$@")
  51. }
  52. cleanup() {
  53. # Remove ONLY resources this test run created. The fixed container name
  54. # "unbound" is gone from the template, so we no longer rm a name that a
  55. # production deployment might use.
  56. docker rm -f "$C1" "$C2" "$C3" "$C4" "$C5" "$C6" "$C7" "$C8" "$RESP" "$RESP_BROKEN" >/dev/null 2>&1
  57. compose_test down --remove-orphans --volumes >/dev/null 2>&1
  58. docker network rm "$NET" >/dev/null 2>&1
  59. docker network rm "ubsec-negnet" >/dev/null 2>&1
  60. rm -rf "$(pwd)/test/fixture-identity" 2>/dev/null
  61. rm -rf "$(pwd)/test/fixture-broken-valid" 2>/dev/null
  62. rm -rf "$(pwd)/test/fixture-broken-broken" 2>/dev/null
  63. rm -rf "$(pwd)/test/fixture-broken-noval" 2>/dev/null
  64. rm -f "$(pwd)/test/.broken-valid.bin" 2>/dev/null
  65. rm -f "$(pwd)/test/.broken-broken.bin" 2>/dev/null
  66. rm -f "$(pwd)/test/.broken-dnskey.bin" 2>/dev/null
  67. rm -f "$(pwd)/test/.broken-anchor.key" 2>/dev/null
  68. rm -rf "$ANCHOR_DIR" 2>/dev/null
  69. rm -f "$(pwd)/test/build.log" 2>/dev/null
  70. }
  71. trap cleanup EXIT
  72. say "== 0. cleanup old artifacts =="
  73. cleanup
  74. say "== 1. build image =="
  75. BUILDLOG="$(pwd)/test/build.log"
  76. # CACHEBUST forces the src stages (SHA256 + GPG verification) to re-run on
  77. # every test run so the build log always contains fresh evidence; the slow
  78. # compile stages stay cached.
  79. if docker build --build-arg CACHEBUST="$(date +%s)" -t "$IMAGE" . >"$BUILDLOG" 2>&1; then
  80. ok "docker build"
  81. # NOTE: we deliberately do NOT tag unbound-sec:latest here. The compose
  82. # template is exercised through test/compose.test.yml, which pins the test
  83. # image — so the production tag is left untouched.
  84. else
  85. bad "docker build"
  86. tail -15 "$BUILDLOG"
  87. say "ABORT: build failed"; exit 1
  88. fi
  89. say "== 2. start hardened container =="
  90. docker network create "$NET" >/dev/null
  91. docker run -d --name "$C1" --network "$NET" \
  92. -p 5053:5053/udp -p 5053:5053/tcp \
  93. --read-only \
  94. --tmpfs /tmp --tmpfs "/run/unbound:uid=1500,gid=1500" \
  95. --tmpfs "/var/unbound:uid=1500,gid=1500" \
  96. --security-opt no-new-privileges:true \
  97. --cap-drop ALL \
  98. "$IMAGE" >/dev/null
  99. sleep 3
  100. if [ "$(docker inspect -f '{{.State.Running}}' "$C1" 2>/dev/null)" = "true" ]; then
  101. ok "container running"
  102. else
  103. bad "container running"; docker logs "$C1" 2>&1 | tail -5
  104. fi
  105. say "== 3. healthcheck =="
  106. healthy="no"
  107. i=0
  108. # WSL/9p container startup can be slow; give it up to 120s.
  109. while [ $i -lt 20 ]; do
  110. h="$(docker inspect -f '{{.State.Health.Status}}' "$C1" 2>/dev/null)"
  111. [ "$h" = "healthy" ] && healthy="yes" && break
  112. sleep 6
  113. i=$((i+1))
  114. done
  115. if [ "$healthy" = "yes" ]; then
  116. ok "healthcheck healthy"
  117. else
  118. bad "healthcheck healthy (status=$h)"
  119. fi
  120. say "== 4. version =="
  121. v="$(docker exec "$C1" unbound -V 2>/dev/null | head -1)"
  122. case "$v" in
  123. *1.26.2*) ok "unbound version 1.26.2 ($v)";;
  124. *) bad "unbound version 1.26.2 (got: $v)";;
  125. esac
  126. say "== 5. DNS resolution =="
  127. if docker exec "$C1" drill -p 5053 example.com @127.0.0.1 2>/dev/null | grep -q "example.com"; then
  128. ok "drill resolves example.com"
  129. else
  130. bad "drill resolves example.com"
  131. fi
  132. say "== 6. DNSSEC (correct EDNS-DO query) =="
  133. # Primary check: drill -D sets the EDNS DO bit; a validating resolver must
  134. # answer with the AD (authenticated data) bit set. Loopback via docker exec
  135. # is reliable (no external overlay-network dependency).
  136. ad_out="$(docker exec "$C1" drill -p 5053 -D example.com @127.0.0.1 2>/dev/null)"
  137. if printf '%s' "$ad_out" | grep -q "rcode: NOERROR" \
  138. && printf '%s' "$ad_out" | grep -qE 'flags:.*\bad\b' \
  139. && printf '%s' "$ad_out" | grep -q "example.com"; then
  140. ok "drill -D: signed zone answers with AD bit (EDNS DO)"
  141. else
  142. bad "drill -D: signed zone answers with AD bit (EDNS DO)"
  143. printf '%s\n' "$ad_out" | sed 's/^/ /'
  144. fi
  145. # Secondary check: raw EDNS-0 DO probe from an external container (also
  146. # verifies the no-DO -> no-AD behavior). The probe distinguishes content
  147. # deviations (exit 1 -> FAIL) from infrastructure timeouts (exit 2 -> NOT
  148. # VERIFIED). The loopback check above is the deterministic primary proof.
  149. probe_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/dns-check.py:/probe.py:ro" python:alpine python /probe.py "$(ip_of "$C1")" 5053 2>&1)"
  150. probe_rc=$?
  151. case $probe_rc in
  152. 0) ok "external EDNS-DO probe (AD=1 with DO, AD=0 without)";;
  153. 1) bad "external EDNS-DO probe: CONTENT deviation"
  154. printf '%s\n' "$probe_out" | sed 's/^/ /';;
  155. 2) nv "external EDNS-DO probe (timeout; loopback check is the proof)"
  156. printf '%s\n' "$probe_out" | sed 's/^/ /';;
  157. *) bad "external EDNS-DO probe: execution error (exit $probe_rc)"
  158. printf '%s\n' "$probe_out" | sed 's/^/ /';;
  159. esac
  160. say "== 7. non-root user =="
  161. uid="$(docker exec "$C1" sh -c 'id -u' 2>/dev/null)"
  162. if [ "$uid" = "1500" ]; then
  163. ok "runs as uid 1500"
  164. else
  165. bad "runs as uid 1500 (got: $uid)"
  166. fi
  167. say "== 8. capabilities =="
  168. capeff="$(docker exec "$C1" sh -c 'grep ^CapEff /proc/self/status' 2>/dev/null | awk '{print $2}')"
  169. if [ "$capeff" = "0000000000000000" ]; then
  170. ok "CapEff = 0"
  171. else
  172. bad "CapEff = 0 (got: $capeff)"
  173. fi
  174. say "== 9. no SUID binaries =="
  175. suid="$(docker exec "$C1" sh -c 'find / -perm -4000 -type f 2>/dev/null' 2>/dev/null)"
  176. if [ -z "$suid" ]; then
  177. ok "no SUID-root binaries"
  178. else
  179. bad "no SUID-root binaries (found: $suid)"
  180. fi
  181. say "== 10. read-only root filesystem =="
  182. if docker exec "$C1" sh -c 'touch /usr/writetest 2>/dev/null'; then
  183. bad "root fs is read-only (write to /usr succeeded)"
  184. else
  185. ok "root fs is read-only"
  186. fi
  187. if docker exec "$C1" sh -c 'touch /run/unbound/writetest && rm /run/unbound/writetest' 2>/dev/null; then
  188. ok "/run/unbound writable (tmpfs)"
  189. else
  190. bad "/run/unbound writable (tmpfs)"
  191. fi
  192. say "== 11. no-new-privileges + seccomp =="
  193. nnp="$(docker exec "$C1" sh -c 'grep ^NoNewPrivs /proc/self/status' 2>/dev/null | awk '{print $2}')"
  194. if [ "$nnp" = "1" ]; then
  195. ok "NoNewPrivs = 1"
  196. else
  197. bad "NoNewPrivs = 1 (got: $nnp)"
  198. fi
  199. sc="$(docker exec "$C1" sh -c 'grep ^Seccomp: /proc/self/status' 2>/dev/null | awk '{print $2}')"
  200. if [ "$sc" = "2" ]; then
  201. ok "seccomp filter active"
  202. else
  203. bad "seccomp filter active (got: $sc)"
  204. fi
  205. say "== 12. hide-identity / hide-version (CHAOS) =="
  206. # CHAOS id.server/version.server probes. The Python probe distinguishes
  207. # content deviations (exit 1 -> FAIL, e.g. an identity string leaked) from
  208. # infrastructure timeouts (exit 2 -> NOT VERIFIED). A leaked identity is a
  209. # real finding and MUST fail the run; a network hiccup is visible but not a
  210. # failure. The config value is asserted separately below (deterministic).
  211. id_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/chaos-check.py:/probe.py:ro" python:alpine python /probe.py "$(ip_of "$C1")" 5053 id.server. empty 2>&1)"
  212. id_rc=$?
  213. case $id_rc in
  214. 0) ok "hide-identity: id.server. answer is empty";;
  215. 1) bad "hide-identity: id.server. answer is empty (CONTENT deviation)"
  216. printf '%s\n' "$id_out" | sed 's/^/ /';;
  217. 2) nv "hide-identity CHAOS probe (timeout; config asserted below)"
  218. printf '%s\n' "$id_out" | sed 's/^/ /';;
  219. *) bad "hide-identity CHAOS probe: execution error (exit $id_rc)"
  220. printf '%s\n' "$id_out" | sed 's/^/ /';;
  221. esac
  222. # Config-level assertion (deterministic): the base config must set it.
  223. if docker exec "$C1" grep -q "hide-identity: yes" /etc/unbound/unbound.conf 2>/dev/null; then
  224. ok "config: hide-identity: yes in base config"
  225. else
  226. bad "config: hide-identity: yes in base config"
  227. fi
  228. # Control: same image with hide-identity switched off must reveal the identity.
  229. CFG_ID="${REPO_PATH:-$(pwd)}/test/fixture-identity"
  230. mkdir -p "$CFG_ID"
  231. printf 'server:\n hide-identity: no\n' > "$CFG_ID/00-identity.conf"
  232. docker run -d --name "$C4" --network "$NET" -v "$CFG_ID:/config:ro" "$IMAGE" >/dev/null
  233. sleep 3
  234. id2_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/chaos-check.py:/probe.py:ro" python:alpine python /probe.py "$(ip_of "$C4")" 5053 id.server. unbound-sec 2>&1)"
  235. id2_rc=$?
  236. case $id2_rc in
  237. 0) ok "identity control: hide-identity:no reveals identity";;
  238. 1) bad "identity control: hide-identity:no reveals identity (CONTENT deviation)"
  239. printf '%s\n' "$id2_out" | sed 's/^/ /';;
  240. 2) nv "identity-control CHAOS probe (timeout)"
  241. printf '%s\n' "$id2_out" | sed 's/^/ /';;
  242. *) bad "identity-control CHAOS probe: execution error (exit $id2_rc)"
  243. printf '%s\n' "$id2_out" | sed 's/^/ /';;
  244. esac
  245. say "== 13. drop-in config include =="
  246. # Fixtures are created on the host by test/run.sh (host paths required).
  247. if [ ! -f "$CFG1/99-test.conf" ] || [ ! -f "$CFG2/99-broken.conf" ]; then
  248. bad "test fixtures missing (run via test/run.sh)"
  249. fi
  250. docker run -d --name "$C2" --network "$NET" -v "$CFG1:/config:ro" "$IMAGE" >/dev/null
  251. sleep 3
  252. if [ "$(docker inspect -f '{{.State.Running}}' "$C2" 2>/dev/null)" = "true" ]; then
  253. ok "valid drop-in config accepted (include works)"
  254. else
  255. bad "valid drop-in config accepted"
  256. fi
  257. docker run -d --name "$C3" --network "$NET" -v "$CFG2:/config:ro" "$IMAGE" >/dev/null
  258. sleep 3
  259. if [ "$(docker inspect -f '{{.State.Running}}' "$C3" 2>/dev/null)" = "false" ]; then
  260. ok "broken drop-in config rejects start (checkconf)"
  261. else
  262. bad "broken drop-in config rejects start"
  263. fi
  264. say "== 14. package attack surface =="
  265. has_curl="$(docker exec "$C1" sh -c 'test -x /usr/bin/curl && echo yes' 2>/dev/null)"
  266. if [ "$has_curl" = "yes" ]; then
  267. bad "curl not in runtime image"
  268. else
  269. ok "no curl in runtime image"
  270. fi
  271. has_shadow="$(docker exec "$C1" sh -c 'test -x /usr/bin/chsh && echo yes' 2>/dev/null)"
  272. if [ "$has_shadow" = "yes" ]; then
  273. bad "shadow package not removed"
  274. else
  275. ok "shadow package removed"
  276. fi
  277. say "== 15. supply chain: GPG + SHA256 of source tarballs =="
  278. # The build echoes "GPG-VALIDSIG <tarball> signer=<fpr>" after each successful
  279. # signature verification (the signer fingerprint is extracted from GnuPG's
  280. # machine-readable --status-fd output and must equal the pinned fingerprint).
  281. sigs="$(grep -c "GPG-VALIDSIG" "$BUILDLOG" 2>/dev/null)"
  282. if [ "$sigs" -ge 2 ]; then
  283. ok "GPG: signer bound via VALIDSIG x$sigs (unbound + ldns tarballs)"
  284. else
  285. bad "GPG: signer bound via VALIDSIG (found: $sigs, need >= 2)"
  286. fi
  287. if grep -E "unbound\.tar\.gz: OK|ldns\.tar\.gz: OK" "$BUILDLOG" >/dev/null 2>&1; then
  288. ok "SHA256: tarball checksums OK"
  289. else
  290. bad "SHA256: tarball checksums OK"
  291. fi
  292. say "== 16. binary hardening: PIE =="
  293. # ELF e_type: 3 = ET_DYN (PIE/shared). Read from the final binary itself,
  294. # so this works even on cached builds.
  295. etype="$(docker exec "$C1" sh -c 'od -An -N32 -td2 /usr/sbin/unbound' 2>/dev/null | awk 'NR==2{print $1}')"
  296. if [ "$etype" = "3" ]; then
  297. ok "unbound binary is PIE (ELF e_type=ET_DYN)"
  298. else
  299. bad "unbound binary is PIE (ELF e_type=$etype, expected 3)"
  300. fi
  301. say "== 17. features compiled in (configure line) =="
  302. confline="$(docker exec "$C1" unbound -V 2>/dev/null | grep -i 'configure line')"
  303. for flag in --enable-subnet --enable-tfo-server --enable-tfo-client --enable-pie --enable-relro-now; do
  304. if printf '%s' "$confline" | grep -q -- "$flag"; then
  305. ok "compiled with $flag"
  306. else
  307. bad "compiled with $flag"
  308. fi
  309. done
  310. say "== 18. RFC5011 anchor: initialized once, not reset on restart =="
  311. # Deterministic entrypoint test (unbound stubbed so it cannot rewrite the
  312. # file): the entrypoint must (a) initialize /var/unbound/root.key from the
  313. # package anchor when it is missing, and (b) NOT overwrite an existing file.
  314. # The old bug (unconditional `cp -f`) reset learned RFC5011 state on every
  315. # start.
  316. STUB='mkdir -p /tmp/stub && printf "#!/bin/sh\nexit 0\n" > /tmp/stub/unbound && chmod +x /tmp/stub/unbound && PATH=/tmp/stub:$PATH sh /entrypoint.sh'
  317. rm -rf "$ANCHOR_DIR"
  318. mkdir -p "$ANCHOR_DIR"
  319. chmod 777 "$ANCHOR_DIR"
  320. h_pkg="$(docker run --rm --entrypoint cat "$IMAGE" /usr/share/dnssec-root/trusted-key.key 2>/dev/null | sha256sum | cut -d' ' -f1)"
  321. # (a) missing -> initialized from the package anchor
  322. docker run --rm --entrypoint sh -v "$ANCHOR_DIR:/var/unbound" "$IMAGE" -c "$STUB" >/dev/null 2>&1
  323. h_init="$(sha256sum "$ANCHOR_DIR/root.key" 2>/dev/null | cut -d' ' -f1)"
  324. if [ -n "$h_init" ] && [ "$h_init" = "$h_pkg" ]; then
  325. ok "entrypoint initializes anchor from package when missing"
  326. else
  327. bad "entrypoint initializes anchor from package when missing (init=$h_init pkg=$h_pkg)"
  328. fi
  329. # (b) existing -> kept, not reset (RFC5011 state must survive)
  330. printf '\n; sentinel: entrypoint must not overwrite this file\n' >> "$ANCHOR_DIR/root.key"
  331. h_sentinel="$(sha256sum "$ANCHOR_DIR/root.key" 2>/dev/null | cut -d' ' -f1)"
  332. docker run --rm --entrypoint sh -v "$ANCHOR_DIR:/var/unbound" "$IMAGE" -c "$STUB" >/dev/null 2>&1
  333. h_now="$(sha256sum "$ANCHOR_DIR/root.key" 2>/dev/null | cut -d' ' -f1)"
  334. if [ "$h_now" = "$h_sentinel" ]; then
  335. ok "entrypoint keeps existing anchor (no cp -f reset)"
  336. else
  337. bad "entrypoint keeps existing anchor (now=$h_now sentinel=$h_sentinel pkg=$h_pkg)"
  338. fi
  339. # Integration: real unbound, DNSSEC validates, RFC5011 state persisted to
  340. # the file, and still validating after a restart.
  341. rm -rf "$ANCHOR_DIR"; mkdir -p "$ANCHOR_DIR"; chmod 777 "$ANCHOR_DIR"
  342. docker run -d --name "$C5" --network "$NET" \
  343. -v "$ANCHOR_DIR:/var/unbound" "$IMAGE" >/dev/null
  344. ready="no"; i=0
  345. while [ $i -lt 10 ]; do
  346. if docker exec "$C5" drill -p 5053 unbound.net @127.0.0.1 >/dev/null 2>&1; then
  347. ready="yes"; break
  348. fi
  349. sleep 2; i=$((i+1))
  350. done
  351. [ "$ready" = "yes" ] || bad "resolver ready (initial)"
  352. # unbound writes its RFC5011 state into the anchor file (autotrust format)
  353. sleep 3
  354. if grep -q "autotrust trust anchor file" "$ANCHOR_DIR/root.key" 2>/dev/null; then
  355. ok "unbound persists RFC5011 state into anchor file"
  356. else
  357. bad "unbound persists RFC5011 state into anchor file"
  358. fi
  359. p1_out="$(docker exec "$C5" drill -p 5053 -D example.com @127.0.0.1 2>/dev/null)"
  360. if printf '%s' "$p1_out" | grep -q "rcode: NOERROR" \
  361. && printf '%s' "$p1_out" | grep -qE 'flags:.*\bad\b'; then
  362. ok "DNSSEC AD=1 (initial, drill -D loopback)"
  363. else
  364. bad "DNSSEC AD=1 (initial, drill -D loopback)"
  365. printf '%s\n' "$p1_out" | sed 's/^/ /'
  366. fi
  367. docker restart "$C5" >/dev/null 2>&1
  368. ready="no"; i=0
  369. while [ $i -lt 10 ]; do
  370. if docker exec "$C5" drill -p 5053 unbound.net @127.0.0.1 >/dev/null 2>&1; then
  371. ready="yes"; break
  372. fi
  373. sleep 2; i=$((i+1))
  374. done
  375. [ "$ready" = "yes" ] || bad "resolver ready after restart"
  376. if [ -f "$ANCHOR_DIR/root.key" ]; then
  377. ok "anchor file persists after restart"
  378. else
  379. bad "anchor file persists after restart"
  380. fi
  381. p2_out="$(docker exec "$C5" drill -p 5053 -D example.com @127.0.0.1 2>/dev/null)"
  382. if printf '%s' "$p2_out" | grep -q "rcode: NOERROR" \
  383. && printf '%s' "$p2_out" | grep -qE 'flags:.*\bad\b'; then
  384. ok "DNSSEC AD=1 after restart (drill -D loopback)"
  385. else
  386. bad "DNSSEC AD=1 after restart (drill -D loopback)"
  387. printf '%s\n' "$p2_out" | sed 's/^/ /'
  388. fi
  389. say "== 19. compose template starts (no recursive include) =="
  390. # Regression test for the audit P1 finding: the template must start cleanly
  391. # with the ACTUAL docker-compose.yml (old version mounted ./config over
  392. # itself -> recursive include -> start aborted).
  393. #
  394. # It runs under the dedicated test project (PROJ_TEST) with the test-image
  395. # override, so it creates only its own container/network/volume and never
  396. # touches a production deployment of the template.
  397. if compose_test up -d >/dev/null 2>&1; then
  398. hc="no"
  399. i=0
  400. while [ $i -lt 20 ]; do
  401. h="$(docker inspect -f '{{.State.Health.Status}}' "$C6_CONT" 2>/dev/null)"
  402. [ "$h" = "healthy" ] && hc="yes" && break
  403. sleep 6
  404. i=$((i+1))
  405. done
  406. if [ "$hc" = "yes" ]; then
  407. ok "compose template starts + healthy (no ports, conf.d + anchor volume)"
  408. else
  409. bad "compose template starts + healthy (status=$h)"
  410. docker logs "$C6_CONT" 2>&1 | tail -5 | sed 's/^/ /'
  411. fi
  412. else
  413. bad "compose template starts"
  414. compose_test up -d 2>&1 | tail -5 | sed 's/^/ /'
  415. fi
  416. compose_test down --remove-orphans --volumes >/dev/null 2>&1
  417. say "== 20. DNSSEC negative test: invalid signature -> SERVFAIL =="
  418. # Controlled zone "broken.example" with an EXPLICIT trust anchor (the zone
  419. # DNSKEY), so validation is well-defined (RFC 4035 5.2 — no parent DS needed):
  420. # C6 valid : A + RRSIG (valid) -> NOERROR + AD
  421. # C7 broken : A (data modified) + RRSIG -> SERVFAIL (signature rejected)
  422. # C8 noval : broken zone, validator off -> NOERROR (resolvable, counter-test)
  423. # The counter-test (C8) separates SIGNATURE REJECTION from a generic
  424. # resolution failure: the broken answer resolves fine with validation off,
  425. # yet its signature is rejected with validation on. A static responder that
  426. # merely failed to serve the DNSKEY would not produce this pattern.
  427. #
  428. # RESP/RESP_BROKEN and C6/C7/C8 get their OWN network (not $NET), so they
  429. # can't be affected by the other test containers and the test stays
  430. # deterministic.
  431. REPO="${REPO_PATH:-$(pwd)}"
  432. NEG_NET="ubsec-negnet"
  433. docker network create "$NEG_NET" >/dev/null 2>&1
  434. BZDIR="$(pwd)/test"
  435. # Remove stale zone files first: a DIRECTORY left there by a previous mount of
  436. # a missing file would make the generator fail with IsADirectoryError.
  437. rm -f "$BZDIR/.broken-valid.bin" "$BZDIR/.broken-broken.bin" \
  438. "$BZDIR/.broken-dnskey.bin" "$BZDIR/.broken-anchor.key"
  439. docker run --rm -v "$REPO/test/make-broken-zone.py:/m.py:ro" -v "$BZDIR:/o" \
  440. python:alpine sh -c "pip install -q dnspython cryptography 2>/dev/null; python /m.py /o" >/dev/null 2>&1
  441. if [ ! -f "$BZDIR/.broken-valid.bin" ] || [ ! -f "$BZDIR/.broken-broken.bin" ] \
  442. || [ ! -f "$BZDIR/.broken-dnskey.bin" ] || [ ! -f "$BZDIR/.broken-anchor.key" ]; then
  443. bad "generate broken.example zone packets"
  444. else
  445. ok "generate broken.example zone packets (valid + broken + anchor)"
  446. fi
  447. # Two static responders: VALID (5353) and BROKEN (5354). Both serve the same
  448. # DNSKEY packet (for trust-anchor priming); only the A packet differs.
  449. docker run -d --name "$RESP" --network "$NEG_NET" \
  450. -v "$BZDIR/.broken-valid.bin:/a.bin:ro" -v "$BZDIR/.broken-dnskey.bin:/k.bin:ro" \
  451. -v "$REPO/test/broken-zone-responder.py:/r.py:ro" \
  452. python:alpine python /r.py /a.bin /k.bin 5353 >/dev/null
  453. docker run -d --name "$RESP_BROKEN" --network "$NEG_NET" \
  454. -v "$BZDIR/.broken-broken.bin:/a.bin:ro" -v "$BZDIR/.broken-dnskey.bin:/k.bin:ro" \
  455. -v "$REPO/test/broken-zone-responder.py:/r.py:ro" \
  456. python:alpine python /r.py /a.bin /k.bin 5354 >/dev/null
  457. # Get a responder's IP from ITS OWN log (it prints its container IP); fall
  458. # back to docker inspect. This is more reliable than host-side inspect, which
  459. # can be empty while a container is starting on a slow WSL daemon.
  460. get_ip() {
  461. _c="$1"; _ip=""; _i=0
  462. while [ $_i -lt 15 ]; do
  463. _ip="$(docker logs "$_c" 2>&1 | grep -oE 'responder ip=[0-9.]+' | head -1 | cut -d= -f2)"
  464. [ -n "$_ip" ] && [ "$_ip" != "0.0.0.0" ] && break
  465. sleep 2; _i=$((_i+1))
  466. done
  467. if [ -z "$_ip" ] || [ "$_ip" = "0.0.0.0" ]; then
  468. _ip="$(docker inspect -f '{{(index .NetworkSettings.Networks "'"$NEG_NET"').IPAddress}}' "$_c" 2>/dev/null)"
  469. fi
  470. printf '%s' "$_ip"
  471. }
  472. RVALID="$(get_ip "$RESP")"
  473. RBROKEN="$(get_ip "$RESP_BROKEN")"
  474. [ -n "$RVALID" ] && [ -n "$RBROKEN" ] || {
  475. bad "negative-test responders have IPs (valid=$RVALID broken=$RBROKEN)"
  476. docker logs "$RESP" 2>&1 | tail -2 | sed 's/^/ /'
  477. docker logs "$RESP_BROKEN" 2>&1 | tail -2 | sed 's/^/ /'
  478. }
  479. # C6: trust anchor + forward to VALID + validator -> expect NOERROR + AD
  480. FWD_V="$(pwd)/test/fixture-broken-valid"; rm -rf "$FWD_V"; mkdir -p "$FWD_V"; chmod 755 "$FWD_V"
  481. printf 'trust-anchor-file: "/anchor.key"\nforward-zone:\n name: "broken.example"\n forward-addr: %s@5353\n' "$RVALID" > "$FWD_V/99.conf"
  482. chmod 644 "$FWD_V/99.conf"
  483. docker run -d --name "$C6" --network "$NEG_NET" -v "$FWD_V:/config:ro" -v "$BZDIR/.broken-anchor.key:/anchor.key:ro" "$IMAGE" >/dev/null
  484. # C7: trust anchor + forward to BROKEN + validator -> expect SERVFAIL
  485. FWD_B="$(pwd)/test/fixture-broken-broken"; rm -rf "$FWD_B"; mkdir -p "$FWD_B"; chmod 755 "$FWD_B"
  486. printf 'trust-anchor-file: "/anchor.key"\nforward-zone:\n name: "broken.example"\n forward-addr: %s@5354\n' "$RBROKEN" > "$FWD_B/99.conf"
  487. chmod 644 "$FWD_B/99.conf"
  488. docker run -d --name "$C7" --network "$NEG_NET" -v "$FWD_B:/config:ro" -v "$BZDIR/.broken-anchor.key:/anchor.key:ro" "$IMAGE" >/dev/null
  489. # C8: validator disabled (module-config: iterator) + forward to BROKEN ->
  490. # expect NOERROR (the broken answer is resolvable, just not validated).
  491. FWD_N="$(pwd)/test/fixture-broken-noval"; rm -rf "$FWD_N"; mkdir -p "$FWD_N"; chmod 755 "$FWD_N"
  492. printf 'module-config: iterator\nforward-zone:\n name: "broken.example"\n forward-addr: %s@5354\n' "$RBROKEN" > "$FWD_N/99.conf"
  493. chmod 644 "$FWD_N/99.conf"
  494. docker run -d --name "$C8" --network "$NEG_NET" -v "$FWD_N:/config:ro" "$IMAGE" >/dev/null
  495. # Readiness: unbound is ready when it answers the query at all — a SERVFAIL is
  496. # a valid response, so check for ANY rcode, not a successful (exit 0) drill.
  497. ready_c() {
  498. _c="$1"; _r="no"; _i=0
  499. while [ $_i -lt 15 ]; do
  500. if docker exec "$_c" drill -p 5053 -D www.broken.example A @127.0.0.1 2>/dev/null | grep -q "rcode:"; then
  501. _r="yes"; break
  502. fi
  503. sleep 2; _i=$((_i+1))
  504. done
  505. [ "$_r" = "yes" ]
  506. }
  507. ready_c "$C6" && ok "negative-test: valid resolver ready" || bad "negative-test: valid resolver ready"
  508. ready_c "$C7" && ok "negative-test: broken resolver ready" || bad "negative-test: broken resolver ready"
  509. ready_c "$C8" && ok "negative-test: noval resolver ready" || bad "negative-test: noval resolver ready"
  510. # 1) VALID zone validates: NOERROR + AD
  511. v_out="$(docker exec "$C6" drill -p 5053 -D www.broken.example A @127.0.0.1 2>/dev/null)"
  512. if printf '%s' "$v_out" | grep -q "rcode: NOERROR" && printf '%s' "$v_out" | grep -qE "flags:.*ad"; then
  513. ok "valid zone validates (NOERROR + AD with explicit trust anchor)"
  514. else
  515. bad "valid zone validates (got: $(printf '%s' "$v_out" | grep rcode | head -1))"
  516. printf '%s\n' "$v_out" | sed 's/^/ /'
  517. fi
  518. # 2) BROKEN signature rejected: SERVFAIL (no CD)
  519. b_out="$(docker exec "$C7" drill -p 5053 -D www.broken.example A @127.0.0.1 2>/dev/null)"
  520. if printf '%s' "$b_out" | grep -q "rcode: SERVFAIL"; then
  521. ok "broken signature -> SERVFAIL (no CD; validator rejects the invalid RRSIG)"
  522. else
  523. bad "broken signature -> SERVFAIL (got: $(printf '%s' "$b_out" | grep rcode | head -1))"
  524. printf '%s\n' "$b_out" | sed 's/^/ /'
  525. fi
  526. # 3) Counter-test: broken answer is RESOLVABLE with validation off -> NOERROR.
  527. # This proves step 2 is a signature rejection, not a resolution failure.
  528. n_out="$(docker exec "$C8" drill -p 5053 www.broken.example A @127.0.0.1 2>/dev/null)"
  529. if printf '%s' "$n_out" | grep -q "rcode: NOERROR" && printf '%s' "$n_out" | grep -qE "IN[[:space:]]+A[[:space:]]"; then
  530. ok "broken answer resolvable with validation off (counter-test: signature rejection, not resolution failure)"
  531. else
  532. bad "broken answer resolvable with validation off (got: $(printf '%s' "$n_out" | grep rcode | head -1))"
  533. printf '%s\n' "$n_out" | sed 's/^/ /'
  534. fi
  535. rm -rf "$FWD_V" "$FWD_B" "$FWD_N"
  536. say ""
  537. say "=================================================="
  538. say "RESULT: $PASS passed, $FAIL failed, $NOTVERIFIED not verified"
  539. if [ "$NOTVERIFIED" -gt 0 ]; then
  540. say "(not verified = infrastructure timeout, not a content finding; re-run to clear)"
  541. fi
  542. say "=================================================="
  543. # A content finding (FAIL) fails the run. Not-verified checks are visible but
  544. # do not fail the run on their own (they are infrastructure, re-run to clear).
  545. [ $FAIL -eq 0 ]