mirror of
https://github.com/restic/restic.git
synced 2025-08-13 16:16:11 +00:00
Add key hinting (#2097)
This commit is contained in:
@@ -330,7 +330,7 @@ func TestCheckerModifiedData(t *testing.T) {
|
||||
|
||||
beError := &errorBackend{Backend: repo.Backend()}
|
||||
checkRepo := repository.New(beError)
|
||||
test.OK(t, checkRepo.SearchKey(context.TODO(), test.TestPassword, 5))
|
||||
test.OK(t, checkRepo.SearchKey(context.TODO(), test.TestPassword, 5, ""))
|
||||
|
||||
chkr := checker.New(checkRepo)
|
||||
|
||||
|
@@ -112,9 +112,26 @@ func OpenKey(ctx context.Context, s *Repository, name string, password string) (
|
||||
// given password. If none could be found, ErrNoKeyFound is returned. When
|
||||
// maxKeys is reached, ErrMaxKeysReached is returned. When setting maxKeys to
|
||||
// zero, all keys in the repo are checked.
|
||||
func SearchKey(ctx context.Context, s *Repository, password string, maxKeys int) (k *Key, err error) {
|
||||
func SearchKey(ctx context.Context, s *Repository, password string, maxKeys int, keyHint string) (k *Key, err error) {
|
||||
checked := 0
|
||||
|
||||
if len(keyHint) > 0 {
|
||||
id, err := restic.Find(s.Backend(), restic.KeyFile, keyHint)
|
||||
|
||||
if err == nil {
|
||||
key, err := OpenKey(ctx, s, id, password)
|
||||
|
||||
if err == nil {
|
||||
debug.Log("successfully opened hinted key %v", id)
|
||||
return key, nil
|
||||
}
|
||||
|
||||
debug.Log("could not open hinted key %v", id)
|
||||
} else {
|
||||
debug.Log("Could not find hinted key %v", keyHint)
|
||||
}
|
||||
}
|
||||
|
||||
listCtx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
|
@@ -510,8 +510,8 @@ func LoadIndex(ctx context.Context, repo restic.Repository, id restic.ID) (*Inde
|
||||
|
||||
// SearchKey finds a key with the supplied password, afterwards the config is
|
||||
// read and parsed. It tries at most maxKeys key files in the repo.
|
||||
func (r *Repository) SearchKey(ctx context.Context, password string, maxKeys int) error {
|
||||
key, err := SearchKey(ctx, r, password, maxKeys)
|
||||
func (r *Repository) SearchKey(ctx context.Context, password string, maxKeys int, keyHint string) error {
|
||||
key, err := SearchKey(ctx, r, password, maxKeys, keyHint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -99,7 +99,7 @@ func TestOpenLocal(t testing.TB, dir string) (r restic.Repository) {
|
||||
}
|
||||
|
||||
repo := New(be)
|
||||
err = repo.SearchKey(context.TODO(), test.TestPassword, 10)
|
||||
err = repo.SearchKey(context.TODO(), test.TestPassword, 10, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user