Encrypted packages (databases, PKI, client profiles) with safe restore and a pre-restore safety copy, usable for server-to-server migration across container and system-service installs. New Management page, ovp_backups volume, migrations code in the profiler image, server name in package names. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
99 lines
7.5 KiB
Markdown
99 lines
7.5 KiB
Markdown
# Implementation Plan - Authentication & Security
|
|
|
|
## Goal Description
|
|
Add secure authentication to the OpenVPN Monitor application.
|
|
This includes:
|
|
- **Database Storage**: Store users and credentials in the existing SQLite database.
|
|
- **2FA**: Support Google Authenticator (TOTP) for two-factor authentication.
|
|
- **Brute-force Protection**: Rate limiting on login attempts.
|
|
- **Universal Access Control**: Secure all UI routes and API endpoints.
|
|
|
|
## User Review Required
|
|
> [!IMPORTANT]
|
|
> **Initial admin**: no default user is created. On first run with an empty users table the admin is seeded only from OVPMON_INITIAL_ADMIN_USER / OVPMON_INITIAL_ADMIN_PASSWORD; the username can be changed in Account (see DOCS/Changes/2026-09-30_Admin_Username_Change.md).
|
|
|
|
> [!WARNING]
|
|
> **Breaking Change**: Access to the current dashboard will be blocked until the user logs in.
|
|
|
|
## Proposed Changes
|
|
|
|
### Backend (Python/Flask)
|
|
#### [MODIFY] [requirements.txt](../../APP_CORE/requirements.txt)
|
|
- Add `pyjwt`, `pyotp`, `qrcode`, `bcrypt`, `flask-bcrypt` (or `werkzeug.security`).
|
|
|
|
#### [MODIFY] [db.py](../../APP_CORE/db.py)
|
|
- Update `init_database` to create:
|
|
- `users` table: `id`, `username`, `password_hash`, `totp_secret`, `is_2fa_enabled`.
|
|
- `login_attempts` table (for brute-force protection): `ip_address`, `attempts`, `last_attempt`.
|
|
|
|
#### [MODIFY] [openvpn_api_v3.py](../../APP_CORE/openvpn_api_v3.py)
|
|
- **New Imports**: `jwt`, `pyotp`, `functools.wraps`.
|
|
- **Helper Functions**:
|
|
- `check_rate_limit(ip)`: Verify login attempts.
|
|
- `token_required(f)`: Decorator to check `Authorization` header.
|
|
- **New Routes**:
|
|
- `POST /api/auth/login`: Validate user/pass. Returns JWT (or 2FA required status).
|
|
- `POST /api/auth/verify-2fa`: Validate TOTP. Returns access JWT.
|
|
- `POST /api/auth/setup-2fa`: Generate secret & QR code.
|
|
- `POST /api/auth/enable-2fa`: Confirm and save secret.
|
|
- **Protect Routes**: Apply `@token_required` to all existing API routes (except auth).
|
|
- **Signing key (fail closed)**: tokens are signed with `OVPMON_API_SECRET_KEY` (or `[api] secret_key`); there is no built-in default. A missing, blank, placeholder (`change-me`/`changeme`) or shorter than 32 characters key stops the API at start (critical log without the key, exit code 1, before the database is opened). The same rule is checked by the install and update scripts. See [Configuration](Configuration.md).
|
|
|
|
### Frontend (Vue.js)
|
|
#### [NEW] [Login.vue](../../APP_UI/src/views/Login.vue)
|
|
- Login form (Username/Password).
|
|
- 2FA Input (conditional, appears if server responses "2FA required").
|
|
|
|
#### [NEW] Setup2FA.vue (planned; the 2FA setup lives in `Account.vue`)
|
|
- Screen to show QR code and verify OTP to enable 2FA for the first time.
|
|
|
|
#### [MODIFY] [router/index.js](../../APP_UI/src/router/index.js)
|
|
- Add `/login` route.
|
|
- Add global `beforeEach` guard:
|
|
- Check if route `requiresAuth`.
|
|
- Check if token exists in `localStorage`.
|
|
- Redirect to `/login` if unauthorized.
|
|
|
|
#### [MODIFY] [App.vue](../../APP_UI/src/App.vue)
|
|
- Add `Logout` button to the sidebar.
|
|
- Conditionally render Sidebar only if logged in (optional, or just redirect).
|
|
|
|
#### [MODIFY] [main.js](../../APP_UI/src/main.js)
|
|
- Setup `axios` interceptors:
|
|
- **Request**: Add `Authorization: Bearer <token>`.
|
|
- **Response**: On `401 Unauthorized`, clear token and redirect to `/login`.
|
|
|
|
## Backups
|
|
|
|
The Backup and Restore function ([guide](../Profiler_Management/Backup_Restore.md)) makes a package of the application data. What matters for security:
|
|
|
|
- **What a package holds:** the CA private key and all issued keys (`pki/`), the monitoring database (administrator password hashes, 2FA secrets), the profiler database and the issued client profiles. Whoever can read a package can impersonate the VPN server and its clients. The JWT signing key and the environment file are **not** in it.
|
|
- **Encryption:** with a passphrase (at least 8 characters) the package is encrypted with AES-256-GCM in 1 MiB blocks; the key comes from scrypt (N=2^15) with a random salt; the header is authenticated, so truncation and tampering are detected. **Remote destinations (S3, SFTP, FTP) refuse to work without a passphrase.** A local folder works without it and the page warns. The passphrase is never stored in a package; without it an encrypted package cannot be opened.
|
|
- **Secrets of the function:** destination secrets (keys, passwords, SFTP private key) and the package passphrase are stored in `ovpmon_backup.db`, encrypted (Fernet) with a key derived (HKDF-SHA256) from the JWT signing key. The API returns `********` instead of a value and never logs one; error messages carry no secrets. If the signing key changes they cannot be read any more and must be entered again. `ovpmon_backup.db` (mode 600) is not part of a package and is never replaced by a restore.
|
|
- **SFTP:** the SSH host key is pinned by its SHA-256 fingerprint. The first Test shows the fingerprint and the administrator confirms it; until then nothing is sent to the server (not even the password) and every operation is refused. Unknown keys are never accepted automatically, and a changed key breaks the connection before credentials are sent. **FTP:** explicit TLS and certificate verification are on by default; plain FTP and an unchecked certificate are explicit, warned choices. S3: TLS by default.
|
|
- **Local storage:** the backup root (`/var/lib/ovpmon/backups`, containers `/app/backups`) and its folders are mode 700, packages 600, owner `ovpmon`; folder names are validated, links and paths outside the root are refused. In containers the volume `ovp_backups` is not covered by `deploy/docker/backup.sh`.
|
|
- **Restore as a trust boundary:** all routes need the JWT (any administrator can start a restore, and a restore replaces the administrator accounts). The package is unpacked into a temporary directory with an allowlist of names (no absolute paths, `..`, links, devices; size and entry limits), checked against the SHA-256 sums of its manifest and migrated there; the running system is touched only after all that and after a safety copy of the current state. The sums are inside the package: they detect damage, not forgery. **Authenticity comes from encryption** (a forged encrypted package fails the GCM check), so an unencrypted package from a place others can write to must not be restored. During a restore requests that change the PKI, profiles, settings or the OpenVPN process return `409`.
|
|
- **Limits:** the passphrase and destination secrets are only as safe as the JWT signing key and the file permissions of the server; a stolen package with its passphrase, or a stolen `.env` with `ovpmon_backup.db`, opens the CA key.
|
|
|
|
## Verification Plan
|
|
|
|
### Automated Tests
|
|
Since this project does not have a comprehensive test suite, we will verify manually and with targeted scripts.
|
|
|
|
### Manual Verification
|
|
1. **Initial Setup**:
|
|
- Start backend and frontend.
|
|
- Visit root URL -> Should redirect to `/login`.
|
|
2. **Login Flow**:
|
|
- Attempt login with wrong password -> Should show error.
|
|
- Attempt brute force (5x wrong) -> Should block for X minutes.
|
|
- Login with `admin` / `password` -> Should succeed.
|
|
3. **2FA Setup**:
|
|
- Go to 2FA Setup page (or trigger via API).
|
|
- Scan QR code with Google Auth.
|
|
- enter code -> Success.
|
|
- Logout and Login again -> Should ask for 2FA code.
|
|
4. **API Security**:
|
|
- Try `curl http://localhost:5000/api/v1/stats` without header -> Should return 401.
|
|
- Try with header -> Should return 200.
|