dorfteich/apps/api/src/config/secret-store.ts
Claude Fable 5 8dbff86537
All checks were successful
CI / Lint, typecheck, test (push) Successful in 3m9s
CI / Build container images (push) Has been skipped
CD / Build and push images (push) Successful in 3m47s
CD / Deploy to Test (push) Successful in 8s
CD / Smoke tests against Test (push) Successful in 1m10s
CD / Promote to Int (push) Successful in 9s
CI / Auth e2e pack (push) Successful in 5m25s
CI / Import/export fidelity gate (push) Successful in 45s
Add backup sidecar: nightly dump, volume archive, prune, status, restore (#83)
New apps/backup service (ADR 0015): nightly pg_dump -Fc plus one tar of the
uploads/plugins volumes as a consistent restore set on a new backups volume,
retention prune that never removes the newest complete set, atomic
status.json for the readiness/admin consumers (#85/#86), and a failure mail
sent directly via nodemailer (the api may be the broken part) with de/en
texts in the shared mails catalog. BACKUP_RUN_ONCE=1 gives the on-demand
path; deploy/backup/restore.sh automates the documented restore runbook.
The pure secret-store helpers moved to @dorfteich/shared so the sidecar
resolves the wizard-written SMTP relay exactly like the api.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EwZ4jR4KFAPvpjWevfUGX1
2026-07-11 17:29:15 +02:00

39 lines
1.6 KiB
TypeScript

import { existsSync, readFileSync } from 'node:fs';
import { chmod, mkdir, rename, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
import { parseSecretsFile, serializeSecrets } from '@dorfteich/shared';
/**
* File I/O for the env-backed secret store (security.md §Secrets, issue #80).
* The pure format/merge helpers (`parseSecretsFile`, `serializeSecrets`,
* `overlayEnv`) moved to `@dorfteich/shared` in issue #83 so the backup
* sidecar can read the same store; they are re-exported here to keep the
* api-internal import paths stable.
*/
export { overlayEnv, parseSecretsFile, serializeSecrets } from '@dorfteich/shared';
/** Reads the store file; a missing file is an empty store, not an error. */
export function readSecretsFile(path: string): Record<string, string> {
if (!existsSync(path)) return {};
return parseSecretsFile(readFileSync(path, 'utf8'));
}
/**
* Merges entries into the store file atomically (staging file + rename, so a
* crash mid-write never leaves a torn file) and keeps it owner-only readable.
*/
export async function writeSecretsFile(
path: string,
entries: Record<string, string>,
): Promise<void> {
const merged = { ...readSecretsFile(path), ...entries };
await mkdir(dirname(path), { recursive: true });
const staging = `${path}.tmp-${process.pid}`;
await writeFile(staging, serializeSecrets(merged), { mode: 0o600 });
await rename(staging, path);
// rename preserves the staging file's mode, but be explicit in case a
// pre-existing file with looser permissions was replaced on some platforms.
await chmod(path, 0o600);
}