Certificates: profile modal with notes, note tooltip; async PKI init
- Profiler: UserProfile.note (nullable, startup migration), PUT
/profiles/{id}/note (max 1000 chars); add missing `requests`
dependency (client creation failed without Public IP set).
- UI: clickable rows on Certificates open a compact profile details
modal with an editable note; tooltip with the note on the client
name (v-note-tooltip) and a note icon.
- PKI init runs in a background thread: POST /system/init returns 202,
GET /system/init/status, 409 for a second start, Clear PKI and client
creation are blocked while it runs. A marker file identifies an
interrupted run (only such a PKI is wiped on the next Init).
- PKI Configuration page: state from the server, progress spinner and
step text, polling, Init/Clear/Save locked while running, Init locked
once initialized.
- Tests: one pytest each for notes and async init (requirements-dev.txt).
- Docs: plans and summaries in DOCS/Changes, API reference, README, Index.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3836049230
commit
d251ec73d0
24 files changed
+722
-39
No files matched your search
@@ -1,9 +1,12 @@
|
||||
import os
|
||||
import subprocess
|
||||
import logging
|
||||
import shutil
|
||||
import threading
|
||||
from .config import get_pki_settings
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime
|
||||
from database import SessionLocal
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -120,37 +123,114 @@ def _get_easyrsa_env(db: Session):
|
||||
}
|
||||
return env
|
||||
|
||||
def init_pki(db: Session):
|
||||
# --- Async PKI initialization -------------------------------------------
|
||||
# Single Profiler process only: the job state lives in memory.
|
||||
class InitConflict(Exception):
|
||||
"""PKI init cannot start: a job is running or the PKI is already complete."""
|
||||
|
||||
_init_lock = threading.Lock()
|
||||
_init_job = {"state": "idle", "step": None, "started_at": None, "finished_at": None, "error": None}
|
||||
|
||||
def _marker_path() -> str:
|
||||
# Outside PKI_DIR: "easyrsa init-pki" recreates that directory
|
||||
return os.path.join(os.path.dirname(PKI_DIR), ".pki-init-in-progress")
|
||||
|
||||
def is_initialized(db: Session = None) -> bool:
|
||||
"""A CA exists and no init run was left unfinished. A PKI without the marker is never
|
||||
wiped automatically, even if it was built by an older version."""
|
||||
return os.path.isfile(os.path.join(PKI_DIR, "ca.crt")) and not os.path.exists(_marker_path())
|
||||
|
||||
def is_init_running() -> bool:
|
||||
with _init_lock:
|
||||
return _init_job["state"] == "running"
|
||||
|
||||
def init_status(db: Session) -> dict:
|
||||
with _init_lock:
|
||||
job = dict(_init_job)
|
||||
job["initialized"] = is_initialized()
|
||||
return job
|
||||
|
||||
def _set_step(step: str):
|
||||
with _init_lock:
|
||||
_init_job["step"] = step
|
||||
|
||||
def _gen_ta_key():
|
||||
# TLS auth key requires openvpn, not easyrsa
|
||||
subprocess.run(["openvpn", "--genkey", "secret", os.path.join(PKI_DIR, "ta.key")], check=True)
|
||||
|
||||
def init_pki(db: Session, on_step=None):
|
||||
env = _get_easyrsa_env(db)
|
||||
pki_settings = get_pki_settings(db)
|
||||
|
||||
if os.path.exists(os.path.join(PKI_DIR, "ca.crt")):
|
||||
step = on_step or (lambda name: None)
|
||||
|
||||
if is_initialized():
|
||||
logger.warning("PKI already initialized")
|
||||
return "PKI already initialized"
|
||||
|
||||
# Init PKI
|
||||
step("init-pki")
|
||||
_run_easyrsa(["init-pki"], env)
|
||||
|
||||
# Build CA
|
||||
step("ca")
|
||||
_run_easyrsa(["--req-cn=" + pki_settings.fqdn_ca, "build-ca", "nopass"], env)
|
||||
|
||||
# Build Server Cert
|
||||
step("server-cert")
|
||||
_run_easyrsa(["build-server-full", pki_settings.fqdn_server, "nopass"], env)
|
||||
|
||||
# Gen DH
|
||||
step("dh")
|
||||
_run_easyrsa(["gen-dh"], env)
|
||||
|
||||
# Gen TLS Auth Key (requires openvpn, not easyrsa)
|
||||
ta_key_path = os.path.join(PKI_DIR, "ta.key")
|
||||
subprocess.run(["openvpn", "--genkey", "secret", ta_key_path], check=True)
|
||||
# Gen TLS Auth Key
|
||||
step("ta-key")
|
||||
_gen_ta_key()
|
||||
|
||||
# Gen CRL
|
||||
step("crl")
|
||||
_run_easyrsa(["gen-crl"], env)
|
||||
_publish_crl()
|
||||
|
||||
return "PKI Initialized"
|
||||
|
||||
def _init_worker():
|
||||
db = SessionLocal() # own session: the request session is closed after the 202
|
||||
try:
|
||||
init_pki(db, on_step=_set_step)
|
||||
os.remove(_marker_path())
|
||||
result = {"state": "done", "step": None, "error": None}
|
||||
except Exception as e:
|
||||
logger.exception("Async PKI init failed")
|
||||
result = {"state": "error", "error": str(e)[:1000]}
|
||||
finally:
|
||||
db.close()
|
||||
with _init_lock:
|
||||
_init_job.update(result, finished_at=datetime.utcnow().isoformat() + "Z")
|
||||
|
||||
def start_init_async(db: Session):
|
||||
with _init_lock:
|
||||
if _init_job["state"] == "running":
|
||||
raise InitConflict("PKI initialization is already running")
|
||||
if is_initialized():
|
||||
raise InitConflict("PKI is already initialized")
|
||||
# Here the PKI is absent, has no CA, or carries the marker of an interrupted run:
|
||||
# nothing usable in it, start from scratch
|
||||
if os.path.exists(PKI_DIR):
|
||||
shutil.rmtree(PKI_DIR)
|
||||
os.makedirs(os.path.dirname(_marker_path()), exist_ok=True)
|
||||
open(_marker_path(), "w").close()
|
||||
_init_job.update(state="running", step=None, error=None, finished_at=None,
|
||||
started_at=datetime.utcnow().isoformat() + "Z")
|
||||
threading.Thread(target=_init_worker, daemon=True).start()
|
||||
|
||||
def clear_pki(db: Session):
|
||||
with _init_lock:
|
||||
if _init_job["state"] != "running":
|
||||
_init_job.update(state="idle", step=None, started_at=None, finished_at=None, error=None)
|
||||
if os.path.exists(_marker_path()):
|
||||
os.remove(_marker_path())
|
||||
|
||||
# 1. Clear Database Users
|
||||
from models import UserProfile
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user