TLS backup repositories with a private CA and optional mutual TLS

This commit is contained in:
Jarek Krochmalski committed 2026-08-23 10:20:59 +02:00
1 parent f6ffadbacb
commit 7692758467
13 files changed
+7634 -17

No files matched your search

@@ -33,6 +33,11 @@ function prepareDestination(dest: any, opts: { includeEnvVars: boolean }): any {
} else {
delete result.envVars;
}
// TLS cert PEMs never reach the client; expose only whether each is set.
result.hasCacert = !!dest.cacert;
result.hasTlsClientCert = !!dest.tlsClientCert;
delete result.cacert;
delete result.tlsClientCert;
return result;
}
@@ -60,7 +65,7 @@ export const GET: RequestHandler = async ({ cookies }) => {
* @openapi
* summary: Create a restic backup destination, auto-initialize and test the repository, and register its default maintenance schedules
* description: Permission denial (403, "backups:manage") is produced by the shared requireBackups route guard.
* body: {name:string!, repository:string!, password:string!, envVars:{}, flags:string, hostPath:string, policies:string}
* body: {name:string!, repository:string!, password:string!, envVars:{}, flags:string, hostPath:string, cacert:string, tlsClientCert:string, policies:string}
* body-example: {"name":"S3 Offsite","repository":"s3:s3.amazonaws.com/my-bucket/restic","password":"***","envVars":{"AWS_ACCESS_KEY_ID":"***","AWS_SECRET_ACCESS_KEY":"***"}}
* resp-201: The created backup destination object (includes decrypted envVars since the caller just supplied them; password is stripped)
* resp-400: Invalid input — missing name/repository/password, unsupported/SSRF-blocked repository, invalid restic flags, or an invalid cron schedule in the policies
@@ -115,6 +120,8 @@ export const POST: RequestHandler = async (event) => {
envVars: body.envVars ? JSON.stringify(body.envVars) : null,
flags: flagsColumn,
hostPath: body.hostPath ?? null,
cacert: body.cacert || null,
tlsClientCert: body.tlsClientCert || null,
policies: body.policies ?? defaultPolicies
});
@@ -29,6 +29,13 @@ function prepareDestination(dest: any, includeSecrets: boolean): any {
} else {
delete result.envVars;
}
// TLS certs (PEM) are never sent to the client - not even the ciphertext, and not even
// to a manage caller (like the password). The edit form only needs to know one IS set so
// it can show "leave blank to keep", so expose booleans and strip the values.
result.hasCacert = !!dest.cacert;
result.hasTlsClientCert = !!dest.tlsClientCert;
delete result.cacert;
delete result.tlsClientCert;
// Split the stored `flags` JSON into separate fields the edit form binds to (legacy
// bare strings surface as backupFlags), so the UI never parses the string-vs-JSON column.
const { backup, restore } = parseBackupFlags(dest.flags);
@@ -72,7 +79,7 @@ export const GET: RequestHandler = async ({ params, cookies }) => {
* summary: Update a backup destination, re-validating repository and flags when supplied and re-registering maintenance schedules when policies change
* description: Permission denial (403, "backups:manage") is produced by the shared requireBackups route guard.
* path: id:integer! Backup destination id (from GET /api/backup/destinations)
* body: {name:string, repository:string, password:string, envVars:{}, flags:string, hostPath:string, policies:string}
* body: {name:string, repository:string, password:string, envVars:{}, flags:string, hostPath:string, cacert:string, tlsClientCert:string, policies:string}
* body-example: {"name":"S3 Offsite (renamed)","policies":"{\"pruneEnabled\":true,\"pruneSchedule\":\"0 0 1 * *\"}"}
* resp-200: The updated backup destination object (password stripped, envVars echoed back to the managing caller)
* resp-400: Invalid input — invalid id, unsupported/SSRF-blocked repository, invalid restic flags, invalid policy cron, or switching to a local repository used by a remote-environment config
@@ -134,6 +141,8 @@ export const PUT: RequestHandler = async (event) => {
envVars: body.envVars !== undefined ? JSON.stringify(body.envVars) : undefined,
flags: flagsColumn,
hostPath: body.hostPath,
cacert: body.cacert,
tlsClientCert: body.tlsClientCert,
policies: body.policies
});
if (!updated) return json({ error: 'Update failed' }, { status: 500 });
@@ -5,7 +5,7 @@ import { requireBackups } from '$lib/server/backups/route-guards';
import { getBackupDestination, decryptBackupDestination, updateBackupDestinationTestStatus } from '$lib/server/db';
import { spawn } from 'child_process';
import { buildResticEnv, cleanErrorMsg, isRepoNotInitializedError, RESTIC_EXIT_REPO_NOT_FOUND, validateRepositoryForSave } from '$lib/server/backups/helpers';
import { withGcsCredFile } from '$lib/server/backups/restic';
import { withGcsCredFile, withTlsCertFiles } from '$lib/server/backups/restic';
/**
* Test a backup destination configuration.
@@ -15,7 +15,7 @@ import { withGcsCredFile } from '$lib/server/backups/restic';
* @openapi
* summary: Test a backup repository — either a saved destination (by destinationId) or inline credentials supplied for a not-yet-saved destination
* description: Provide `destinationId` to test a saved destination using its stored credentials, or omit it and supply `repository`/`password`/`envVars` to test before saving. Permission denial (403, "backups:manage") is produced by the shared requireBackups route guard. destinationId from GET /api/backup/destinations.
* body: {destinationId:integer, repository:string, password:string, envVars:{}}
* body: {destinationId:integer, repository:string, password:string, envVars:{}, cacert:string, tlsClientCert:string}
* body-example: {"repository":"s3:s3.amazonaws.com/my-bucket/restic","password":"***","envVars":{"AWS_ACCESS_KEY_ID":"***","AWS_SECRET_ACCESS_KEY":"***"}}
* resp-200: Test result — { success: true } when the repository is reachable, or { success: false, needsInit?, error } otherwise
* resp-200-example: {"success":true}
@@ -32,6 +32,9 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
let repository: string;
let password: string;
let envVars: Record<string, string> = {};
// TLS certs must reach restic here too, or testing a saved self-signed/private-CA
// destination fails the handshake even though its backups succeed (they apply the cert).
let certs: { cacert: string | null; clientCert: string | null } = { cacert: null, clientCert: null };
if (body.destinationId) {
// Use saved destination from DB
@@ -41,6 +44,7 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
repository = dest.repository;
password = decrypted.decryptedPassword;
envVars = decrypted.decryptedEnvVars;
certs = { cacert: decrypted.decryptedCacert, clientCert: decrypted.decryptedTlsClientCert };
} else {
// Inline test (before save)
if (!body.repository || !body.password) {
@@ -60,12 +64,17 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
if (typeof value === 'string') envVars[key] = value;
}
}
// Pre-save test of a private-CA backend: take the PEMs straight from the body.
certs = {
cacert: typeof body.cacert === 'string' && body.cacert ? body.cacert : null,
clientCert: typeof body.tlsClientCert === 'string' && body.tlsClientCert ? body.tlsClientCert : null
};
}
const env = buildResticEnv(process.env, { repository, password, envVars });
try {
const result = await withGcsCredFile(env, (runEnv) => new Promise<{ code: number; stdout: string; stderr: string }>((resolve) => {
const result = await withTlsCertFiles(certs, env, (tlsEnv) => withGcsCredFile(tlsEnv, (runEnv) => new Promise<{ code: number; stdout: string; stderr: string }>((resolve) => {
const proc = spawn('restic', ['snapshots', '--json', '--no-lock', '--latest', '1'], { env: runEnv, timeout: 30000 });
let stdout = '';
let stderr = '';
@@ -73,7 +82,7 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
proc.stderr.on('data', (d) => { stderr += d; });
proc.on('close', (code) => resolve({ code: code ?? 1, stdout, stderr }));
proc.on('error', (err) => resolve({ code: 1, stdout: '', stderr: err.message }));
}));
})));
if (result.code === 0) {
if (body.destinationId) await updateBackupDestinationTestStatus(body.destinationId, 'success');
@@ -25,6 +25,8 @@
repository: string;
hostPath?: string | null;
envVars?: Record<string, string>;
hasCacert?: boolean; // a CA cert is stored (the PEM itself is never sent back)
hasTlsClientCert?: boolean; // a client cert is stored
flags?: string;
backupFlags?: string; // split from the GET endpoint (legacy strings surface here)
restoreFlags?: string;
@@ -207,6 +209,29 @@
}
input.value = ''; // let the same file be re-picked
}
// TLS certs (PEM). Sent as content to the server, which stores them encrypted and
// NEVER sends the PEM back (only hasCacert/hasTlsClientCert booleans). So on edit
// these start blank = "keep the stored cert"; an explicit clear zeroes them out.
let formCacert = $state('');
let formTlsClientCert = $state('');
let hadCacert = $state(false); // a cert was already stored when the modal opened
let hadTlsClientCert = $state(false);
let caFileInput = $state<HTMLInputElement | null>(null);
let clientCertFileInput = $state<HTMLInputElement | null>(null);
async function uploadPem(which: 'ca' | 'client', e: Event) {
const input = e.target as HTMLInputElement;
const file = input.files?.[0];
if (!file) return;
try {
const text = await file.text();
if (which === 'ca') formCacert = text; else formTlsClientCert = text;
} catch {
toast.error('Could not read the selected file');
}
input.value = '';
}
let formPassword = $state('');
let formBackupFlags = $state('');
let formRestoreFlags = $state('');
@@ -282,6 +307,7 @@
function resetForm() {
formName = ''; formBackendType = 'local'; formFields = {}; formPassword = '';
formBackupFlags = ''; formRestoreFlags = ''; formError = '';
formCacert = ''; formTlsClientCert = ''; hadCacert = false; hadTlsClientCert = false;
formSaving = false;
policyPruneEnabled = true; policyPruneSchedule = '0 0 1 * *'; policyPruneMaxUnused = '10';
policyCheckEnabled = true; policyCheckSchedule = '0 0 1 * *';
@@ -316,6 +342,11 @@
// (legacy bare strings surface as backupFlags), so the form binds them directly.
formBackupFlags = destination.backupFlags ?? '';
formRestoreFlags = destination.restoreFlags ?? '';
// PEMs are never returned; start blank and remember one is stored so the
// UI can say "leave blank to keep" and offer a Clear button.
formCacert = ''; formTlsClientCert = '';
hadCacert = !!destination.hasCacert;
hadTlsClientCert = !!destination.hasTlsClientCert;
formError = '';
// Load policies
const pol = destination.policies ? (() => { try { return JSON.parse(destination.policies); } catch { return {}; } })() : {};
@@ -380,7 +411,13 @@
// instance role) that hangs on a retry loop instead of failing fast.
const repository = selectedBackend.buildRepo(formFields);
const envVars = collectEnvVars();
payload = { repository, password: formPassword, envVars: Object.keys(envVars).length > 0 ? envVars : undefined };
payload = {
repository, password: formPassword,
envVars: Object.keys(envVars).length > 0 ? envVars : undefined,
// Carry the TLS PEMs so a pre-save test of a private-CA backend can validate.
cacert: formCacert.trim() || undefined,
tlsClientCert: formTlsClientCert.trim() || undefined
};
}
const res = await fetch('/api/backup/destinations/test', {
method: 'POST',
@@ -486,6 +523,15 @@
policies: JSON.stringify(policies)
};
if (formPassword || !isEditing) body.password = formPassword || undefined;
// TLS certs, with keep/clear semantics on edit:
// typed value -> send it (set/replace)
// blank + one stored -> omit (server keeps the existing cert)
// explicitly cleared (blank + none was stored, or user hit Clear) -> send ''
// On create, blank just means "no cert" so an empty string is fine to omit.
if (formCacert.trim()) body.cacert = formCacert;
else if (isEditing && !hadCacert) body.cacert = '';
if (formTlsClientCert.trim()) body.tlsClientCert = formTlsClientCert;
else if (isEditing && !hadTlsClientCert) body.tlsClientCert = '';
const res = await fetch(isEditing ? `/api/backup/destinations/${destination!.id}` : '/api/backup/destinations', {
method: isEditing ? 'PUT' : 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body)
});
@@ -681,6 +727,63 @@
</div>
</div>
<!-- TLS certificates: for any backend that speaks TLS with a private CA (self-hosted
rest-server, MinIO/S3, rclone-over-https). Optional; blank = use the system trust
store, no client cert. Shown for all backend types (someone may run MinIO with a
private CA). PEM content, uploaded or pasted. -->
<div class="border-t pt-3 mt-2 space-y-3">
<span class="text-xs font-medium text-muted-foreground uppercase tracking-wide">TLS certificates (optional)</span>
<p class="text-xs text-muted-foreground -mt-1">
For a backend served over HTTPS with a private/self-signed CA. Leave blank to use the system trust store.
</p>
<div class="grid grid-cols-2 gap-6">
<div class="space-y-1">
<div class="flex items-center justify-between">
<Label for="dest-cacert">CA certificate</Label>
<div class="flex gap-1">
{#if isEditing && hadCacert && !formCacert}
<Button variant="outline" size="sm" class="h-7 px-2 text-xs text-destructive" onclick={() => { hadCacert = false; }}>Clear</Button>
{/if}
<Button variant="outline" size="sm" class="h-7 px-2 text-xs" onclick={() => caFileInput?.click()}>
<Upload class="mr-1 h-3 w-3" />Upload
</Button>
</div>
</div>
<input type="file" accept=".pem,.crt,.cer,.ca,application/x-pem-file,text/plain" class="hidden" bind:this={caFileInput} onchange={(e) => uploadPem('ca', e)} />
<Textarea
id="dest-cacert"
rows={4}
bind:value={formCacert}
placeholder={isEditing && hadCacert ? '(a CA certificate is stored - leave blank to keep it)' : '-----BEGIN CERTIFICATE-----'}
class="field-sizing-fixed max-h-40 resize-y overflow-auto font-mono text-xs"
/>
<p class="text-xs text-muted-foreground">Verifies the backend's TLS certificate (restic <code class="font-mono">RESTIC_CACERT</code>).</p>
</div>
<div class="space-y-1">
<div class="flex items-center justify-between">
<Label for="dest-client-cert">Client certificate (mTLS)</Label>
<div class="flex gap-1">
{#if isEditing && hadTlsClientCert && !formTlsClientCert}
<Button variant="outline" size="sm" class="h-7 px-2 text-xs text-destructive" onclick={() => { hadTlsClientCert = false; }}>Clear</Button>
{/if}
<Button variant="outline" size="sm" class="h-7 px-2 text-xs" onclick={() => clientCertFileInput?.click()}>
<Upload class="mr-1 h-3 w-3" />Upload
</Button>
</div>
</div>
<input type="file" accept=".pem,.crt,.cer,.key,application/x-pem-file,text/plain" class="hidden" bind:this={clientCertFileInput} onchange={(e) => uploadPem('client', e)} />
<Textarea
id="dest-client-cert"
rows={4}
bind:value={formTlsClientCert}
placeholder={isEditing && hadTlsClientCert ? '(a client certificate is stored - leave blank to keep it)' : 'certificate + private key in one PEM'}
class="field-sizing-fixed max-h-40 resize-y overflow-auto font-mono text-xs"
/>
<p class="text-xs text-muted-foreground">Certificate + key in one PEM for mutual TLS (restic <code class="font-mono">RESTIC_TLS_CLIENT_CERT</code>).</p>
</div>
</div>
</div>
<!-- Policies section -->
<div class="border-t pt-3 mt-2 space-y-3">