Files
OpenVPN-Monitoring-Simple/DOCS/Changes/2026-10-04_PKI_Dependency_Guard_PLAN.md
T

59 lines
5.3 KiB
Markdown
Raw Normal View History

# Logical protection: server setup depends on PKI (plan, 2026-10-04)
Goal: while the PKI is not initialized, the VPN (server) section is closed, and nothing that needs keys can run. The rule is enforced in the backend (the source of truth) and reflected in the UI.
## Dependency analysis
What the server needs: `ca.crt`, server certificate and key (`issued/<fqdn_server>.crt`, `private/<fqdn_server>.key`), `dh.pem`, `ta.key`, `crl.pem` (used when `crl_verify` is on). `server.conf.j2` points to all of them.
| Operation | Needs PKI? | Today | Planned |
|---|---|---|---|
| `POST /server/configure` (build and install `server.conf`) | yes, all server files | renders paths to files that do not exist and installs the config | `409` with the list of missing files |
| `POST /server/process/start`, `restart` | yes (config + keys) | tries to start; may start with a stale `server.conf` from an earlier PKI | `409` unless the server files exist; `stop` stays allowed |
| `POST /profiles` (create client) | yes (CA) | blocked only while an init job runs | `409` unless the PKI is initialized |
| `DELETE /profiles/{id}` (revoke), download | CA / existing file | no PKI means no profiles (Clear PKI wipes them) | unchanged |
| `PUT /config/server` (save settings), `GET /config` | no (only DB values) | open | open at API level (see decision 1) |
| `PUT /config/pki` | no | open | open; UI warns that changing the server name after init makes the server certificate name stale |
| Monitoring (`APP_CORE`: Analytics, Clients) | no | open | unchanged |
Hidden dependencies found:
1. Server name: `fqdn_server` is used to build the certificate path. If it is edited after init, `server.conf` would point to a missing certificate. The check must test the actual files, not just "PKI exists".
2. Clear PKI leaves `/etc/openvpn/server.conf` and possibly a running OpenVPN that has lost its keys. After Clear the server must not be startable, and Clear should not pull the keys from under a running server (decision 2).
3. An init job in progress counts as "not ready".
## Steps
### 1. Backend (`APP_PROFILER/`)
- `services/pki.py`: `server_files_missing(db)` returns the list of missing server files (honours `fqdn_server`; `crl.pem` required only when `crl_verify` is on); `require_pki_ready()` helpers raising a `PkiNotReady` exception with a clear message.
- `routers/server.py` (`/server/configure`), `routers/server_process.py` (`start`, `restart`), `routers/profiles.py` (`POST /profiles`): return `409 {"detail": "PKI is not initialized. Initialize PKI first."}` (or the list of missing files).
- `GET /system/init/status` gets two fields: `pki_ready` (initialized, no job running) and `server_ready` (all server files present) plus `missing` (file names). One endpoint feeds the whole UI.
- Decision 2: `DELETE /system/pki` returns `409 "Stop OpenVPN first"` while the OpenVPN process runs.
### 2. UI (`APP_UI/`)
- `composables/usePkiStatus.js`: shared reactive status (`pki_ready`, `server_ready`, `missing`, `state`), `refresh()`; loaded after login, on every route change and by `PKIConfig.vue` when init finishes or PKI is cleared (the sidebar updates without reload).
- Sidebar (`App.vue`): the **VPN** item stays visible but locked (lock icon, muted, tooltip "Initialize PKI first"); click redirects to the PKI page.
- Router guard (`router/index.js`): `/config/vpn` is `meta.requiresPki`; without a ready PKI the navigation is redirected to `/config/pki` with a toast "Initialize PKI before configuring the server". Direct URL entry is covered by the same guard.
- `ServerManagement.vue`: Start/Restart disabled with the hint when `server_ready` is false (Stop stays enabled).
- `Certificates.vue`: "New Client" disabled with the hint when the PKI is not initialized.
- `VPNConfig.vue`: if the status changes while the page is open (PKI cleared in another tab), the page shows the lock notice instead of the form.
- Backend `409` messages are shown through the existing toast.
### 3. Tests (minimal)
- One pytest in `APP_PROFILER` with PKI absent: `POST /server/configure`, `POST /server/process/start` and `POST /profiles` return `409`; `GET /system/init/status` shows `pki_ready: false`, `server_ready: false`. With the PKI files created (stub directory) the three calls are no longer `409` for this reason. No UI tests.
### 4. Verification (containers, stand port 8089)
- Clear PKI: VPN menu locked, `/config/vpn` redirects, Start/Restart and New Client disabled; the same calls through the API return `409`.
- Init PKI: after it finishes the menu unlocks without a page reload; configure, start, create client work.
- Edit the server name after init: configure answers `409` naming the missing certificate.
- Clear PKI with OpenVPN running: `409` (decision 2).
### 5. Documentation
- `API_Reference.md` (409 cases, new status fields), summary `DOCS/Changes/2026-10-04_PKI_Dependency_Guard.md`, README row, Index link.
## Decisions for the owner
1. `PUT /config/server` stays open at the API level (it touches only DB values and is not a key operation); the page itself is locked in the UI. Alternative: also answer `409` there.
2. Block Clear PKI while OpenVPN is running (recommended) or allow it and only warn.
3. VPN menu item: visible and locked (recommended, the user sees why) or hidden.
## Out of scope
Automatic stop of OpenVPN, certificate expiry checks, locking the Server Process page as a whole.