- deploy/native: install.sh, update.sh (backup, new code prepared aside, stop services, separate DB migration phase, start, automatic rollback; OpenVPN keeps running), backup.sh, restore.sh, build-ui.sh (Node 20 container), lib.sh OS adapter (OpenRC and systemd). - Root helper reads /etc/ovpmon/helper.conf (service manager, unit, config path), validated strictly; process.py/server.py honour OVPMON_RUNTIME, OVPMON_OPENVPN_SERVICE, OVPMON_OPENVPN_CONFIG. Debian/Ubuntu use openvpn-server@server and /etc/openvpn/server/server.conf. - Service definitions rewritten (systemd, OpenRC). Fixes found by real runs: no seccomp-based options in the profiler unit (they imply NoNewPrivileges and break setuid doas); OpenRC start_pre extends PATH instead of replacing it; helper syntax check works on a read-only tree. - Verified in containers with real init systems (Debian 12, Ubuntu 24.04, Alpine 3.23): install, PKI/server config/OpenVPN start, update on pre-versioned databases, rollback, backup/restore. - Test: test_helper_config.py. Docs: Scenario 2 guide, change summary, README, Index. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
56 lines
2.6 KiB
Python
56 lines
2.6 KiB
Python
import os
|
|
import logging
|
|
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, pki
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
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 = process.openvpn_config_path()
|
|
|
|
# Unprivileged service: render to the staging dir, the root helper validates and installs it
|
|
if not process.is_container() and os.geteuid() != 0:
|
|
staged = os.path.join(os.getenv("OVPMON_STAGING_DIR", "/var/lib/ovpmon/staging"), "server.conf")
|
|
os.makedirs(os.path.dirname(staged), exist_ok=True)
|
|
generator.generate_server_config(db, output_path=staged)
|
|
ok, msg = process.install_config()
|
|
if not ok:
|
|
raise HTTPException(status_code=400, detail=f"Configuration rejected: {msg}")
|
|
ok, msg = process.publish_crl()
|
|
if not ok:
|
|
if config.get_system_settings(db).crl_verify:
|
|
raise HTTPException(status_code=500, detail=f"Configuration installed, but CRL publishing failed: {msg}")
|
|
logger.warning(f"[SERVER] CRL not published (crl_verify is off): {msg}")
|
|
return {"message": "Server configuration generated", "path": output_path}
|
|
|
|
# Ensure we can write to /etc/openvpn
|
|
if not os.path.exists(os.path.dirname(output_path)) or not os.access(os.path.dirname(output_path), os.W_OK):
|
|
# For local dev or non-root host, use staging
|
|
output_path = "staging/server.conf"
|
|
os.makedirs("staging", exist_ok=True)
|
|
logger.info(f"[SERVER] /etc/openvpn not writable, using staging path: {output_path}")
|
|
else:
|
|
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
|
|
|
|
|
content = generator.generate_server_config(db, output_path=output_path)
|
|
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:
|
|
raise HTTPException(status_code=500, detail=str(e))
|