run-tests.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. NET="ubsec-testnet"
  18. # Fixtures must use HOST paths (bind mounts are resolved by the daemon).
  19. # run.sh sets REPO_PATH to the host repo path and pre-creates the fixtures.
  20. CFG1="${REPO_PATH:-$(pwd)}/test/fixture-config-ok"
  21. CFG2="${REPO_PATH:-$(pwd)}/test/fixture-config-bad"
  22. PASS=0
  23. FAIL=0
  24. say() { printf '%s\n' "$*"; }
  25. ok() { PASS=$((PASS+1)); say " [PASS] $1"; }
  26. bad() { FAIL=$((FAIL+1)); say " [FAIL] $1"; }
  27. cleanup() {
  28. docker rm -f "$C1" "$C2" "$C3" "$C4" >/dev/null 2>&1
  29. docker network rm "$NET" >/dev/null 2>&1
  30. rm -rf "$(pwd)/test/fixture-identity" 2>/dev/null
  31. }
  32. trap cleanup EXIT
  33. say "== 0. cleanup old artifacts =="
  34. cleanup
  35. say "== 1. build image =="
  36. if docker build -t "$IMAGE" . >/dev/null 2>&1; then
  37. ok "docker build"
  38. else
  39. bad "docker build"
  40. say "ABORT: build failed"; exit 1
  41. fi
  42. say "== 2. start hardened container =="
  43. docker network create "$NET" >/dev/null
  44. docker run -d --name "$C1" --network "$NET" \
  45. -p 5053:5053/udp -p 5053:5053/tcp \
  46. --read-only \
  47. --tmpfs /tmp --tmpfs "/run/unbound:uid=1500,gid=1500" \
  48. --security-opt no-new-privileges:true \
  49. --cap-drop ALL \
  50. "$IMAGE" >/dev/null
  51. sleep 3
  52. if [ "$(docker inspect -f '{{.State.Running}}' "$C1" 2>/dev/null)" = "true" ]; then
  53. ok "container running"
  54. else
  55. bad "container running"; docker logs "$C1" 2>&1 | tail -5
  56. fi
  57. say "== 3. healthcheck =="
  58. healthy="no"
  59. i=0
  60. while [ $i -lt 12 ]; do
  61. h="$(docker inspect -f '{{.State.Health.Status}}' "$C1" 2>/dev/null)"
  62. [ "$h" = "healthy" ] && healthy="yes" && break
  63. sleep 6
  64. i=$((i+1))
  65. done
  66. if [ "$healthy" = "yes" ]; then
  67. ok "healthcheck healthy"
  68. else
  69. bad "healthcheck healthy (status=$h)"
  70. fi
  71. say "== 4. version =="
  72. v="$(docker exec "$C1" unbound -V 2>/dev/null | head -1)"
  73. case "$v" in
  74. *1.26.2*) ok "unbound version 1.26.2 ($v)";;
  75. *) bad "unbound version 1.26.2 (got: $v)";;
  76. esac
  77. say "== 5. DNS resolution =="
  78. if docker exec "$C1" drill -p 5053 example.com @127.0.0.1 2>/dev/null | grep -q "example.com"; then
  79. ok "drill resolves example.com"
  80. else
  81. bad "drill resolves example.com"
  82. fi
  83. say "== 6. DNSSEC (AD bit, DO query) =="
  84. 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)"
  85. probe_rc=$?
  86. if [ $probe_rc -ne 0 ] || printf '%s' "$probe_out" | grep -q "FAIL"; then
  87. bad "DNSSEC AD-bit probe"
  88. printf '%s\n' "$probe_out" | sed 's/^/ /'
  89. else
  90. ok "DNSSEC AD-bit probe"
  91. fi
  92. say "== 7. non-root user =="
  93. uid="$(docker exec "$C1" sh -c 'id -u' 2>/dev/null)"
  94. if [ "$uid" = "1500" ]; then
  95. ok "runs as uid 1500"
  96. else
  97. bad "runs as uid 1500 (got: $uid)"
  98. fi
  99. say "== 8. capabilities =="
  100. capeff="$(docker exec "$C1" sh -c 'grep ^CapEff /proc/self/status' 2>/dev/null | awk '{print $2}')"
  101. if [ "$capeff" = "0000000000000000" ]; then
  102. ok "CapEff = 0"
  103. else
  104. bad "CapEff = 0 (got: $capeff)"
  105. fi
  106. say "== 9. no SUID binaries =="
  107. suid="$(docker exec "$C1" sh -c 'find / -perm -4000 -type f 2>/dev/null' 2>/dev/null)"
  108. if [ -z "$suid" ]; then
  109. ok "no SUID-root binaries"
  110. else
  111. bad "no SUID-root binaries (found: $suid)"
  112. fi
  113. say "== 10. read-only root filesystem =="
  114. if docker exec "$C1" sh -c 'touch /usr/writetest 2>/dev/null'; then
  115. bad "root fs is read-only (write to /usr succeeded)"
  116. else
  117. ok "root fs is read-only"
  118. fi
  119. if docker exec "$C1" sh -c 'touch /run/unbound/writetest && rm /run/unbound/writetest' 2>/dev/null; then
  120. ok "/run/unbound writable (tmpfs)"
  121. else
  122. bad "/run/unbound writable (tmpfs)"
  123. fi
  124. say "== 11. no-new-privileges + seccomp =="
  125. nnp="$(docker exec "$C1" sh -c 'grep ^NoNewPrivs /proc/self/status' 2>/dev/null | awk '{print $2}')"
  126. if [ "$nnp" = "1" ]; then
  127. ok "NoNewPrivs = 1"
  128. else
  129. bad "NoNewPrivs = 1 (got: $nnp)"
  130. fi
  131. sc="$(docker exec "$C1" sh -c 'grep ^Seccomp: /proc/self/status' 2>/dev/null | awk '{print $2}')"
  132. if [ "$sc" = "2" ]; then
  133. ok "seccomp filter active"
  134. else
  135. bad "seccomp filter active (got: $sc)"
  136. fi
  137. say "== 12. hide-identity / hide-version =="
  138. 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)"
  139. id_rc=$?
  140. if [ $id_rc -ne 0 ] || printf '%s' "$id_out" | grep -q "FAIL"; then
  141. bad "hide-identity: id.server. answer is empty"
  142. printf '%s\n' "$id_out" | sed 's/^/ /'
  143. else
  144. ok "hide-identity: id.server. answer is empty"
  145. fi
  146. # Control: same image with hide-identity switched off must reveal the identity.
  147. CFG_ID="${REPO_PATH:-$(pwd)}/test/fixture-identity"
  148. mkdir -p "$CFG_ID"
  149. printf 'server:\n hide-identity: no\n' > "$CFG_ID/00-identity.conf"
  150. docker run -d --name "$C4" --network "$NET" -v "$CFG_ID:/config:ro" "$IMAGE" >/dev/null
  151. sleep 3
  152. 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)"
  153. id2_rc=$?
  154. if [ $id2_rc -ne 0 ] || printf '%s' "$id2_out" | grep -q "FAIL"; then
  155. bad "identity control: hide-identity:no reveals identity"
  156. printf '%s\n' "$id2_out" | sed 's/^/ /'
  157. else
  158. ok "identity control: hide-identity:no reveals identity"
  159. fi
  160. say "== 13. drop-in config include =="
  161. # Fixtures are created on the host by test/run.sh (host paths required).
  162. if [ ! -f "$CFG1/99-test.conf" ] || [ ! -f "$CFG2/99-broken.conf" ]; then
  163. bad "test fixtures missing (run via test/run.sh)"
  164. fi
  165. docker run -d --name "$C2" --network "$NET" -v "$CFG1:/config:ro" "$IMAGE" >/dev/null
  166. sleep 3
  167. if [ "$(docker inspect -f '{{.State.Running}}' "$C2" 2>/dev/null)" = "true" ]; then
  168. ok "valid drop-in config accepted (include works)"
  169. else
  170. bad "valid drop-in config accepted"
  171. fi
  172. docker run -d --name "$C3" --network "$NET" -v "$CFG2:/config:ro" "$IMAGE" >/dev/null
  173. sleep 3
  174. if [ "$(docker inspect -f '{{.State.Running}}' "$C3" 2>/dev/null)" = "false" ]; then
  175. ok "broken drop-in config rejects start (checkconf)"
  176. else
  177. bad "broken drop-in config rejects start"
  178. fi
  179. say "== 14. package attack surface =="
  180. has_curl="$(docker exec "$C1" sh -c 'test -x /usr/bin/curl && echo yes' 2>/dev/null)"
  181. if [ "$has_curl" = "yes" ]; then
  182. bad "curl not in runtime image"
  183. else
  184. ok "no curl in runtime image"
  185. fi
  186. has_shadow="$(docker exec "$C1" sh -c 'test -x /usr/bin/chsh && echo yes' 2>/dev/null)"
  187. if [ "$has_shadow" = "yes" ]; then
  188. bad "shadow package not removed"
  189. else
  190. ok "shadow package removed"
  191. fi
  192. say ""
  193. say "=================================================="
  194. say "RESULT: $PASS passed, $FAIL failed"
  195. say "=================================================="
  196. [ $FAIL -eq 0 ]