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

+23 -3
View File
@@ -38,9 +38,13 @@
<router-link to="/config/pki" class="nav-link" active-class="active" title="PKI">
<i class="fas fa-fingerprint"></i> <span>PKI</span>
</router-link>
<router-link to="/config/vpn" class="nav-link" active-class="active" title="VPN">
<router-link v-if="pkiStatus.pki_ready" to="/config/vpn" class="nav-link" active-class="active" title="VPN">
<i class="fas fa-server"></i> <span>VPN</span>
</router-link>
<!-- Locked until the PKI is initialized: stays visible so the reason is clear -->
<a v-else href="/config/pki" class="nav-link nav-link-locked" title="Initialize PKI first" @click.prevent="onLockedVpnClick">
<i class="fas fa-lock"></i> <span>VPN</span>
</a>
<!-- SECTION: MANAGEMENT -->
<div class="nav-section-header mt-3">MANAGEMENT</div>
@@ -129,6 +133,8 @@ import { useAppConfig } from './composables/useAppConfig';
import { useApi } from './composables/useApi';
import { useRoute, useRouter } from 'vue-router';
import ToastContainer from './components/ToastContainer.vue';
import { usePkiStatus } from './composables/usePkiStatus';
import { useToast } from './composables/useToast';
const { loadConfig, isLoaded } = useAppConfig();
const { apiClient } = useApi();
@@ -139,6 +145,8 @@ const isSidebarOpen = ref(false);
const isCompact = ref(false);
const route = useRoute();
const router = useRouter();
const { status: pkiStatus, refresh: refreshPkiStatus } = usePkiStatus();
const toast = useToast();
const isAuthenticated = computed(() => route.name !== 'Login');
const username = ref(localStorage.getItem('ovpmon_user') || '');
@@ -160,6 +168,12 @@ const syncUsername = async () => {
};
const onUserChanged = () => { username.value = localStorage.getItem('ovpmon_user') || ''; };
// The locked VPN item points to the PKI page instead of the VPN page
const onLockedVpnClick = () => {
toast.show('Initialize PKI first', 'info');
if (route.name !== 'PKIConfig') router.push('/config/pki');
};
const handleLogout = () => {
localStorage.removeItem('ovpmon_token');
localStorage.removeItem('ovpmon_user');
@@ -185,13 +199,19 @@ const refreshPage = () => {
// Close sidebar on route change
watch(() => route.path, () => {
isSidebarOpen.value = false;
if (route.name !== 'Login') syncUsername();
if (route.name !== 'Login') {
syncUsername();
refreshPkiStatus();
}
});
onMounted(async () => {
await loadConfig();
window.addEventListener('ovpmon-user-changed', onUserChanged);
if (route.name !== 'Login') syncUsername();
if (route.name !== 'Login') {
syncUsername();
refreshPkiStatus();
}
// Init Theme
const savedTheme = localStorage.getItem('theme') || 'light';
+10
View File
@@ -253,6 +253,16 @@ body {
flex-shrink: 0;
}
/* Section locked until the PKI is initialized */
.nav-link-locked {
opacity: 0.45;
cursor: not-allowed;
}
.nav-link-locked:hover {
opacity: 0.6;
}
.nav-section-header {
padding: 15px 15px 5px 15px;
font-size: 0.75rem;
+28
View File
@@ -0,0 +1,28 @@
import { reactive } from 'vue';
import { useApi } from './useApi';
// Module-level state: sidebar, router guard and views share one PKI status.
// Optimistic defaults: nothing is locked until the first answer from GET /system/init/status.
const status = reactive({ pki_ready: true, server_ready: true, missing: [], state: 'idle' });
// Returns true when the status was updated. Safe on errors (keeps the previous values), and does
// nothing without a session, so the Login page never calls the API.
const refresh = async () => {
if (!localStorage.getItem('ovpmon_token')) return false;
try {
const { profilesApiClient } = useApi();
const res = await profilesApiClient.get('/system/init/status');
const d = res.data || {};
status.pki_ready = d.pki_ready !== false;
status.server_ready = d.server_ready !== false;
status.missing = d.missing || [];
status.state = d.state || 'idle';
return true;
} catch (e) {
return false;
}
};
export function usePkiStatus() {
return { status, refresh };
}
+15 -2
View File
@@ -1,6 +1,8 @@
import { createRouter, createWebHistory } from 'vue-router';
import Clients from '../views/Clients.vue';
import Login from '../views/Login.vue';
import { usePkiStatus } from '../composables/usePkiStatus';
import { useToast } from '../composables/useToast';
const routes = [
{
@@ -27,7 +29,8 @@ const routes = [
{
path: '/config/vpn',
name: 'VPNConfig',
component: () => import('../views/VPNConfig.vue')
component: () => import('../views/VPNConfig.vue'),
meta: { requiresPki: true }
},
{
path: '/server',
@@ -52,13 +55,23 @@ const router = createRouter({
routes
});
router.beforeEach((to, from, next) => {
router.beforeEach(async (to, from, next) => {
const isAuthenticated = !!localStorage.getItem('ovpmon_token');
if (!to.meta.public && !isAuthenticated) {
next({ name: 'Login' });
} else if (to.name === 'Login' && isAuthenticated) {
next({ name: 'Analytics' });
} else if (to.meta.requiresPki) {
// A failed status request does not block navigation: the backend still enforces the rule
const { status, refresh } = usePkiStatus();
const ok = await refresh();
if (ok && !status.pki_ready) {
useToast().show('Initialize PKI before configuring the server', 'warning');
next({ name: 'PKIConfig' });
} else {
next();
}
} else {
next();
}
+5 -1
View File
@@ -24,7 +24,8 @@
<span class="input-group-text"><i class="fas fa-search"></i></span>
<input type="text" class="form-control" placeholder="Search by client name..." v-model="searchQuery">
</div>
<button class="btn-action btn-action-save btn-sm" @click="showNewClientModal">
<button class="btn-action btn-action-save btn-sm" @click="showNewClientModal"
:disabled="!pkiStatus.pki_ready" :title="pkiStatus.pki_ready ? '' : 'Initialize PKI first'">
<i class="fas fa-plus me-1"></i> New Client
</button>
</div>
@@ -157,9 +158,11 @@ import ConfirmModal from '../components/ConfirmModal.vue';
import ProfileDetailsModal from '../components/ProfileDetailsModal.vue';
import vNoteTooltip from '../directives/noteTooltip';
import { useToast } from '../composables/useToast';
import { usePkiStatus } from '../composables/usePkiStatus';
const { profilesApiClient } = useApi();
const toast = useToast();
const { status: pkiStatus, refresh: refreshPkiStatus } = usePkiStatus();
const loading = ref(true);
const allProfiles = ref([]);
@@ -339,6 +342,7 @@ const confirmRevoke = async (cert) => {
};
onMounted(() => {
refreshPkiStatus();
loadCerts();
});
</script>
+6 -2
View File
@@ -173,9 +173,11 @@ import { ref, computed, onMounted, onUnmounted, reactive } from 'vue';
import { useApi } from '../composables/useApi';
import { useToast } from '../composables/useToast';
import { usePkiStatus } from '../composables/usePkiStatus';
const { profilesApiClient } = useApi();
const toast = useToast();
const { refresh: refreshPkiStatus } = usePkiStatus();
// State
const form = reactive({
@@ -265,6 +267,7 @@ const loadStatus = async (initial = false) => {
return;
}
stopPolling();
if (wasRunning) refreshPkiStatus(); // unlock the sidebar without a reload
// Report the result once: a job seen running on this page, or a failure found on page load
if (wasRunning && initStatus.state === 'done') {
showMessage('PKI initialized successfully!', 'success');
@@ -301,7 +304,7 @@ const handleInit = async () => {
// Already running (second tab) or already initialized: attach to the server state
await loadStatus();
} else {
showMessage(`Error initializing: ${err.message}`, 'error');
showMessage(`Error initializing: ${err.response?.data?.detail || err.message}`, 'error');
}
} finally {
isLoading.value = false;
@@ -319,8 +322,9 @@ const handleClear = async () => {
const data = res.data;
showMessage(data.message || 'PKI cleared successfully!', 'warning');
await loadStatus();
refreshPkiStatus();
} catch (err) {
showMessage(`Error clearing PKI: ${err.message}`, 'error');
showMessage(`Error clearing PKI: ${err.response?.data?.detail || err.message}`, 'error');
} finally {
isLoading.value = false;
action.value = '';
+16 -3
View File
@@ -56,7 +56,8 @@
<!-- Start -->
<button type="button" class="btn-action btn-action-primary"
@click="handleControl('start')"
:disabled="isLoading || stats.status === 'running'">
:disabled="isLoading || stats.status === 'running' || !pkiStatus.server_ready"
:title="startHint">
<i v-if="isLoading && action==='start'" class="fas fa-spinner fa-spin"></i>
<i v-else class="fas fa-play"></i>
Start Service
@@ -65,7 +66,8 @@
<!-- Restart -->
<button type="button" class="btn-action btn-action-save"
@click="handleControl('restart')"
:disabled="isLoading">
:disabled="isLoading || !pkiStatus.server_ready"
:title="startHint">
<i v-if="isLoading && action==='restart'" class="fas fa-spinner fa-spin"></i>
<i v-else class="fas fa-sync-alt"></i>
Restart Service
@@ -86,12 +88,22 @@
</template>
<script setup>
import { ref, reactive, onMounted, onUnmounted } from 'vue';
import { ref, reactive, computed, onMounted, onUnmounted } from 'vue';
import { useApi } from '../composables/useApi';
import { useToast } from '../composables/useToast';
import { usePkiStatus } from '../composables/usePkiStatus';
const { profilesApiClient } = useApi();
const toast = useToast();
const { status: pkiStatus, refresh: refreshPkiStatus } = usePkiStatus();
// Start/Restart need the PKI server files (Stop stays available)
const startHint = computed(() => {
if (pkiStatus.server_ready) return '';
return pkiStatus.pki_ready && pkiStatus.missing.length
? `Missing server files: ${pkiStatus.missing.join(', ')}`
: 'Initialize PKI first';
});
// State
const stats = reactive({
@@ -141,6 +153,7 @@ const handleControl = async (cmd) => {
};
onMounted(() => {
refreshPkiStatus();
fetchStats();
// Poll every 5 seconds
pollInterval = setInterval(fetchStats, 5000);
+16 -3
View File
@@ -1,6 +1,15 @@
<template>
<div class="config-wrapper">
<div class="config-card">
<!-- PKI cleared while the page is open (e.g. in another tab) -->
<div v-if="!pkiStatus.pki_ready" class="config-card">
<div class="config-header">
<h1>Server Infrastructure Configuration</h1>
</div>
<p><i class="fas fa-lock me-2"></i>The server cannot be configured until the PKI is initialized.</p>
<router-link to="/config/pki">Go to PKI configuration</router-link>
</div>
<div v-else class="config-card">
<div class="config-header">
<h1>Server Infrastructure Configuration</h1>
</div>
@@ -254,9 +263,11 @@
import { ref, onMounted, reactive, watch } from 'vue';
import { useApi } from '../composables/useApi';
import { useToast } from '../composables/useToast';
import { usePkiStatus } from '../composables/usePkiStatus';
const { profilesApiClient } = useApi();
const toast = useToast();
const { status: pkiStatus, refresh: refreshPkiStatus } = usePkiStatus();
// State
const form = reactive({
@@ -356,7 +367,7 @@ const handleSave = async () => {
await profilesApiClient.put('/config/server', payload);
showMessage('Server configuration saved!', 'success');
} catch (err) {
showMessage(`Error saving: ${err.message}`, 'error');
showMessage(`Error saving: ${err.response?.data?.detail || err.message}`, 'error');
} finally {
isLoading.value = false;
action.value = '';
@@ -371,7 +382,8 @@ const handleApply = async () => {
const data = res.data;
showMessage(data.message || 'Configuration applied!', 'success');
} catch (err) {
showMessage(`Error applying: ${err.message}`, 'error');
showMessage(`Error applying: ${err.response?.data?.detail || err.message}`, 'error');
refreshPkiStatus();
} finally {
isLoading.value = false;
action.value = '';
@@ -379,6 +391,7 @@ const handleApply = async () => {
};
onMounted(() => {
refreshPkiStatus();
loadConfig();
});
</script>