Files
OpenVPN-Monitoring-Simple/deploy/allinone/entrypoint.sh
T
ayurishchevandClaude Sonnet 5.5 ad6995bb63 Docs restructure, code review, gatherer reliability, JWT key fail-closed
Documentation
- Three levels: DOCS/General (overview, configuration), DOCS/Deployment
  (choose a method, first steps), details per method in Containers/ and
  System_Services/; new root README and DOCS/README map.
- Remove superseded pages (Deployment.md, Deployment_Docker/Native.md, Index.md);
  split the two scenario guides by section.
- Isolate the Timeweb/Hysteria2 records in DEPLOYMENTS/timeweb-hysteria2/
  (not linked from DOCS, listed in .graphifyignore).
- Add DOCS/code_review/ with a time-stamped analysis of the code base.

Gatherer (BUG-02, BUG-03)
- Session key (CN, address, connected_since_ts), baseline from the
  active_sessions snapshot, grace period for sessions missing from the status.
- Status file integrity (TITLE/TIME/END) and freshness; a torn read skips the tick.
- Cache applied after commit; main loop survives exceptions, backs off, and
  stops cleanly on SIGTERM/SIGINT; new client rows start with the first
  frame's totals; five tests.

JWT signing key (SEC-01)
- No built-in default: Core and Profiler refuse to start with a missing,
  placeholder or short (< 32) key; shipped config.ini has an empty value.
- Same rule in install.sh, update.sh (issues a new key when missing or weak),
  docker lib/entrypoint; five tests, conftest.py for the test key.

Each change has a plan and a summary under DOCS/Changes/.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-05 20:30:45 +03:00

104 lines
5.4 KiB
Bash
Executable File

#!/bin/sh
# Entrypoint of the all-in-one container (variant A).
#
# Order: environment -> TUN device -> workspace (easy-rsa) -> NAT -> nginx config ->
# DATABASE MIGRATION (explicit step, container does not start on failure) -> supervisord.
#
# Re-running (container restart) is safe: every step is idempotent.
set -eu
log() { echo "[ovpmon] $*"; }
warn() { echo "[ovpmon] WARNING: $*" >&2; }
die() { echo "[ovpmon] FATAL: $*" >&2; exit 1; }
# --- 1. Secret ----------------------------------------------------------------------------
# Both APIs read OVPMON_API_SECRET_KEY; JWT_SECRET is accepted as an alias (same name as in .env).
if [ -z "${OVPMON_API_SECRET_KEY:-}" ] && [ -n "${JWT_SECRET:-}" ]; then
OVPMON_API_SECRET_KEY="$JWT_SECRET"
fi
[ -n "${OVPMON_API_SECRET_KEY:-}" ] || die "JWT_SECRET (or OVPMON_API_SECRET_KEY) is not set. Generate one: openssl rand -hex 32"
export OVPMON_API_SECRET_KEY
# Key rule (same as APP_CORE/secret_key.py; lib.sh cannot be sourced in the image): no placeholder, at least 32 characters
_key_lc=$(printf '%s' "$OVPMON_API_SECRET_KEY" | tr '[:upper:]' '[:lower:]')
case "$_key_lc" in
*[![:space:]]*) ;;
*) die "JWT_SECRET is blank. Generate one: openssl rand -hex 32" ;;
esac
case "$_key_lc" in
*change-me*|*changeme*) die "JWT_SECRET is a placeholder/default value: the API refuses to start with a weak signing key. Generate one: openssl rand -hex 32" ;;
esac
[ "${#OVPMON_API_SECRET_KEY}" -ge 32 ] || die "JWT_SECRET is too short (${#OVPMON_API_SECRET_KEY} characters, at least 32 required). Generate one: openssl rand -hex 32"
unset _key_lc
# Database locations (same defaults as in the image and in docker-compose.allinone.yml)
export OVPMON_PROFILER_DB_PATH="${OVPMON_PROFILER_DB_PATH:-/app/db/ovpn_profiler.db}"
export OVPMON_OPENVPN_MONITOR_DB_PATH="${OVPMON_OPENVPN_MONITOR_DB_PATH:-/app/db/openvpn_monitor.db}"
# --- 2. TUN device ------------------------------------------------------------------------
if [ ! -c /dev/net/tun ]; then
log "Creating /dev/net/tun"
mkdir -p /dev/net
mknod /dev/net/tun c 10 200 || warn "cannot create /dev/net/tun (add devices: /dev/net/tun to the container)"
chmod 600 /dev/net/tun 2>/dev/null || true
fi
# IP forwarding is set by the container runtime (sysctls: net.ipv4.ip_forward=1 in compose)
mkdir -p /run /run/nginx /app/db /app/client-config /etc/openvpn /var/log/openvpn
# --- 3. Easy-RSA workspace ----------------------------------------------------------------
# /app/easy-rsa is the PKI volume; the profiler (working directory /app) uses /app/easy-rsa/pki.
# Copy the easy-rsa scripts only if the volume does not have them yet (never touches pki/).
if [ ! -f /app/easy-rsa/easyrsa ]; then
log "Initializing Easy-RSA workspace in /app/easy-rsa"
mkdir -p /app/easy-rsa
cp -r /usr/share/easy-rsa/* /app/easy-rsa/
fi
# --- 4. NAT and MSS clamping (as in APP_PROFILER/entrypoint.sh, without duplicates) --------
# The rules live in the container's network namespace and survive a plain container restart,
# so each rule is added only if it is not there yet (-C checks for it).
NAT_IFACE="${OVPMON_NAT_IFACE:-eth0}"
ensure_rule() {
table="$1"; shift
if iptables -t "$table" -C "$@" 2>/dev/null; then
return 0
fi
iptables -t "$table" -A "$@" || warn "iptables rule not added: -t $table $* (NET_ADMIN missing? VPN traffic will not be routed)"
}
ensure_rule nat POSTROUTING -o "$NAT_IFACE" -j MASQUERADE
ensure_rule mangle FORWARD -o "$NAT_IFACE" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
ensure_rule mangle FORWARD -i "$NAT_IFACE" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
# --- 5. Nginx configuration ---------------------------------------------------------------
# The same template as the UI image of variant B; all upstreams are local. Only the four
# OVP_* variables are substituted, nginx's own $variables stay intact.
export OVP_API_HOST=127.0.0.1 OVP_API_PORT=5001
export OVP_PROFILER_HOST=127.0.0.1 OVP_PROFILER_PORT=8000
envsubst '${OVP_API_HOST} ${OVP_API_PORT} ${OVP_PROFILER_HOST} ${OVP_PROFILER_PORT}' \
< /etc/ovpmon/default.conf.template > /etc/nginx/http.d/ovpmon.conf
nginx -t || die "generated nginx configuration is invalid (see above)"
# --- 6. Database migration (explicit step, before any service starts) ----------------------
# Same command as the update scripts run. Pending migrations are applied after a consistent
# backup (<db>.bak-<UTC timestamp> next to the database). A database that is newer than this
# image, or a failed migration, stops the container here: nothing is started on a bad database.
# See DOCS/General/Database_Migrations.md
run_migration() {
name="$1"; dir="$2"
log "Database migration: $name"
if ! ( cd "$dir" && python migrate.py ); then
die "database migration of $name failed (see the report above). Nothing was started. Restore the volumes from the last backup or use the previous image; see DOCS/Deployment/Containers/Operations.md"
fi
}
run_migration "profiler" /app/APP_PROFILER
run_migration "monitoring" /app/APP_CORE
# The migration has been done: the services only verify the schema and never migrate on their own
# (two gunicorn workers and the gatherer must not race for it).
export OVPMON_AUTO_MIGRATE=0
# --- 7. Services --------------------------------------------------------------------------
log "Starting services (supervisord)"
exec supervisord -n -c /etc/supervisord.conf