#!/bin/sh # SPDX-License-Identifier: GPL-2.0-or-later # Copyright (C) 2026 Bryan Joshua Pedini set -eu if [ "$#" -ne 3 ]; then echo "usage: $0 " >&2 exit 2 fi cd "$(dirname "$0")/.." . ./scripts/docker-test-lib.sh image=$1 version=$2 commit=$3 probe=${PROBE_IMAGE:-golang:1.27-alpine} mysql_image=${MYSQL_IMAGE:-mysql:8.4} name="${IMG_NAME:-cdwu-img-${IMG_ID:-$$}}" network="$name" mysql="${name}-mysql" server="${name}-app" exported="${name}-export" probe_name="${name}-probe" require_free_docker_name network "$network" for container in "$mysql" "$server" "$exported" "$probe_name"; do require_free_docker_name container "$container" done network_id= mysql_id= server_id= exported_id= probe_id= tmp=$(mktemp -d) root_password=$(od -An -tx1 -N16 /dev/urandom | tr -d ' \n') db_password=coredns_webui_dev pw_admin="pw$(od -An -tx1 -N12 /dev/urandom | tr -d ' \n')" pw_user="pw$(od -An -tx1 -N12 /dev/urandom | tr -d ' \n')" pw_reset="pw$(od -An -tx1 -N12 /dev/urandom | tr -d ' \n')" pw_short="pw$(od -An -tx1 -N4 /dev/urandom | tr -d ' \n')" rc=0 cleanup() { status=$? if [ "$status" -ne 0 ]; then for container in "$server_id" "$mysql_id"; do if [ -n "$container" ]; then docker logs --tail 40 "$container" >&2 2>&1 || true fi done fi for container in "$server_id" "$mysql_id" "$exported_id" "$probe_id"; do if [ -n "$container" ]; then docker rm --force --volumes "$container" >/dev/null 2>&1 || true fi done if [ -n "$network_id" ]; then docker network rm "$network_id" >/dev/null 2>&1 || true fi rm -rf "$tmp" exit "$status" } trap cleanup EXIT trap 'exit 130' INT TERM fail() { echo "FAIL: $*" >&2 exit 1 } ok() { echo "ok: $*" } label() { docker image inspect --format "{{index .Config.Labels \"$1\"}}" "$image" } expect_rc() { [ "$rc" -eq "$2" ] || fail "$1: exit code $rc, expected $2: $(head -c 300 "$tmp/err")" } capture() { "$@" >"$tmp/out" 2>"$tmp/err" && rc=0 || rc=$? } capture_probe() { probe_id=$(docker create --name "$probe_name" "$@") || fail "could not create probe" capture docker start --attach "$probe_id" docker rm --force --volumes "$probe_id" >/dev/null probe_id= } scan() { if grep -qF -e "$pw_admin" -e "$pw_user" -e "$pw_reset" -e "$pw_short" -e "$db_password" -e "$root_password" "$@"; then fail "a secret appears in the output" fi } cli() { input=$1 shift printf '%s\n' "$input" | docker exec -i "$server_id" /app "$@" >"$tmp/out" 2>"$tmp/err" && rc=0 || rc=$? scan "$tmp/out" "$tmp/err" } sql() { docker exec --env MYSQL_PWD="$root_password" "$mysql_id" mysql --user=root --batch --skip-column-names --execute "$1" } usage_text() { cat <<'EOF' usage: coredns-web-ui start the HTTP server coredns-web-ui help print this usage text coredns-web-ui version print the version and commit coredns-web-ui healthcheck query the local /health endpoint coredns-web-ui user list [all] list the active users, or every user with all coredns-web-ui user add create a user with role admin or user, reading the password from stdin coredns-web-ui user passwd set the password of a user, reading it from stdin coredns-web-ui user role set the role of a user to admin or user coredns-web-ui user delete mark a user as deleted and end their sessions coredns-web-ui check [zone] report the rows the plugin cannot serve correctly coredns-web-ui audit [n] print the latest n audit rows (default 20) EOF } usage_text >"$tmp/usage" [ "$(label org.opencontainers.image.version)" = "$version" ] || fail "image version label is not $version" [ "$(label org.opencontainers.image.revision)" = "$commit" ] || fail "image revision label is not $commit" case $(label org.opencontainers.image.source) in https://*/coredns-web-ui) ;; *) fail "image source label is not a coredns-web-ui repository URL" ;; esac [ "$(label org.opencontainers.image.licenses)" = "GPL-2.0-or-later" ] || fail "image licenses label is not GPL-2.0-or-later" ok "OCI labels" [ "$(docker image inspect --format '{{.Config.User}}' "$image")" = "65532:65532" ] || fail "image user is not 65532:65532" [ "$(docker image inspect --format '{{json .Config.Entrypoint}}' "$image")" = '["/app"]' ] || fail "image entrypoint is not /app" [ "$(docker image inspect --format '{{json .Config.Healthcheck.Test}}' "$image")" = '["CMD","/app","healthcheck"]' ] || fail "image healthcheck is not /app healthcheck" ok "image user, entrypoint and healthcheck" capture_probe --entrypoint /bin/sh "$image" -c true [ "$rc" -ne 0 ] || fail "the image runs /bin/sh" exported_id=$(docker create --name "$exported" "$image") docker export "$exported_id" | tar -t >"$tmp/files" docker rm --force --volumes "$exported_id" >/dev/null exported_id= while IFS= read -r entry; do case ${entry%/} in app | etc | etc/ssl | etc/ssl/cert.pem | etc/hostname | etc/hosts | etc/resolv.conf | etc/mtab) ;; dev | dev/console | dev/pts | dev/shm | proc | sys | .dockerenv) ;; *) fail "unexpected path in the image filesystem: $entry" ;; esac done <"$tmp/files" grep -qx 'app' "$tmp/files" || fail "/app missing from the image filesystem" grep -qx 'etc/ssl/cert.pem' "$tmp/files" || fail "CA bundle missing from the image filesystem" ok "image filesystem has no shell" capture_probe "$image" expect_rc "start without configuration" 2 ok "exit 2 without configuration" capture_probe "$image" version expect_rc "version" 0 [ "$(cat "$tmp/out")" = "coredns-web-ui $version $commit" ] || fail "version output is not 'coredns-web-ui $version $commit'" ok "version output" capture_probe "$image" help expect_rc "help" 0 cmp -s "$tmp/out" "$tmp/usage" || fail "help output differs from the usage text" [ ! -s "$tmp/err" ] || fail "help wrote to stderr" ok "help output" for cmd in bogus help; do set -- "$cmd" if [ "$cmd" = help ]; then set -- "$@" extra fi capture_probe "$image" "$@" expect_rc "usage error ($*)" 2 [ ! -s "$tmp/out" ] || fail "usage error ($*) wrote to stdout" head -n 1 "$tmp/err" | grep -q '^coredns-web-ui: usage error: ' || fail "usage error ($*) message has no usage error prefix" tail -n +2 "$tmp/err" | cmp -s - "$tmp/usage" || fail "usage error ($*) does not end with the usage text" done ok "usage errors exit 2 with the usage text on stderr" capture_probe "$image" healthcheck expect_rc "healthcheck without a server" 1 ok "healthcheck fails without a server" network_id=$(docker network create --internal "$network") mysql_id=$(docker create --name "$mysql" --network "$network_id" --network-alias mysql --tmpfs /var/lib/mysql \ --env MYSQL_ROOT_PASSWORD="$root_password" "$mysql_image") docker cp dev/mysql-init.sql "$mysql_id:/docker-entrypoint-initdb.d/mysql-init.sql" docker start "$mysql_id" >/dev/null attempt=0 until docker exec --env MYSQL_PWD="$root_password" "$mysql_id" mysql --host=127.0.0.1 --user=root --execute 'SELECT 1' >/dev/null 2>&1; do attempt=$((attempt + 1)) [ "$attempt" -lt 120 ] || fail "mysql did not become ready" sleep 1 done [ "$(sql 'SELECT COUNT(*) FROM coredns.coredns_records')" = 0 ] || fail "records table is not initialized" ok "mysql ready" server_id=$(docker create --name "$server" --network "$network_id" --network-alias app \ --health-interval 2s --health-timeout 5s --health-start-period 2s --health-retries 5 \ --env DB_HOST=mysql --env DB_USER=coredns_webui --env DB_PASSWORD="$db_password" --env DB_NAME=coredns \ --env COOKIE_SECURE=false --env LOG_LEVEL=TRACE "$image") docker start "$server_id" >/dev/null attempt=0 while :; do state=$(docker inspect --format '{{.State.Status}} {{if .State.Health}}{{.State.Health.Status}}{{end}}' "$server_id") [ "$state" != "running healthy" ] || break case $state in running*) ;; *) fail "the container is not running: $state" ;; esac attempt=$((attempt + 1)) [ "$attempt" -lt 90 ] || fail "the container did not become healthy: $state" sleep 1 done ok "container healthy through its HEALTHCHECK" [ "$(docker inspect --format '{{.Config.User}}' "$server_id")" = "65532:65532" ] || fail "the container does not run as 65532:65532" ok "container runs as 65532:65532" capture docker exec "$server_id" /bin/sh -c true [ "$rc" -ne 0 ] || fail "docker exec /bin/sh succeeded" ok "docker exec /bin/sh fails" capture docker exec "$server_id" /app healthcheck expect_rc "docker exec healthcheck" 0 ok "docker exec healthcheck" capture docker exec "$server_id" /app version expect_rc "docker exec version" 0 [ "$(cat "$tmp/out")" = "coredns-web-ui $version $commit" ] || fail "docker exec version returned unexpected output" capture docker exec "$server_id" /app help expect_rc "docker exec help" 0 cmp -s "$tmp/out" "$tmp/usage" || fail "docker exec help differs from the usage text" [ ! -s "$tmp/err" ] || fail "docker exec help wrote to stderr" ok "docker exec version and help" capture_probe --network "$network_id" "$probe" wget -qO- http://app:8080/health expect_rc "GET /health" 0 [ "$(cat "$tmp/out")" = '{"status":"ok"}' ] || fail "unexpected /health body" capture_probe --network "$network_id" "$probe" wget -qO- http://app:8080/version expect_rc "GET /version" 0 grep -qF "$version" "$tmp/out" || fail "/version does not render the version" grep -qF "$commit" "$tmp/out" || fail "/version does not render the commit" ok "/health and /version" docker logs "$server_id" >"$tmp/startup.log" 2>&1 grep -qF "$commit" "$tmp/startup.log" || fail "commit $commit not in the startup log" grep -qF "$version" "$tmp/startup.log" || fail "version $version not in the startup log" ok "version and commit in the startup log" cli "$pw_short" user add imgshort user [ "$rc" -ne 0 ] || fail "user add accepted a short password" [ "$(sql "SELECT COUNT(*) FROM coredns.coredns_webui_users WHERE username = 'imgshort'")" = 0 ] || fail "user add created a user with a short password" cli "$pw_admin" user add imgadmin admin expect_rc "user add admin" 0 cli "$pw_user" user add imguser user expect_rc "user add user" 0 [ "$(sql "SELECT role FROM coredns.coredns_webui_users WHERE username = 'imgadmin'")" = admin ] || fail "imgadmin does not have role admin" [ "$(sql "SELECT role FROM coredns.coredns_webui_users WHERE username = 'imguser'")" = user ] || fail "imguser does not have role user" [ "$(sql "SELECT COUNT(*) FROM coredns.coredns_webui_users WHERE password_hash LIKE '\$argon2id\$%' AND deleted_at IS NULL")" = 2 ] || fail "passwords are not stored as argon2id hashes" ok "user add with both roles" hash_query="SELECT SHA2(password_hash, 256) FROM coredns.coredns_webui_users WHERE username = 'imguser'" old_hash=$(sql "$hash_query") sql "INSERT INTO coredns.coredns_webui_sessions (token_hash, user_id, created_at, last_seen_at, expires_at, origin, user_agent) SELECT UNHEX(REPEAT('11', 32)), id, UTC_TIMESTAMP(6), UTC_TIMESTAMP(6), UTC_TIMESTAMP(6) + INTERVAL 1 HOUR, '192.0.2.1', 'image test' FROM coredns.coredns_webui_users WHERE username = 'imguser'" cli "$pw_reset" user passwd imguser expect_rc "user passwd" 0 new_hash=$(sql "$hash_query") [ -n "$new_hash" ] && [ "$old_hash" != "$new_hash" ] || fail "user passwd did not replace the stored hash" [ "$(sql 'SELECT COUNT(*) FROM coredns.coredns_webui_sessions')" = 0 ] || fail "user passwd did not revoke the session" ok "user passwd changes the hash and revokes sessions" cli "" user list expect_rc "user list" 0 grep -q '^imgadmin ' "$tmp/out" || fail "user list does not show imgadmin" grep -q '^imguser ' "$tmp/out" || fail "user list does not show imguser" cli "" user role imguser admin expect_rc "user role admin" 0 [ "$(sql "SELECT role FROM coredns.coredns_webui_users WHERE username = 'imguser'")" = admin ] || fail "user role did not set admin" cli "" user role imguser user expect_rc "user role user" 0 [ "$(sql "SELECT role FROM coredns.coredns_webui_users WHERE username = 'imguser'")" = user ] || fail "user role did not set user" ok "user role" cli "" user delete imguser expect_rc "user delete" 0 [ "$(sql "SELECT COUNT(*) FROM coredns.coredns_webui_users WHERE username = 'imguser' AND deleted_at IS NOT NULL")" = 1 ] || fail "user delete did not leave the row with deleted_at set" ok "user delete leaves the row with deleted_at" cli "" user list expect_rc "user list after delete" 0 if grep -q '^imguser ' "$tmp/out"; then fail "user list shows a deleted user" fi cli "" user list all expect_rc "user list all" 0 deleted_at=$(awk '$1 == "imguser" { print $NF }' "$tmp/out") [ -n "$deleted_at" ] && [ "$deleted_at" != "-" ] || fail "user list all does not show the deleted user with its deletion time" [ "$(awk '$1 == "imgadmin" { print $NF }' "$tmp/out")" = "-" ] || fail "user list all shows a deletion time for an active user" ok "user list and user list all" cli "$pw_user" user add imguser user [ "$rc" -ne 0 ] || fail "user add accepted the username of a deleted user" [ "$(sql "SELECT COUNT(*) FROM coredns.coredns_webui_users WHERE username = 'imguser'")" = 1 ] || fail "user add changed the row of a deleted user" ok "the username of a deleted user cannot be reused" [ "$(sql "SELECT GROUP_CONCAT(operation ORDER BY id) FROM coredns.coredns_webui_audit")" = "user_create,user_create,user_password,user_role,user_role,user_delete" ] || fail "unexpected audit rows for the CLI changes" [ "$(sql "SELECT COUNT(*) FROM coredns.coredns_webui_audit WHERE actor = 'cli' AND origin = 'local'")" = 6 ] || fail "CLI audit rows do not have actor cli and origin local" ok "CLI changes are audited" docker logs "$server_id" >"$tmp/server.log" 2>&1 scan "$tmp/server.log" ok "no secret in the container logs" started=$(date +%s) docker stop --time 40 "$server_id" >/dev/null elapsed=$(($(date +%s) - started)) [ "$(docker inspect --format '{{.State.ExitCode}}' "$server_id")" = 0 ] || fail "SIGTERM did not stop the container with exit code 0" [ "$elapsed" -lt 25 ] || fail "SIGTERM took ${elapsed}s, longer than the 25s grace period" docker logs "$server_id" >"$tmp/server.log" 2>&1 scan "$tmp/server.log" ok "SIGTERM stops the container with exit 0 in ${elapsed}s" echo "image test passed"