Guard server setup behind an initialized PKI

- Profiler: server_files_missing/require_* helpers; 409 for
  /server/configure, start/restart and POST /profiles when the PKI is
  not initialized or server files (honouring fqdn_server) are missing;
  stop stays allowed; DELETE /system/pki returns 409 while OpenVPN runs;
  GET /system/init/status adds pki_ready, server_ready, missing.
- UI: shared usePkiStatus; VPN menu item locked until the PKI is ready,
  route guard for /config/vpn, Start/Restart and New Client disabled
  with hints, lock notice on the VPN page.
- Test: one pytest (test_pki_guard.py).
- Docs: plan and summary in DOCS/Changes, API reference, README, Index.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5.5 committed 2026-10-04 21:52:40 +03:00
1 parent dcc3d7f2d5
commit 56dae90ff5
19 files changed
+342 -22

No files matched your search

@@ -0,0 +1,48 @@
# PKI dependency guard (2026-10-04)
Goal: while the PKI is not initialized the VPN (server) section is closed, and nothing that needs keys can run. Plan: [PKI_Dependency_Guard_PLAN](2026-10-04_PKI_Dependency_Guard_PLAN.md).
## Rules (backend is the source of truth)
| Operation | Rule |
|---|---|
| `POST /server/configure`, `POST /server/process/start`, `restart` | `409` unless the PKI is initialized, no init job runs and every file `server.conf` points to exists (`ca.crt`, `issued/<fqdn_server>.crt`, `private/<fqdn_server>.key`, `dh.pem`, `ta.key`, `crl.pem` when `crl_verify` is on). The message names the missing files, so a changed `fqdn_server` is caught too |
| `POST /server/process/stop` | always allowed |
| `POST /profiles` | `409` unless the PKI is initialized and no init job runs |
| `DELETE /system/pki` | `409` while an init job runs and `409 "Stop OpenVPN first"` while OpenVPN runs |
| `PUT /config/server`, `GET /config`, monitoring | open (no key operation) |
| `GET /system/init/status` | new fields `pki_ready`, `server_ready`, `missing` |
## UI
| Area | Change |
|---|---|
| `composables/usePkiStatus.js` | Shared status, refreshed on login, on every route change and by the PKI page (init finished, Clear PKI) |
| Sidebar (`App.vue`) | VPN item is locked (lock icon, muted, tooltip "Initialize PKI first") until the PKI is ready; a click goes to the PKI page |
| Router (`router/index.js`) | `/config/vpn` requires a ready PKI; otherwise redirect to the PKI page with a toast (also for a typed URL). A failed status request does not block navigation |
| `ServerManagement.vue` | Start / Restart disabled with a hint (missing files); Stop stays enabled |
| `Certificates.vue` | New Client disabled with a hint |
| `VPNConfig.vue` | Lock notice with a link to the PKI page instead of the form when the PKI is not ready; backend error text in toasts |
## Verification (containers)
| Check | Result |
|---|---|
| pytest (`test_pki_guard`, `test_async_pki_init`, `test_profile_note`) in `python:3.12-alpine` | 3 passed |
| After Clear PKI: status | `pki_ready: false`, `server_ready: false`, missing files listed |
| After Clear PKI: configure, start, restart, create client | `409`; stop `200` |
| Init PKI, then configure, start | `200`, `200` |
| Clear PKI while OpenVPN runs | `409 Stop OpenVPN first` |
| `fqdn_server` renamed after init: configure, start | `409`, names `issued/renamed-srv.crt` and `private/renamed-srv.key` |
| Create a client after init | `200` |
| UI bundle contains the guard texts | yes; behaviour in the browser is checked manually |
## Notes
- The status is refreshed on navigation and by the PKI page, not by polling: an open VPN page notices a cleared PKI on its next refresh (navigation or a failed Apply).
- After a rename of `fqdn_server` the VPN page stays reachable (the PKI itself is intact); configure, start and restart are blocked and name the missing certificate.
- `DELETE /system/pki` takes about a second longer while OpenVPN runs (process statistics).
## Existing installations
No manual steps. Rebuild and restart the Profiler and the UI. A running server is not affected until it is restarted.
@@ -0,0 +1,59 @@
# 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.