Skip to content

Commit

Permalink
Merge pull request #4648 from MichaelEischer/repository-removekey
Browse files Browse the repository at this point in the history
repository: Introduce RemoveKey function
  • Loading branch information
MichaelEischer committed Jan 27, 2024
2 parents 25ac154 + c13bf0b commit 724ec17
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
10 changes: 3 additions & 7 deletions cmd/restic/cmd_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"sync"

"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic"
Expand Down Expand Up @@ -152,8 +151,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")
}

h := backend.Handle{Type: restic.KeyFile, Name: id.String()}
err := repo.Backend().Remove(ctx, h)
err := repository.RemoveKey(ctx, repo, id)
if err != nil {
return err
}
Expand All @@ -179,8 +177,7 @@ func changePassword(ctx context.Context, repo *repository.Repository, gopts Glob
return err
}

h := backend.Handle{Type: restic.KeyFile, Name: oldID.String()}
err = repo.Backend().Remove(ctx, h)
err = repository.RemoveKey(ctx, repo, oldID)
if err != nil {
return err
}
Expand All @@ -196,8 +193,7 @@ func switchToNewKeyAndRemoveIfBroken(ctx context.Context, repo *repository.Repos
err := repo.SearchKey(ctx, pw, 0, key.ID().String())
if err != nil {
// the key is invalid, try to remove it
h := backend.Handle{Type: restic.KeyFile, Name: key.ID().String()}
_ = repo.Backend().Remove(ctx, h)
_ = repository.RemoveKey(ctx, repo, key.ID())
return errors.Fatalf("failed to access repository with new key: %v", err)
}
return nil
Expand Down
9 changes: 9 additions & 0 deletions internal/repository/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ func AddKey(ctx context.Context, s *Repository, password, username, hostname str
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 {
if k == nil {
return "<Key nil>"
Expand Down
13 changes: 10 additions & 3 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,12 +743,19 @@ func (r *Repository) SearchKey(ctx context.Context, password string, maxKeys int
return err
}

oldKey := r.key
oldKeyID := r.keyID

r.key = key.master
r.keyID = key.ID()
cfg, err := restic.LoadConfig(ctx, r)
if err == crypto.ErrUnauthenticated {
return fmt.Errorf("config or key %v is damaged: %w", key.ID(), err)
} else if err != nil {
if err != nil {
r.key = oldKey
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)
}

Expand Down

0 comments on commit 724ec17

Please sign in to comment.