# Async PKI initialization: implementation plan (2026-10-04) Goal: `Init PKI` can take minutes (DH parameters). Run it in the background; the PKI Configuration page shows progress, survives a page reload and prevents a second start. ## Current state - `POST /system/init` (`APP_PROFILER/routers/system.py`) runs `pki.init_pki()` synchronously: init-pki, build-ca, server cert, gen-dh, ta.key, gen-crl, CRL publish. The HTTP request stays open until it ends. - `pki.init_pki()` treats the PKI as initialized if `ca.crt` exists, so a PKI interrupted after the CA step (restart, crash) would be reported as ready. - `PKIConfig.vue`: `isLoading` is local to the page, so a reload loses the state and the button becomes active again. - The Profiler runs as a single process (`python main.py` in Docker, one `uvicorn` in the native unit), so in-memory job state is valid. ## Steps ### 1. Backend (`APP_PROFILER/`) - `services/pki.py` - `is_initialized()`: true only when the PKI is complete (`ca.crt`, `dh.pem`, `ta.key`, `crl.pem` and the server certificate exist). Existing complete PKIs are detected without migration. - Job state `init_job` (module level, guarded by a lock): `state` = `idle | running | done | error`, `step` (init-pki, CA, server certificate, DH, TLS key, CRL), `started_at`, `finished_at`, `error`. - `start_init_async()`: refuses to start when a job is running or the PKI is complete; removes an incomplete PKI left by an interrupted run; starts a daemon thread with its **own** DB session; the thread updates `step` and ends in `done` or `error` (error text from EasyRSA, no secrets). - `routers/system.py` - `POST /system/init` returns `202 {"state": "running"}` immediately; `409` if a job is running or the PKI is already initialized. - `GET /system/init/status` returns `{state, step, initialized, started_at, finished_at, error}`. - `DELETE /system/pki` returns `409` while an init job is running. - `POST /profiles` returns `409` while an init job is running (the PKI is not complete). - After an application restart the state is `idle`; an interrupted PKI is reported as `initialized: false`, and Init PKI cleans it up and starts again. ### 2. UI (`APP_UI/src/views/PKIConfig.vue`) - On mount call `GET /system/init/status`; state is taken from the server, not from the page. - `running`: spinner on the Init PKI button, the current step text ("Generating DH parameters..."), polling every 3 s, Init / Clear / Save disabled. - `done` or `initialized: true`: Init PKI is disabled with the hint "PKI is already initialized. Use Clear PKI to start over"; a success message is shown once when the job finishes while the page is open. - `error`: error message, Init PKI is enabled again. - After Clear PKI the status is reloaded and the button is enabled again. - A double click or a second tab cannot start a second job (button locked immediately on click, backend answers `409` anyway; the UI treats `409 running` as "attach to the running job"). - Polling stops on unmount. ### 3. Tests (minimal) - One pytest in `APP_PROFILER`: with the EasyRSA steps replaced by fast stubs, `POST /system/init` returns `202`, a second `POST` returns `409`, `GET /system/init/status` ends in `done` and `initialized: true`, a third `POST` returns `409`. No UI tests. ### 4. Verification (containers, stand `ovpmon-test`, port 8089) - Rebuild `app-profiler` and `app-ui`. Clear PKI, press Init PKI: spinner appears, the page can be reloaded and still shows progress, buttons are locked, the result appears on completion, Init PKI stays locked afterwards, Clear PKI unlocks it. - Restart `ovp-profiler` during the run: status is `idle` and `initialized: false`, Init PKI cleans up and completes. - Existing PKI from the current stand is detected as initialized. ### 5. Documentation - `DOCS/Profiler_Management/API_Reference.md` (new endpoints and `409` cases), `README.md`, `DOCS/General/Index.md`, summary `DOCS/Changes/2026-10-04_Async_PKI_Init.md`. Note for operators: single Profiler process is required (do not run several uvicorn workers). ## Out of scope Cancelling a running init, progress percentage, async Clear PKI, job history.