2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-25 06:07:44 +00:00

repository: Introduce RemoveKey function

This replaces directly removing keys via the backend.
This commit is contained in:
Michael Eischer 2024-01-21 20:38:42 +01:00
parent 6696195f38
commit c13bf0b607
3 changed files with 22 additions and 10 deletions

View File

@ -7,7 +7,6 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
@ -150,8 +149,7 @@ func deleteKey(ctx context.Context, repo *repository.Repository, id restic.ID) e
return errors.Fatal("refusing to remove key currently used to access repository") return errors.Fatal("refusing to remove key currently used to access repository")
} }
h := backend.Handle{Type: restic.KeyFile, Name: id.String()} err := repository.RemoveKey(ctx, repo, id)
err := repo.Backend().Remove(ctx, h)
if err != nil { if err != nil {
return err return err
} }
@ -177,8 +175,7 @@ func changePassword(ctx context.Context, repo *repository.Repository, gopts Glob
return err return err
} }
h := backend.Handle{Type: restic.KeyFile, Name: oldID.String()} err = repository.RemoveKey(ctx, repo, oldID)
err = repo.Backend().Remove(ctx, h)
if err != nil { if err != nil {
return err return err
} }
@ -194,8 +191,7 @@ func switchToNewKeyAndRemoveIfBroken(ctx context.Context, repo *repository.Repos
err := repo.SearchKey(ctx, pw, 0, key.ID().String()) err := repo.SearchKey(ctx, pw, 0, key.ID().String())
if err != nil { if err != nil {
// the key is invalid, try to remove it // the key is invalid, try to remove it
h := backend.Handle{Type: restic.KeyFile, Name: key.ID().String()} _ = repository.RemoveKey(ctx, repo, key.ID())
_ = repo.Backend().Remove(ctx, h)
return errors.Fatalf("failed to access repository with new key: %v", err) return errors.Fatalf("failed to access repository with new key: %v", err)
} }
return nil return nil

View File

@ -285,6 +285,15 @@ func AddKey(ctx context.Context, s *Repository, password, username, hostname str
return newkey, nil return newkey, nil
} }
func RemoveKey(ctx context.Context, repo *Repository, id restic.ID) error {
if id == repo.KeyID() {
return errors.New("refusing to remove key currently used to access repository")
}
h := backend.Handle{Type: restic.KeyFile, Name: id.String()}
return repo.be.Remove(ctx, h)
}
func (k *Key) String() string { func (k *Key) String() string {
if k == nil { if k == nil {
return "<Key nil>" return "<Key nil>"

View File

@ -743,12 +743,19 @@ func (r *Repository) SearchKey(ctx context.Context, password string, maxKeys int
return err return err
} }
oldKey := r.key
oldKeyID := r.keyID
r.key = key.master r.key = key.master
r.keyID = key.ID() r.keyID = key.ID()
cfg, err := restic.LoadConfig(ctx, r) cfg, err := restic.LoadConfig(ctx, r)
if err == crypto.ErrUnauthenticated { if err != nil {
return fmt.Errorf("config or key %v is damaged: %w", key.ID(), err) r.key = oldKey
} else if err != nil { r.keyID = oldKeyID
if err == crypto.ErrUnauthenticated {
return fmt.Errorf("config or key %v is damaged: %w", key.ID(), err)
}
return fmt.Errorf("config cannot be loaded: %w", err) return fmt.Errorf("config cannot be loaded: %w", err)
} }