# OpenVPN Profiler API Reference This module (`APP_PROFILER`) is built with **FastAPI** and provides management capabilities. **Base URL**: `http://:8000/api` ## Authentication All endpoints (except initial setup) require a Bearer Token. **Header**: `Authorization: Bearer ` *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/.crt`, `private/.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 }` --- ## 4. Backup and Restore Guide: [Backup and Restore](Backup_Restore.md). All routes need the JWT. Errors: `400` invalid request, `409` another job is running (or a restore is running), `502` the destination could not be reached. Messages never contain secrets. While a restore runs, `POST /profiles`, `DELETE /profiles/{id}`, `PUT /profiles/{id}/note`, `PUT /config/*`, `POST /system/init`, `DELETE /system/pki`, `POST /server/configure` and `POST /server/process/{action}` return `409` (`"A backup restore is running"`). | Method | Path | Purpose | |---|---|---| | GET, PUT | `/backup/settings` | encryption flag and passphrase, server name, default retention, local root, the implemented destination types with their fields | | GET, POST | `/backup/destinations` | list, create (`201`) | | PUT, DELETE | `/backup/destinations/{id}` | update, delete | | POST | `/backup/destinations/{id}/test` | check connection, write and delete (SFTP: shows the host key when it is not trusted) | | POST | `/backup/destinations/{id}/trust-host-key` | SFTP: pin the host key the administrator saw | | GET | `/backup/destinations/{id}/packages` | check the source and list the packages, newest first | | POST | `/backup/run` | start a backup (`202`) | | POST | `/backup/restore` | start a restore (`202`) | | GET | `/backup/status` | the current or last job | | GET | `/backup/history` | last runs (`limit`, default 20) | ### `GET /backup/settings`, `PUT /backup/settings` - **GET response**: `{"encryption_enabled": true, "default_retention": 14, "server_name": "vpn-main", "effective_server_name": "vpn-main", "server_name_source": "setting", "local_root": "/app/backups", "types": [{"id": "local", "label": "...", "remote": false, "pins_host_key": false, "fields": [...]}, ...]}`. The passphrase is never returned. `server_name` is the configured name (`""` when none); `effective_server_name` is the name used in package names and `server_name_source` says where it comes from: `setting`, `public_ip` (the address of the VPN settings) or `hostname`. A field of `fields` has `name`, `label`, `kind` (`text`, `number`, `bool`, `secret`, `secret_multiline`), `required`, `default`, optional `hint` and `warning` + `warn_if` (the warning applies while the value equals `warn_if`). - **PUT body**: `{"passphrase": "..."}` (at least 8 characters) sets or changes it; `{"clear_passphrase": true}` removes it (`400` while an enabled remote destination exists); `{"default_retention": 14}`; `{"server_name": "vpn-main"}` (`^[A-Za-z0-9][A-Za-z0-9.-]{0,62}$`, else `400`; `""` clears it). ### Destinations - **Body (POST, PUT)**: `{"name": "offsite", "type": "s3", "enabled": true, "retention_count": 14, "params": {...}, "secrets": {...}}`. `type` must be an implemented one (`local`, `s3`, `sftp`, `ftp`), otherwise `400`. - `local`: `params: {"folder": "daily"}`, no secrets. - `s3`: `params: {"endpoint": "s3.example.com", "secure": true, "region": "", "bucket": "name", "prefix": "ovpmon/"}`, `secrets: {"access_key": "...", "secret_key": "..."}`. - `sftp`: `params: {"host": "files.example.com", "port": 22, "username": "backup", "directory": "ovpmon/daily"}`, `secrets: {"password": "..."}` **or** `{"private_key": "", "key_passphrase": "..."}` (not both). `params` also carries `host_key` and `host_key_type` once a key is trusted; they are set only by `trust-host-key` (ignored in the body, kept on `PUT` while `host` and `port` stay the same, cleared when one of them changes). - `ftp`: `params: {"host": "ftp.example.com", "port": 21, "username": "backup", "directory": "ovpmon/daily", "tls": true, "verify_cert": true, "passive": true}`, `secrets: {"password": "..."}`. - `directory` (SFTP, FTP): relative, letters, digits, `.`, `_`, `-`, no `..`, at most 4 levels. - **Response**: the destination with `secrets` masked (`"********"` for each secret that is set), `last_status`, `last_message`, `last_at`. On `PUT`, a secret that is missing, empty or `"********"` keeps the stored value. - **Test response**: `{"ok": true, "message": "..."}` (`ok: false` carries the reason). For an SFTP destination whose host key is not trusted (or changed): `{"ok": false, "message": "...", "needs_trust": true, "fingerprint": "SHA256:...", "key_type": "ssh-ed25519", "key_changed": false}`. ### `POST /backup/destinations/{id}/trust-host-key` - **Body**: `{"fingerprint": "SHA256:..."}`: the one shown by Test. The server connects again (no credentials are sent) and pins the key only if it shows exactly that fingerprint; otherwise `400` ("run Test again"). Allowed for `sftp` destinations only (`400` otherwise); `409` while a job runs; `502` when the server cannot be reached. - **Response**: the updated destination (`params.host_key` set). - Until a key is trusted, SFTP backup, listing, restore and delete are refused (`502`, "The host key of the server is not trusted yet"); on a mismatch the connection is refused. ### `GET /backup/destinations/{id}/packages` - **Response**: `{"packages": [{"name": "ovpmon-backup_vpn-main_20261007T101500Z.ovpbak.enc", "server_name": "vpn-main", "created_at": "2026-10-07T10:15:00Z", "size": 123456, "encrypted": true}]}`, newest first (time, then the `-2`, `-3` suffix). `created_at` is UTC and comes from the file name. `502` when the source cannot be read. ### `POST /backup/run` - **Body (optional)**: `{"destination_ids": [1, 2]}`; without it all enabled destinations are used. - **Response `400`**: nothing selected, a disabled destination, or a remote destination without a passphrase. ### `POST /backup/restore` - **Body**: `{"destination_id": 1, "package": "", "passphrase": "..."}`. The name is checked against the pattern and must be in the destination's listing; paths are never accepted. The passphrase is required for `.enc` packages. - **Response `409`**: a job or an initialization of the PKI is running. ### `GET /backup/status` - **Response**: `{"kind": "backup|restore|null", "state": "idle|running|done|partial|error", "stages": [{"name": "upload", "state": "pending|running|done|error"}], "results": [{"destination_id": 1, "name": "...", "state": "pending|running|ok|error|skipped", "message": "..."}], "warnings": [], "message": "...", "relogin": false, "started_at": "...", "finished_at": null}`. - Backup stages: `package`, `encrypt` (only with a passphrase), `upload`. Restore stages: `download`, `decrypt`, `verify`, `prepare`, `safety_backup`, `stop_openvpn`, `replace`, `finalize`. `partial`: stored in some destinations only. `relogin` is true after a successful restore. The state is kept in memory (single Profiler process); the history is kept in `ovpmon_backup.db`.