mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-18 22:36:19 +00:00
feat(recovery): add safe-mode recovery surface and emergency CLI (#1286)
* feat(recovery): add safe-mode recovery surface and emergency CLI Add a read-only Recovery tab under Settings (admin-only) backed by a new GET /api/diagnostics endpoint reporting app version, database integrity, encryption-key status, Docker reachability, account and SSO counts, and non-secret configuration. The endpoint loads without Docker or live metrics so it stays available when the dashboard does not, requires a genuine admin session, and builds its config block from a non-secret allowlist so no credentials are ever exposed. Expand the emergency command-line toolkit beyond the two-factor reset with seven host-level commands: reset-password, create-emergency-admin, clear-sessions, disable-sso, diagnostics, validate-db, and backup-data. Each prints its result, exits with a meaningful status code, and writes an audit entry where it changes state. Document the toolkit in a new operator guide and link it from the recovery and two-factor pages. * feat(recovery): download the emergency command reference as a text file The recovery commands are needed exactly when the dashboard is unreachable, so reading them only in-app is a chicken-and-egg problem. Add a Download button to the command-line section that saves the full `docker compose exec sencho ...` reference as a text file, letting operators keep it on hand before they need it. Reuses a shared download helper with the existing diagnostics export. * fix(recovery): harden diagnostics, backup, and emergency-admin against edge cases Address findings from an independent review of the recovery toolkit: - DiagnosticsService now degrades instead of throwing when a queried table is missing or corrupt: each read falls back and is folded into database.ok, so a broken database reports "problem detected" rather than failing the whole endpoint or showing a misleading healthy state with zeroed counts. - backup-data refuses a destination that resolves to the live database, which would otherwise report success while producing no separate copy. - create-emergency-admin now applies the same username rule as the user- management route, extracted to a shared helper so both stay in sync. Adds tests for a missing read table, a malformed emergency-admin username, and the backup same-target rejection.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Emergency CLI: validate that the database and encryption key are intact.
|
||||
* Runs a SQLite integrity check, confirms the core tables exist, confirms the
|
||||
* encryption key is present and usable, and confirms at least one admin exists.
|
||||
* Exits non-zero if any check fails, so it can gate a restore decision in a
|
||||
* script. Read-only.
|
||||
*
|
||||
* Run via:
|
||||
* docker compose exec sencho node dist/cli/validateDb.js
|
||||
*/
|
||||
import { collectDiagnostics } from '../services/DiagnosticsService';
|
||||
import { exitWith, type CliResult } from './_shared';
|
||||
|
||||
export async function validateDb(): Promise<CliResult> {
|
||||
const report = await collectDiagnostics();
|
||||
const problems: string[] = [];
|
||||
|
||||
if (!report.database.ok) {
|
||||
problems.push(`database integrity: ${report.database.integrity}`);
|
||||
if (report.database.missingTables.length > 0) {
|
||||
problems.push(`missing core tables: ${report.database.missingTables.join(', ')}`);
|
||||
}
|
||||
}
|
||||
if (!report.encryptionKey.present) {
|
||||
problems.push('encryption.key is missing');
|
||||
} else if (!report.encryptionKey.valid) {
|
||||
problems.push('encryption.key is present but invalid');
|
||||
}
|
||||
if (report.auth.adminCount === 0) {
|
||||
problems.push('no admin users exist');
|
||||
}
|
||||
|
||||
if (problems.length > 0) {
|
||||
return { ok: false, message: `Validation failed:\n - ${problems.join('\n - ')}` };
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
message: `Database OK (integrity ok, ${report.auth.adminCount} admin(s), encryption key valid).`,
|
||||
};
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
exitWith(await validateDb());
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
void main();
|
||||
}
|
||||
Reference in New Issue
Block a user