backup engine fixes: shared-repo unlock, non-root uid reads, lock release, local-path repo, backblaze key id

This commit is contained in:
jarek committed 2026-07-31 10:10:55 +02:00
1 parent e700b5b6aa
commit e3c07f32da
15 files changed
+375 -313

No files matched your search

+3 -29
View File
@@ -4,26 +4,12 @@ import { authorize } from '$lib/server/authorize';
import { auditBackup } from '$lib/server/audit';
import {
getBackupConfigs,
createBackupConfig,
getBackupDestination,
getEnvironment
createBackupConfig
} from '$lib/server/db';
import { registerSchedule, isValidCron } from '$lib/server/scheduler';
import { assertStackBackupable } from '$lib/server/backups';
import { validateRetention, retentionToStore } from '$lib/server/backups/helpers';
// Inlined to avoid pulling lucide-svelte / svelte runtime into the server bundle
// via $lib/utils/backup. Keep in sync with that module if the predicates change.
function isLocalRepo(repository: string): boolean {
return repository.startsWith('/') || repository.startsWith('./');
}
function isRemoteEnv(connectionType?: string | null, host?: string | null): boolean {
if (!connectionType) return false;
if (connectionType === 'hawser-standard' || connectionType === 'hawser-edge') return true;
if (connectionType === 'direct' && !!host) return true;
return false;
}
export const GET: RequestHandler = async ({ url, cookies }) => {
const auth = await authorize(cookies);
if (auth.authEnabled && !await auth.can('backups', 'view')) {
@@ -92,20 +78,8 @@ export const POST: RequestHandler = async (event) => {
return json({ error: 'Access denied to this environment' }, { status: 403 });
}
// Refuse local repo + remote env: the restic helper runs on the REMOTE
// daemon, which has no access to Dockhand's filesystem. The backup would
// silently write to a wrong path on the remote host.
if (body.environmentId) {
const [dest, env] = await Promise.all([
getBackupDestination(body.destinationId),
getEnvironment(body.environmentId)
]);
if (dest && env && isRemoteEnv(env.connectionType, env.host) && isLocalRepo(dest.repository)) {
return json({
error: `Local repository "${dest.name}" cannot back up containers on remote environment "${env.name}". Use S3, REST, or another non-local backend.`
}, { status: 400 });
}
}
// A local-path repo is allowed on any env; if the target daemon can't see the
// repo (wrong-host mount) the backup fails loud via the helper's localRepoGuard.
// External stacks (no known compose path) can't be backed up usefully —
// see assertStackBackupable for the why.
+3 -19
View File
@@ -4,15 +4,12 @@ import { authorize } from '$lib/server/authorize';
import { auditBackup } from '$lib/server/audit';
import {
updateBackupConfig,
deleteBackupConfig,
getBackupDestination,
getEnvironment
deleteBackupConfig
} from '$lib/server/db';
import { registerSchedule, unregisterSchedule, isValidCron } from '$lib/server/scheduler';
import { isBackupRunning } from '$lib/server/backups';
import { validateRetention, retentionToStore, resolveEnabledOnScheduleChange } from '$lib/server/backups/helpers';
import { requireBackups, loadConfigGateEnv } from '$lib/server/backups/route-guards';
import { isLocalRepo, isRemoteEnvironment } from '$lib/shared/repo-predicates';
export const GET: RequestHandler = async ({ params, cookies }) => {
const auth = await authorize(cookies);
@@ -59,21 +56,8 @@ export const PUT: RequestHandler = async (event) => {
return json({ error: 'Cannot change the destination while a backup is running for this config' }, { status: 409 });
}
// Refuse local repo + remote env (same rule as POST). Effective destination
// is the body's value if supplied, otherwise the existing config's.
const effectiveDestId = body.destinationId ?? existing.destinationId;
const effectiveEnvId = existing.environmentId; // env is fixed at creation
if (effectiveEnvId) {
const [dest, env] = await Promise.all([
getBackupDestination(effectiveDestId),
getEnvironment(effectiveEnvId)
]);
if (dest && env && isRemoteEnvironment(env) && isLocalRepo(dest.repository)) {
return json({
error: `Local repository "${dest.name}" cannot back up containers on remote environment "${env.name}". Use S3, REST, or another non-local backend.`
}, { status: 400 });
}
}
// A local-path repo is allowed on any env; a wrong-host mount fails loud via
// the helper's localRepoGuard at run time.
// Auto-enable a config that transitions from manual (no schedule) to scheduled
// (a real cron) — otherwise a paused run-once config that the user edits to add a
@@ -7,25 +7,12 @@ import {
updateBackupDestination,
deleteBackupDestination,
decryptBackupDestination,
getBackupConfigs,
getEnvironment
getBackupConfigs
} from '$lib/server/db';
import { registerSchedule, unregisterSchedule } from '$lib/server/scheduler';
import { validatePolicySchedules, validateRepositoryForSave, validateFlags } from '$lib/server/backups/helpers';
import { destinationHasRunningBackup } from '$lib/server/backups';
// Inlined predicates (kept in sync with the config routes) — see
// configs/+server.ts. A local repo cannot back up a remote environment.
function isLocalRepo(repository: string): boolean {
return repository.startsWith('/') || repository.startsWith('./');
}
function isRemoteEnv(connectionType?: string | null, host?: string | null): boolean {
if (!connectionType) return false;
if (connectionType === 'hawser-standard' || connectionType === 'hawser-edge') return true;
if (connectionType === 'direct' && !!host) return true;
return false;
}
/**
* Single-destination response shape. envVars (decrypted cloud credentials) are
* ONLY included for callers with backups:manage — the edit form that pre-fills
@@ -98,23 +85,8 @@ export const PUT: RequestHandler = async (event) => {
const flagError = validateFlags(body.flags);
if (flagError) return json({ error: flagError }, { status: 400 });
// (audit #8) Changing the repository to a LOCAL path bypasses the config-route
// guard: refuse if any config on this destination targets a remote environment
// (the restic helper runs on the remote daemon, which can't see Dockhand's fs).
const effectiveRepository = body.repository !== undefined ? body.repository : existing.repository;
if (body.repository !== undefined && isLocalRepo(effectiveRepository)) {
const configs = await getBackupConfigs();
const envIds = [...new Set(
configs.filter((c: any) => c.destinationId === id && c.environmentId).map((c: any) => c.environmentId as number)
)];
const envs = await Promise.all(envIds.map((eid) => getEnvironment(eid)));
const remote = envs.find((env) => env && isRemoteEnv(env.connectionType, env.host));
if (remote) {
return json({
error: `Local repository cannot back up containers on remote environment "${remote.name}". Use S3, REST, or another non-local backend.`
}, { status: 400 });
}
}
// A local-path repo is allowed on any env; a wrong-host mount fails loud via
// the helper's localRepoGuard at backup/restore time.
try {
const updated = await updateBackupDestination(id, {
+5 -7
View File
@@ -10,7 +10,7 @@
import CronEditor from '$lib/components/cron-editor.svelte';
import { Package, Box, Layers, Search, Loader2, CheckCircle2, ArrowBigRight, Settings, Clock, Play } from 'lucide-svelte';
import EnvironmentIcon from '$lib/components/EnvironmentIcon.svelte';
import { getRepoTypeIcon, formatCron, runBackupAction, isRemoteEnvironment, type BackupFormState } from '$lib/utils/backup';
import { getRepoTypeIcon, formatCron, runBackupAction, type BackupFormState } from '$lib/utils/backup';
import { toast } from 'svelte-sonner';
import VolumePicker from '$lib/components/backup/VolumePicker.svelte';
import DestinationPicker from '$lib/components/backup/DestinationPicker.svelte';
@@ -27,7 +27,7 @@
let { open = $bindable(), onCreated, onRun }: Props = $props();
interface Environment { id: number; name: string; icon?: string; connectionType?: string; host?: string | null; }
interface Destination { id: number; name: string; repository: string; }
interface Destination { id: number; name: string; repository: string; hostPath?: string | null; }
interface ContainerItem { name: string; type: 'container' | 'stack'; envId: number; envName: string; envIcon?: string; volumes: VolumeInfo[]; }
let step = $state<1 | 2 | 3>(1);
@@ -176,11 +176,9 @@
const getDestIcon = getRepoTypeIcon;
const selectedDest = $derived(destinations.find(d => d.id === selectedDestId));
// Used by the DestinationPicker to disable local-path destinations when
// the chosen environment is remote (helper container on the remote host
// can't read Dockhand's local filesystem).
// Passed to DestinationPicker so it can hint that a local-path repo needs the
// env's daemon co-located with Dockhand (advisory only, not a block).
const selectedEnv = $derived(environments.find((e) => e.id === selectedItem?.envId));
const isSelectedEnvRemote = $derived(isRemoteEnvironment(selectedEnv));
</script>
<Dialog.Root bind:open onOpenChange={(isOpen) => { if (!isOpen && saving) return; open = isOpen; }}>
@@ -306,7 +304,7 @@
<DestinationPicker
destinations={destinations}
bind:value={selectedDestId}
disableLocalForRemoteEnv={isSelectedEnvRemote}
env={selectedEnv}
/>
</div>
+8 -9
View File
@@ -22,7 +22,7 @@
import { formatDateTime } from '$lib/stores/settings';
import { watchJob } from '$lib/utils/sse-fetch';
import ConfirmPopover from '$lib/components/ConfirmPopover.svelte';
import { getRepoTypeIcon, isLocalRepo, isRemoteEnvironment, parseRetention, parseOptions, retentionSummary as getRetentionSummary, formatCron, runBackupAction, classifyJobResult, tagLogLine, fetchBackupExecutions, type BackupAction, type BackupFormState } from '$lib/utils/backup';
import { getRepoTypeIcon, parseRetention, parseOptions, retentionSummary as getRetentionSummary, formatCron, runBackupAction, classifyJobResult, tagLogLine, fetchBackupExecutions, type BackupAction, type BackupFormState } from '$lib/utils/backup';
interface Props {
containerName: string;
@@ -37,7 +37,7 @@
let { containerName, volumes, type = 'container', environmentId, onConfigSaved, autoShowRunForm = false, onTally }: Props = $props();
interface Destination { id: number; name: string; repository: string; }
interface Destination { id: number; name: string; repository: string; hostPath?: string | null; }
interface BackupConfig {
id: number; destinationId: number; enabled: boolean; schedule: string;
retention: string | null; options?: string | null; stopBeforeBackup: boolean; selectedVolumes: string[] | null;
@@ -154,7 +154,9 @@
const progressEnv = $derived($environments.find((e) => e.id === envId));
const envId = $derived(environmentId ?? $currentEnvironment?.id ?? null);
const isCurrentEnvRemote = $derived(() => isRemoteEnvironment($currentEnvironment));
// The full env carries connectionType/host (currentEnvironment does not), needed
// for the local-repo guard.
const fullEnv = $derived($environments.find((e) => e.id === envId));
async function fetchDestinations() {
try {
@@ -327,11 +329,8 @@
*/
async function submitForm(action: BackupAction) {
if (!editDestinationId) { toast.error('Select a backup repository'); return; }
const selectedDest = destinations.find(d => d.id === editDestinationId);
if (selectedDest && isCurrentEnvRemote() && isLocalRepo(selectedDest.repository)) {
toast.error('Local repositories cannot be used with remote environments');
return;
}
// A local repo on a non-co-located env is allowed here; it fails loud at run
// time via the helper's localRepoGuard rather than being blocked up front.
// Cron only matters when the schedule will be persisted. run-once
// clears it server-side so the field value is irrelevant.
if ((action === 'save' || action === 'save-run') && editScheduleInvalid) {
@@ -596,7 +595,7 @@
<DestinationPicker
destinations={destinations}
bind:value={editDestinationId}
disableLocalForRemoteEnv={isCurrentEnvRemote()}
env={fullEnv}
triggerClass="h-9 w-[220px] flex-shrink-0"
/>
<CronEditor bind:value={editSchedule} bind:invalid={editScheduleInvalid} />
@@ -6,7 +6,7 @@
import { TogglePill } from '$lib/components/ui/toggle-pill';
import * as Tooltip from '$lib/components/ui/tooltip';
import { Input } from '$lib/components/ui/input';
import { Plus, Check, RefreshCw, Wifi, Database, HardDrive, Dices, Copy, ChevronDown, BarChart3, Loader2, Clock, PackageCheck, FolderCheck, Unlock, CircleHelp, AlertTriangle } from 'lucide-svelte';
import { Plus, Check, RefreshCw, Wifi, Database, HardDrive, Dices, Copy, BarChart3, Loader2, Clock, PackageCheck, FolderCheck, Unlock, CircleHelp, AlertTriangle } from 'lucide-svelte';
import cronstrue from 'cronstrue';
import { formatBytes } from '$lib/utils/format';
import CronEditor from '$lib/components/cron-editor.svelte';
@@ -20,8 +20,10 @@
id: number;
name: string;
repository: string;
hostPath?: string | null;
envVars?: Record<string, string>;
flags?: string;
policies?: string | null;
lastTestStatus?: string | null;
createdAt: string;
updatedAt: string;
@@ -122,7 +124,7 @@
fields: [
{ key: 'bucket', label: 'Bucket', placeholder: 'my-backup-bucket' },
{ key: 'path', label: 'Path (optional)', placeholder: 'dockhand' },
{ key: 'accountId', label: 'Account ID', placeholder: '', envKey: 'B2_ACCOUNT_ID' },
{ key: 'accountId', label: 'Key ID', placeholder: '', envKey: 'B2_ACCOUNT_ID' },
{ key: 'accountKey', label: 'Application key', placeholder: '', secret: true, envKey: 'B2_ACCOUNT_KEY' }
],
buildRepo: (f) => `b2:${f.bucket}:${f.path || ''}`.replace(/:$/, ''),
@@ -191,6 +193,11 @@
let policyAutoUnlock = $state(true);
let testing = $state(false);
let initializing = $state(false);
// Set when a Test on a NEW (unsaved) destination reports the repo reachable but
// not yet initialised. It flips the create button to "Save and init" so the user
// can save + init in one click. Reset whenever the repo fields change (a stale
// verdict must not drive the button after the user edits the target).
let needsInit = $state(false);
const selectedBackend = $derived(backendTypes.find(b => b.value === formBackendType) ?? backendTypes[0]);
const repoFields = $derived(selectedBackend.fields.filter(f => !f.envKey));
@@ -305,8 +312,12 @@
});
const data = await res.json();
if (data.success && data.needsInit) {
// Only the create flow gets the "Save and init" button; editing an
// existing destination already has its own "Init repo" action.
needsInit = !isEditing;
toast.info(data.message || 'Connection works, but repository needs initialization');
} else {
needsInit = false;
toast[res.ok && data.success ? 'success' : 'error'](data.success ? 'Connection test successful' : (data.error || 'Connection test failed'));
}
} catch { toast.error('Connection test failed'); } finally { testing = false; }
@@ -342,6 +353,12 @@
currentRepo; // track
repoConflictName = null;
});
// Any change to the target (repo string OR credentials OR password) invalidates a
// prior Test verdict, so the "Save and init" state must not linger.
$effect(() => {
currentRepo; formFields; formPassword; // track
needsInit = false;
});
async function save() {
if (!formName.trim()) { formError = 'Name is required'; return; }
@@ -389,7 +406,10 @@
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)
});
if (res.ok) { open = false; onSaved(); toast.success(isEditing ? 'Destination updated' : 'Destination created'); }
// Creating a destination auto-initialises its repository server-side, so a
// create over a reachable-but-uninitialised repo (needsInit) needs no extra
// call here — the button just tells the user that up front ("Save and init").
if (res.ok) { open = false; onSaved(); toast.success(needsInit ? 'Destination created and repository initialized' : isEditing ? 'Destination updated' : 'Destination created'); }
else { const data = await res.json(); formError = data.error || 'Failed'; }
} catch { formError = 'Failed'; } finally { formSaving = false; }
}
@@ -408,7 +428,7 @@
{#if selectedBackend.value === 'local'}
<div class="flex items-start gap-2 p-2.5 mt-4 rounded-md bg-amber-500/10 border border-amber-500/20 text-xs text-amber-600 dark:text-amber-400">
<HardDrive class="w-3.5 h-3.5 shrink-0 mt-0.5" />
<span>Local path repositories can only be used to back up and restore containers running on the same Docker host as Dockhand. For remote environments or cross-env restore, use S3, REST, or another remote backend.</span>
<span>Local path repositories work on the local Docker host, or a co-located socket-proxy on that same host. For remote hosts, use S3, REST, or another remote backend.</span>
</div>
{/if}
<div class="grid grid-cols-2 gap-6 py-4">
@@ -508,6 +528,7 @@
</div>
</div>
<!-- Policies section -->
<div class="border-t pt-3 mt-2 space-y-3">
<span class="text-xs font-medium text-muted-foreground uppercase tracking-wide">Repository policies</span>
@@ -657,7 +678,7 @@
</div>
{/if}
<Dialog.Footer class="flex-shrink-0 border-t mt-auto pt-4">
<div class="flex gap-2 mr-auto">
<div class="flex items-center gap-2 mr-auto">
<Button variant="outline" size="sm" onclick={testConnection} disabled={testing}>
{#if testing}<RefreshCw class="w-4 h-4 mr-1 animate-spin" />{:else}<Wifi class="w-4 h-4 mr-1" />{/if}
Test
@@ -667,12 +688,14 @@
{#if initializing}<RefreshCw class="w-4 h-4 mr-1 animate-spin" />{:else}<Database class="w-4 h-4 mr-1" />{/if}
Init repo
</Button>
{:else if needsInit}
<span class="text-xs text-muted-foreground">Connection OK — repository will be initialized on save.</span>
{/if}
</div>
<Button variant="outline" onclick={() => { open = false; onClose(); }}>Cancel</Button>
<Button onclick={save} disabled={formSaving} variant={repoConflictName ? 'destructive' : 'default'}>
{#if formSaving}<RefreshCw class="w-4 h-4 mr-1 animate-spin" />{:else if repoConflictName}<AlertTriangle class="w-4 h-4 mr-1" />{:else if isEditing}<Check class="w-4 h-4" />{:else}<Plus class="w-4 h-4" />{/if}
{repoConflictName ? 'Save anyway' : isEditing ? 'Save' : 'Create'}
{#if formSaving}<RefreshCw class="w-4 h-4 mr-1 animate-spin" />{:else if repoConflictName}<AlertTriangle class="w-4 h-4 mr-1" />{:else if needsInit && !isEditing}<Database class="w-4 h-4 mr-1" />{:else if isEditing}<Check class="w-4 h-4" />{:else}<Plus class="w-4 h-4" />{/if}
{repoConflictName ? 'Save anyway' : needsInit && !isEditing ? 'Create and init' : isEditing ? 'Save' : 'Create'}
</Button>
</Dialog.Footer>
</Dialog.Content>
@@ -5,7 +5,7 @@
import * as Select from '$lib/components/ui/select';
import CronEditor from '$lib/components/cron-editor.svelte';
import { Box, Layers, Loader2, Plus, ChevronDown, ChevronRight, ArrowRight, Zap, GitBranch } from 'lucide-svelte';
import { formatCron, getRepoTypeIcon, isLocalRepo, isRemoteEnvironment } from '$lib/utils/backup';
import { formatCron, getRepoTypeIcon } from '$lib/utils/backup';
import { standaloneContainers, volumesForStack, type BackupItem } from '$lib/utils/mounts';
import BackupPanel from '../../containers/BackupPanel.svelte';
@@ -36,8 +36,9 @@
let batchDestId = $state<number>(0);
let batchSchedule = $state('0 2 * * *');
let batchSaving = $state(false);
const isRemote = $derived(isRemoteEnvironment({ connectionType, host }));
const usableDestinations = $derived(isRemote ? destList.filter(d => !isLocalRepo(d.repository)) : destList);
// All destinations are selectable; a local repo on a non-co-located env fails
// loud at run time (helper localRepoGuard), not hidden here.
const usableDestinations = $derived(destList);
// External stacks can't be backed up — exclude them from the unconfigured count
// and from "Schedule all" so the number reflects only backup-able targets. The
// list mixes stacks and standalone containers, and "Schedule all" configs BOTH,