mirror of
https://github.com/restic/restic.git
synced 2025-08-22 01:51:47 +00:00
repository: Introduce RemoveKey function
This replaces directly removing keys via the backend.
This commit is contained in:
@@ -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>"
|
||||
|
@@ -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)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user