games_app/deploy/vps-hardening/verify-harden.sh
Your Name 5ce6a6811a
Some checks failed
CI / test-and-build (push) Failing after 25s
Add Gitea CI/CD stack, tloto-deploy, and tagged Compose images
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-17 19:56:39 +02:00

66 lines
1.9 KiB
Bash

#!/usr/bin/env bash
# Post-harden verification (run as deploy or root on the VPS).
set -euo pipefail
ok=0
fail=0
ufw_status() {
if [[ "$(id -u)" -eq 0 ]]; then
ufw status
elif command -v sudo >/dev/null 2>&1 && sudo -n true 2>/dev/null; then
sudo -n ufw status
else
ufw status
fi
}
_ufw_active() { ufw_status 2>/dev/null | grep -qi 'Status: active'; }
_ufw_9080() { ufw_status 2>/dev/null | grep -q 9080; }
_ufw_9443() { ufw_status 2>/dev/null | grep -q 9443; }
_pg_localhost() {
# Allow loopback + Docker bridge IPs; fail if listening on a public address
! ss -tlnp | grep -E ':5432\b' | grep -vE '127\.0\.0\.1|\[::1\]|172\.(1[6-9]|2[0-9]|3[0-1])\.' | grep -q .
}
_ufw_docker_pg() { ufw_status 2>/dev/null | grep -qE '5432.*172\.16'; }
_redis_closed() { ! ss -tlnp | grep -qE ':6379\b'; }
check() {
local name="$1"
shift
if "$@"; then
printf '[OK] %s\n' "$name"
ok=$((ok + 1))
else
printf '[FAIL] %s\n' "$name"
fail=$((fail + 1))
fi
}
echo "Tloto VPS harden verification"
echo "Host: $(hostname) User: $(id -un)"
echo
check "ufw active" _ufw_active
check "ufw allows 9080" _ufw_9080
check "ufw allows 9443" _ufw_9443
check "ufw docker→5432" _ufw_docker_pg
check "fail2ban running" systemctl is-active --quiet fail2ban
check "docker available" command -v docker
check "sshd drop-in exists" test -f /etc/ssh/sshd_config.d/99-tloto.conf
check "sysctl drop-in exists" test -f /etc/sysctl.d/99-tloto-harden.conf
if [[ -f /etc/ssh/sshd_config.d/99-tloto.conf ]]; then
check "PasswordAuthentication no" grep -Eq '^PasswordAuthentication no' /etc/ssh/sshd_config.d/99-tloto.conf
check "PermitRootLogin no" grep -Eq '^PermitRootLogin no' /etc/ssh/sshd_config.d/99-tloto.conf
fi
if command -v ss >/dev/null 2>&1; then
check "postgres not on public iface" _pg_localhost
check "redis not published on host" _redis_closed
fi
echo
echo "Passed: $ok Failed: $fail"
[[ "$fail" -eq 0 ]]