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

+4
View File
@@ -76,6 +76,10 @@ def create_profile(
):
if pki.is_init_running():
raise HTTPException(status_code=409, detail="PKI initialization is running")
try:
pki.require_pki_ready()
except pki.PkiNotReady as e:
raise HTTPException(status_code=409, detail=str(e))
# Check existing
existing = db.query(UserProfile).filter(UserProfile.username == profile_in.username).first()
+4 -1
View File
@@ -4,7 +4,7 @@ from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from database import get_db
from utils.auth import verify_token
from services import generator, process, config
from services import generator, process, config, pki
logger = logging.getLogger(__name__)
@@ -13,6 +13,7 @@ router = APIRouter(dependencies=[Depends(verify_token)])
@router.post("/server/configure")
def configure_server(db: Session = Depends(get_db)):
try:
pki.require_server_ready(db)
# Generate to a temporary location or standard location
# As per plan, we behave like srvconf
output_path = "/etc/openvpn/server.conf"
@@ -46,6 +47,8 @@ def configure_server(db: Session = Depends(get_db)):
return {"message": "Server configuration generated", "path": output_path}
except HTTPException:
raise
except pki.PkiNotReady as e:
raise HTTPException(status_code=409, detail=str(e))
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
+11 -2
View File
@@ -1,6 +1,8 @@
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from services import process
from services import process, pki
from database import get_db
from sqlalchemy.orm import Session
from utils.auth import verify_token
from typing import Optional
from fastapi import Depends
@@ -21,7 +23,7 @@ class ProcessStats(BaseModel):
uptime: Optional[str] = None
@router.post("/server/process/{action}", response_model=ProcessActionResponse)
def manage_process(action: str):
def manage_process(action: str, db: Session = Depends(get_db)):
"""
Control the OpenVPN server process.
Action: start, stop, restart
@@ -29,6 +31,13 @@ def manage_process(action: str):
if action not in ["start", "stop", "restart"]:
raise HTTPException(status_code=400, detail="Invalid action. Use start, stop, or restart")
# Stop stays allowed: it must work after Clear PKI or with a broken setup
if action in ("start", "restart"):
try:
pki.require_server_ready(db)
except pki.PkiNotReady as e:
raise HTTPException(status_code=409, detail=str(e))
result = process.control_service(action)
if result["status"] == "error":
+4 -1
View File
@@ -6,7 +6,7 @@ from schemas import (
ConfigResponse, SystemSettings, PKISetting,
SystemSettingsUpdate, PKISettingUpdate
)
from services import config, pki
from services import config, pki, process
router = APIRouter(dependencies=[Depends(verify_token)])
@@ -52,5 +52,8 @@ def get_init_status(db: Session = Depends(get_db)):
def clear_system_pki(db: Session = Depends(get_db)):
if pki.is_init_running():
raise HTTPException(status_code=409, detail="PKI initialization is running")
# Do not pull the keys from under a running server
if process.get_process_stats()["status"] == "running":
raise HTTPException(status_code=409, detail="Stop OpenVPN first")
msg = pki.clear_pki(db)
return {"message": msg}