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:
1 parent
dcc3d7f2d5
commit
56dae90ff5
19 files changed
+342
-22
No files matched your search
@@ -3,7 +3,7 @@ import subprocess
|
||||
import logging
|
||||
import shutil
|
||||
import threading
|
||||
from .config import get_pki_settings
|
||||
from .config import get_pki_settings, get_system_settings
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime
|
||||
from database import SessionLocal
|
||||
@@ -144,10 +144,41 @@ def is_init_running() -> bool:
|
||||
with _init_lock:
|
||||
return _init_job["state"] == "running"
|
||||
|
||||
class PkiNotReady(Exception):
|
||||
"""An operation needs keys that are not there (PKI absent, init running or server files missing)."""
|
||||
|
||||
NOT_INITIALIZED_MSG = "PKI is not initialized. Initialize PKI first."
|
||||
|
||||
def server_files_missing(db: Session) -> list:
|
||||
"""Names (relative to PKI_DIR) of the files server.conf points to that do not exist.
|
||||
Checks the real files: a changed fqdn_server leaves the old certificate behind."""
|
||||
fqdn = get_pki_settings(db).fqdn_server
|
||||
files = ["ca.crt", f"issued/{fqdn}.crt", f"private/{fqdn}.key", "dh.pem", "ta.key"]
|
||||
if get_system_settings(db).crl_verify:
|
||||
files.append("crl.pem")
|
||||
return [f for f in files if not os.path.isfile(os.path.join(PKI_DIR, f))]
|
||||
|
||||
def require_pki_ready():
|
||||
"""CA operations (client certificates): initialized PKI and no init job running."""
|
||||
if is_init_running() or not is_initialized():
|
||||
raise PkiNotReady(NOT_INITIALIZED_MSG)
|
||||
|
||||
def require_server_ready(db: Session):
|
||||
"""Server operations (configure, start, restart): CA plus every file server.conf references."""
|
||||
require_pki_ready()
|
||||
missing = server_files_missing(db)
|
||||
if missing:
|
||||
raise PkiNotReady(f"PKI server files are missing: {', '.join(missing)}")
|
||||
|
||||
def init_status(db: Session) -> dict:
|
||||
with _init_lock:
|
||||
job = dict(_init_job)
|
||||
job["initialized"] = is_initialized()
|
||||
missing = server_files_missing(db)
|
||||
running = job["state"] == "running"
|
||||
job["missing"] = missing
|
||||
job["pki_ready"] = job["initialized"] and not running
|
||||
job["server_ready"] = job["pki_ready"] and not missing
|
||||
return job
|
||||
|
||||
def _set_step(step: str):
|
||||
|
||||
Reference in new issue
Block a user