#!/usr/bin/env bash set -euo pipefail if [[ ${1:-} != --remote ]]; then ssh_alias=${1:-akyldash_vps} exec ssh -F "${HOME}/.ssh/config" -o BatchMode=yes "$ssh_alias" 'sudo -n /usr/local/sbin/akyldash-verify' fi if [[ ${EUID} -ne 0 ]]; then echo "Run the remote check as root; it is read-only." >&2 exit 1 fi failed=0 for package in git docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin; do if dpkg-query -W -f='${Status}' "$package" 2>/dev/null | grep -qx 'install ok installed'; then printf 'PASS package %s\n' "$package" else printf 'FAIL package %s missing\n' "$package" failed=1 fi done if systemctl is-active --quiet docker; then echo 'PASS Docker service active' else echo 'FAIL Docker service inactive' failed=1 fi if docker compose version >/dev/null 2>&1; then echo 'PASS Docker Compose responds' else echo 'FAIL Docker Compose unavailable' failed=1 fi if python3 -c 'import json; d=json.load(open("/etc/docker/daemon.json")); assert d.get("log-driver") == "local" and d.get("log-opts",{}).get("max-size") == "10m" and d.get("log-opts",{}).get("max-file") == "3"' 2>/dev/null; then echo 'PASS Docker log rotation configuration' else echo 'FAIL Docker log rotation configuration differs from repository target' failed=1 fi if id -nG codex | tr ' ' '\n' | grep -Eq '^(sudo|docker)$'; then echo 'FAIL codex has sudo or Docker group access' failed=1 else echo 'PASS codex is outside sudo and docker groups' fi for path in /opt/akyldash /etc/akyldash /var/lib/akyldash/data /var/backups/akyldash; do if [[ -d $path ]]; then printf 'PASS directory %s\n' "$path" else printf 'FAIL directory %s missing\n' "$path" failed=1 fi done for path in /etc/akyldash /var/backups/akyldash; do if [[ -d $path && $(stat -c %a "$path") == 700 ]]; then printf 'PASS permissions %s mode 0700\n' "$path" elif [[ -d $path ]]; then printf 'FAIL permissions %s mode %s, expected 0700\n' "$path" "$(stat -c %a "$path")" failed=1 fi done for path in /etc/akyldash/production.env /etc/akyldash/review_secret; do if [[ -f $path && $(stat -c %U:%a "$path") == root:600 ]]; then printf 'PASS protected file %s mode 0600\n' "$path" elif [[ -e $path ]]; then printf 'FAIL protected file %s must be root-owned mode 0600\n' "$path" failed=1 else printf 'FAIL protected file %s missing\n' "$path" failed=1 fi done checkout=/opt/akyldash/current compose_file=$checkout/deploy/production/compose.yaml if git -C "$checkout" diff --quiet HEAD -- deploy/production/compose.yaml deploy/production/Caddyfile deploy/production/Dockerfile.backend deploy/production/Dockerfile.opensearch; then echo 'PASS production Compose, Caddyfile, and Dockerfiles match the checked-out Git revision' else echo 'FAIL production Compose or Dockerfile has local drift' failed=1 fi if docker compose --env-file /etc/akyldash/production.env -f "$compose_file" config --quiet >/dev/null 2>&1; then echo 'PASS rendered production Compose configuration' else echo 'FAIL production Compose configuration cannot be rendered' failed=1 fi if ufw status | grep -q '^Status: active$'; then echo 'PASS UFW active' if ! ufw status verbose | grep -q '^Default: deny (incoming)'; then echo 'FAIL UFW default incoming policy is not deny' failed=1 fi while read -r rule action direction; do [[ $action == ALLOW && $direction == IN ]] || continue ports=${rule//\/tcp/} ports=${ports//\/udp/} IFS=, read -ra allowed <<< "$ports" for port in "${allowed[@]}"; do case "$port" in 22|80|443) ;; *) printf 'FAIL unexpected UFW allow rule %s\n' "$rule"; failed=1 ;; esac done done < <(ufw status | awk 'NR > 3 && NF >= 3 {print $1, $2, $3}') else echo 'FAIL UFW inactive or unavailable' failed=1 fi tcp_listeners=$(ss -H -lnt | awk '{print $4}') for endpoint in $tcp_listeners; do address=${endpoint%:*} port=${endpoint##*:} case "$address" in 127.*|::1|\[::1\]) continue ;; esac case "$port" in 22|80|443) ;; *) printf 'FAIL unexpected non-loopback TCP listener %s\n' "$endpoint"; failed=1 ;; esac done printf 'INFO non-loopback TCP listeners: %s\n' "${tcp_listeners//$'\n'/,}" udp_listeners=$(ss -H -lnu | awk '{print $5}') for endpoint in $udp_listeners; do address=${endpoint%:*} case "$address" in 127.*|::1|\[::1\]) continue ;; esac printf 'FAIL unexpected non-loopback UDP listener %s\n' "$endpoint" failed=1 done expected_sudoers=$'codex ALL=(root) NOPASSWD: /usr/local/sbin/akyldash-verify\ncodex ALL=(root) NOPASSWD: /usr/local/sbin/akyldash-rollback *\ncodex ALL=(root) NOPASSWD: /usr/local/sbin/akyldash-compose config\ncodex ALL=(root) NOPASSWD: /usr/local/sbin/akyldash-compose status' if [[ -x /usr/local/sbin/akyldash-verify && $(stat -c %U:%a /usr/local/sbin/akyldash-verify) == root:750 && \ -x /usr/local/sbin/akyldash-rollback && $(stat -c %U:%a /usr/local/sbin/akyldash-rollback) == root:750 && \ -x /usr/local/sbin/akyldash-compose && $(stat -c %U:%a /usr/local/sbin/akyldash-compose) == root:750 && \ -f /usr/local/lib/akyldash/archive.py && $(stat -c %U:%a /usr/local/lib/akyldash/archive.py) == root:644 && \ -f /etc/sudoers.d/akyldash-codex && $(stat -c %U:%a /etc/sudoers.d/akyldash-codex) == root:440 && \ $(cat /etc/sudoers.d/akyldash-codex) == "$expected_sudoers" ]]; then echo 'PASS read-only verifier, Compose status/config helper, rollback helper, and exact sudoers rules present' else echo 'FAIL restricted helper or sudoers installation missing/different' failed=1 fi if [[ -f /opt/akyldash/deployed-image.txt ]]; then expected=$(cat /opt/akyldash/deployed-image.txt) container=$(docker ps -aq --filter label=com.docker.compose.project=akyldash --filter label=com.docker.compose.service=backend | head -n1) actual=$(docker inspect --format '{{.Image}}' "$container" 2>/dev/null || true) if [[ $(stat -c %U:%a /opt/akyldash/deployed-image.txt) == root:600 && $expected =~ ^sha256:[0-9a-f]{64}$ && $actual == "$expected" ]]; then printf 'PASS running backend image ID matches %s\n' "$expected" else echo 'FAIL running backend image does not match recorded digest' failed=1 fi else echo 'FAIL deployed image digest record missing' failed=1 fi expected_proxy=$(docker compose --env-file /etc/akyldash/production.env -f "$compose_file" config --format json 2>/dev/null | python3 -c 'import json,sys; print(json.load(sys.stdin)["services"]["caddy"]["image"])' 2>/dev/null || true) proxy_container=$(docker ps -aq --filter label=com.docker.compose.project=akyldash --filter label=com.docker.compose.service=caddy | head -n1) actual_proxy=$(docker inspect --format '{{.Config.Image}}' "$proxy_container" 2>/dev/null || true) if [[ $expected_proxy == *@sha256:* && $actual_proxy == "$expected_proxy" ]]; then printf 'PASS running Caddy image matches %s\n' "$expected_proxy" else echo 'FAIL running Caddy image does not match the pinned Compose digest' failed=1 fi exit "$failed"