Files
OpenVPN-Monitoring-Simple/DOCS/Profiler_Management/API_Reference.md
T
ayurishchevandClaude Sonnet 5.5 56dae90ff5 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>
2026-10-04 21:52:40 +03:00

97 lines
4.4 KiB
Markdown

# OpenVPN Profiler API Reference
This module (`APP_PROFILER`) is built with **FastAPI** and provides management capabilities.
**Base URL**: `http://<your-server>:8000/api`
## Authentication
All endpoints (except initial setup) require a Bearer Token.
**Header**: `Authorization: Bearer <JWT_TOKEN>`
*Note: The token is shared with the Core Monitoring API.*
---
## 1. User Profiles
Manage OpenVPN Client profiles (`.ovpn` configs and certificates).
### `GET /profiles`
List all user profiles.
- **Response**: Array of profile objects (id, username, status, expiration_date, note, etc.).
### `POST /profiles`
Create a new user profile.
- **Body**: `{"username": "jdoe"}`
- **Action**: Generates keys, requests certificate, builds `.ovpn` file.
- **Response `409`**: PKI initialization is running, or the PKI is not initialized (`"PKI is not initialized. Initialize PKI first."`).
### `DELETE /profiles/{id}`
Revoke a user profile.
- **Action**: Revokes certificate in CRL and marks profile as revoked in DB.
### `PUT /profiles/{id}/note`
Set or clear the free-text note of a profile.
- **Body**: `{"note": "text"}` (max 1000 characters; empty or `null` clears the note)
- **Response**: Updated profile object (including `note`). `404` if the profile does not exist.
### `GET /profiles/{id}/download`
Download the `.ovpn` configuration file for a user.
- **Response**: File stream (application/x-openvpn-profile).
---
## 2. System Configuration
Manage global settings for the server and PKI.
### `GET /config`
Get current configuration.
- **Query Params**: `section` (optional: 'server' or 'pki')
- **Response**: `{ "server": {...}, "pki": {...} }`
### `PUT /config/server`
Update OpenVPN Server settings (e.g., protocol, port, DNS).
- **Body**: JSON object matching `SystemSettings` schema.
### `PUT /config/pki`
Update PKI settings (e.g., Key Size, Certificate Expiry).
- **Body**: JSON object matching `PKISetting` schema.
### `POST /system/init`
Start PKI initialization (init-pki, CA, server certificate, DH, TLS key, CRL) as a background job. Returns immediately.
- **Response `202`**: `{"state": "running"}`
- **Response `409`**: a job is already running, or the PKI is already complete (`ca.crt`, `dh.pem`, `ta.key`, `crl.pem` and the server certificate). An incomplete PKI left by an interrupted run is removed and initialization starts again.
- **Note**: Job state is kept in memory; the Profiler must run as a single process (one uvicorn worker). After a restart the state is `idle`.
### `GET /system/init/status`
Current state of the initialization job.
- **Response**: `{ "state": "idle|running|done|error", "step": "init-pki|ca|server-cert|dh|ta-key|crl|null", "initialized": true, "started_at": "2026-10-04T10:00:00Z", "finished_at": null, "error": null, "pki_ready": true, "server_ready": true, "missing": [] }`
- `initialized` reflects the files on disk (complete PKI), independent of `state`.
- `pki_ready`: the PKI is initialized and no job is running (client certificates can be issued).
- `server_ready`: `pki_ready` and every file `server.conf` references exists; `missing` lists the absent ones (relative to the PKI directory: `ca.crt`, `issued/<fqdn_server>.crt`, `private/<fqdn_server>.key`, `dh.pem`, `ta.key`, and `crl.pem` only when `crl_verify` is on). The check uses the current `fqdn_server`, so renaming the server after init makes it not ready.
- `error` contains the EasyRSA message when `state` is `error`.
### `DELETE /system/pki`
**DANGER**: Completely wipes the PKI directory and resets the job state.
- **Response `409`**: an initialization job is running, or OpenVPN is running (`"Stop OpenVPN first"`).
---
## 3. Server Management
### `POST /server/configure`
Generate the `server.conf` file based on current database settings.
- **Response**: `{"message": "Server configuration generated", "path": "/etc/openvpn/server.conf"}`
- **Response `409`**: the PKI is not initialized or an init job is running (`"PKI is not initialized. Initialize PKI first."`), or server files are missing (`"PKI server files are missing: ..."`).
### `POST /server/process/{action}`
Control the OpenVPN system service.
- **Path Param**: `action` (start, stop, restart)
- **Response**: Status of the command execution.
- **Response `409`**: for `start` and `restart` only, same conditions as `POST /server/configure`. `stop` is always allowed.
### `GET /server/process/stats`
Get telemetry for the OpenVPN process.
- **Response**: `{ "status": "running", "cpu_percent": 1.2, "memory_mb": 45.0 }`