#!/usr/bin/env bash # Gate for the controller image: prove the embedded controller is live on # FileDB, not merely linked. # # Compile success is not evidence. `ZT_CONTROLLER=1` on a box with libpq # present but unconfigured builds cleanly and silently falls back, which is # exactly the failure this gate exists to catch (SPEC.md V21, §C). # # Usage: # tools/controller-image-gate.sh # docker: build image + run # tools/controller-image-gate.sh --local # test ./backone directly # tools/controller-image-gate.sh --image NAME # test an existing image set -uo pipefail MODE=docker IMAGE=backone-controller PORT=19993 NWID=8056c5e10c000000 while [[ $# -gt 0 ]]; do case "$1" in --local) MODE=local; shift ;; --image) MODE=image; IMAGE="$2"; shift 2 ;; --port) PORT="$2"; shift 2 ;; *) echo "unknown arg: $1" >&2; exit 2 ;; esac done ROOT="$(cd "$(dirname "$0")/.." && pwd)" fail=0 ok() { echo " ok $*"; } bad() { echo " FAIL $*"; fail=1; } say() { echo; echo "== $*"; } CURL=(curl -sS --max-time 10) # ---------------------------------------------------------------- local mode if [[ "$MODE" == local ]]; then BIN="$ROOT/backone" [[ -x "$BIN" ]] || { echo "no ./backone -- run: make ZT_CONTROLLER=1 ZT_CONTROLLER_NO_SQL=1 one" >&2; exit 2; } HOMEDIR="$(mktemp -d /tmp/bo-gate.XXXXXX)" CID="" trap '[[ -n "$CID" ]] && kill "$CID" 2>/dev/null; rm -rf "$HOMEDIR"' EXIT say "V21: binary carries no SQL backend symbols" if nm -C "$BIN" 2>/dev/null | grep -qiE 'pqxx|PostgreSQL::|sw::redis|smeeclient'; then bad "found PostgreSQL/Redis/smeeclient symbols in $BIN" else ok "no pqxx / PostgreSQL:: / sw::redis / smeeclient symbols" fi if ldd "$BIN" | grep -qiE 'libpq|libssl|libcrypto'; then bad "binary dynamically links libpq/libssl/libcrypto" else ok "no libpq/libssl/libcrypto in ldd" fi say "controller answers on FileDB" "$BIN" -p"$PORT" -U "$HOMEDIR" >"$HOMEDIR/log" 2>&1 & CID=$! else # ------------------------------------------------------------ docker mode if [[ "$MODE" == image ]]; then say "using existing image $IMAGE" else say "building image $IMAGE from Dockerfile.controller" # shellcheck disable=SC2086 sudo -n docker build -f "$ROOT/Dockerfile.controller" -t "$IMAGE" "$ROOT" || exit 2 fi say "V21: binary carries no SQL backend symbols" # The runtime image is intentionally minimal and has no nm/strings, so # asserting "nm -C ... found nothing" there would pass even if the tool # were simply absent. Test with the tool that exists, and let a missing # tool fail rather than pass. if sudo -n docker run --rm --entrypoint sh "$IMAGE" -c \ "! grep -qaE 'pqxx|PostgreSQL__|sw__redis|smeeclient' /usr/sbin/backone"; then ok "no pqxx / PostgreSQL / sw::redis / smeeclient strings in image binary" else bad "SQL backend strings present in image binary" fi say "controller answers on FileDB" NAME="bo-gate-$$" # --network host, not -p: the API authorizes loopback only unless # settings.allowManagementFrom widens it (OneService.cpp:1859-1905), and a # published port DNATs, so the request arrives from the bridge gateway and # gets 401 no matter how correct the token is. Host networking keeps the # request on loopback and exercises the posture operators actually get. sudo -n docker run -d --name "$NAME" --network host \ -v "$NAME:/var/lib/backone" \ "$IMAGE" /usr/sbin/backone -U -p"$PORT" >/dev/null || exit 2 trap 'sudo -n docker rm -f "$NAME" >/dev/null 2>&1; sudo -n docker volume rm -f "$NAME" >/dev/null 2>&1' EXIT fi # ------------------------------------------------------------------- shared API="http://127.0.0.1:$PORT" TOKEN="" for _ in $(seq 1 30); do if [[ "$MODE" == local ]]; then [[ -s "$HOMEDIR/authtoken.secret" ]] && { TOKEN="$(cat "$HOMEDIR/authtoken.secret")"; break; } else TOKEN="$(sudo -n docker exec "$NAME" cat /var/lib/backone/authtoken.secret 2>/dev/null)" && [[ -n "$TOKEN" ]] && break fi sleep 1 done if [[ -z "$TOKEN" ]]; then bad "daemon never produced an auth token (see logs)" [[ "$MODE" == local ]] && cat "$HOMEDIR/log" [[ "$MODE" != local ]] && sudo -n docker logs "$NAME" 2>&1 | tail -20 exit 1 fi ok "daemon up, control plane listening on $PORT" # The token is written before the control plane finishes binding, and the API # can answer /status a moment before the node reports itself online, so wait # for the online field rather than sleeping a fixed amount. STATUS="" for _ in $(seq 1 30); do STATUS="$("${CURL[@]}" -H "X-ZT1-Auth: $TOKEN" "$API/status" 2>/dev/null)" echo "$STATUS" | grep -q '"online":true' && break sleep 1 done if ! echo "$STATUS" | grep -q '"online":true'; then bad "node never reported online:true -- got: ${STATUS:0:200}" [[ "$MODE" == local ]] && tail -20 "$HOMEDIR/log" [[ "$MODE" != local ]] && sudo -n docker logs "$NAME" 2>&1 | tail -20 exit 1 fi ok "control plane answered /status with online:true" # create a network: proves the controller, not just the API server. CREATE="$("${CURL[@]}" -H "X-ZT1-Auth: $TOKEN" -X POST \ -d '{"name":"gate","private":true}' "$API/controller/network/$NWID" 2>/dev/null)" if echo "$CREATE" | grep -q '"id":"'"$NWID"'"'; then ok "controller created network $NWID" else bad "controller did not create network $NWID -- got: ${CREATE:0:200}" bad "a controller that is absent or on an unconfigured backend answers exactly like this" exit 1 fi # authorize a member: proves the write path through the DB. PUB="$(echo "$STATUS" | sed -n 's/.*"publicIdentity":"\([^"]*\)".*/\1/p')" MID="${PUB%%:*}" if [[ -n "$MID" && ${#MID} -eq 10 ]]; then AUTH="$("${CURL[@]}" -H "X-ZT1-Auth: $TOKEN" -X POST \ -d "{\"authorized\":true,\"identity\":\"$PUB\"}" \ "$API/controller/network/$NWID/member/$MID" 2>/dev/null)" if echo "$AUTH" | grep -q '"authorized":true'; then ok "controller authorized member $MID" else bad "member authorize failed -- got: ${AUTH:0:200}" fi LIST="$("${CURL[@]}" -H "X-ZT1-Auth: $TOKEN" "$API/controller/network/$NWID/member" 2>/dev/null)" if echo "$LIST" | grep -q "$MID"; then ok "member $MID reads back from the member list" else bad "member list did not contain $MID -- got: ${LIST:0:200}" fi else bad "could not derive a 10-hex member id from /status publicIdentity" fi # state must be on disk, not just in memory. if [[ "$MODE" == local ]]; then NFILE="$HOMEDIR/controller.d/network/$NWID.json" [[ -f "$NFILE" ]] && ok "FileDB wrote $NFILE" || bad "no FileDB network file at $NFILE" else if sudo -n docker exec "$NAME" test -f "/var/lib/backone/controller.d/network/$NWID.json"; then ok "FileDB wrote controller.d/network/$NWID.json" else bad "no FileDB network file in the container" fi fi echo if [[ $fail -eq 0 ]]; then echo "PASS: controller live on FileDB, no SQL backend linked" exit 0 fi echo "FAIL: see above" exit 1