feat(api): wire /api/servers routes, live WS metric/state streaming, and Panic Lock SSH teardown
This commit is contained in:
parent
03e996d743
commit
2d7709035b
3 changed files with 543 additions and 1 deletions
|
|
@ -5,13 +5,20 @@ import type Database from 'better-sqlite3';
|
|||
import express, { type Express } from 'express';
|
||||
import { ConfigManager } from './config/config-manager.js';
|
||||
import { openDatabase } from './db/database.js';
|
||||
import { ServerInspector } from './monitoring/inspector.js';
|
||||
import { MonitoringService } from './monitoring/monitoring-service.js';
|
||||
import { ProbeRunner } from './monitoring/probes.js';
|
||||
import { AuditService } from './security/audit.js';
|
||||
import { AuthService } from './security/auth.js';
|
||||
import { RateLimiter, attachClientIp } from './security/guards.js';
|
||||
import { VaultService } from './security/vault.js';
|
||||
import { KeyManager } from './ssh/key-manager.js';
|
||||
import { KnownHostsService } from './ssh/known-hosts.js';
|
||||
import { SSHPool } from './ssh/ssh-pool.js';
|
||||
import { createAuditRouter } from './api/routes/audit.routes.js';
|
||||
import { createAuthRouter } from './api/routes/auth.routes.js';
|
||||
import { createConfigRouter } from './api/routes/config.routes.js';
|
||||
import { createServersRouter } from './api/routes/servers.routes.js';
|
||||
import { createVaultRouter } from './api/routes/vault.routes.js';
|
||||
import { WsHub } from './api/ws-hub.js';
|
||||
|
||||
|
|
@ -34,6 +41,11 @@ export interface GCoordinatorInstance {
|
|||
authService: AuthService;
|
||||
configManager: ConfigManager;
|
||||
auditService: AuditService;
|
||||
knownHosts: KnownHostsService;
|
||||
sshPool: SSHPool;
|
||||
keyManager: KeyManager;
|
||||
serverInspector: ServerInspector;
|
||||
monitoringService: MonitoringService;
|
||||
wsHub: WsHub;
|
||||
port: number;
|
||||
close(): Promise<void>;
|
||||
|
|
@ -87,7 +99,47 @@ export async function createGCoordinatorServer(
|
|||
const httpServer = http.createServer(app);
|
||||
const wsHub = new WsHub(httpServer, authService, vaultService);
|
||||
|
||||
const knownHosts = new KnownHostsService(db, auditService);
|
||||
const sshPool = new SSHPool(configManager, vaultService, knownHosts);
|
||||
const keyManager = new KeyManager(sshPool, vaultService, configManager, auditService);
|
||||
const serverInspector = new ServerInspector(sshPool, configManager);
|
||||
const probeRunner = new ProbeRunner(sshPool);
|
||||
const monitoringService = new MonitoringService(
|
||||
db,
|
||||
configManager,
|
||||
vaultService,
|
||||
sshPool,
|
||||
probeRunner,
|
||||
);
|
||||
|
||||
sshPool.onStateChange((states) => {
|
||||
wsHub.broadcastChannel('metrics', { type: 'servers:state', states });
|
||||
for (const s of states) {
|
||||
if (s.hostKeyMismatch) {
|
||||
wsHub.broadcastChannel('alerts', {
|
||||
type: 'alerts:update',
|
||||
activeCount: knownHosts.listPendingMismatches().length,
|
||||
hostKeyMismatch: s.hostKeyMismatch,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
monitoringService.onTick((event) => {
|
||||
wsHub.broadcastChannel('metrics', {
|
||||
type: 'metrics:tick',
|
||||
serverId: event.serverId,
|
||||
sample: event.sample,
|
||||
disks: event.disks,
|
||||
probes: event.probes,
|
||||
});
|
||||
});
|
||||
|
||||
monitoringService.start();
|
||||
|
||||
vaultService.onPanicLock(() => {
|
||||
monitoringService.stopAll();
|
||||
sshPool.closeAll('panic_lock');
|
||||
authService.revokeAllSessions();
|
||||
wsHub.handlePanicLock();
|
||||
});
|
||||
|
|
@ -100,6 +152,12 @@ export async function createGCoordinatorServer(
|
|||
warnings: event.warnings,
|
||||
errors: event.errors,
|
||||
});
|
||||
if (event.entityType === 'server' || event.entityType === 'settings' || event.entityType === 'all') {
|
||||
if (event.entityType === 'server' && event.id) {
|
||||
sshPool.closeServer(event.id);
|
||||
}
|
||||
monitoringService.syncServers();
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/healthz', (_req, res) => {
|
||||
|
|
@ -125,10 +183,26 @@ export async function createGCoordinatorServer(
|
|||
auditService,
|
||||
rateLimiter,
|
||||
wsHub,
|
||||
onVaultUnlocked: () => {
|
||||
monitoringService.syncServers();
|
||||
},
|
||||
});
|
||||
|
||||
app.use('/api/vault', vaultRouter);
|
||||
app.use('/api/security', panicLockHandler);
|
||||
app.use(
|
||||
'/api/servers',
|
||||
createServersRouter({
|
||||
authService,
|
||||
configManager,
|
||||
auditService,
|
||||
knownHosts,
|
||||
sshPool,
|
||||
keyManager,
|
||||
serverInspector,
|
||||
monitoringService,
|
||||
}),
|
||||
);
|
||||
app.use(
|
||||
'/api/config',
|
||||
createConfigRouter({ authService, configManager, auditService }),
|
||||
|
|
@ -168,9 +242,16 @@ export async function createGCoordinatorServer(
|
|||
authService,
|
||||
configManager,
|
||||
auditService,
|
||||
knownHosts,
|
||||
sshPool,
|
||||
keyManager,
|
||||
serverInspector,
|
||||
monitoringService,
|
||||
wsHub,
|
||||
port: actualPort,
|
||||
async close() {
|
||||
monitoringService.close();
|
||||
sshPool.closeAll('shutdown');
|
||||
wsHub.close();
|
||||
await configManager.close();
|
||||
await new Promise<void>((resolve) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue