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>
This commit is contained in:
ayurishchevandClaude Sonnet 5.5 committed 2026-10-05 20:30:45 +03:00
1 parent f95a0b63e6
commit ad6995bb63
85 files changed
+2805 -1280

No files matched your search

+12 -1
View File
@@ -18,6 +18,17 @@ if [ -z "${OVPMON_API_SECRET_KEY:-}" ] && [ -n "${JWT_SECRET:-}" ]; then
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}"
@@ -77,7 +88,7 @@ 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/Scenario1_Containers.md"
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
+1 -1
View File
@@ -13,7 +13,7 @@
# Result: backups/<UTC timestamp>/ with <volume>.tar.gz, SHA256SUMS, .env (mode 600), MANIFEST and the
# recorded image IDs. The directory can be set with OVPMON_BACKUP_DIR. Exit code 0 only if the
# backup was written and verified. Restore: deploy/docker/restore.sh <directory>
# See DOCS/Deployment/Scenario1_Containers.md
# See DOCS/Deployment/Containers/README.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
+1 -1
View File
@@ -12,7 +12,7 @@
#
# Safe to run again: .env is created only if it does not exist, an existing stack is only rebuilt
# and started. Variants A and B cannot run on the same host together (same ports and names).
# See DOCS/Deployment/Scenario1_Containers.md
# See DOCS/Deployment/Containers/README.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
+25 -1
View File
@@ -201,7 +201,7 @@ resolve_stack() {
sv=$(state_get OVPMON_VARIANT)
sp=$(state_get OVPMON_PROJECT)
if [ -n "$sv" ]; then
[ -z "${ARG_VARIANT:-}" ] || [ "$ARG_VARIANT" = "$sv" ] || die "--variant $ARG_VARIANT does not match the installed variant '$sv' (state file $STATE_FILE). Switching variants is described in DOCS/Deployment/Scenario1_Containers.md"
[ -z "${ARG_VARIANT:-}" ] || [ "$ARG_VARIANT" = "$sv" ] || die "--variant $ARG_VARIANT does not match the installed variant '$sv' (state file $STATE_FILE). Switching variants is described in DOCS/Deployment/Containers/Operations.md"
[ -z "${ARG_PROJECT:-}" ] || [ "$ARG_PROJECT" = "$sp" ] || die "--project $ARG_PROJECT does not match the installed project '$sp' (state file $STATE_FILE)"
PROJECT="$sp"
set_variant "$sv"
@@ -251,9 +251,33 @@ env_remove_admin() {
mv "$tmp" "$ENV_FILE"
}
# secret_problem KEY: JWT key rule (same as APP_CORE/secret_key.py: not empty, no change-me/changeme,
# at least 32 characters). Prints the reason (never the key) and returns 1 when the key is not acceptable.
# Use it as: if reason=$(secret_problem "$key"); then ...ok...; else ...$reason...; fi (set -e safe)
secret_problem() {
local _lc
case "$1" in
*[![:space:]]*) ;;
*) printf 'not set or empty'; return 1 ;;
esac
_lc=$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')
case "$_lc" in
*change-me*|*changeme*) printf 'a placeholder/default value (contains change-me)'; return 1 ;;
esac
if [ "${#1}" -lt 32 ]; then
printf 'too short (%s characters, at least 32 required)' "${#1}"
return 1
fi
return 0
}
check_env_file() {
local _reason
[ -f "$ENV_FILE" ] || die ".env not found at $ENV_FILE (install.sh creates it)"
[ -n "$(env_get JWT_SECRET)" ] || die "JWT_SECRET is empty in $ENV_FILE"
if ! _reason=$(secret_problem "$(env_get JWT_SECRET)"); then
die "JWT_SECRET in $ENV_FILE is $_reason: the API refuses to start with a weak signing key. Set a random value of at least 32 characters (for example: openssl rand -hex 32); note that a changed key signs out all users"
fi
if [ -n "$(find "$ENV_FILE" \( -perm -040 -o -perm -004 \) 2>/dev/null)" ]; then
warn ".env is readable by other users; fix with: chmod 600 $ENV_FILE"
fi
+1 -1
View File
@@ -10,7 +10,7 @@
# first saved to backups/<timestamp>-pre-restore (OVPMON_NO_SAFETY_BACKUP=1 skips it), then each volume
# is emptied and refilled from its archive. Volumes are never removed. .env is not touched: the copy
# in the backup is only reported. To also go back to the previous images use: update.sh --rollback
# See DOCS/Deployment/Scenario1_Containers.md
# See DOCS/Deployment/Containers/README.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
+1 -1
View File
@@ -13,7 +13,7 @@
# 4 STOP, 5 DATABASE MIGRATION (own step, nothing is started yet), 6 START and health check.
# A failure in phases 4-6 triggers an automatic rollback: the volumes are restored from the backup of
# this run and the previous images are started. Volumes are never removed.
# See DOCS/Deployment/Scenario1_Containers.md and DOCS/General/Database_Migrations.md
# See DOCS/Deployment/Containers/README.md and DOCS/General/Database_Migrations.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
+1 -1
View File
@@ -15,7 +15,7 @@
# service definitions, nginx site, UI files, doas rules, MANIFEST, SHA256SUMS and COMPLETE (written last).
# Exit code 0 only if the backup was written and verified. Restore: deploy/native/restore.sh <directory>
# The backup holds private keys and password hashes: keep it private and store copies off the host.
# See DOCS/Deployment/Scenario2_System_Services.md
# See DOCS/Deployment/System_Services/README.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
+1 -1
View File
@@ -24,7 +24,7 @@
# the current sources. On the target host, use the build directly or pass the tarball:
# deploy/native/install.sh --ui-dist deploy/native/ui-dist.tar.gz
# deploy/native/update.sh --ui-dist deploy/native/ui-dist.tar.gz
# See DOCS/Deployment/Scenario2_System_Services.md
# See DOCS/Deployment/System_Services/README.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
+1 -1
View File
@@ -6,7 +6,7 @@
# Prints fresh, stale or missing. Exit code: 0 fresh, 1 stale (the build is older than the sources: run
# deploy/native/build-ui.sh --publish and commit deploy/ui-dist), 2 missing or not checkable.
# Run it before a commit or a release that touches APP_UI. It changes nothing.
# See DOCS/Deployment/Scenario2_System_Services.md
# See DOCS/Deployment/System_Services/README.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
+1 -1
View File
@@ -25,7 +25,7 @@
# distribution's nftables service are never touched. IPv4 only. The options are not remembered: give them on
# every run (e.g. after the VPN subnet was changed in the UI). Persistence: /etc/ovpmon/nftables.nft loaded at
# boot by the service ovpmon-nft. install.sh and update.sh do not call this script.
# See DOCS/Deployment/Scenario2_System_Services.md
# See DOCS/Deployment/System_Services/README.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
+7 -1
View File
@@ -25,7 +25,7 @@
# Run as root from a checkout of the repository. Refuses when an installation exists (use update.sh).
# After a failure it is safe to run again: every step is idempotent and nothing is generated twice.
# It does NOT touch the firewall: NAT/forwarding commands are printed and documented.
# See DOCS/Deployment/Scenario2_System_Services.md
# See DOCS/Deployment/System_Services/README.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
@@ -174,6 +174,12 @@ if [ ! -f "$INCOMPLETE_MARK" ]; then
fi
done
fi
# A kept env file (resumed install) must hold a usable JWT key: the APIs refuse to start with a weak one
if [ -f "$ENV_FILE" ]; then
if ! _reason=$(secret_problem "$(env_get OVPMON_API_SECRET_KEY)"); then
die "OVPMON_API_SECRET_KEY in $ENV_FILE is $_reason: the APIs refuse to start with a weak signing key. Set a random value of at least 32 characters (openssl rand -hex 32) and run install.sh again"
fi
fi
[ -f "$SRC/APP_CORE/openvpn_api_v3.py" ] && [ -f "$SRC/APP_PROFILER/main.py" ] \
|| die "run the script from a complete checkout of the repository (APP_CORE and APP_PROFILER not found in $SRC)"
+21 -1
View File
@@ -363,6 +363,26 @@ kv_set() {
}
env_get() { kv_get "$ENV_FILE" "$1"; }
# secret_problem KEY: JWT key rule (same as APP_CORE/secret_key.py: not empty, no change-me/changeme,
# at least 32 characters). Prints the reason (never the key) and returns 1 when the key is not acceptable.
# Use it as: if reason=$(secret_problem "$key"); then ...ok...; else ...$reason...; fi (set -e safe)
secret_problem() {
local _lc
case "$1" in
*[![:space:]]*) ;;
*) printf 'not set or empty'; return 1 ;;
esac
_lc=$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')
case "$_lc" in
*change-me*|*changeme*) printf 'a placeholder/default value (contains change-me)'; return 1 ;;
esac
if [ "${#1}" -lt 32 ]; then
printf 'too short (%s characters, at least 32 required)' "${#1}"
return 1
fi
return 0
}
state_get() { kv_get "$STATE_FILE" "$1"; }
state_set() { kv_set "$STATE_FILE" "$1" "$2" 644; }
@@ -718,7 +738,7 @@ ensure_runtime_files() {
# ---- UI: sources, committed build, staging ----
# The UI is part of the repository: deploy/ui-dist holds a build of APP_UI together with SOURCE_HASH, the hash
# of the sources it was built from (ui_source_hash). The installed web root records the hash it came from in
# $UI_HASH_FILE, so update.sh can tell when the UI changed. See DOCS/Deployment/Scenario2_System_Services.md
# $UI_HASH_FILE, so update.sh can tell when the UI changed. See DOCS/Deployment/System_Services/README.md
UI_HASH_FILE=.ui-source-hash
# sha256_run [FILE...]: sha256sum, or shasum -a 256 (macOS); "hash name" lines, stdin when no file is given
+1 -1
View File
@@ -7,7 +7,7 @@
# Installed to the nginx include directory (Alpine /etc/nginx/http.d, Debian/Ubuntu /etc/nginx/conf.d),
# both are included in the http {} block of the distribution nginx.conf. No ssl_session_cache is declared
# here on purpose: a second "shared:SSL" zone with another size would conflict with nginx.conf.
# Reference: DOCS/General/Nginx_Configuration.md, APP_UI/default.conf.template
# Reference: DOCS/Deployment/System_Services/Nginx_Configuration.md, APP_UI/default.conf.template
# 5 login attempts per minute per client address (the application has its own lockout as well)
limit_req_zone $binary_remote_addr zone=ovpmon_login:1m rate=5r/m;
+1 -1
View File
@@ -12,7 +12,7 @@
# /etc/ovpmon, /etc/openvpn, PKI, client profiles, helper, service definitions, nginx site, UI and doas
# rules are put back from the backup. OpenVPN is not touched. The packages and the ovpmon user must
# exist (on a new host run install.sh first, then restore).
# See DOCS/Deployment/Scenario2_System_Services.md
# See DOCS/Deployment/System_Services/README.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
+12 -1
View File
@@ -22,7 +22,7 @@
# started on an unmigrated database), 6 UI, nginx, helper, doas rules, service definitions, 7 START and health
# checks. A failure after the stop triggers an automatic rollback from the backup of this run.
# The PKI (easy-rsa), client profiles and databases are never replaced or deleted by an update.
# See DOCS/Deployment/Scenario2_System_Services.md and DOCS/General/Database_Migrations.md
# See DOCS/Deployment/System_Services/README.md and DOCS/General/Database_Migrations.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
@@ -456,6 +456,17 @@ main() {
done
svc_defs_reload
for _s in $SERVICES; do svc_enable "$_s" || warn "could not enable $_s at boot"; done
# JWT key: the APIs refuse to start with a missing or weak key, so one is issued here, before the start.
# The env file is part of the backup, a failed update restores the previous one.
if ! _reason=$(secret_problem "$(env_get OVPMON_API_SECRET_KEY)"); then
_new_key=$(openssl rand -hex 32 2>/dev/null || true)
[ "${#_new_key}" -eq 64 ] || _new_key=$(od -An -N32 -tx1 /dev/urandom | tr -d ' \n')
[ "${#_new_key}" -eq 64 ] || die "cannot generate a random JWT key"
kv_set "$ENV_FILE" OVPMON_API_SECRET_KEY "$_new_key" 600
unset _new_key
warn "OVPMON_API_SECRET_KEY in $ENV_FILE was $_reason: a new random key was issued. ALL USER SESSIONS ARE RESET: everyone has to sign in again"
CHANGES=$((CHANGES + 1))
fi
# environment: add the settings of this version when they are missing (existing values are never changed)
for _kv in "OVPMON_RUNTIME=host" "OVPMON_OPENVPN_SERVICE=$OVPN_SERVICE" "OVPMON_OPENVPN_CONFIG=$OVPN_CONFIG"; do
_k=${_kv%%=*}