Files
OpenVPN-Monitoring-Simple/DOCS/Changes/2026-10-05_Scenario2_System_Services.md
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

96 lines
11 KiB
Markdown

# Scenario 2: system services, Alpine, Debian, Ubuntu (2026-10-05)
Phase 3 of [Deployment_Scenarios_PLAN](2026-10-05_Deployment_Scenarios_PLAN.md). Guide: [Scenario 2: system services](../Deployment/System_Services/README.md).
## What was added
| Area | Content |
|---|---|
| `deploy/native/` | `install.sh`, `update.sh`, `backup.sh`, `restore.sh`, `build-ui.sh`, `lib.sh` (OS adapter for Alpine/OpenRC and Debian/Ubuntu/systemd), nginx template |
| Install | Packages, `ovpmon` user, code, two virtualenvs, easy-rsa workspace, `/etc/ovpmon/env` (random secret, admin password shown once), root helper + `doas`, service definitions, OpenVPN prerequisites, nginx with TLS on 8088, health checks. The firewall is **not** touched (hints are printed) |
| Update (phases) | preflight, **backup** (databases through the SQLite backup API, configuration, PKI, client configs, code with virtualenvs, system files; verified), source update, new code and virtualenvs prepared aside, **stop services (OpenVPN keeps running)** and swap code, **database migration as its own step** with the new code, UI/helper/doas/service files, start and health checks, **automatic rollback** from any failure after the stop |
| UI build | `build-ui.sh` builds the UI in a Node 20 container into a tarball; the host needs no Node |
| Debian/Ubuntu support in code | Root helper reads `/etc/ovpmon/helper.conf` (install dir, service manager `openrc|systemd`, unit, config path), validated strictly, defaults equal the old Alpine behaviour; `process.py` and `routers/server.py` use `OVPMON_RUNTIME`, `OVPMON_OPENVPN_SERVICE`, `OVPMON_OPENVPN_CONFIG`. Debian/Ubuntu: unit `openvpn-server@server`, config `/etc/openvpn/server/server.conf` |
| Service definitions | `DOCS/General/systemd/*.service`, `DOCS/General/openrc/*` rewritten (user `ovpmon`, environment file, real commands, no `--reload`); `Service_Management.md` updated |
| Docs | `DOCS/Deployment/Scenario2_System_Services.md`; `Deployment_Native.md` is a pointer |
| Test | `APP_PROFILER/test_helper_config.py` (helper configuration, defaults, rejected values, command lines) |
## Defects found by the real runs and fixed
| Defect | Fix |
|---|---|
| The profiler unit set `ProtectKernelTunables` and `ProtectKernelModules`: systemd implements them with seccomp and then sets NoNewPrivileges implicitly, so the setuid `doas` failed with "not installed setuid" and the root helper never ran | Both options removed from the profiler unit; the reason and the check are in the unit and in the guide |
| OpenRC scripts replaced `PATH` in `start_pre`, dropping `/lib/rc/bin`: OpenRC itself failed with `ebegin: not found` | `PATH` is extended, not replaced (all three scripts) |
| The helper syntax check used `py_compile`, which fails on a read-only source tree | `compile()` without writing a `.pyc` |
## Verification (containers with a real init: systemd on Debian 12 and Ubuntu 24.04, OpenRC on Alpine 3.23)
| Check | Debian 12 | Ubuntu 24.04 | Alpine 3.23 |
|---|---|---|---|
| `install.sh`, health checks, admin seed removed | ok | ok | ok |
| PKI init, server config through the root helper, client, note | ok | ok | ok |
| OpenVPN started through the helper | `openvpn-server@server` | `openvpn-server@server` | `openvpn` (OpenRC) |
| `update.sh` on databases without a version (real data) | ok, migration phase | ok | ok |
| OpenVPN untouched by the update | same PID | active | active |
| Rollback after an injected failure (broken new Profiler) | ok, data intact | not run | ok, data intact |
| `backup.sh`, change, `restore.sh` | not run | ok | not run |
| pytest: helper config test and the earlier tests | passed (all five test files) |||
Test containers, images and the UI archive were removed afterwards.
## Follow-up: the install directory is always asked for
A first run on a clean Alpine 3.24 host failed with "an installation already exists": the repository had been cloned straight into `/opt/OpenVPN-Monitoring-Simple`, the default prefix, and the preflight took the checkout's `APP_CORE` for an installation. My tests always ran from a separate source directory, so this was not caught.
| Change | Detail |
|---|---|
| `--prefix` is mandatory | No default any more: the option, or an interactive prompt with a confirmation; without a terminal the script stops. `/opt/OpenVPN-Monitoring-Simple` is only a suggestion |
| Prefix checks (before anything is changed) | absolute and normalised path; not a system directory (the installer sets permissions on it, `chmod 755 /root` would be harmful); not the repository checkout, not inside it, not containing it; new or empty (a resumed install is allowed); an existing non-empty directory is refused and points to `update.sh` |
| Not done on purpose | An "install in place" mode (checkout = install directory) was drafted and dropped: with the source and the install directory separated it is unnecessary, and a rollback after a `git pull` in place was not verified (the git `HEAD` could stay on the new commit while the files are restored) |
| Messages | A refusal in preflight now says "nothing was changed on this system"; a later failure says to rerun with the same `--prefix` |
| `update.sh`, `backup.sh`, `restore.sh` | Unchanged: they read the prefix from `/etc/ovpmon/deploy.state`. Adoption of a hand-made installation by `update.sh` (including a checkout used as the install directory) stays as it was |
| `.gitignore` | `__pycache__/` and `*.pyc` |
Checked without installing anything: system directories, the checkout itself, a path inside the checkout, `..`/relative/trailing-slash paths, a missing `--prefix` without a terminal, and the interactive prompt (empty answer, system directory, the checkout, a valid path declined three times) all stop before any change; nothing was created on the host.
## Follow-up: the UI is part of the repository (plan: [UI_Delivery_PLAN](2026-10-05_UI_Delivery_PLAN.md))
Incident: `install.sh` without `--ui-dist` installed only a placeholder page, so after the install "the UI was not there". The UI is now delivered like any other part of the checkout.
| Change | Detail |
|---|---|
| Committed build | `deploy/ui-dist/` (build of `APP_UI`) with `SOURCE_HASH`; refreshed by a maintainer with `deploy/native/build-ui.sh --publish` before committing UI changes. Outside `APP_UI`, so the container builds are unaffected; `deploy/ui-dist` is in `.dockerignore` (the all-in-one image builds the UI itself); `.gitignore` does not ignore it |
| `ui_source_hash` (`lib.sh`) | SHA-256 over `src/`, `public/`, `index.html`, `package.json`, `package-lock.json`, `vite.config.js`, `jsconfig.json` (relative paths in `LC_ALL=C` order, hash per file, hash of the list; never `node_modules`/`dist`); busybox and GNU (`sha256sum`, or `shasum` on macOS). One function for `build-ui.sh`, `check-ui.sh`, `install.sh`, `update.sh` |
| `check-ui.sh` | prints `fresh`, `stale` or `missing`; exit 0, 1, 2 |
| Staleness guard | committed build older than the sources: a loud, precise warning in `install.sh`/`update.sh` (hashes, `build-ui.sh --publish` for maintainers, `--build-ui` for the host); not fatal |
| Order (`install.sh`, `update.sh`) | `--ui-dist FILE\|DIR`, `--build-ui`, committed `deploy/ui-dist`, (install only: `deploy/native/ui-dist.tar.gz`); nothing found: `install.sh` stops unless `--no-ui` (placeholder only then); `update.sh` keeps the installed UI |
| Web root hash | `/var/www/ovpmon/.ui-source-hash`; `update.sh` skips the UI swap when the repository build has the same hash, otherwise swaps it in phase 6 (inside the backup and the automatic rollback); the source is decided after `git pull` |
| `stage_ui` | accepts a tarball or a directory, validates `index.html` before the live web root is touched, same atomic swap |
| `build-ui.sh` | default unchanged in effect (tarball), now Docker if it works else local; `--docker`, `--local` (`npm ci` in a temporary copy of `APP_UI`, Node >= 20.19 check, per-OS instructions), `--node-download` (official Node 20, SHA-256 verified, glibc only, temporary), `--publish`, `--check`, `--repo`; tarballs contain `SOURCE_HASH` |
Checked on a temporary copy of the repository (nothing installed, no service touched): `sh -n`/`bash -n` of all scripts; `ui_source_hash` identical under dash, bash and busybox sh and equal to an independent Python computation, unaffected by `node_modules`, `dist` and `.DS_Store`, changed by a source edit; `check-ui.sh` fresh/stale/missing with exit codes; `ui_resolve` and `stage_ui` for a directory, a tarball with and without `SOURCE_HASH`, an invalid source and the fallback archive; `build-ui.sh --local --publish -o` with a stub `node`/`npm` (old Node refused with instructions, source tree stays clean, `deploy/ui-dist` and the tarball written, check says fresh); `ui_resolve --build-ui` through the default Docker builder produced a real tarball whose `SOURCE_HASH` equals the hash of the sources. Not tested here: `--node-download` (needs nodejs.org), `--docker --publish` on the real tree, a real install and update with the new UI logic on a host, and `build-ui.sh --local` with a real Node.
Real runs after the implementation (Alpine 3.23 container with OpenRC; Docker only for the maintainer build):
| Check | Result |
|---|---|
| `build-ui.sh --publish` (Docker) and `check-ui.sh` | `deploy/ui-dist` (1.3 MB) written, `fresh` |
| `install.sh --prefix ...` with **no UI option** (the incident) | The UI of the repository is installed automatically: real index page (`OpenVPN Controller`), `.ui-source-hash` equals `SOURCE_HASH`, HTTPS health check 200 |
| `build-ui.sh --local` with the distribution's Node (v24, no Docker) | Builds in a temporary copy; the source tree stays clean (no `node_modules`, no `dist`); the archive's `SOURCE_HASH` equals the Docker build's |
| `update.sh`, UI sources unchanged | "UI: unchanged (same sources as the installed UI)", no swap |
| `update.sh`, a UI source changed after the build | Loud warning that the committed build is older than the sources and how to fix it; the update still completes |
| Not run | `--node-download` (needs the network and nodejs.org), Debian/Ubuntu runs of the new UI logic, `install.sh --build-ui` on a host |
## Notes and limits
- A container is not a host: no real firewall, cgroup and init details can differ. Run the procedure once on a VM of the target OS before production. Test containers needed explicit help (an init process for OpenRC, writable cgroup); these are test artefacts, not product defects.
- Ubuntu 22.04 and other Debian or Alpine versions are not tested (the script warns and needs `--force`).
- Not tested: BusyBox `su` paths on hosts other than the test image, an upgrade of an existing real Alpine host through `update.sh` (adoption without a state file is implemented, not exercised), a source tree that is itself the install directory.
- Updates need access to the Python package index (fresh virtualenvs).
- `openvpn-server@server` shows as failed at boot until a `server.conf` exists.
- The version in the logs is `unknown` when `git` is not installed or the source is not a checkout.
## Existing installations
An existing Alpine host installed by hand can be adopted: `update.sh` records the state on the first run. Keep a copy of `/etc/doas.d/*` and the helper before the first update; the update replaces files that carry the deploy marker and reports, but does not overwrite, files that differ and have none.