From bbc915b22a12c90bbe15170ddee498c5ecb2862c Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:24:29 +0100 Subject: [PATCH 1/5] feat(restore): put a node identity backup back from the app instead of the command line Desktop could export a .cpbackup file but could only put one back with cmd/cpbackup and manual docker steps. This adds the restore behind the app, with the web's safety rules: it only creates (no container of the service, no identity volume), passphrase files only, a file this app did not export needs the service name typed, a foreign file is refused while the machine it names still runs the service, and the container is created without starting, written, read back and compared against the backup before it starts. A restore that did not finish holds the service so Deploy and Start refuse it until the partial restore is deleted. The export now records each file's SHA-256 so its own files restore without the typed name. The manifest code is checked against vectors the web's own state_backup code wrote. --- app.go | 8 + internal/runtime/restore.go | 573 ++++++++++++++++++ internal/runtime/restore_live_test.go | 119 ++++ internal/runtime/restore_test.go | 411 +++++++++++++ internal/runtime/runtime.go | 13 + internal/services/manager.go | 4 + internal/services/restore.go | 189 ++++++ internal/services/routes.go | 6 + internal/statebackup/manifest.go | 222 +++++++ internal/statebackup/manifest_test.go | 204 +++++++ .../testdata/gen_restore_vectors.py | 124 ++++ .../testdata/web_restore_vectors.json | 400 ++++++++++++ internal/store/state_backups.go | 117 ++++ internal/store/store.go | 6 + restore_binding.go | 351 +++++++++++ restore_binding_test.go | 362 +++++++++++ state_backup_binding.go | 8 + 17 files changed, 3117 insertions(+) create mode 100644 internal/runtime/restore.go create mode 100644 internal/runtime/restore_live_test.go create mode 100644 internal/runtime/restore_test.go create mode 100644 internal/services/restore.go create mode 100644 internal/statebackup/manifest.go create mode 100644 internal/statebackup/manifest_test.go create mode 100644 internal/statebackup/testdata/gen_restore_vectors.py create mode 100644 internal/statebackup/testdata/web_restore_vectors.json create mode 100644 restore_binding.go create mode 100644 restore_binding_test.go diff --git a/app.go b/app.go index e5f211b1..59da079d 100644 --- a/app.go +++ b/app.go @@ -54,6 +54,14 @@ type App struct { // it is a field so the export path can be driven in a test, which has no window // for a native dialog to open in. saveDialog func(wailsruntime.SaveDialogOptions) (string, error) + // openDialog is saveDialog's twin for choosing a file to read (a backup to + // restore). Zero value means the real Wails dialog. + openDialog func(wailsruntime.OpenDialogOptions) (string, error) + // restoreFiles are the backup files the restore preview checked, by token, until + // one is restored or restoreFileTTL passes (restore_binding.go). Guarded by + // restoreMu. + restoreMu sync.Mutex + restoreFiles map[string]restoreFile // importMu serializes ApplyImport's read-merge-write of stored credentials. importMu sync.Mutex // fleetKey is the fleet bearer token held in memory for the per-request auth diff --git a/internal/runtime/restore.go b/internal/runtime/restore.go new file mode 100644 index 00000000..490d2090 --- /dev/null +++ b/internal/runtime/restore.go @@ -0,0 +1,573 @@ +package runtime + +import ( + "bytes" + "context" + "crypto/rand" + "encoding/hex" + "errors" + "fmt" + "io" + "path" + "regexp" + "sort" + "strings" + + "github.com/GeiserX/CashPilot-Desktop/internal/catalog" + "github.com/GeiserX/CashPilot-Desktop/internal/statebackup" + cerrdefs "github.com/containerd/errdefs" + "github.com/moby/moby/client" +) + +// Restore from a backup file (issue #298), the Desktop port of the web's restore +// (GeiserX/CashPilot #741, orchestrator.restore_state). +// +// Only into nothing: new volumes, a new container that is created and not started, +// the backup written into the volumes, read back and compared file by file, and only +// then started. Never over a live identity, and nothing half-restored is left +// running. Desktop talks to its own Docker daemon directly, so the write is a plain +// archive copy into the created container: Docker writes into a stopped container's +// volumes. The web goes through a helper container only because its socket proxy +// allows archive writes into one folder. + +// LabelRestore marks a volume a restore created, with a random value only that +// restore knows. Docker names a volume and keeps no other identity, so this is how +// the undo, and a later Delete the partial restore, tell the restore's own volume +// from one another program made under the same name. Same label as the web. +const LabelRestore = "cashpilot.restore" + +// volumeName is a literal named volume, never ${VAR} or a path. Same rule as the +// web's state_backup.restore_volumes. +var volumeName = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_.-]*$`) + +// RestoreTargets maps each critical target of svc to the named volume a restore +// creates for it. It refuses a service with a critical folder that is not on a named +// volume the catalog names literally (a host folder: Storj, Sia keep terabytes +// there, and a restore cannot prove a host folder is new), and one that declares +// nothing critical. +func RestoreTargets(svc catalog.Service) (map[string]string, error) { + critical := CriticalTargets(svc) + if len(critical) == 0 { + return nil, fmt.Errorf("%s keeps nothing a backup restores", svc.Name) + } + sources := map[string]string{} + for _, mapping := range svc.Docker.Volumes { + parts := strings.Split(mapping, ":") + if len(parts) >= 2 { + sources[normaliseMountTarget(parts[1])] = parts[0] + } + } + found := map[string]string{} + folders := map[string]bool{} + for target := range critical { + source := sources[target] + if !volumeName.MatchString(source) { + return nil, fmt.Errorf("%s keeps its state in a host folder, so the app cannot restore it. Use the command line steps in the backup guide", svc.Name) + } + folder := statebackup.FolderName(target) + if folders[folder] { + return nil, fmt.Errorf("%s keeps two critical folders named %s, so a backup cannot tell them apart", svc.Name, folder) + } + folders[folder] = true + found[target] = source + } + return found, nil +} + +// RestoreRequest is one restore. Archives are statebackup.SplitArchives' output, +// keyed by each critical folder's last path component. +type RestoreRequest struct { + Spec DeploySpec + Archives map[string][]byte + // Hold records, before anything is made, that a restore of the slug is under way + // and which volumes it will create with which LabelRestore value. A restore that + // cannot record it makes nothing. The record outlives the app, so a crash + // mid-restore leaves a service that Deploy and Start refuse. + Hold func(volumes map[string]string) error + // Release lifts that record once every folder read back as the backup, right + // before the start. One that fails undoes the restore. + Release func() error + Progress func(string) +} + +// RestoreResult is what a restore made. +type RestoreResult struct { + ContainerID string `json:"containerId"` + Name string `json:"name"` + Image string `json:"image"` + Targets []string `json:"targets"` + Files int `json:"files"` + // Started is false when a fence-gated node waits for the LAN fence: its data is + // checked, and the fence guard starts it once the fence holds. + Started bool `json:"started"` + // Waiting says why a fence-gated node did not start yet. + Waiting string `json:"waiting,omitempty"` + // Left names what the undo of a failed restore could not remove: then the + // restored data may still be on this machine, and the hold stays. + Left []string `json:"left,omitempty"` +} + +// RestoreProvider is a runtime that can restore a backup. Only Docker can. +type RestoreProvider interface { + // RestoreRefusal says why slug cannot be restored on this machine now, or "". + RestoreRefusal(ctx context.Context, slug string, svc catalog.Service) (string, error) + RestoreState(ctx context.Context, req RestoreRequest) (RestoreResult, error) + // DiscardRestore removes what a restore that did not finish left: the service's + // container, and each volume that still carries the label value the restore made + // it with. A volume under the name without that value is another program's and + // stays. It returns what could not be removed. + DiscardRestore(ctx context.Context, slug string, volumes map[string]string) ([]string, error) +} + +// restoreClient is the part of the Docker client a restore needs, so a test can stand +// in for the daemon. +type restoreClient interface { + inspectClient + ContainerList(ctx context.Context, options client.ContainerListOptions) (client.ContainerListResult, error) + ContainerRemove(ctx context.Context, containerID string, options client.ContainerRemoveOptions) (client.ContainerRemoveResult, error) + VolumeInspect(ctx context.Context, volumeID string, options client.VolumeInspectOptions) (client.VolumeInspectResult, error) + VolumeCreate(ctx context.Context, options client.VolumeCreateOptions) (client.VolumeCreateResult, error) + VolumeRemove(ctx context.Context, volumeID string, options client.VolumeRemoveOptions) (client.VolumeRemoveResult, error) + CopyToContainer(ctx context.Context, containerID string, options client.CopyToContainerOptions) (client.CopyToContainerResult, error) + CopyFromContainer(ctx context.Context, containerID string, options client.CopyFromContainerOptions) (client.CopyFromContainerResult, error) +} + +// restoreSteps are the parts of a restore that need the whole provider: the deploy's +// create and the guarded start. A test replaces them. +type restoreSteps struct { + create func(ctx context.Context) (ContainerInfo, error) + // start starts the container; for a fence-gated one an error means it waits. + start func(ctx context.Context) error +} + +func (p *DockerProvider) RestoreRefusal(ctx context.Context, slug string, svc catalog.Service) (string, error) { + targets, err := RestoreTargets(svc) + if err != nil { + return err.Error(), nil + } + cli, err := dockerClient() + if err != nil { + return "", err + } + defer cli.Close() + return restoreRefusal(ctx, cli, slug, targets) +} + +func (p *DockerProvider) RestoreState(ctx context.Context, req RestoreRequest) (RestoreResult, error) { + cli, err := dockerClient() + if err != nil { + return RestoreResult{}, fmt.Errorf("nothing was restored: %w", err) + } + defer cli.Close() + name := containerName(req.Spec.Slug) + steps := restoreSteps{ + create: func(ctx context.Context) (ContainerInfo, error) { + spec := req.Spec + spec.CreateOnly, spec.Instance = true, 1 + return p.Deploy(ctx, spec, req.Progress) + }, + start: func(ctx context.Context) error { + return p.startGuarded(ctx, cli, name, func() error { + _, err := cli.ContainerStart(ctx, name, client.ContainerStartOptions{}) + return err + }) + }, + } + return p.restoreState(ctx, cli, req, steps) +} + +func (p *DockerProvider) DiscardRestore(ctx context.Context, slug string, volumes map[string]string) ([]string, error) { + cli, err := dockerClient() + if err != nil { + return nil, err + } + defer cli.Close() + return discardRestore(ctx, cli, slug, volumes) +} + +// restoreRefusal says why slug cannot be restored now, or "". Any container of the +// service (any copy, any state, one set aside, a proxied copy) or under its names +// refuses, and so does any of its critical volumes existing at all: a restore never +// writes over something that is there. A volume that cannot be checked counts as +// there. +func restoreRefusal(ctx context.Context, cli restoreClient, slug string, targets map[string]string) (string, error) { + listed, err := cli.ContainerList(ctx, client.ContainerListOptions{All: true, Filters: make(client.Filters).Add("label", LabelService+"="+slug)}) + if err != nil { + return "", fmt.Errorf("Docker would not list the containers on this machine: %w", err) + } + if len(listed.Items) > 0 { + return fmt.Sprintf("%s already has a container on this machine (%s). A restore only creates a new one: back it up, remove it and say yes to deleting its data, then restore", slug, strings.TrimPrefix(firstName(listed.Items[0].Names), "/")), nil + } + name := containerName(slug) + for _, candidate := range []string{name, name + setAsideSuffix} { + _, err := cli.ContainerInspect(ctx, candidate, client.ContainerInspectOptions{}) + if err == nil { + return fmt.Sprintf("a container named %s already exists on this machine", candidate), nil + } + if !cerrdefs.IsNotFound(err) { + return fmt.Sprintf("the container name %s could not be checked, so it counts as taken (%v)", candidate, err), nil + } + } + for _, target := range sortedNames(targets) { + volume := targets[target] + _, err := cli.VolumeInspect(ctx, volume, client.VolumeInspectOptions{}) + if err == nil { + return fmt.Sprintf("the volume %s already exists on this machine. A restore never writes into an existing volume: if it holds nothing you need, remove it, then restore", volume), nil + } + if !cerrdefs.IsNotFound(err) { + return fmt.Sprintf("the volume %s could not be checked, so it counts as there (%v)", volume, err), nil + } + } + return "", nil +} + +func sortedNames(m map[string]string) []string { + out := make([]string, 0, len(m)) + for k := range m { + out = append(out, k) + } + sort.Strings(out) + return out +} + +func newNonce() (string, error) { + b := make([]byte, 16) + if _, err := rand.Read(b); err != nil { + return "", err + } + return hex.EncodeToString(b), nil +} + +// restoreError is a restore that failed after it began making things, with what the +// undo left. +type restoreError struct { + msg string + left []string +} + +func (e *restoreError) Error() string { return e.msg } + +func (p *DockerProvider) restoreState(ctx context.Context, cli restoreClient, req RestoreRequest, steps restoreSteps) (RestoreResult, error) { + svc := req.Spec.Service + slug := req.Spec.Slug + name := containerName(slug) + targets, err := RestoreTargets(svc) + if err != nil { + return RestoreResult{}, err + } + byFolder := map[string]string{} + for target := range targets { + byFolder[statebackup.FolderName(target)] = target + } + if !sameKeys(byFolder, req.Archives) { + have := make([]string, 0, len(req.Archives)) + for k := range req.Archives { + have = append(have, k) + } + sort.Strings(have) + want := sortedNames(byFolder) + shown := strings.Join(have, ", ") + if shown == "" { + shown = "nothing" + } + return RestoreResult{}, fmt.Errorf("this backup holds %s, but %s keeps %s. It is not a backup of %s", shown, svc.Name, strings.Join(want, ", "), svc.Name) + } + expected := map[string]map[string]statebackup.ManifestEntry{} + writes := map[string][]byte{} + for folder, archive := range req.Archives { + target := byFolder[folder] + if expected[target], err = statebackup.TreeManifest(archive); err != nil { + return RestoreResult{}, err + } + if writes[target], err = statebackup.WritableArchive(archive); err != nil { + return RestoreResult{}, err + } + } + + if !p.restoring.TryLock() { + return RestoreResult{}, errors.New("nothing was restored: another restore is running. Try again when it ends") + } + defer p.restoring.Unlock() + + why, err := restoreRefusal(ctx, cli, slug, targets) + if err != nil { + return RestoreResult{}, fmt.Errorf("nothing was restored: %w", err) + } + if why != "" { + return RestoreResult{}, fmt.Errorf("nothing was restored: %s", strings.TrimSuffix(why, ".")) + } + + // The critical volumes are made new, and so is any other named volume the service + // mounts that is not there yet (Anyone's config), so the undo removes only volumes + // this restore proved it made. One that is already there is used as a deploy uses + // it, and never removed. + critical := map[string]bool{} + nonces := map[string]string{} + for _, volume := range targets { + critical[volume] = true + } + for _, m := range buildMounts(svc.Docker.Volumes, buildEnv(svc, req.Spec.Env)) { + if string(m.Type) != "volume" || critical[m.Source] { + continue + } + if _, err := cli.VolumeInspect(ctx, m.Source, client.VolumeInspectOptions{}); cerrdefs.IsNotFound(err) { + nonces[m.Source] = "" + } + } + for volume := range critical { + nonces[volume] = "" + } + for volume := range nonces { + if nonces[volume], err = newNonce(); err != nil { + return RestoreResult{}, fmt.Errorf("nothing was restored: %w", err) + } + } + if err := req.Hold(nonces); err != nil { + return RestoreResult{}, fmt.Errorf("nothing was restored: the app could not record that a restore of %s is under way (%v)", svc.Name, err) + } + + gated := svc.Docker.FenceGated + wasStopped := false + made := map[string]string{} + var createdID string + result := RestoreResult{Name: name, Targets: sortedNames(targets)} + failure := func() error { + if gated { + stopped, err := p.fence.userStopped() + if err != nil { + return fmt.Errorf("the app could not read which fence-gated nodes are held (%v)", err) + } + wasStopped = stopped[name] + // On disk before the container exists: the fence guard starts a created + // node that waits for the fence, and must never start this one + // half-written, now or after the app died mid-restore. + if err := p.fence.markStopped(name, true); err != nil { + return fmt.Errorf("the app could not hold %s until its data is checked (%v)", name, err) + } + } + for _, volume := range sortedNames(nonces) { + created, err := cli.VolumeCreate(ctx, client.VolumeCreateOptions{Name: volume, Labels: map[string]string{LabelRestore: nonces[volume]}}) + if err != nil { + return fmt.Errorf("Docker could not create the volume %s (%v)", volume, err) + } + if created.Volume.Labels[LabelRestore] != nonces[volume] { + // Docker hands back the volume that is there for a name that exists. + if critical[volume] { + return fmt.Errorf("the volume %s already existed, so it was not used", volume) + } + continue + } + made[volume] = nonces[volume] + } + + info, err := steps.create(ctx) + createdID = info.ContainerID + if err != nil { + return fmt.Errorf("Docker could not create %s (%v)", name, err) + } + result.ContainerID, result.Image = info.ContainerID, info.Image + if why := notCreated(ctx, cli, createdID); why != "" { + return fmt.Errorf("%s %s, so it was not written to", name, why) + } + + for _, target := range result.Targets { + volume := targets[target] + got, err := cli.VolumeInspect(ctx, volume, client.VolumeInspectOptions{}) + if err != nil || got.Volume.Labels[LabelRestore] != made[volume] { + return fmt.Errorf("another program replaced the volume %s after this restore made it, so it was not used", volume) + } + // Into the folder that holds the mount point: the archive's entries sit + // under the folder's own name, so its first entry gives the volume's root + // the backup's owner and mode. + if _, err := cli.CopyToContainer(ctx, createdID, client.CopyToContainerOptions{DestinationPath: path.Dir(target), Content: bytes.NewReader(writes[target])}); err != nil { + return fmt.Errorf("Docker did not write the volume %s (%v)", volume, err) + } + } + for _, target := range result.Targets { + back, err := readBack(ctx, cli, createdID, target) + if err != nil { + return fmt.Errorf("%s could not be read back (%v)", target, err) + } + found, err := statebackup.TreeManifest(back) + if err != nil { + return fmt.Errorf("%s could not be read back (%v)", target, err) + } + if diff := statebackup.ManifestDifferences(expected[target], found); len(diff) > 0 { + shown := strings.Join(diff[:min(5, len(diff))], "; ") + if len(diff) > 5 { + shown += fmt.Sprintf("; and %d more", len(diff)-5) + } + return fmt.Errorf("%s does not read back as the backup: %s", target, shown) + } + for _, e := range found { + if e.Kind == "file" { + result.Files++ + } + } + } + if why := notCreated(ctx, cli, createdID); why != "" { + return fmt.Errorf("%s %s before the restore finished", name, why) + } + // Every folder read back as the backup: the hold goes now, right before the start. + if err := req.Release(); err != nil { + return fmt.Errorf("the app could not record that the restore of %s finished, so it was not started (%v)", svc.Name, err) + } + if !gated { + if err := steps.start(ctx); err != nil { + return fmt.Errorf("Docker could not start %s (%v)", name, err) + } + result.Started = true + return nil + } + // Last before the start: from here the fence guard may start it too. + if err := p.fence.markStopped(name, false); err != nil { + return fmt.Errorf("the app could not lift the hold on %s (%v)", name, err) + } + if err := steps.start(ctx); err != nil { + // Its data is checked: it waits for the fence, and the guard starts it. + result.Waiting = err.Error() + } else { + result.Started = true + } + return nil + }() + if failure == nil { + return result, nil + } + left := undoRestore(context.WithoutCancel(ctx), cli, slug, createdID, made) + if gated && (len(left) == 0 || left[0] != name) { + // No container of ours is left, so the stop record goes back to what it was. + _ = p.fence.markStopped(name, wasStopped) + } + msg := strings.TrimSuffix(failure.Error(), ".") + ". The restore was undone." + if len(left) > 0 { + msg += fmt.Sprintf(" Could not remove %s, so %s stays held: Deploy and Start refuse it until you delete the partial restore under Settings, Restore from a backup.", strings.Join(left, ", "), svc.Name) + } + return RestoreResult{Left: left}, &restoreError{msg: msg, left: left} +} + +func sameKeys(a map[string]string, b map[string][]byte) bool { + if len(a) != len(b) { + return false + } + for k := range b { + if _, ok := a[k]; !ok { + return false + } + } + return true +} + +// notCreated says how the container is no longer the one this restore created and +// never started, or "". +func notCreated(ctx context.Context, cli restoreClient, id string) string { + got, err := cli.ContainerInspect(ctx, id, client.ContainerInspectOptions{}) + if err != nil { + return fmt.Sprintf("could not be checked (%v)", err) + } + if got.Container.State == nil || got.Container.State.Status != "created" { + status := "in an unknown state" + if got.Container.State != nil { + status = "is " + string(got.Container.State.Status) + } + return status + ", not newly created" + } + return "" +} + +// readBack is Docker's archive of target in the created container, capped like an +// export. +func readBack(ctx context.Context, cli restoreClient, id, target string) ([]byte, error) { + res, err := cli.CopyFromContainer(ctx, id, client.CopyFromContainerOptions{SourcePath: target}) + if err != nil { + return nil, err + } + defer res.Content.Close() + data, err := io.ReadAll(io.LimitReader(res.Content, statebackup.MaxStateBytes+1)) + if err != nil { + return nil, err + } + if len(data) > statebackup.MaxStateBytes { + return nil, errors.New("it reads back larger than a backup can be, so it is not the backup") + } + return data, nil +} + +// undoRestore removes what a failed restore made: its container (and one a create +// that failed after Docker made it left under the name, never started), then each +// volume that still carries the label value this restore made it with. It returns +// what could not be removed, the container's name first. +func undoRestore(ctx context.Context, cli restoreClient, slug, createdID string, made map[string]string) []string { + var left []string + name := containerName(slug) + ids := []string{} + if createdID != "" { + ids = append(ids, createdID) + } + if got, err := cli.ContainerInspect(ctx, name, client.ContainerInspectOptions{}); err == nil && got.Container.ID != createdID && + got.Container.Config != nil && got.Container.Config.Labels[LabelService] == slug && + got.Container.State != nil && got.Container.State.Status == "created" { + ids = append(ids, got.Container.ID) + } + for _, id := range ids { + if _, err := cli.ContainerRemove(ctx, id, client.ContainerRemoveOptions{Force: true}); err != nil && !cerrdefs.IsNotFound(err) { + left = append(left, name) + break + } + } + for _, volume := range sortedNames(made) { + if !removeOwnVolume(ctx, cli, volume, made[volume]) { + left = append(left, volume) + } + } + return left +} + +// removeOwnVolume removes volume when it still carries nonce. False only when it is +// the restore's and could not be removed: one that is gone, or another program's +// under the name, is never the restore's to remove. +func removeOwnVolume(ctx context.Context, cli restoreClient, volume, nonce string) bool { + got, err := cli.VolumeInspect(ctx, volume, client.VolumeInspectOptions{}) + if cerrdefs.IsNotFound(err) { + return true + } + if err != nil { + return false + } + if nonce == "" || got.Volume.Labels[LabelRestore] != nonce { + return true + } + if _, err := cli.VolumeRemove(ctx, volume, client.VolumeRemoveOptions{}); err != nil && !cerrdefs.IsNotFound(err) { + return false + } + return true +} + +func discardRestore(ctx context.Context, cli restoreClient, slug string, volumes map[string]string) ([]string, error) { + name := containerName(slug) + got, err := cli.ContainerInspect(ctx, name, client.ContainerInspectOptions{}) + switch { + case err == nil: + if got.Container.Config == nil || got.Container.Config.Labels[LabelService] != slug { + return nil, fmt.Errorf("%s belongs to something else, so it was left as it is", name) + } + if runsOrRestarts(got.Container) { + return nil, fmt.Errorf("%s is running, so the partial restore was not deleted. Stop it first", name) + } + if _, err := cli.ContainerRemove(ctx, got.Container.ID, client.ContainerRemoveOptions{Force: true}); err != nil && !cerrdefs.IsNotFound(err) { + return []string{name}, fmt.Errorf("could not remove %s: %w", name, err) + } + case !cerrdefs.IsNotFound(err): + return nil, fmt.Errorf("could not check %s: %w", name, err) + } + var left []string + for _, volume := range sortedNames(volumes) { + if !removeOwnVolume(ctx, cli, volume, volumes[volume]) { + left = append(left, volume) + } + } + if len(left) > 0 { + return left, fmt.Errorf("could not delete the volume %s", strings.Join(left, ", ")) + } + return nil, nil +} diff --git a/internal/runtime/restore_live_test.go b/internal/runtime/restore_live_test.go new file mode 100644 index 00000000..be6cdbd2 --- /dev/null +++ b/internal/runtime/restore_live_test.go @@ -0,0 +1,119 @@ +package runtime + +import ( + "archive/tar" + "bytes" + "context" + "os" + "strings" + "testing" + "time" + + "github.com/GeiserX/CashPilot-Desktop/internal/catalog" + "github.com/GeiserX/CashPilot-Desktop/internal/statebackup" + "github.com/moby/moby/client" +) + +// TestRestoreLive proves the restore on a real daemon: Docker writes into the volume +// of a container that was created and never started, the volume's root takes the +// backup's owner and mode, and the read-back matches the backup before the start. +// +// It pulls alpine and creates a container and a volume, so it runs only when asked: +// CASHPILOT_RESTORE_LIVE=1. +func TestRestoreLive(t *testing.T) { + if os.Getenv("CASHPILOT_RESTORE_LIVE") != "1" { + t.Skip("set CASHPILOT_RESTORE_LIVE=1 to restore into a real Docker daemon") + } + useAmbientDocker(t) + const slug = "restore-live-test" + svc := catalog.Service{Name: "Restore live test", Slug: slug, Docker: catalog.DockerConfig{ + Image: "alpine:3.22", + Command: "sleep 600", + NetworkMode: "none", + Volumes: []string{"cashpilot-restore-live-data:/var/lib/keeper"}, + CriticalVolumes: []catalog.CriticalVolume{{Target: "/var/lib/keeper", Holds: "identity"}}, + }} + cli, err := dockerClient() + if err != nil { + t.Fatal(err) + } + defer cli.Close() + cleanup := func() { + _, _ = cli.ContainerRemove(context.Background(), containerName(slug), client.ContainerRemoveOptions{Force: true}) + _, _ = cli.VolumeRemove(context.Background(), "cashpilot-restore-live-data", client.VolumeRemoveOptions{Force: true}) + } + cleanup() + t.Cleanup(cleanup) + + at := time.Unix(1767225600, 0) + long := "keystore/UTC--2026-01-01T00-00-00.000000000Z--" + strings.Repeat("0123456789abcdef", 5) + var buf bytes.Buffer + w := tar.NewWriter(&buf) + for _, h := range []*tar.Header{ + {Typeflag: tar.TypeDir, Name: "keeper/", Mode: 0o700, Uid: 1000, Gid: 1000}, + {Typeflag: tar.TypeDir, Name: "keeper/keystore/", Mode: 0o750, Uid: 1000, Gid: 1000}, + {Typeflag: tar.TypeReg, Name: "keeper/" + long, Mode: 0o600, Uid: 1000, Gid: 1000, Size: 6}, + {Typeflag: tar.TypeSymlink, Name: "keeper/current", Linkname: long, Mode: 0o777}, + } { + h.ModTime, h.Format = at, tar.FormatPAX + if err := w.WriteHeader(h); err != nil { + t.Fatal(err) + } + if h.Size > 0 { + _, _ = w.Write([]byte("secret")) + } + } + _ = w.Close() + archives, err := statebackup.SplitArchives(buf.Bytes()) + if err != nil { + t.Fatal(err) + } + + p := NewDockerProvider() + var held map[string]string + released := false + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) + defer cancel() + res, err := p.RestoreState(ctx, RestoreRequest{ + Spec: DeploySpec{Slug: slug, Service: svc}, + Archives: archives, + Hold: func(v map[string]string) error { held = v; return nil }, + Release: func() error { released = true; return nil }, + Progress: func(m string) { t.Log(m) }, + }) + if err != nil { + t.Fatal(err) + } + if !res.Started || res.Files != 1 || !released || held["cashpilot-restore-live-data"] == "" { + t.Fatalf("result %+v released %v held %v", res, released, held) + } + vol, err := cli.VolumeInspect(ctx, "cashpilot-restore-live-data", client.VolumeInspectOptions{}) + if err != nil || vol.Volume.Labels[LabelRestore] != held["cashpilot-restore-live-data"] { + t.Fatalf("volume %+v, %v", vol.Volume.Labels, err) + } + // The running container sees the backup, owners and modes included. + back, err := readBack(ctx, cli, res.ContainerID, "/var/lib/keeper") + if err != nil { + t.Fatal(err) + } + got, err := statebackup.TreeManifest(back) + if err != nil { + t.Fatal(err) + } + want, _ := statebackup.TreeManifest(archives["keeper"]) + if diff := statebackup.ManifestDifferences(want, got); len(diff) > 0 { + t.Fatalf("the running node does not hold the backup: %v", diff) + } + if root := got[statebackup.Root]; root.UID != 1000 || root.Mode != 0o700 { + t.Fatalf("the volume's root is %+v, not the backup's owner and mode", root) + } + + // A second restore of the same service refuses and touches nothing. + if _, err := p.RestoreState(ctx, RestoreRequest{ + Spec: DeploySpec{Slug: slug, Service: svc}, Archives: archives, + Hold: func(map[string]string) error { t.Fatal("a refused restore recorded a hold"); return nil }, + Release: func() error { return nil }, + }); err == nil || !strings.Contains(err.Error(), "already has a container") { + t.Fatalf("second restore: %v", err) + } +} diff --git a/internal/runtime/restore_test.go b/internal/runtime/restore_test.go new file mode 100644 index 00000000..923909de --- /dev/null +++ b/internal/runtime/restore_test.go @@ -0,0 +1,411 @@ +package runtime + +import ( + "archive/tar" + "bytes" + "context" + "errors" + "io" + "path" + "strings" + "testing" + "time" + + "github.com/GeiserX/CashPilot-Desktop/internal/catalog" + "github.com/GeiserX/CashPilot-Desktop/internal/statebackup" + cerrdefs "github.com/containerd/errdefs" + "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/volume" + "github.com/moby/moby/client" +) + +// fakeRestoreDaemon stands in for Docker in a restore: containers by name, volumes +// with their labels, and the archive written into each folder, which it reads back +// as Docker would (or as tamper says). +type fakeRestoreDaemon struct { + containers map[string]*container.InspectResponse + volumes map[string]map[string]string + written map[string][]byte // destination folder -> archive + tamper func([]byte) []byte + // onCreateVolume runs after a volume is made (a test replaces it behind the restore's back). + onCreateVolume func(name string) + calls []string + removeFails bool +} + +func newFakeRestoreDaemon() *fakeRestoreDaemon { + return &fakeRestoreDaemon{containers: map[string]*container.InspectResponse{}, volumes: map[string]map[string]string{}, written: map[string][]byte{}} +} + +func (f *fakeRestoreDaemon) find(ref string) *container.InspectResponse { + if c, ok := f.containers[ref]; ok { + return c + } + for _, c := range f.containers { + if c.ID == ref { + return c + } + } + return nil +} + +func (f *fakeRestoreDaemon) ContainerInspect(_ context.Context, ref string, _ client.ContainerInspectOptions) (client.ContainerInspectResult, error) { + if c := f.find(ref); c != nil { + return client.ContainerInspectResult{Container: *c}, nil + } + return client.ContainerInspectResult{}, cerrdefs.ErrNotFound +} + +func (f *fakeRestoreDaemon) ContainerList(_ context.Context, opts client.ContainerListOptions) (client.ContainerListResult, error) { + var out []container.Summary + for name, c := range f.containers { + for filter := range opts.Filters["label"] { + k, v, _ := strings.Cut(filter, "=") + if c.Config != nil && c.Config.Labels[k] == v { + out = append(out, container.Summary{ID: c.ID, Names: []string{"/" + name}, Labels: c.Config.Labels}) + } + } + } + return client.ContainerListResult{Items: out}, nil +} + +func (f *fakeRestoreDaemon) ContainerRemove(_ context.Context, ref string, _ client.ContainerRemoveOptions) (client.ContainerRemoveResult, error) { + f.calls = append(f.calls, "container-rm") + if f.removeFails { + return client.ContainerRemoveResult{}, errors.New("device busy") + } + for name, c := range f.containers { + if name == ref || c.ID == ref { + delete(f.containers, name) + return client.ContainerRemoveResult{}, nil + } + } + return client.ContainerRemoveResult{}, cerrdefs.ErrNotFound +} + +func (f *fakeRestoreDaemon) VolumeInspect(_ context.Context, name string, _ client.VolumeInspectOptions) (client.VolumeInspectResult, error) { + labels, ok := f.volumes[name] + if !ok { + return client.VolumeInspectResult{}, cerrdefs.ErrNotFound + } + return client.VolumeInspectResult{Volume: volume.Volume{Name: name, Labels: labels}}, nil +} + +func (f *fakeRestoreDaemon) VolumeCreate(_ context.Context, opts client.VolumeCreateOptions) (client.VolumeCreateResult, error) { + f.calls = append(f.calls, "volume-create "+opts.Name) + if _, ok := f.volumes[opts.Name]; !ok { + f.volumes[opts.Name] = opts.Labels + } + if f.onCreateVolume != nil { + f.onCreateVolume(opts.Name) + } + return client.VolumeCreateResult{Volume: volume.Volume{Name: opts.Name, Labels: f.volumes[opts.Name]}}, nil +} + +func (f *fakeRestoreDaemon) VolumeRemove(_ context.Context, name string, _ client.VolumeRemoveOptions) (client.VolumeRemoveResult, error) { + f.calls = append(f.calls, "volume-rm "+name) + delete(f.volumes, name) + return client.VolumeRemoveResult{}, nil +} + +func (f *fakeRestoreDaemon) CopyToContainer(_ context.Context, _ string, opts client.CopyToContainerOptions) (client.CopyToContainerResult, error) { + f.calls = append(f.calls, "write "+opts.DestinationPath) + data, err := io.ReadAll(opts.Content) + if err != nil { + return client.CopyToContainerResult{}, err + } + f.written[opts.DestinationPath] = data + return client.CopyToContainerResult{}, nil +} + +func (f *fakeRestoreDaemon) CopyFromContainer(_ context.Context, _ string, opts client.CopyFromContainerOptions) (client.CopyFromContainerResult, error) { + data := f.written[path.Dir(opts.SourcePath)] + if f.tamper != nil { + data = f.tamper(data) + } + return client.CopyFromContainerResult{Content: io.NopCloser(bytes.NewReader(data))}, nil +} + +// keeper is a service shaped like Mysterium: one critical folder on a named volume. +func keeper(gated bool) catalog.Service { + return catalog.Service{Name: "Keeper", Slug: "keeper", Docker: catalog.DockerConfig{ + Image: "keeper/node:1.0.0", + Volumes: []string{"keeper-data:/var/lib/keeper", "keeper-config:/etc/keeper"}, + CriticalVolumes: []catalog.CriticalVolume{{Target: "/var/lib/keeper/", Holds: "identity"}}, + FenceGated: gated, + }} +} + +// keeperArchive is the archive SplitArchives gives for the keeper folder. +func keeperArchive(t *testing.T) []byte { + t.Helper() + var buf bytes.Buffer + w := tar.NewWriter(&buf) + at := time.Unix(1767225600, 0) + for _, h := range []*tar.Header{ + {Typeflag: tar.TypeDir, Name: "keeper/", Mode: 0o700, Uid: 1000, Gid: 1000, ModTime: at}, + {Typeflag: tar.TypeReg, Name: "keeper/identity.key", Mode: 0o600, Uid: 1000, Gid: 1000, Size: 6, ModTime: at}, + } { + h.Format = tar.FormatPAX + if err := w.WriteHeader(h); err != nil { + t.Fatal(err) + } + if h.Size > 0 { + _, _ = w.Write([]byte("secret")) + } + } + _ = w.Close() + return buf.Bytes() +} + +type restoreRun struct { + daemon *fakeRestoreDaemon + p *DockerProvider + held map[string]string + released bool + created bool + started bool + startErr error + // stoppedAtCreate is whether the fence guard was told to leave the node alone + // while the container existed unchecked. + stoppedAtCreate bool +} + +func (r *restoreRun) restore(t *testing.T, svc catalog.Service, archives map[string][]byte) (RestoreResult, error) { + t.Helper() + req := RestoreRequest{ + Spec: DeploySpec{Slug: svc.Slug, Service: svc}, + Archives: archives, + Hold: func(v map[string]string) error { + r.held = v + return nil + }, + Release: func() error { + r.released = true + return nil + }, + } + steps := restoreSteps{ + create: func(context.Context) (ContainerInfo, error) { + r.created = true + stopped, _ := r.p.fence.userStopped() + r.stoppedAtCreate = stopped[containerName(svc.Slug)] + r.daemon.containers[containerName(svc.Slug)] = &container.InspectResponse{ + ID: "c1", Config: &container.Config{Labels: map[string]string{LabelService: svc.Slug}}, + State: &container.State{Status: container.StateCreated}, + } + return ContainerInfo{ContainerID: "c1", Image: svc.Docker.Image}, nil + }, + start: func(context.Context) error { + if r.startErr != nil { + return r.startErr + } + r.started = true + return nil + }, + } + return r.p.restoreState(context.Background(), r.daemon, req, steps) +} + +func newRestoreRun() *restoreRun { + return &restoreRun{daemon: newFakeRestoreDaemon(), p: &DockerProvider{}} +} + +func TestRestoreTargets(t *testing.T) { + got, err := RestoreTargets(keeper(false)) + if err != nil || len(got) != 1 || got["/var/lib/keeper"] != "keeper-data" { + t.Fatalf("got %v, %v", got, err) + } + storj := catalog.Service{Name: "Storj", Docker: catalog.DockerConfig{ + Volumes: []string{"${IDENTITY_DIR}:/app/identity"}, + CriticalVolumes: []catalog.CriticalVolume{{Target: "/app/identity"}}, + }} + if _, err := RestoreTargets(storj); err == nil || !strings.Contains(err.Error(), "host folder") { + t.Fatalf("a host folder must be refused, got %v", err) + } + if _, err := RestoreTargets(catalog.Service{Name: "Plain"}); err == nil || !strings.Contains(err.Error(), "keeps nothing") { + t.Fatalf("a service with nothing critical must be refused, got %v", err) + } +} + +// The whole path: volumes made new with the restore's label, the container created +// and not started, the backup written into the folder above the mount point, read +// back and compared, the hold lifted, and only then the start. +func TestRestoreWritesVerifiesThenStarts(t *testing.T) { + r := newRestoreRun() + res, err := r.restore(t, keeper(false), map[string][]byte{"keeper": keeperArchive(t)}) + if err != nil { + t.Fatal(err) + } + if !res.Started || res.Files != 1 || res.ContainerID != "c1" || len(res.Left) != 0 { + t.Fatalf("result = %+v", res) + } + if r.held["keeper-data"] == "" || r.held["keeper-config"] == "" { + t.Fatalf("the hold must name every volume the restore makes, with its label: %v", r.held) + } + if got := r.daemon.volumes["keeper-data"][LabelRestore]; got != r.held["keeper-data"] { + t.Fatalf("keeper-data carries %q, the hold says %q", got, r.held["keeper-data"]) + } + if _, ok := r.daemon.written["/var/lib"]; !ok { + t.Fatalf("the backup must be written into the folder that holds the mount point: %v", r.daemon.calls) + } + if !r.released || !r.started { + t.Fatalf("released=%v started=%v", r.released, r.started) + } +} + +// A read-back that is not the backup undoes everything: nothing starts, the hold is +// not lifted by the restore itself, and the container and the volumes it made go. +func TestRestoreUndoesWhenTheReadBackDiffers(t *testing.T) { + r := newRestoreRun() + r.daemon.tamper = func(b []byte) []byte { return bytes.Replace(b, []byte("secret"), []byte("SECRET"), 1) } + res, err := r.restore(t, keeper(false), map[string][]byte{"keeper": keeperArchive(t)}) + if err == nil || !strings.Contains(err.Error(), "/var/lib/keeper does not read back as the backup: identity.key: different content") { + t.Fatalf("err = %v", err) + } + if !strings.Contains(err.Error(), "The restore was undone") { + t.Fatalf("err = %v", err) + } + if r.started || r.released { + t.Fatalf("started=%v released=%v after a failed read-back", r.started, r.released) + } + if len(r.daemon.containers) != 0 || len(r.daemon.volumes) != 0 { + t.Fatalf("left behind: containers %v volumes %v", r.daemon.containers, r.daemon.volumes) + } + if len(res.Left) != 0 { + t.Fatalf("left = %v", res.Left) + } +} + +func TestRestoreRefusesWhenTheServiceOrItsVolumeIsThere(t *testing.T) { + r := newRestoreRun() + r.daemon.containers["cashpilot-keeper-p3"] = &container.InspectResponse{ID: "x", Config: &container.Config{Labels: map[string]string{LabelService: "keeper"}}} + if _, err := r.restore(t, keeper(false), map[string][]byte{"keeper": keeperArchive(t)}); err == nil || !strings.Contains(err.Error(), "already has a container on this machine (cashpilot-keeper-p3)") { + t.Fatalf("err = %v", err) + } + r = newRestoreRun() + r.daemon.volumes["keeper-data"] = map[string]string{} + if _, err := r.restore(t, keeper(false), map[string][]byte{"keeper": keeperArchive(t)}); err == nil || !strings.Contains(err.Error(), "the volume keeper-data already exists") { + t.Fatalf("err = %v", err) + } + if r.held != nil || r.created || len(r.daemon.calls) != 0 { + t.Fatalf("a refused restore made something: held=%v created=%v calls=%v", r.held, r.created, r.daemon.calls) + } + if r.daemon.volumes["keeper-data"] == nil { + t.Fatal("the existing volume was touched") + } +} + +func TestRestoreRefusesABackupOfAnotherService(t *testing.T) { + r := newRestoreRun() + _, err := r.restore(t, keeper(false), map[string][]byte{"mysterium-node": keeperArchive(t)}) + if err == nil || !strings.Contains(err.Error(), "this backup holds mysterium-node, but Keeper keeps keeper. It is not a backup of Keeper") { + t.Fatalf("err = %v", err) + } + if r.held != nil || len(r.daemon.calls) != 0 { + t.Fatal("a foreign backup made something") + } +} + +// A fence-gated node is held from before its container exists until its data read +// back, so the fence guard never starts it half-written; a fence that is not ready +// leaves it waiting, not failed. +func TestRestoreHoldsAFenceGatedNodeUntilChecked(t *testing.T) { + r := newRestoreRun() + r.startErr = errors.New("LAN isolation is off here") + res, err := r.restore(t, keeper(true), map[string][]byte{"keeper": keeperArchive(t)}) + if err != nil { + t.Fatal(err) + } + if !r.stoppedAtCreate { + t.Fatal("the fence guard was free to start the node while its data was unchecked") + } + if res.Started || !strings.Contains(res.Waiting, "LAN isolation is off") { + t.Fatalf("result = %+v", res) + } + stopped, _ := r.p.fence.userStopped() + if stopped["cashpilot-keeper"] { + t.Fatal("a checked node must be left to the fence guard, not held") + } + + // And a failed one gets its record back as it was. + r = newRestoreRun() + r.daemon.tamper = func([]byte) []byte { return nil } + if _, err := r.restore(t, keeper(true), map[string][]byte{"keeper": keeperArchive(t)}); err == nil { + t.Fatal("an empty read-back passed") + } + if stopped, _ := r.p.fence.userStopped(); stopped["cashpilot-keeper"] { + t.Fatal("an undone restore left a stop record that was not there before") + } +} + +// A container the undo cannot remove stays held, and the error says how to clear it. +func TestRestoreReportsWhatTheUndoLeft(t *testing.T) { + r := newRestoreRun() + r.daemon.tamper = func([]byte) []byte { return nil } + r.daemon.removeFails = true + res, err := r.restore(t, keeper(true), map[string][]byte{"keeper": keeperArchive(t)}) + if err == nil || len(res.Left) == 0 || res.Left[0] != "cashpilot-keeper" { + t.Fatalf("res = %+v, err = %v", res, err) + } + if !strings.Contains(err.Error(), "delete the partial restore under Settings") { + t.Fatalf("err = %v", err) + } + if stopped, _ := r.p.fence.userStopped(); !stopped["cashpilot-keeper"] { + t.Fatal("a half-written node that stayed must stay held from the fence guard") + } +} + +// The undo deletes only a volume that still carries the restore's label: one another +// program put under the name is never the restore's to delete. +func TestRestoreUndoLeavesAVolumeAnotherProgramReplaced(t *testing.T) { + r := newRestoreRun() + r.daemon.onCreateVolume = func(name string) { + if name == "keeper-data" { + r.daemon.onCreateVolume = nil + r.daemon.volumes["keeper-data"] = map[string]string{"owner": "someone else"} + } + } + _, err := r.restore(t, keeper(false), map[string][]byte{"keeper": keeperArchive(t)}) + if err == nil || !strings.Contains(err.Error(), "already existed") { + t.Fatalf("err = %v", err) + } + if r.daemon.volumes["keeper-data"]["owner"] != "someone else" { + t.Fatal("another program's volume was deleted") + } +} + +func TestDiscardRestoreRemovesOnlyWhatTheRestoreMade(t *testing.T) { + d := newFakeRestoreDaemon() + d.containers["cashpilot-keeper"] = &container.InspectResponse{ID: "c1", Config: &container.Config{Labels: map[string]string{LabelService: "keeper"}}, State: &container.State{Status: container.StateCreated}} + d.volumes["keeper-data"] = map[string]string{LabelRestore: "n1"} + d.volumes["keeper-config"] = map[string]string{LabelRestore: "other"} + left, err := discardRestore(context.Background(), d, "keeper", map[string]string{"keeper-data": "n1", "keeper-config": "n2", "gone": "n3"}) + if err != nil || len(left) != 0 { + t.Fatalf("left %v, err %v", left, err) + } + if len(d.containers) != 0 { + t.Fatal("the restore's container stayed") + } + if _, ok := d.volumes["keeper-data"]; ok { + t.Fatal("the restore's volume stayed") + } + if _, ok := d.volumes["keeper-config"]; !ok { + t.Fatal("a volume without the restore's label was deleted") + } +} + +// What the restore writes is the backup, byte for byte in content and owners, once +// Docker has it: the manifest of the written archive is the backup's. +func TestRestoreWritesTheBackupsOwnersAndModes(t *testing.T) { + r := newRestoreRun() + archive := keeperArchive(t) + if _, err := r.restore(t, keeper(false), map[string][]byte{"keeper": archive}); err != nil { + t.Fatal(err) + } + want, _ := statebackup.TreeManifest(archive) + got, _ := statebackup.TreeManifest(r.daemon.written["/var/lib"]) + if diff := statebackup.ManifestDifferences(want, got); len(diff) > 0 || got[statebackup.Root].UID != 1000 { + t.Fatalf("diff %v, root %+v", diff, got[statebackup.Root]) + } +} diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.go index dbc999e8..d01693b5 100644 --- a/internal/runtime/runtime.go +++ b/internal/runtime/runtime.go @@ -90,6 +90,10 @@ type DeploySpec struct { // Instance is which copy of the service this is. 0 and 1 both mean the first, // which keeps the name and labels it always had; see LabelInstance. Instance int + // CreateOnly is a restore's create (restore.go): the container is made and never + // started, and a container already under the name refuses it instead of being + // replaced. Every other deploy check applies as on a deploy. + CreateOnly bool } type ContainerInfo struct { @@ -118,6 +122,8 @@ type DockerProvider struct { // fence keeps bridge earners off the LAN (lanfence.go). The zero value works: // with no setting wired in, the setting reads as auto. fence fence + // restoring lets one restore run at a time (restore.go). + restoring sync.Mutex // replacing holds a *sync.Mutex per container name: a deploy holds it from // settling a set-aside leftover to its end, and recovery takes it to settle one, // so recovery never takes a replacement a deploy has made and not yet started. @@ -377,6 +383,9 @@ func (p *DockerProvider) Deploy(ctx context.Context, spec DeploySpec, progress f if inspectErr != nil && !cerrdefs.IsNotFound(inspectErr) { return ContainerInfo{}, fmt.Errorf("could not check what holds %s, so it was left as it is: %w", name, inspectErr) } + if spec.CreateOnly && inspectErr == nil { + return ContainerInfo{}, fmt.Errorf("%s already exists, so nothing was restored. A restore only creates a new container", name) + } aside := name + setAsideSuffix setAside, wasRunning := false, false if inspectErr == nil { @@ -493,6 +502,10 @@ func (p *DockerProvider) Deploy(ctx context.Context, spec DeploySpec, progress f } return ContainerInfo{}, rollBack(err, madeID) } + if spec.CreateOnly { + // Nothing was set aside (the name was free), so there is nothing to put back. + return ContainerInfo{Slug: spec.Slug, ContainerID: created.ID, Name: name, Image: image, Status: "created", Instance: 1}, nil + } if progress != nil { progress("Starting " + name) } diff --git a/internal/services/manager.go b/internal/services/manager.go index b64791a4..dab910de 100644 --- a/internal/services/manager.go +++ b/internal/services/manager.go @@ -274,6 +274,10 @@ func (m *Manager) DeployWith(ctx context.Context, slug string, credentials map[s if err := validateRequired(svc, credentials); err != nil { return store.Deployment{}, err } + if err := m.refuseHeld(slug, "deployed"); err != nil { + m.store.RecordEvent(slug, "deploy_refused", err.Error()) + return store.Deployment{}, err + } leaving, routed, err := m.directAllowed(svc, slug, opts.LeaveRoute) if err != nil { m.store.RecordEvent(slug, "deploy_refused", err.Error()) diff --git a/internal/services/restore.go b/internal/services/restore.go new file mode 100644 index 00000000..b4219790 --- /dev/null +++ b/internal/services/restore.go @@ -0,0 +1,189 @@ +package services + +import ( + "context" + "errors" + "fmt" + "strings" + "time" + + "github.com/GeiserX/CashPilot-Desktop/internal/catalog" + "github.com/GeiserX/CashPilot-Desktop/internal/runtime" + "github.com/GeiserX/CashPilot-Desktop/internal/store" +) + +// Restore from a backup file (issue #298): the manager's side. It decides whether a +// service may be restored here, records the hold that keeps a half-written service +// from starting, and records the deployment once the runtime restored it. + +// restoreTarget is the catalog service a backup restores, and the runtime that can +// restore it. It refuses what a restore from the app cannot do. +func (m *Manager) restoreTarget(slug string) (catalog.Service, runtime.RestoreProvider, map[string]string, error) { + svc, ok := m.catalog.Get(slug) + if !ok || svc.Custom || catalog.IsCustomSlug(slug) { + return catalog.Service{}, nil, nil, errors.New("this backup is not of a service in this app's catalog") + } + if svc.ManualOnly { + return svc, nil, nil, fmt.Errorf("%s is tracked manually and has no Docker image", svc.Name) + } + if err := refuseRetired(svc); err != nil { + return svc, nil, nil, err + } + targets, err := runtime.RestoreTargets(svc) + if err != nil { + return svc, nil, nil, err + } + if m.kindForService(svc) == nativeRuntimeKind { + return svc, nil, nil, fmt.Errorf("%s runs as a native program on this machine, so its identity is not in a Docker volume and the app cannot restore it. Use the command line steps in the backup guide", svc.Name) + } + provider, ok := m.providers[m.defaultKind].(runtime.RestoreProvider) + if !ok { + return svc, nil, nil, errors.New("this machine's container runtime cannot restore a backup") + } + return svc, provider, targets, nil +} + +// RestoreVolumes maps each critical folder of slug to the named volume a restore +// creates for it, or says why the app cannot restore slug at all. +func (m *Manager) RestoreVolumes(slug string) (map[string]string, error) { + _, _, targets, err := m.restoreTarget(slug) + return targets, err +} + +// RestoreRefusal says why slug cannot be restored on this machine now, or "". It runs +// every check a restore runs before it makes anything. +func (m *Manager) RestoreRefusal(ctx context.Context, slug string, credentials map[string]string) (string, error) { + svc, provider, _, err := m.restoreTarget(slug) + if err != nil { + return err.Error(), nil + } + if why, err := m.restoreRefusal(svc, slug, credentials); why != "" || err != nil { + return why, err + } + return provider.RestoreRefusal(ctx, slug, svc) +} + +// restoreRefusal is the part of the refusal the manager knows: the service is not +// deployed here, no earlier restore of it is unfinished, it may run from this +// machine's own IP, and its settings are filled in. +func (m *Manager) restoreRefusal(svc catalog.Service, slug string, credentials map[string]string) (string, error) { + if _, deployed, err := m.store.GetDeployment(slug); err != nil { + return "", err + } else if deployed { + return fmt.Sprintf("%s is already deployed on this machine. A restore only creates a new one: back the running one up, remove it and say yes to deleting its data, then restore", svc.Name), nil + } + if err := m.refuseHeld(slug, "restored"); err != nil { + return err.Error(), nil + } + if _, _, err := m.directAllowed(svc, slug, false); err != nil { + return err.Error(), nil + } + if err := validateRequired(svc, credentials); err != nil { + return fmt.Sprintf("%s needs its settings first (%v). Fill them in on its deploy form and save them, then restore", svc.Name, err), nil + } + if err := checkPublished(svc, credentials); err != nil { + return err.Error(), nil + } + return "", nil +} + +// refuseHeld refuses slug while a restore of it is under way or did not finish: its +// volume may be half-written, and a node started on it would run half an identity. +// An unreadable record refuses too. +func (m *Manager) refuseHeld(slug, what string) error { + hold, err := m.store.RestoreHeld(slug) + if err != nil { + return fmt.Errorf("could not read whether a restore of %s is unfinished (%v), so it was not %s", slug, err, what) + } + if hold != nil { + return fmt.Errorf("a restore of %s is running or did not finish, so its data may be half-written and it was not %s. If no restore is running, delete the partial restore under Settings, Restore from a backup, then restore the backup again", slug, what) + } + return nil +} + +// Restore creates slug from its catalog entry with archives (statebackup.SplitArchives' +// output) in its critical volumes, and starts it once they read back as the backup. +// It holds the service's deploy lock throughout, so no deploy or removal of it runs +// in between. The hold on disk is written before anything is made and lifted only +// when the data read back right, or when the undo removed everything it made. +func (m *Manager) Restore(ctx context.Context, slug string, credentials map[string]string, archives map[string][]byte) (runtime.RestoreResult, error) { + defer m.lockDeploy(slug)() + svc, provider, _, err := m.restoreTarget(slug) + if err != nil { + return runtime.RestoreResult{}, err + } + if why, err := m.restoreRefusal(svc, slug, credentials); err != nil { + return runtime.RestoreResult{}, err + } else if why != "" { + return runtime.RestoreResult{}, fmt.Errorf("nothing was restored: %s", strings.TrimSuffix(why, ".")) + } + held := false + m.store.RecordEvent(slug, "restore_start", "") + res, err := provider.RestoreState(ctx, runtime.RestoreRequest{ + Spec: runtime.DeploySpec{Slug: slug, Service: svc, Env: credentials}, + Archives: archives, + Hold: func(volumes map[string]string) error { + if err := m.store.HoldRestore(slug, volumes, time.Now()); err != nil { + return err + } + held = true + return nil + }, + Release: func() error { return m.store.ReleaseRestore(slug) }, + Progress: func(message string) { m.store.RecordEvent(slug, "runtime_progress", message) }, + }) + if err != nil { + if held && len(res.Left) == 0 { + // The undo removed everything the restore made, so nothing is half-written. + if rerr := m.store.ReleaseRestore(slug); rerr != nil { + err = fmt.Errorf("%w The app could not lift its hold on %s (%v): delete the partial restore under Settings, Restore from a backup", err, svc.Name, rerr) + } + } + m.store.RecordEvent(slug, "restore_error", err.Error()) + return res, err + } + status := "running" + if !res.Started { + status = "created" + } + deployment := store.Deployment{Slug: slug, ContainerID: res.ContainerID, Name: res.Name, Image: res.Image, Status: status, Runtime: defaultRuntimeKind} + if err := m.store.UpsertDeployment(deployment); err != nil { + return res, fmt.Errorf("%s was restored and is on this machine, but the app could not record it (%v). Refresh the dashboard", svc.Name, err) + } + _ = m.store.SetCustomImage(slug, "") + _ = m.store.SetInstances(slug, 1) + m.store.RecordEvent(slug, "restored", fmt.Sprintf("%d files in %s", res.Files, strings.Join(res.Targets, ", "))) + return res, nil +} + +// UnfinishedRestores lists the restores that are running or did not finish. +func (m *Manager) UnfinishedRestores() ([]store.RestoreHold, error) { + return m.store.RestoreHolds() +} + +// DiscardRestore deletes what a restore of slug that did not finish left: its +// container and the volumes it made, never a volume another program put under the +// same name. Then Deploy and Start accept the service again. +func (m *Manager) DiscardRestore(ctx context.Context, slug string) error { + defer m.lockDeploy(slug)() + hold, err := m.store.RestoreHeld(slug) + if err != nil { + return err + } + if hold == nil { + return fmt.Errorf("no restore of %s is unfinished on this machine", slug) + } + provider, ok := m.providers[m.defaultKind].(runtime.RestoreProvider) + if !ok { + return errors.New("this machine's container runtime cannot restore a backup") + } + if _, err := provider.DiscardRestore(ctx, slug, hold.Volumes); err != nil { + m.store.RecordEvent(slug, "restore_error", err.Error()) + return fmt.Errorf("the partial restore of %s was not deleted: %w", slug, err) + } + if err := m.store.ReleaseRestore(slug); err != nil { + return err + } + m.store.RecordEvent(slug, "restore_discarded", "") + return nil +} diff --git a/internal/services/routes.go b/internal/services/routes.go index 569400ca..02d90e85 100644 --- a/internal/services/routes.go +++ b/internal/services/routes.go @@ -74,6 +74,9 @@ func (m *Manager) directAllowed(svc catalog.Service, slug string, leaveRoute boo // CASHPILOT_PROXIED_ONLY was set, or before the service was routed, is held to it // all the same. Leaving a route takes a deploy, so a start never does. func (m *Manager) startAllowed(slug string) error { + if err := m.refuseHeld(slug, "started"); err != nil { + return err + } svc, _ := m.catalog.Get(slug) _, _, err := m.directAllowed(svc, slug, false) return err @@ -185,6 +188,9 @@ func (m *Manager) DeployRouted(ctx context.Context, slug string, credentials map if why := m.ProxyRefusal(slug); why != "" { return ProxiedResult{}, errors.New(why) } + if err := m.refuseHeld(slug, "deployed"); err != nil { + return ProxiedResult{}, err + } if _, deployed, err := m.store.GetDeployment(slug); err != nil { return ProxiedResult{}, err } else if deployed { diff --git a/internal/statebackup/manifest.go b/internal/statebackup/manifest.go new file mode 100644 index 00000000..d309c62d --- /dev/null +++ b/internal/statebackup/manifest.go @@ -0,0 +1,222 @@ +package statebackup + +import ( + "archive/tar" + "bytes" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "io" + "os" + "sort" + "strings" +) + +// Root is the folder's own entry in a manifest: its owner and mode, which a service +// needs as much as its files'. Same key as the web's tree_manifest. +const Root = "." + +// MaxBundleBytes is the largest file a restore reads: the export cap, plus room for +// the header and the encryption. Anything bigger was not made by an export, from +// this app (MaxStateBytes) or from the web (64 MB). +const MaxBundleBytes = MaxStateBytes + 1<<20 + +// ManifestEntry is what one path in a folder is: its type, permission bits, owner +// ids, and a file's SHA-256 or a link's target. Modification times are left out: +// they say nothing about whether a restore is the backup. +type ManifestEntry struct { + Kind string // "file", "dir", "symlink", "hardlink" or "other" + Mode int64 + UID int + GID int + Content string +} + +// belowTop is name without its first path component: the path inside the backed-up +// folder ("" for the folder itself). +func belowTop(name string) string { + parts := cleanParts(name) + if len(parts) < 2 { + return "" + } + return strings.Join(parts[1:], "/") +} + +// TreeManifest is what a folder holds, as path -> entry, from Docker's archive of it +// (entries under the folder's own name). path is below the folder, and Root for the +// folder's own entry. A restore reads it twice: on what it meant to write and on +// what Docker reads back, so a file missing, changed, added or owned by someone else +// shows as a difference. More than MaxArchiveBytes of file data is refused rather +// than read. Port of the web's state_backup.tree_manifest. +func TreeManifest(archive []byte) (map[string]ManifestEntry, error) { + found := map[string]ManifestEntry{} + var total int64 + tr := tar.NewReader(bytes.NewReader(archive)) + for { + hdr, err := tr.Next() + if errors.Is(err, io.EOF) { + break + } + if err != nil { + return nil, fail("the restored folder could not be read back: %v", err) + } + path := belowTop(hdr.Name) + if path == "" { + path = Root + } + entry := ManifestEntry{Mode: hdr.Mode & 0o777, UID: hdr.Uid, GID: hdr.Gid} + switch hdr.Typeflag { + case tar.TypeReg, tar.TypeCont: + if hdr.Size > MaxArchiveBytes-total { + return nil, fail("the restored folder holds more than a backup can, so it was not compared") + } + total += hdr.Size + h := sha256.New() + if _, err := io.Copy(h, tr); err != nil { + return nil, fail("the restored folder could not be read back: %v", err) + } + entry.Kind, entry.Content = "file", hex.EncodeToString(h.Sum(nil)) + case tar.TypeDir: + entry.Kind = "dir" + case tar.TypeSymlink: + entry.Kind, entry.Content = "symlink", hdr.Linkname + case tar.TypeLink: + entry.Kind, entry.Content = "hardlink", belowTop(hdr.Linkname) + default: + entry.Kind = "other" + } + found[path] = entry + } + return found, nil +} + +// ManifestDifferences says how found differs from expected, one line per path, in +// path order; nil when it is the backup. +// +// A folder the image already had at that path (Docker copies the image's folders +// into a new volume when the container is created) may stay when the backup does +// not name it: it is empty, or its files are listed on their own. Every file, link +// and owner must match exactly, the folder's own owner and mode (Root) included. +// Same lines as the web's manifest_differences. +func ManifestDifferences(expected, found map[string]ManifestEntry) []string { + paths := map[string]bool{} + for p := range expected { + paths[p] = true + } + for p := range found { + paths[p] = true + } + sorted := make([]string, 0, len(paths)) + for p := range paths { + sorted = append(sorted, p) + } + sort.Strings(sorted) + var lines []string + for _, path := range sorted { + want, hasWant := expected[path] + got, hasGot := found[path] + if hasWant == hasGot && want == got { + continue + } + if !hasWant && got.Kind == "dir" { + continue + } + shown := path + if path == Root { + shown = "the folder itself" + } + switch { + case !hasWant: + lines = append(lines, shown+": not in the backup") + case !hasGot: + lines = append(lines, shown+": missing") + case want.Kind != got.Kind: + lines = append(lines, fmt.Sprintf("%s: a %s, not a %s", shown, got.Kind, want.Kind)) + case want.Content != got.Content: + lines = append(lines, shown+": different content") + default: + lines = append(lines, shown+": owner or permissions differ") + } + } + return lines +} + +// WritableArchive is one archive from SplitArchives, ready to be extracted into the +// folder that holds the volume's mount point. Its entries already sit under the +// folder's own name, so the folder's entry gives the volume's root the backup's owner +// and mode. The root is the volume's mount point, so only a folder entry may name it: +// any other entry under that exact name is left out, and the read-back then refuses +// the restore (not a folder). Every other entry is copied field by field as +// SplitArchives wrote it. +func WritableArchive(archive []byte) ([]byte, error) { + var buf bytes.Buffer + w := tar.NewWriter(&buf) + tr := tar.NewReader(bytes.NewReader(archive)) + for { + hdr, err := tr.Next() + if errors.Is(err, io.EOF) { + break + } + if err != nil { + return nil, fail("the backup's archive is damaged: %v", err) + } + if belowTop(hdr.Name) == "" && hdr.Typeflag != tar.TypeDir { + continue + } + out := &tar.Header{ + Typeflag: hdr.Typeflag, + Name: hdr.Name, + Linkname: hdr.Linkname, + Size: hdr.Size, + Mode: hdr.Mode & 0o777, + Uid: hdr.Uid, + Gid: hdr.Gid, + ModTime: hdr.ModTime, + Format: tar.FormatPAX, + } + if err := w.WriteHeader(out); err != nil { + return nil, err + } + if hdr.Typeflag == tar.TypeReg { + if _, err := io.Copy(w, tr); err != nil { + return nil, fail("the backup's archive is damaged: %v", err) + } + } + } + if err := w.Close(); err != nil { + return nil, err + } + return buf.Bytes(), nil +} + +// ReadBundleFile reads a backup file, refusing one larger than MaxBundleBytes +// without reading it whole. +func ReadBundleFile(path string) ([]byte, error) { + f, err := os.Open(path) + if err != nil { + return nil, fmt.Errorf("could not open the backup file: %w", err) + } + defer f.Close() + data, err := io.ReadAll(io.LimitReader(f, MaxBundleBytes+1)) + if err != nil { + return nil, fmt.Errorf("could not read the backup file: %w", err) + } + if len(data) > MaxBundleBytes { + return nil, fail("this file is larger than any backup CashPilot makes") + } + return data, nil +} + +// MetadataString is a string field of the header's metadata, or "". +func (h Header) MetadataString(key string) string { + s, _ := h.Metadata[key].(string) + return s +} + +// FolderName is the last path component of a critical target: the name its +// archive's entries sit under (/var/lib/mysterium-node -> mysterium-node). +func FolderName(target string) string { + trimmed := strings.TrimRight(target, "/") + return trimmed[strings.LastIndex(trimmed, "/")+1:] +} diff --git a/internal/statebackup/manifest_test.go b/internal/statebackup/manifest_test.go new file mode 100644 index 00000000..76bbba38 --- /dev/null +++ b/internal/statebackup/manifest_test.go @@ -0,0 +1,204 @@ +package statebackup + +import ( + "archive/tar" + "bytes" + "encoding/json" + "os" + "path/filepath" + "reflect" + "strings" + "testing" + "time" +) + +// webRestoreVectors is testdata/web_restore_vectors.json, written by +// gen_restore_vectors.py with the web app's own restore code. +type webRestoreVectors struct { + Passphrase string `json:"passphrase"` + Bundle string `json:"bundle"` + Manifests map[string][][]any `json:"manifests"` + Cases []struct { + Name string `json:"name"` + Found [][]any `json:"found"` + Lines []string `json:"lines"` + } `json:"cases"` +} + +func loadRestoreVectors(t *testing.T) webRestoreVectors { + t.Helper() + raw, err := os.ReadFile("testdata/web_restore_vectors.json") + if err != nil { + t.Fatal(err) + } + var v webRestoreVectors + if err := json.Unmarshal(raw, &v); err != nil { + t.Fatal(err) + } + return v +} + +// manifestFromRows reads the web's [path, kind, mode, uid, gid, content] rows. +func manifestFromRows(t *testing.T, rows [][]any) map[string]ManifestEntry { + t.Helper() + out := map[string]ManifestEntry{} + for _, r := range rows { + if len(r) != 6 { + t.Fatalf("bad row %v", r) + } + out[r[0].(string)] = ManifestEntry{ + Kind: r[1].(string), Mode: int64(r[2].(float64)), UID: int(r[3].(float64)), GID: int(r[4].(float64)), Content: r[5].(string), + } + } + return out +} + +// A passphrase file the web app sealed opens here, and the folder Go reads out of it +// is the one the web would restore and verify: same paths, types, modes, owners and +// hashes, the setuid bit dropped, the long PAX name intact. +func TestWebBundleManifestMatchesTheWeb(t *testing.T) { + v := loadRestoreVectors(t) + payload, err := Open(b64(t, v.Bundle), v.Passphrase, "") + if err != nil { + t.Fatal(err) + } + archives, err := SplitArchives(payload) + if err != nil { + t.Fatal(err) + } + if len(archives) != len(v.Manifests) { + t.Fatalf("got folders %v, the web has %d", keys(archives), len(v.Manifests)) + } + for top, rows := range v.Manifests { + got, err := TreeManifest(archives[top]) + if err != nil { + t.Fatal(err) + } + want := manifestFromRows(t, rows) + if !reflect.DeepEqual(got, want) { + t.Fatalf("%s manifest differs from the web's\n got %v\n want %v", top, got, want) + } + // What Desktop writes into the volume reads back as the same folder. + writable, err := WritableArchive(archives[top]) + if err != nil { + t.Fatal(err) + } + again, err := TreeManifest(writable) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(again, want) { + t.Fatalf("%s: the archive Desktop writes is not the backup\n got %v\n want %v", top, again, want) + } + } +} + +// The differences a read-back reports are the web's, line for line. +func TestManifestDifferencesMatchTheWeb(t *testing.T) { + v := loadRestoreVectors(t) + base := manifestFromRows(t, v.Manifests["mysterium-node"]) + if len(v.Cases) < 5 { + t.Fatalf("only %d cases in the vectors", len(v.Cases)) + } + for _, c := range v.Cases { + got := ManifestDifferences(base, manifestFromRows(t, c.Found)) + if len(got) == 0 && len(c.Lines) == 0 { + continue + } + if !reflect.DeepEqual(got, c.Lines) { + t.Errorf("%s: got %q, the web says %q", c.Name, got, c.Lines) + } + } +} + +func keys(m map[string][]byte) []string { + var out []string + for k := range m { + out = append(out, k) + } + return out +} + +func headerTar(t *testing.T, entries ...*tar.Header) []byte { + t.Helper() + var buf bytes.Buffer + w := tar.NewWriter(&buf) + for _, h := range entries { + h.ModTime = time.Unix(1767225600, 0) + h.Format = tar.FormatPAX + body := "" + if h.Typeflag == tar.TypeReg { + body = strings.Repeat("k", int(h.Size)) + } + if err := w.WriteHeader(h); err != nil { + t.Fatal(err) + } + if body != "" { + if _, err := w.Write([]byte(body)); err != nil { + t.Fatal(err) + } + } + } + if err := w.Close(); err != nil { + t.Fatal(err) + } + return buf.Bytes() +} + +// The volume's root is its mount point: an archive whose top entry is a file or a +// link must not replace it. The entry is left out, so the read-back finds a folder +// where the backup had something else and refuses. +func TestWritableArchiveDropsANonFolderRoot(t *testing.T) { + archive := headerTar(t, + &tar.Header{Typeflag: tar.TypeSymlink, Name: "anon", Linkname: "elsewhere", Mode: 0o777}, + ) + writable, err := WritableArchive(archive) + if err != nil { + t.Fatal(err) + } + got, err := TreeManifest(writable) + if err != nil { + t.Fatal(err) + } + if len(got) != 0 { + t.Fatalf("the root link was written: %v", got) + } + want, _ := TreeManifest(archive) + found := map[string]ManifestEntry{Root: {Kind: "dir", Mode: 0o755}} + if diff := ManifestDifferences(want, found); len(diff) != 1 || diff[0] != "the folder itself: a dir, not a symlink" { + t.Fatalf("diff = %q", diff) + } +} + +func TestTreeManifestRefusesMoreThanABackupHolds(t *testing.T) { + var buf bytes.Buffer + w := tar.NewWriter(&buf) + _ = w.WriteHeader(&tar.Header{Typeflag: tar.TypeReg, Name: "x/big", Size: MaxArchiveBytes + 1, Mode: 0o600}) + _ = w.Flush() + if _, err := TreeManifest(buf.Bytes()); err == nil || !strings.Contains(err.Error(), "more than a backup can") { + t.Fatalf("err = %v", err) + } +} + +func TestReadBundleFileRefusesAnOversizedFile(t *testing.T) { + path := filepath.Join(t.TempDir(), "big.cpbackup") + f, err := os.Create(path) + if err != nil { + t.Fatal(err) + } + if err := f.Truncate(MaxBundleBytes + 1); err != nil { + t.Fatal(err) + } + f.Close() + if _, err := ReadBundleFile(path); err == nil || !strings.Contains(err.Error(), "larger than any backup") { + t.Fatalf("err = %v", err) + } +} + +func TestFolderName(t *testing.T) { + for in, want := range map[string]string{"/var/lib/mysterium-node": "mysterium-node", "/var/lib/anon/": "anon", "/home/proxybase/.proxybase": ".proxybase"} { + if got := FolderName(in); got != want { + t.Errorf("FolderName(%q) = %q, want %q", in, got, want) + } + } +} diff --git a/internal/statebackup/testdata/gen_restore_vectors.py b/internal/statebackup/testdata/gen_restore_vectors.py new file mode 100644 index 00000000..bf0065b8 --- /dev/null +++ b/internal/statebackup/testdata/gen_restore_vectors.py @@ -0,0 +1,124 @@ +"""Writes web_restore_vectors.json: what the web app's own restore code says about one backup. + +The Go restore tests (manifest_test.go) read it, so the two apps keep judging a +restore the same way: the bundle opens in both, the folder manifests match, and +the differences come out as the same lines. Regenerate from a checkout of the +web repository, with its own environment: + + cd /path/to/CashPilot + uv run python /path/to/CashPilot-Desktop/internal/statebackup/testdata/gen_restore_vectors.py \ + app/state_backup.py > /path/to/CashPilot-Desktop/internal/statebackup/testdata/web_restore_vectors.json + +The passphrase is a fixed test string, not a secret. +""" + +import base64 +import importlib.util +import io +import json +import sys +import tarfile + +PASSPHRASE = "restore vector passphrase, not a secret" +# Over 100 bytes, so it travels as a PAX path record, as a real keystore file does. +KEY_NAME = "UTC--2026-01-01T00-00-00.000000000Z--" + "0123456789abcdef" * 5 + + +def load(path): + spec = importlib.util.spec_from_file_location("state_backup", path) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod + + +def archive(entries): + """One tar, shaped like Docker's archive of a single mount.""" + buf = io.BytesIO() + with tarfile.open(fileobj=buf, mode="w", format=tarfile.PAX_FORMAT) as tar: + for name, kind, mode, uid, data, link in entries: + info = tarfile.TarInfo(name) + info.mode = mode + info.uid = info.gid = uid + info.mtime = 1767225600 + if kind == "dir": + info.type = tarfile.DIRTYPE + tar.addfile(info) + elif kind == "sym": + info.type = tarfile.SYMTYPE + info.linkname = link + tar.addfile(info) + elif kind == "hard": + info.type = tarfile.LNKTYPE + info.linkname = link + tar.addfile(info) + else: + info.size = len(data) + tar.addfile(info, io.BytesIO(data)) + return buf.getvalue() + + +def rows(manifest): + return [[path, *entry] for path, entry in sorted(manifest.items())] + + +def main(): + sb = load(sys.argv[1]) + key = b'{"address":"0x00000000000000000000000000000000000000aa","crypto":{"cipher":"test"}}' + payload = archive( + [ + ("mysterium-node", "dir", 0o700, 0, None, None), + ("mysterium-node/keystore", "dir", 0o700, 0, None, None), + ("mysterium-node/keystore/" + KEY_NAME, "file", 0o600, 0, key, None), + ("mysterium-node/keystore/remember.json", "file", 0o4644, 1000, b'{"identity":"0xaa"}', None), + ("mysterium-node/keystore/copy.json", "hard", 0o4644, 1000, None, "mysterium-node/keystore/remember.json"), + ("mysterium-node/current", "sym", 0o777, 0, None, "keystore/" + KEY_NAME), + ] + ) + meta = {"slug": "mysterium", "worker": "vector-box", "targets": ["/var/lib/mysterium-node"]} + bundle = sb.seal(payload, passphrase=PASSPHRASE, metadata=meta) + archives = sb.split_archives(sb.open_bundle(bundle, passphrase=PASSPHRASE)) + manifests = {top: sb.tree_manifest(data) for top, data in archives.items()} + # The web writes through relative_archive; its manifest must be the backup's. + relative = {top: sb.tree_manifest(sb.relative_archive(data, "volume")) for top, data in archives.items()} + assert relative == manifests, (relative, manifests) + + base = manifests["mysterium-node"] + cases = [] + + def case(name, found): + cases.append({"name": name, "found": rows(found), "lines": sb.manifest_differences(base, found)}) + + case("identical", dict(base)) + changed = dict(base) + path = "keystore/" + KEY_NAME + changed[path] = (*base[path][:4], "0" * 64) + case("changed file", changed) + missing = {k: v for k, v in base.items() if k != "keystore/remember.json"} + case("missing file", missing) + extra = dict(base) + extra["keystore/extra.json"] = ("file", 0o644, 0, 0, "1" * 64) + extra["image-folder"] = ("dir", 0o755, 0, 0, "") + case("extra file and image folder", extra) + owner = dict(base) + owner["."] = ("dir", 0o755, 0, 0, "") + case("root owner", owner) + kind = dict(base) + kind["current"] = ("file", 0o777, 0, 0, "2" * 64) + case("kind", kind) + + print( + json.dumps( + { + "passphrase": PASSPHRASE, + "bundle": base64.b64encode(bundle).decode(), + "manifests": {top: rows(m) for top, m in sorted(manifests.items())}, + "cases": cases, + }, + indent=1, + sort_keys=True, + ) + ) + + +if __name__ == "__main__": + main() diff --git a/internal/statebackup/testdata/web_restore_vectors.json b/internal/statebackup/testdata/web_restore_vectors.json new file mode 100644 index 00000000..8eb8cfd1 --- /dev/null +++ b/internal/statebackup/testdata/web_restore_vectors.json @@ -0,0 +1,400 @@ +{ + "bundle": "AAABMXsibWFnaWMiOiJDQVNIUElMT1QtQkFDS1VQIiwibWV0YWRhdGEiOnsic2x1ZyI6Im15c3Rlcml1bSIsInRhcmdldHMiOlsiL3Zhci9saWIvbXlzdGVyaXVtLW5vZGUiXSwid29ya2VyIjoidmVjdG9yLWJveCJ9LCJtb2RlIjoicGFzc3BocmFzZSIsIm5vbmNlIjoiZmMyYTU0NmNmNTM0YTMyN2VlMjkyM2JlIiwic2FsdCI6IjkyNzZhNWFjN2Q0NWUxZDVmOWNmNmQ1ZTg5MjUzNzE2Iiwic2hhMjU2IjoiMzZmMWNhMGMzZDVhMWEwMGQ3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v5dC6L5BSH3/oW8yOTVCq7DJl5ZxoqLsog4LaDZeJDssIkMyERExqgcO+qjPkgDzDfq9Qn37xLYQFXd1e/fGWaQC/Coe2T4+nnx3y7C7hC4oGuI3kNcdoyXCflY+AdDY7J8X1lu1Ecl0+jxeOIJ8zzcauiNFc6EvovGjyg/aQe0j+i8OQketMb6Pg9JrpXsK0BSbyOdy8FzoC7JWkEKb45ih3B9IzcodwT4ipzHJ0L8Os/rUe8j9G3ci80Gvx+W37yuBxfN7sXTKcpVEBYdpycmtWusMLrlJvMgSyg95/TotZuTyoQuITgJ11+QKd7B7mydH07vCyd5s8sOYjc8/GKxETi5+RX0TB6UVwNgV39mXHAe2cElQruXS0bcLW23pLhdufDC329hF+RW6S/CdxBcvCBhvTsVkzvvneShu7D6pd5a73Kl8zPA8m5sOsHOevmnCpXULB3vSVbYNqSY8YtH/poZ8mmiN4wWfe93hWFMwTEgOW/izHKM0NUhGN4xvZcUqj2IF/gArm41ehSY1e/bApIBIK79Wrb4xkpCjqvnvlte/WI5z+xLGmjXXGHGBnfhv977NHGOcnmjWVNqQnUSCTTyUUKo8GIxCn46kM+dnXd85p3j/dAQp4OLY7qHWpJUYN9qxi6joxAetl/Epbsc+5V723CrgsudVaFzbsQ3fQXaVlowugg6eX0GlXmzFakKfUM8ou01IvAN7Nt4SQncL07Ijcs4d5fbPpu2tjLWkm/FFVjgnAomy0lxQuCZtHVN8GxZFUUvRQtyHhSFvDxQawb5xKJmRaDCRPNe8dWZuHc4pIjyPVxfV+fc03hIj8nqk+mL7el+H/g9qKtp6LtSvBXNvntWqB3jLUOaCdFQF8M1Hpn3fg0KgQ=", + "cases": [ + { + "found": [ + [ + ".", + "dir", + 448, + 0, + 0, + "" + ], + [ + "current", + "symlink", + 511, + 0, + 0, + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" + ], + [ + "keystore", + "dir", + 448, + 0, + 0, + "" + ], + [ + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", + "file", + 384, + 0, + 0, + "2a44306a3fe92b44923c4cb26effc9c4df0282b11778cc7d056610d5703fcfd7" + ], + [ + "keystore/copy.json", + "hardlink", + 420, + 1000, + 1000, + "keystore/remember.json" + ], + [ + "keystore/remember.json", + "file", + 420, + 1000, + 1000, + "1b21960b6523fd2ed8725060199ada94d035c4fd7c2b2729c3a113e67ff150e8" + ] + ], + "lines": [], + "name": "identical" + }, + { + "found": [ + [ + ".", + "dir", + 448, + 0, + 0, + "" + ], + [ + "current", + "symlink", + 511, + 0, + 0, + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" + ], + [ + "keystore", + "dir", + 448, + 0, + 0, + "" + ], + [ + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", + "file", + 384, + 0, + 0, + "0000000000000000000000000000000000000000000000000000000000000000" + ], + [ + "keystore/copy.json", + "hardlink", + 420, + 1000, + 1000, + "keystore/remember.json" + ], + [ + "keystore/remember.json", + "file", + 420, + 1000, + 1000, + "1b21960b6523fd2ed8725060199ada94d035c4fd7c2b2729c3a113e67ff150e8" + ] + ], + "lines": [ + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef: different content" + ], + "name": "changed file" + }, + { + "found": [ + [ + ".", + "dir", + 448, + 0, + 0, + "" + ], + [ + "current", + "symlink", + 511, + 0, + 0, + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" + ], + [ + "keystore", + "dir", + 448, + 0, + 0, + "" + ], + [ + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", + "file", + 384, + 0, + 0, + "2a44306a3fe92b44923c4cb26effc9c4df0282b11778cc7d056610d5703fcfd7" + ], + [ + "keystore/copy.json", + "hardlink", + 420, + 1000, + 1000, + "keystore/remember.json" + ] + ], + "lines": [ + "keystore/remember.json: missing" + ], + "name": "missing file" + }, + { + "found": [ + [ + ".", + "dir", + 448, + 0, + 0, + "" + ], + [ + "current", + "symlink", + 511, + 0, + 0, + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" + ], + [ + "image-folder", + "dir", + 493, + 0, + 0, + "" + ], + [ + "keystore", + "dir", + 448, + 0, + 0, + "" + ], + [ + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", + "file", + 384, + 0, + 0, + "2a44306a3fe92b44923c4cb26effc9c4df0282b11778cc7d056610d5703fcfd7" + ], + [ + "keystore/copy.json", + "hardlink", + 420, + 1000, + 1000, + "keystore/remember.json" + ], + [ + "keystore/extra.json", + "file", + 420, + 0, + 0, + "1111111111111111111111111111111111111111111111111111111111111111" + ], + [ + "keystore/remember.json", + "file", + 420, + 1000, + 1000, + "1b21960b6523fd2ed8725060199ada94d035c4fd7c2b2729c3a113e67ff150e8" + ] + ], + "lines": [ + "keystore/extra.json: not in the backup" + ], + "name": "extra file and image folder" + }, + { + "found": [ + [ + ".", + "dir", + 493, + 0, + 0, + "" + ], + [ + "current", + "symlink", + 511, + 0, + 0, + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" + ], + [ + "keystore", + "dir", + 448, + 0, + 0, + "" + ], + [ + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", + "file", + 384, + 0, + 0, + "2a44306a3fe92b44923c4cb26effc9c4df0282b11778cc7d056610d5703fcfd7" + ], + [ + "keystore/copy.json", + "hardlink", + 420, + 1000, + 1000, + "keystore/remember.json" + ], + [ + "keystore/remember.json", + "file", + 420, + 1000, + 1000, + "1b21960b6523fd2ed8725060199ada94d035c4fd7c2b2729c3a113e67ff150e8" + ] + ], + "lines": [ + "the folder itself: owner or permissions differ" + ], + "name": "root owner" + }, + { + "found": [ + [ + ".", + "dir", + 448, + 0, + 0, + "" + ], + [ + "current", + "file", + 511, + 0, + 0, + "2222222222222222222222222222222222222222222222222222222222222222" + ], + [ + "keystore", + "dir", + 448, + 0, + 0, + "" + ], + [ + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", + "file", + 384, + 0, + 0, + "2a44306a3fe92b44923c4cb26effc9c4df0282b11778cc7d056610d5703fcfd7" + ], + [ + "keystore/copy.json", + "hardlink", + 420, + 1000, + 1000, + "keystore/remember.json" + ], + [ + "keystore/remember.json", + "file", + 420, + 1000, + 1000, + "1b21960b6523fd2ed8725060199ada94d035c4fd7c2b2729c3a113e67ff150e8" + ] + ], + "lines": [ + "current: a file, not a symlink" + ], + "name": "kind" + } + ], + "manifests": { + "mysterium-node": [ + [ + ".", + "dir", + 448, + 0, + 0, + "" + ], + [ + "current", + "symlink", + 511, + 0, + 0, + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" + ], + [ + "keystore", + "dir", + 448, + 0, + 0, + "" + ], + [ + "keystore/UTC--2026-01-01T00-00-00.000000000Z--0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", + "file", + 384, + 0, + 0, + "2a44306a3fe92b44923c4cb26effc9c4df0282b11778cc7d056610d5703fcfd7" + ], + [ + "keystore/copy.json", + "hardlink", + 420, + 1000, + 1000, + "keystore/remember.json" + ], + [ + "keystore/remember.json", + "file", + 420, + 1000, + 1000, + "1b21960b6523fd2ed8725060199ada94d035c4fd7c2b2729c3a113e67ff150e8" + ] + ] + }, + "passphrase": "restore vector passphrase, not a secret" +} diff --git a/internal/store/state_backups.go b/internal/store/state_backups.go index b2d96b9e..d69000c7 100644 --- a/internal/store/state_backups.go +++ b/internal/store/state_backups.go @@ -2,6 +2,8 @@ package store import ( "database/sql" + "sort" + "strings" "time" "github.com/GeiserX/CashPilot-Desktop/internal/statebackup" @@ -53,3 +55,118 @@ func (s *Store) StateBackup(slug string) (*statebackup.Confirmation, error) { c.ConfirmedAt, _ = time.Parse(time.RFC3339, at) return &c, nil } + +// Every backup file this app saved, by the SHA-256 of the file. A restore of a file +// listed here needs no typed confirmation: this app made it, so it knows whose +// identity is inside. Rows are never removed; a hash names one file. +const exportedBackupsDDL = ` + CREATE TABLE IF NOT EXISTS exported_backups ( + sha256 TEXT PRIMARY KEY, + slug TEXT NOT NULL, + exported_at TEXT NOT NULL + ); +` + +// RecordExportedBackup records that this app saved the file with this SHA-256. +func (s *Store) RecordExportedBackup(sha256, slug string, at time.Time) error { + _, err := s.execWrite(`INSERT INTO exported_backups(sha256, slug, exported_at) VALUES(?, ?, ?) ON CONFLICT(sha256) DO NOTHING`, + sha256, slug, at.UTC().Format(time.RFC3339)) + return err +} + +// ExportedBackup returns the service this app saved the file with this SHA-256 +// for, and whether it did. +func (s *Store) ExportedBackup(sha256 string) (string, bool, error) { + var slug string + err := s.db.QueryRow(`SELECT slug FROM exported_backups WHERE sha256 = ?`, sha256).Scan(&slug) + if err == sql.ErrNoRows { + return "", false, nil + } + if err != nil { + return "", false, err + } + return slug, true, nil +} + +// A restore that is under way or did not finish, one row per service, with the +// volumes it creates, each as name:label value (runtime.LabelRestore). Written before +// the restore makes anything and removed once its data read back as the backup, or +// once its undo or Delete the partial restore removed what it made. While a row is +// here, Deploy and Start refuse the service: its volume may be half-written. +const restoreHoldsDDL = ` + CREATE TABLE IF NOT EXISTS restore_holds ( + slug TEXT PRIMARY KEY, + volumes TEXT NOT NULL DEFAULT '', + started_at TEXT NOT NULL + ); +` + +// RestoreHold is one restore that has not finished. +type RestoreHold struct { + Slug string `json:"slug"` + Volumes map[string]string `json:"-"` + StartedAt time.Time `json:"startedAt"` +} + +// HoldRestore records that a restore of slug is under way and makes these volumes. +func (s *Store) HoldRestore(slug string, volumes map[string]string, at time.Time) error { + names := make([]string, 0, len(volumes)) + for name := range volumes { + names = append(names, name) + } + sort.Strings(names) + parts := make([]string, 0, len(names)) + for _, name := range names { + parts = append(parts, name+":"+volumes[name]) + } + _, err := s.execWrite(` + INSERT INTO restore_holds(slug, volumes, started_at) VALUES(?, ?, ?) + ON CONFLICT(slug) DO UPDATE SET volumes=excluded.volumes, started_at=excluded.started_at + `, slug, strings.Join(parts, " "), at.UTC().Format(time.RFC3339)) + return err +} + +// ReleaseRestore removes slug's hold. +func (s *Store) ReleaseRestore(slug string) error { + _, err := s.execWrite(`DELETE FROM restore_holds WHERE slug = ?`, slug) + return err +} + +// RestoreHolds lists every restore that has not finished, by slug. +func (s *Store) RestoreHolds() ([]RestoreHold, error) { + rows, err := s.db.Query(`SELECT slug, volumes, started_at FROM restore_holds ORDER BY slug`) + if err != nil { + return nil, err + } + defer rows.Close() + var out []RestoreHold + for rows.Next() { + var h RestoreHold + var volumes, at string + if err := rows.Scan(&h.Slug, &volumes, &at); err != nil { + return nil, err + } + h.Volumes = map[string]string{} + for _, part := range strings.Fields(volumes) { + name, nonce, _ := strings.Cut(part, ":") + h.Volumes[name] = nonce + } + h.StartedAt, _ = time.Parse(time.RFC3339, at) + out = append(out, h) + } + return out, rows.Err() +} + +// RestoreHeld returns slug's hold, or nil when no restore of it is unfinished. +func (s *Store) RestoreHeld(slug string) (*RestoreHold, error) { + holds, err := s.RestoreHolds() + if err != nil { + return nil, err + } + for i := range holds { + if holds[i].Slug == slug { + return &holds[i], nil + } + } + return nil, nil +} diff --git a/internal/store/store.go b/internal/store/store.go index 2d10aef0..97226277 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -1003,6 +1003,12 @@ func (s *Store) migrate() error { if _, err = s.execWrite(stateBackupsDDL); err != nil { return err } + if _, err = s.execWrite(exportedBackupsDDL); err != nil { + return err + } + if _, err = s.execWrite(restoreHoldsDDL); err != nil { + return err + } if _, err = s.execWrite(customServicesDDL); err != nil { return err } diff --git a/restore_binding.go b/restore_binding.go new file mode 100644 index 00000000..d4a2829f --- /dev/null +++ b/restore_binding.go @@ -0,0 +1,351 @@ +package main + +import ( + "crypto/rand" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "path/filepath" + "sort" + "strings" + "time" + + "github.com/GeiserX/CashPilot-Desktop/internal/runtime" + "github.com/GeiserX/CashPilot-Desktop/internal/statebackup" + wailsruntime "github.com/wailsapp/wails/v2/pkg/runtime" +) + +// Settings > Restore from a backup (issue #298): puts a node identity or wallet back +// on this machine from a .cpbackup file. Desktop port of the web's restore +// (GeiserX/CashPilot #741, #768), with the same rules: +// +// - It only creates: the service must not be on this machine in any state, and its +// identity volume must not exist (services.Manager.RestoreRefusal). +// - Passphrase files only. A file sealed to a key stays on cpbackup: its private +// key never goes through the app. +// - A file this app did not export needs the service's name typed, because nobody +// can tell from the file whose identity is inside. The export records each +// file's SHA-256 for this (store.RecordExportedBackup). +// - One identity, one machine: a file this app does not know is refused while the +// machine named inside it runs the service, as far as this app's fleet list +// shows (the web's made_on rule). The header is used only to refuse more. +// - The frontend asks the user to confirm, in place of the web's password step-up: +// Desktop has one user. +// +// The runtime creates without starting, writes, reads back and compares, and starts +// only once the data is the backup (internal/runtime/restore.go). + +// restoreFileTTL is how long a chosen file stays usable. The file is never copied; +// only its path and SHA-256 are kept, and the restore reads it again. +const restoreFileTTL = 15 * time.Minute + +// restoreFile is a file the user chose and the preview checked. +type restoreFile struct { + path string + sha256 string + slug string + chosen time.Time +} + +// RestoreVolume is one volume a restore creates. +type RestoreVolume struct { + Target string `json:"target"` + Volume string `json:"volume"` +} + +// RestorePreview is what restoring the chosen file would do on this machine. +type RestorePreview struct { + // Token names the chosen file in RestoreStateBackup. Single use. + Token string `json:"token"` + File string `json:"file"` + Slug string `json:"slug"` + Service string `json:"service"` + Bytes int `json:"bytes"` + SHA256 string `json:"sha256"` + // Known is true when this app exported this exact file. Otherwise the restore + // needs the service's name typed. + Known bool `json:"known"` + // MadeOn is the machine the file's header names. Anyone with the passphrase can + // write a header, so it is shown and used only to refuse more. + MadeOn string `json:"madeOn"` + Container string `json:"container"` + Creates []RestoreVolume `json:"creates"` + // OK is false when this machine refuses the restore now; Why says why. + OK bool `json:"ok"` + Why string `json:"why"` + // RunsOn names the fleet machines that report this service now. For a file + // this app does not know, one of them may run the very identity inside it. + RunsOn []string `json:"runsOn"` +} + +// UnfinishedRestore is a restore that is running or did not finish. +type UnfinishedRestore struct { + Slug string `json:"slug"` + Service string `json:"service"` + Volumes []string `json:"volumes"` + StartedAt string `json:"startedAt"` +} + +// askWhichFile opens the open-file dialog, or the stand-in a test installed. +func (a *App) askWhichFile(opts wailsruntime.OpenDialogOptions) (string, error) { + if a.openDialog != nil { + return a.openDialog(opts) + } + return wailsruntime.OpenFileDialog(a.ctx, opts) +} + +// ChooseRestoreFile asks for a .cpbackup file and says what restoring it would do. +// Nil when the dialog was dismissed. Nothing secret is asked for yet: a file sealed +// to a key is refused here. +func (a *App) ChooseRestoreFile() (*RestorePreview, error) { + if err := a.ready(); err != nil { + return nil, err + } + path, err := a.askWhichFile(wailsruntime.OpenDialogOptions{ + Title: "Choose a backup to restore", + Filters: []wailsruntime.FileFilter{ + {DisplayName: "CashPilot backups (*.cpbackup)", Pattern: "*.cpbackup"}, + }, + }) + if err != nil { + return nil, err + } + if strings.TrimSpace(path) == "" { + return nil, nil + } + data, header, sum, err := readRestoreFile(path) + if err != nil { + return nil, err + } + slug := header.MetadataString("slug") + targets, err := a.services.RestoreVolumes(slug) + if err != nil { + return nil, err + } + preview := &RestorePreview{ + File: filepath.Base(path), + Slug: slug, + Service: a.serviceName(slug), + Bytes: len(data), + SHA256: sum, + MadeOn: header.MetadataString("worker"), + Container: "cashpilot-" + slug, + } + for _, target := range sortedKeys(targets) { + preview.Creates = append(preview.Creates, RestoreVolume{Target: target, Volume: targets[target]}) + } + if preview.Known, err = a.exportedHere(sum, slug); err != nil { + return nil, err + } + preview.RunsOn = a.fleetRunning(slug) + why, err := a.restoreRefusal(slug, preview.Known, preview.MadeOn) + if err != nil { + return nil, err + } + preview.Why, preview.OK = why, why == "" + + token, err := restoreToken() + if err != nil { + return nil, err + } + preview.Token = token + a.restoreMu.Lock() + defer a.restoreMu.Unlock() + a.dropExpiredRestoreFiles(time.Now()) + if a.restoreFiles == nil { + a.restoreFiles = map[string]restoreFile{} + } + a.restoreFiles[token] = restoreFile{path: path, sha256: sum, slug: slug, chosen: time.Now()} + return preview, nil +} + +// RestoreStateBackup restores the file the preview named by token. passphrase opens +// it; typedName is the service's name or slug, needed when this app did not export +// the file. It is used once and never kept or logged. +func (a *App) RestoreStateBackup(token, passphrase, typedName string) (runtime.RestoreResult, error) { + if err := a.ready(); err != nil { + return runtime.RestoreResult{}, err + } + a.restoreMu.Lock() + a.dropExpiredRestoreFiles(time.Now()) + chosen, ok := a.restoreFiles[token] + // Single use: a second press, or a retry after a failure, chooses the file again + // and sees a fresh preview. + delete(a.restoreFiles, token) + a.restoreMu.Unlock() + if !ok { + return runtime.RestoreResult{}, errors.New("choose the backup file again: the one chosen was already used or waited longer than 15 minutes") + } + data, header, sum, err := readRestoreFile(chosen.path) + if err != nil { + return runtime.RestoreResult{}, err + } + slug := header.MetadataString("slug") + if sum != chosen.sha256 || slug != chosen.slug { + return runtime.RestoreResult{}, errors.New("the file changed since you chose it, so nothing was restored. Choose it again") + } + known, err := a.exportedHere(sum, slug) + if err != nil { + return runtime.RestoreResult{}, err + } + if !known { + typed := strings.ToLower(strings.TrimSpace(typedName)) + if typed == "" || (typed != strings.ToLower(slug) && typed != strings.ToLower(a.serviceName(slug))) { + return runtime.RestoreResult{}, fmt.Errorf("this app did not make this file, so type %s to say whose identity is inside", a.serviceName(slug)) + } + } + if why, err := a.restoreRefusal(slug, known, header.MetadataString("worker")); err != nil { + return runtime.RestoreResult{}, err + } else if why != "" { + return runtime.RestoreResult{}, fmt.Errorf("nothing was restored: %s", strings.TrimSuffix(why, ".")) + } + payload, err := statebackup.Open(data, passphrase, "") + if err != nil { + return runtime.RestoreResult{}, err + } + archives, err := statebackup.SplitArchives(payload) + if err != nil { + return runtime.RestoreResult{}, err + } + creds, err := a.store.GetCredentials(slug) + if err != nil { + return runtime.RestoreResult{}, err + } + res, err := a.services.Restore(a.ctx, slug, creds, archives) + if err != nil { + a.emitError("restore", err) + return res, err + } + // The user holds the file this node now runs from, so its backup is confirmed. + live, _ := a.services.StateFingerprint(a.ctx, slug) + _ = a.store.ConfirmStateBackup(slug, live, "restore", time.Now()) + if dep, ok, err := a.store.GetDeployment(slug); err == nil && ok { + a.emitEvent("deployment:changed", dep) + } + if a.collectors != nil && a.collectors.Supports(slug) { + go a.collectOne(a.ctx, slug) + } + return res, nil +} + +// UnfinishedRestores lists the restores that are running or did not finish. While +// one is listed, Deploy and Start refuse its service. +func (a *App) UnfinishedRestores() ([]UnfinishedRestore, error) { + if err := a.ready(); err != nil { + return nil, err + } + holds, err := a.services.UnfinishedRestores() + if err != nil { + return nil, err + } + out := []UnfinishedRestore{} + for _, h := range holds { + out = append(out, UnfinishedRestore{ + Slug: h.Slug, + Service: a.serviceName(h.Slug), + Volumes: sortedKeys(h.Volumes), + StartedAt: h.StartedAt.Format(time.RFC3339), + }) + } + return out, nil +} + +// DiscardPartialRestore deletes what an unfinished restore of slug left: its +// container and the volumes the restore made, never a volume another program put +// under the same name. Then Deploy and Start accept the service again. +func (a *App) DiscardPartialRestore(slug string) error { + if err := a.ready(); err != nil { + return err + } + if err := a.services.DiscardRestore(a.ctx, slug); err != nil { + a.emitError("restore", err) + return err + } + return nil +} + +// readRestoreFile reads a backup file, refuses one that is not a passphrase file, +// and returns it with its header and SHA-256. +func readRestoreFile(path string) ([]byte, statebackup.Header, string, error) { + data, err := statebackup.ReadBundleFile(path) + if err != nil { + return nil, statebackup.Header{}, "", err + } + header, err := statebackup.ReadHeader(data) + if err != nil { + return nil, statebackup.Header{}, "", err + } + if header.Mode != statebackup.ModePassphrase { + return nil, statebackup.Header{}, "", errors.New("this file is sealed to a key, not a passphrase. Its private key never goes through the app: restore it with the command line steps in the backup guide") + } + sum := sha256.Sum256(data) + return data, header, hex.EncodeToString(sum[:]), nil +} + +// exportedHere is whether this app exported this exact file, of this service. +func (a *App) exportedHere(sum, slug string) (bool, error) { + exported, ok, err := a.store.ExportedBackup(sum) + if err != nil { + return false, fmt.Errorf("could not read which backups this app made: %w", err) + } + return ok && exported == slug, nil +} + +// fleetRunning names the fleet machines that report slug, sorted. +func (a *App) fleetRunning(slug string) []string { + names := []string{} + for _, device := range a.store.ListFleetDevices() { + for _, running := range device.Services { + if strings.TrimSpace(running) == slug { + names = append(names, strings.TrimSpace(device.Name)) + break + } + } + } + sort.Strings(names) + return names +} + +// restoreRefusal says why slug cannot be restored here now, or "": every check of +// the manager and the runtime, then the one-identity rule. A file this app does not +// know is refused while the machine its header names runs the service: the file may +// hold the very identity that machine runs. An offline machine counts, since it may +// come back with the node. +func (a *App) restoreRefusal(slug string, known bool, madeOn string) (string, error) { + creds, err := a.store.GetCredentials(slug) + if err != nil { + return "", err + } + if why, err := a.services.RestoreRefusal(a.ctx, slug, creds); why != "" || err != nil { + return why, err + } + madeOn = strings.TrimSpace(madeOn) + if known || madeOn == "" { + return "", nil + } + for _, name := range a.fleetRunning(slug) { + if strings.EqualFold(name, madeOn) { + return fmt.Sprintf("this file was made on %s, which still runs %s. Two machines on one identity look like one node to the provider, which may merge or ban them. Remove %s from %s first, then restore", name, a.serviceName(slug), a.serviceName(slug), name), nil + } + } + return "", nil +} + +// dropExpiredRestoreFiles forgets chosen files older than restoreFileTTL. The +// caller holds restoreMu. +func (a *App) dropExpiredRestoreFiles(now time.Time) { + for token, f := range a.restoreFiles { + if now.Sub(f.chosen) > restoreFileTTL { + delete(a.restoreFiles, token) + } + } +} + +func restoreToken() (string, error) { + b := make([]byte, 18) + if _, err := rand.Read(b); err != nil { + return "", err + } + return hex.EncodeToString(b), nil +} diff --git a/restore_binding_test.go b/restore_binding_test.go new file mode 100644 index 00000000..f78f83fc --- /dev/null +++ b/restore_binding_test.go @@ -0,0 +1,362 @@ +package main + +import ( + "context" + "errors" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/GeiserX/CashPilot-Desktop/internal/catalog" + "github.com/GeiserX/CashPilot-Desktop/internal/runtime" + "github.com/GeiserX/CashPilot-Desktop/internal/services" + "github.com/GeiserX/CashPilot-Desktop/internal/statebackup" + "github.com/GeiserX/CashPilot-Desktop/internal/store" + wailsruntime "github.com/wailsapp/wails/v2/pkg/runtime" +) + +const restorePass = "correct horse battery" + +// restoreStub is the Docker provider with the restore's runtime side replaced: it +// records what it was asked to restore, holds and releases as the runtime does, and +// fails when told to. +type restoreStub struct { + *runtime.DockerProvider + refusal string + fail error + left []string + got *runtime.RestoreRequest + calls *int + discards *int +} + +func (s restoreStub) RestoreRefusal(context.Context, string, catalog.Service) (string, error) { + return s.refusal, nil +} + +func (s restoreStub) RestoreState(_ context.Context, req runtime.RestoreRequest) (runtime.RestoreResult, error) { + *s.calls++ + *s.got = req + if err := req.Hold(map[string]string{"keeper-data": "nonce"}); err != nil { + return runtime.RestoreResult{}, err + } + if s.fail != nil { + return runtime.RestoreResult{Left: s.left}, s.fail + } + if err := req.Release(); err != nil { + return runtime.RestoreResult{}, err + } + return runtime.RestoreResult{ContainerID: "c1", Name: "cashpilot-keeper", Image: "keeper/node:1.0.0", Targets: []string{"/var/lib/keeper"}, Files: 1, Started: true}, nil +} + +func (s restoreStub) DiscardRestore(context.Context, string, map[string]string) ([]string, error) { + *s.discards++ + return nil, nil +} + +func (s restoreStub) StateFingerprint(context.Context, string, []string) (string, error) { + return "fp", nil +} + +// restoreApp is backupApp with keeper not deployed (a restore only creates) and the +// stub runtime. The file chooser returns *chosen. +type restoreHarness struct { + app *App + stub *restoreStub + chosen string + calls int + discards int + req runtime.RestoreRequest +} + +func newRestoreHarness(t *testing.T) *restoreHarness { + t.Helper() + var dialogs int + h := &restoreHarness{app: backupApp(t, &dialogs)} + if err := h.app.store.DeleteDeployment("keeper"); err != nil { + t.Fatal(err) + } + h.stub = &restoreStub{DockerProvider: runtime.NewDockerProvider(), got: &h.req, calls: &h.calls, discards: &h.discards} + h.use() + h.app.openDialog = func(wailsruntime.OpenDialogOptions) (string, error) { return h.chosen, nil } + return h +} + +// use installs the stub as it is now. +func (h *restoreHarness) use() { + h.app.services = services.NewManager(*h.stub, h.app.catalog, h.app.store) +} + +// keeperFile seals the keeper folder into a passphrase file the way an export does, +// and makes it the file the chooser returns. +func (h *restoreHarness) keeperFile(t *testing.T, worker string) string { + t.Helper() + bundle, err := statebackup.Seal(dirsTar(t, 2), restorePass, "", map[string]any{ + "slug": "keeper", "worker": worker, "targets": []string{"/var/lib/keeper"}, + }) + if err != nil { + t.Fatal(err) + } + h.chosen = filepath.Join(t.TempDir(), "keeper.cpbackup") + if err := os.WriteFile(h.chosen, bundle, 0o600); err != nil { + t.Fatal(err) + } + return h.chosen +} + +func (h *restoreHarness) choose(t *testing.T) *RestorePreview { + t.Helper() + p, err := h.app.ChooseRestoreFile() + if err != nil { + t.Fatal(err) + } + if p == nil { + t.Fatal("no preview") + } + return p +} + +// A file this app did not make needs the service's name typed, and the restore then +// runs once: the archives reach the runtime, the deployment is recorded, the backup +// counts as confirmed, and the hold is gone. +func TestRestoreOfAForeignFileNeedsTheNameTyped(t *testing.T) { + h := newRestoreHarness(t) + h.keeperFile(t, "some-box") + p := h.choose(t) + if p.Known || !p.OK || p.Slug != "keeper" || p.Service != "Keeper" || p.MadeOn != "some-box" || + len(p.Creates) != 1 || p.Creates[0] != (RestoreVolume{Target: "/var/lib/keeper", Volume: "keeper-data"}) { + t.Fatalf("preview = %+v", p) + } + if _, err := h.app.RestoreStateBackup(p.Token, restorePass, "plain"); err == nil || !strings.Contains(err.Error(), "type Keeper") { + t.Fatalf("restored a foreign file without its name: %v", err) + } + if h.calls != 0 { + t.Fatal("the runtime was asked to restore") + } + // The token was used: the retry chooses the file again. + if _, err := h.app.RestoreStateBackup(p.Token, restorePass, "keeper"); err == nil || !strings.Contains(err.Error(), "choose the backup file again") { + t.Fatalf("a used token restored: %v", err) + } + p = h.choose(t) + res, err := h.app.RestoreStateBackup(p.Token, restorePass, " KEEPER ") + if err != nil { + t.Fatal(err) + } + if h.calls != 1 || !res.Started || len(h.req.Archives["keeper"]) == 0 || len(h.req.Archives) != 1 { + t.Fatalf("calls %d, result %+v, archives %v", h.calls, res, len(h.req.Archives)) + } + if dep, ok, _ := h.app.store.GetDeployment("keeper"); !ok || dep.Status != "running" || dep.ContainerID != "c1" { + t.Fatalf("deployment = %+v %v", dep, ok) + } + if c, _ := h.app.store.StateBackup("keeper"); c == nil || c.Method != "restore" { + t.Fatalf("backup confirmation = %+v", c) + } + if hold, _ := h.app.store.RestoreHeld("keeper"); hold != nil { + t.Fatalf("hold left after a restore that finished: %+v", hold) + } +} + +// A file this app exported restores with no name typed: the export recorded its hash. +func TestRestoreOfAnExportedFileNeedsNoName(t *testing.T) { + h := newRestoreHarness(t) + // Export through the real binding, with keeper deployed and a stub state read. + if err := h.app.store.UpsertDeployment(store.Deployment{Slug: "keeper", Name: "cashpilot-keeper", Status: "running", Runtime: "existing-docker"}); err != nil { + t.Fatal(err) + } + dest := filepath.Join(t.TempDir(), "keeper.cpbackup") + h.app.saveDialog = func(wailsruntime.SaveDialogOptions) (string, error) { return dest, nil } + h.app.services = services.NewManager(stateStub{runtime.NewDockerProvider(), dirsTar(t, 2)}, h.app.catalog, h.app.store) + if _, err := h.app.ExportStateBackup("keeper", restorePass, restorePass, ""); err != nil { + t.Fatal(err) + } + if err := h.app.store.DeleteDeployment("keeper"); err != nil { + t.Fatal(err) + } + h.use() + h.chosen = dest + p := h.choose(t) + if !p.Known || !p.OK { + t.Fatalf("the app's own export is not known: %+v", p) + } + if _, err := h.app.RestoreStateBackup(p.Token, restorePass, ""); err != nil { + t.Fatal(err) + } + // The same file with one byte changed is another file: not known. + data, _ := os.ReadFile(dest) + data = append(data, 0) + altered := filepath.Join(t.TempDir(), "altered.cpbackup") + _ = os.WriteFile(altered, data, 0o600) + _ = h.app.store.DeleteDeployment("keeper") + h.chosen = altered + if p := h.choose(t); p.Known { + t.Fatal("a changed file counted as the app's own export") + } +} + +// A file sealed to a key is refused before anything secret is asked for. +func TestRestoreRefusesAKeySealedFile(t *testing.T) { + h := newRestoreHarness(t) + _, public, err := statebackup.GenerateKeypair() + if err != nil { + t.Fatal(err) + } + bundle, err := statebackup.Seal(dirsTar(t, 1), "", public, map[string]any{"slug": "keeper"}) + if err != nil { + t.Fatal(err) + } + h.chosen = filepath.Join(t.TempDir(), "sealed.cpbackup") + _ = os.WriteFile(h.chosen, bundle, 0o600) + if p, err := h.app.ChooseRestoreFile(); err == nil || !strings.Contains(err.Error(), "sealed to a key") { + t.Fatalf("preview %+v, err %v", p, err) + } +} + +// Every refusal stops the restore before the runtime is asked, and the preview says +// why: the service is deployed here, the runtime refuses (its volume exists), the +// machine named in a foreign file runs the service, a wrong passphrase, a file that +// changed since it was chosen, a service the app cannot restore. +func TestRestoreRefusals(t *testing.T) { + h := newRestoreHarness(t) + h.keeperFile(t, "other-box") + + // Deployed here. + _ = h.app.store.UpsertDeployment(store.Deployment{Slug: "keeper", Name: "cashpilot-keeper", Status: "exited", Runtime: "existing-docker"}) + if p := h.choose(t); p.OK || !strings.Contains(p.Why, "already deployed") { + t.Fatalf("deployed: %+v", p) + } + _ = h.app.store.DeleteDeployment("keeper") + + // The runtime's own refusal (a container or the volume is there). + h.stub.refusal = "the volume keeper-data already exists on this machine" + h.use() + p := h.choose(t) + if p.OK || p.Why != h.stub.refusal { + t.Fatalf("runtime refusal: %+v", p) + } + if _, err := h.app.RestoreStateBackup(p.Token, restorePass, "keeper"); err == nil || !strings.Contains(err.Error(), "already exists") { + t.Fatalf("restored over a refusal: %v", err) + } + h.stub.refusal = "" + h.use() + + // The machine the foreign file names still runs the service. + if _, err := h.app.store.UpsertFleetDevice(store.FleetDevice{Name: "Other-Box", Services: []string{"keeper"}}); err != nil { + t.Fatal(err) + } + p = h.choose(t) + if p.OK || !strings.Contains(p.Why, "made on Other-Box, which still runs Keeper") || len(p.RunsOn) != 1 { + t.Fatalf("identity elsewhere: %+v", p) + } + if _, err := h.app.RestoreStateBackup(p.Token, restorePass, "keeper"); err == nil || !strings.Contains(err.Error(), "still runs Keeper") { + t.Fatalf("restored an identity another machine runs: %v", err) + } + // A machine the file does not name only shows up as running the service. + h.keeperFile(t, "third-box") + if p := h.choose(t); !p.OK || len(p.RunsOn) != 1 || p.RunsOn[0] != "Other-Box" { + t.Fatalf("another machine: %+v", p) + } + + // Wrong passphrase. + p = h.choose(t) + if _, err := h.app.RestoreStateBackup(p.Token, "not the passphrase", "keeper"); err == nil || !strings.Contains(err.Error(), "could not decrypt") { + t.Fatalf("wrong passphrase: %v", err) + } + + // The file changed after the preview. + p = h.choose(t) + h.keeperFile(t, "third-box") + other := h.chosen + data, _ := os.ReadFile(other) + h.app.restoreMu.Lock() + f := h.app.restoreFiles[p.Token] + h.app.restoreMu.Unlock() + _ = os.WriteFile(f.path, data, 0o600) // same path, new contents (a new salt) + if _, err := h.app.RestoreStateBackup(p.Token, restorePass, "keeper"); err == nil || !strings.Contains(err.Error(), "changed since you chose it") { + t.Fatalf("restored a file that changed: %v", err) + } + + // An expired choice. + p = h.choose(t) + h.app.restoreMu.Lock() + f = h.app.restoreFiles[p.Token] + f.chosen = time.Now().Add(-restoreFileTTL - time.Second) + h.app.restoreFiles[p.Token] = f + h.app.restoreMu.Unlock() + if _, err := h.app.RestoreStateBackup(p.Token, restorePass, "keeper"); err == nil || !strings.Contains(err.Error(), "15 minutes") { + t.Fatalf("an expired choice restored: %v", err) + } + + // A service with nothing a backup restores. + bundle, _ := statebackup.Seal(dirsTar(t, 1), restorePass, "", map[string]any{"slug": "plain"}) + h.chosen = filepath.Join(t.TempDir(), "plain.cpbackup") + _ = os.WriteFile(h.chosen, bundle, 0o600) + if _, err := h.app.ChooseRestoreFile(); err == nil || !strings.Contains(err.Error(), "nothing a backup restores") { + t.Fatalf("plain: %v", err) + } + + if h.calls != 0 { + t.Fatalf("the runtime was asked to restore %d times", h.calls) + } +} + +// A restore whose undo could not remove what it made keeps the service held: Deploy +// and Start refuse it, the settings list it, and deleting the partial restore lifts +// the hold. One the undo cleaned up is not held. +func TestAnUnfinishedRestoreHoldsTheService(t *testing.T) { + h := newRestoreHarness(t) + h.keeperFile(t, "") + + h.stub.fail = errors.New("read back differs. The restore was undone.") + h.use() + p := h.choose(t) + if _, err := h.app.RestoreStateBackup(p.Token, restorePass, "keeper"); err == nil { + t.Fatal("a failed restore reported success") + } + if hold, _ := h.app.store.RestoreHeld("keeper"); hold != nil { + t.Fatalf("a restore the undo cleaned up is still held: %+v", hold) + } + + h.stub.left = []string{"keeper-data"} + h.use() + p = h.choose(t) + if _, err := h.app.RestoreStateBackup(p.Token, restorePass, "keeper"); err == nil { + t.Fatal("a failed restore reported success") + } + list, err := h.app.UnfinishedRestores() + if err != nil || len(list) != 1 || list[0].Slug != "keeper" || list[0].Service != "Keeper" || len(list[0].Volumes) != 1 || list[0].Volumes[0] != "keeper-data" { + t.Fatalf("unfinished = %+v, %v", list, err) + } + ctx := context.Background() + if _, err := h.app.services.DeployWith(ctx, "keeper", nil, services.DeployOptions{}); err == nil || !strings.Contains(err.Error(), "half-written") { + t.Fatalf("deploy of a held service: %v", err) + } + if err := h.app.services.Start(ctx, "keeper"); err == nil || !strings.Contains(err.Error(), "half-written") { + t.Fatalf("start of a held service: %v", err) + } + if p := h.choose(t); p.OK || !strings.Contains(p.Why, "did not finish") { + t.Fatalf("restore of a held service: %+v", p) + } + + if err := h.app.DiscardPartialRestore("keeper"); err != nil { + t.Fatal(err) + } + if h.discards != 1 { + t.Fatalf("discards = %d", h.discards) + } + if list, _ := h.app.UnfinishedRestores(); len(list) != 0 { + t.Fatalf("still listed: %+v", list) + } + if err := h.app.DiscardPartialRestore("keeper"); err == nil { + t.Fatal("discarded a restore that is not unfinished") + } +} + +func TestChooseRestoreFileDismissed(t *testing.T) { + h := newRestoreHarness(t) + h.chosen = "" + if p, err := h.app.ChooseRestoreFile(); p != nil || err != nil { + t.Fatalf("dismissed: %+v, %v", p, err) + } +} diff --git a/state_backup_binding.go b/state_backup_binding.go index aec6f33a..0af3fafd 100644 --- a/state_backup_binding.go +++ b/state_backup_binding.go @@ -1,7 +1,9 @@ package main import ( + "crypto/sha256" "crypto/subtle" + "encoding/hex" "errors" "fmt" "os" @@ -152,6 +154,12 @@ func (a *App) ExportStateBackup(slug, passphrase, passphraseRepeat, recipientPub if err := os.WriteFile(path, bundle, 0o600); err != nil { return "", fmt.Errorf("could not save the backup: %w", err) } + // A restore of this exact file needs no typed service name: this app made it, + // so it knows whose identity is inside (restore_binding.go). + sum := sha256.Sum256(bundle) + if err := a.store.RecordExportedBackup(hex.EncodeToString(sum[:]), slug, time.Now()); err != nil { + return path, fmt.Errorf("the backup was saved to %s, but the app could not record that it made it, so a restore of it will ask for the service name: %w", path, err) + } live, _ := a.services.StateFingerprint(a.ctx, slug) if err := a.store.ConfirmStateBackup(slug, live, "export", time.Now()); err != nil { return path, fmt.Errorf("the backup was saved to %s, but could not be recorded as confirmed: %w", path, err) From abf3c3411c6d01f29e5199510ff6bf55e0c8850c Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:28:26 +0100 Subject: [PATCH 2/5] feat(restore): add the Restore from a backup card to Settings The restore needs a place in the app: Settings gets a card that chooses a .cpbackup file, shows what the restore creates and why this machine would refuse it, asks for the service name for a file the app did not make, and confirms before it writes. Restores that did not finish are listed there with a button to delete the partial restore. The backup guide and the changelog describe the rules. --- CHANGELOG.md | 4 ++ docs/backup.md | 49 ++++++++++++- frontend/package.json | 2 +- frontend/scripts/backups_render_check.mjs | 1 + frontend/scripts/restore_render_check.mjs | 82 ++++++++++++++++++++++ frontend/src/main.ts | 84 ++++++++++++++++++++++- frontend/src/render/backups.ts | 3 +- frontend/src/render/restore.ts | 77 +++++++++++++++++++++ frontend/src/wails.d.ts | 51 ++++++++++++++ frontend/wailsjs/go/main/App.js | 16 +++++ 10 files changed, 365 insertions(+), 4 deletions(-) create mode 100644 frontend/scripts/restore_render_check.mjs create mode 100644 frontend/src/render/restore.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 40b66166..7251124b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- **Restore a node identity backup from the app.** Settings > Restore from a backup takes a `.cpbackup` file made with a passphrase and puts Mysterium, ProxyBase or Anyone back on this machine, with the web app's rules. It only creates: the service must not be here in any state and its identity volume must not exist. A file sealed to a key stays on `cpbackup`. A file this app did not export needs the service name typed, and is refused while the machine named inside it still runs the service. The container is created without starting, the backup is written into its new volume, read back and compared file by file, and only then does it start; Mysterium only behind the LAN fence. Any failure removes what the restore made. A restore that did not finish holds the service, so Deploy and Start refuse it until the partial restore is deleted. Exports now record each file's SHA-256 so the app knows its own files (docs/backup.md). + ### Changed - **1.0.0 is the first stable release.** Nothing breaks: config, database, saved credentials and deployed services carry over. An install from before v0.37.0 keeps its earners on Docker's default bridge until Turn on isolation is pressed, and a proxied copy from before v0.39.1 keeps its tunnel there until it is deployed again. `wails.json`, `package.json` and `frontend/package.json` now say 1.0.0; release builds still take the version from the tag. What 1.0 covers, upgrade notes and known limits: docs/whats-new-1.0.md. diff --git a/docs/backup.md b/docs/backup.md index 95c5b2fc..8a300394 100644 --- a/docs/backup.md +++ b/docs/backup.md @@ -41,7 +41,54 @@ scheduled backup: a timer would need a stored key. The file format is the CashPilot web app's, so either app's tool opens either app's backups. -## Restore a backup +## Restore from the app + +**Settings > Restore from a backup** puts a node identity or wallet back on this +machine from a `.cpbackup` file. It covers Mysterium, ProxyBase and Anyone, whose +identity sits on a named Docker volume. Choose the file, read what the restore +would create, type the passphrase and confirm. The rules: + +- **It only creates.** This machine must not have the service in any state (no + container, no copy, nothing set aside), and its identity volume must not exist. + The restore never writes over anything. To replace a running identity, download + its backup first, remove the service and say yes to deleting its data, then + restore. +- **Passphrase files only.** A file sealed to a key is refused before anything + secret is asked for: its private key never goes through the app. Use the + command line steps below for it. +- **Files the app did not make need the name typed.** The app keeps the SHA-256 of + every file it exports. For any other file you type the service name, because + nobody can tell from the file whose identity is inside. A file exported before + this record existed is one of these. +- **One identity, one machine.** For a file the app did not make, the restore is + refused while the machine named inside the file still runs the service, as far + as this app's fleet list shows. The preview also names the fleet machines that + run the service now. Two machines on one identity look like one node to the + provider, which may merge or ban them. +- **Checked before it starts.** The app creates the volumes, each with a random + `cashpilot.restore` label, and the container without starting it. It writes the + backup into the volume, reads the folder back and compares every file, link, + owner and permission with the backup. Only then does the container start. + Mysterium starts only behind the LAN fence; until the fence holds it waits, and + the fence guard never starts it before its data is checked. On any failure the + container and the volumes the restore made are removed. A volume under the same + name without the restore's label belongs to another program and stays. +- **A restore that did not finish holds the service.** Before it makes anything, + the app records that a restore is under way. The record goes once the data reads + back as the backup, or once the undo removed everything. If the app stops + mid-restore, or the undo cannot remove something, the record stays: Deploy, + Start and Restore refuse the service, and the Restore card lists it. **Delete + the partial restore** removes its container and the volumes the restore made, + then the service is free again. +- **Nothing kept.** The app reads the file where it is and keeps only its path and + SHA-256 for 15 minutes; the restore reads it again and refuses a file that + changed. The passphrase is used once and never stored or logged. The service's + saved settings are used, so fill them in on its deploy form first. + +Storj and Sia keep their state in host folders, and Storj's is over the export +cap: restore those with the command line steps. + +## Restore a backup from the command line **1. Open the file** on a machine you trust (needs Go and git). Run `cpbackup` from a checkout of the release that made the backup: diff --git a/frontend/package.json b/frontend/package.json index 521d4db7..db74a0e1 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -8,7 +8,7 @@ "build": "tsc && vite build", "preview": "vite preview", "harness:build": "tsc -p tsconfig.harness.json", - "test": "npm run harness:build && node scripts/fleet_render_check.mjs && node scripts/earnings_render_check.mjs && node scripts/status_render_check.mjs && node scripts/total_render_check.mjs && node scripts/fields_check.mjs && node scripts/removal_check.mjs && node scripts/preflight_render_check.mjs && node scripts/producer_render_check.mjs && node scripts/details_check.mjs && node scripts/wizard_render_check.mjs && node scripts/image_check.mjs && node scripts/instances_check.mjs && node scripts/console_render_check.mjs && node scripts/topbar_render_check.mjs && node scripts/drift_render_check.mjs && node scripts/usage_render_check.mjs && node scripts/backups_render_check.mjs && node scripts/activity_check.mjs && node scripts/custom_check.mjs && node scripts/importer_check.mjs && node scripts/rating_check.mjs && node scripts/isolation_render_check.mjs && node scripts/proxies_check.mjs && node scripts/route_check.mjs" + "test": "npm run harness:build && node scripts/fleet_render_check.mjs && node scripts/earnings_render_check.mjs && node scripts/status_render_check.mjs && node scripts/total_render_check.mjs && node scripts/fields_check.mjs && node scripts/removal_check.mjs && node scripts/preflight_render_check.mjs && node scripts/producer_render_check.mjs && node scripts/details_check.mjs && node scripts/wizard_render_check.mjs && node scripts/image_check.mjs && node scripts/instances_check.mjs && node scripts/console_render_check.mjs && node scripts/topbar_render_check.mjs && node scripts/drift_render_check.mjs && node scripts/usage_render_check.mjs && node scripts/backups_render_check.mjs && node scripts/restore_render_check.mjs && node scripts/activity_check.mjs && node scripts/custom_check.mjs && node scripts/importer_check.mjs && node scripts/rating_check.mjs && node scripts/isolation_render_check.mjs && node scripts/proxies_check.mjs && node scripts/route_check.mjs" }, "dependencies": {}, "devDependencies": { diff --git a/frontend/scripts/backups_render_check.mjs b/frontend/scripts/backups_render_check.mjs index 3bd755ed..08a0f74e 100644 --- a/frontend/scripts/backups_render_check.mjs +++ b/frontend/scripts/backups_render_check.mjs @@ -53,6 +53,7 @@ check("changed says to back it up again", backupStatusText(row({state: "changed" check("expired says six months", backupStatusText(row({state: "expired", confirmedAt: "2026-01-01T00:00:00Z"})).includes("more than six months ago")); check("recheck asks once more", backupStatusText(row({state: "recheck", confirmedAt: "2026-01-01T00:00:00Z"})).includes("Confirm it once more")); check("an export says it was saved from here", backupStatusText(row({state: "confirmed", confirmedAt: "2026-10-01T09:00:00Z", method: "export"})).includes("(saved from here)")); +check("a restore says it came from that file", backupStatusText(row({state: "confirmed", confirmedAt: "2026-10-01T09:00:00Z", method: "restore"})).includes("(restored from that file)")); check("an untracked confirmation says only the time limit applies", backupStatusText(row({state: "confirmed", confirmedAt: "2026-10-01T09:00:00Z", tracksChanges: false})).includes("only the six-month limit applies")); diff --git a/frontend/scripts/restore_render_check.mjs b/frontend/scripts/restore_render_check.mjs new file mode 100644 index 00000000..2965d6bf --- /dev/null +++ b/frontend/scripts/restore_render_check.mjs @@ -0,0 +1,82 @@ +#!/usr/bin/env node +// Browser-free checks on Settings > Restore from a backup. +// +// THE RULE: the preview says what the restore creates, never offers Restore when this +// machine refuses it, asks for the service name for a file this app did not make, +// and names the unfinished restores that hold a service. +// +// node scripts/restore_render_check.mjs # against ./.harness-build + +import { renderRestoreCard, renderRestorePreview, restoreResultText } from "../.harness-build/render/restore.js"; + +let failures = 0; +let checks = 0; + +function check(name, condition, detail = "") { + checks++; + if (condition) return; + failures++; + console.error(`FAIL ${name}${detail ? `\n ${detail}` : ""}`); +} + +function preview(overrides = {}) { + return { + token: "tok123", + file: "cashpilot-mysterium-box-20261101.cpbackup", + slug: "mysterium", + service: "Mysterium", + bytes: 2048, + sha256: "ab".repeat(32), + known: true, + madeOn: "box", + container: "cashpilot-mysterium", + creates: [{target: "/var/lib/mysterium-node", volume: "mysterium-data"}], + ok: true, + why: "", + runsOn: [], + ...overrides, + }; +} + +const card = renderRestoreCard([]); +check("the card has the file button", card.includes('id="restore-choose"'), card); +check("the card says it only creates", card.includes("only creates a new container")); +check("the card points key-sealed files to the command line", card.includes("Files sealed to a key") && card.includes("docs/backup.md#restore-a-backup-from-the-command-line")); +check("no unfinished restore, no list", !card.includes("did not finish")); + +const stuck = renderRestoreCard([{slug: "mysterium", service: "Mysterium", volumes: ["mysterium-data"], startedAt: "2026-11-16T10:20:30Z"}]); +check("an unfinished restore is listed by name", stuck.includes("Mysterium") && stuck.includes("did not finish"), stuck); +check("it says Deploy and Start refuse it", stuck.includes("Deploy and Start refuse it")); +check("it names the volumes the restore made", stuck.includes("mysterium-data")); +check("it offers to delete the partial restore", stuck.includes('data-restore-discard="mysterium"')); + +const known = renderRestorePreview(preview()); +check("the preview names the container and the volume", known.includes("cashpilot-mysterium") && known.includes("mysterium-data at /var/lib/mysterium-node"), known); +check("the preview carries the token", known.includes('data-token="tok123"')); +check("a known file asks for no name", !known.includes('name="confirm_name"') && known.includes("This app made this file")); +check("the passphrase is a password field", known.includes('type="password" name="passphrase"')); +check("Restore is offered when this machine accepts it", /