Merge pull request #2307 from gary-kim/fix-2306-password-retries
Allow multiple retries for interactive password input
This commit is contained in:
commit
26d1f9f4ba
3 changed files with 32 additions and 7 deletions
7
changelog/unreleased/issue-2306
Normal file
7
changelog/unreleased/issue-2306
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
Enhancement: Allow multiple retries for interactive password input
|
||||||
|
|
||||||
|
Restic used to quit if the repository password was typed incorrectly once.
|
||||||
|
Restic will now ask the user again for the repository password if typed incorrectly.
|
||||||
|
The user will now get three tries to input the correct password before restic quits.
|
||||||
|
|
||||||
|
https://github.com/restic/restic/issues/2306
|
|
@ -320,7 +320,7 @@ func ReadPassword(opts GlobalOptions, prompt string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(password) == 0 {
|
if len(password) == 0 {
|
||||||
return "", errors.Fatal("an empty password is not a password")
|
return "", errors.New("an empty password is not a password")
|
||||||
}
|
}
|
||||||
|
|
||||||
return password, nil
|
return password, nil
|
||||||
|
@ -366,15 +366,33 @@ func OpenRepository(opts GlobalOptions) (*repository.Repository, error) {
|
||||||
|
|
||||||
s := repository.New(be)
|
s := repository.New(be)
|
||||||
|
|
||||||
|
passwordTriesLeft := 1
|
||||||
|
if stdinIsTerminal() && opts.password == "" {
|
||||||
|
passwordTriesLeft = 3
|
||||||
|
}
|
||||||
|
|
||||||
|
for ; passwordTriesLeft > 0; passwordTriesLeft-- {
|
||||||
opts.password, err = ReadPassword(opts, "enter password for repository: ")
|
opts.password, err = ReadPassword(opts, "enter password for repository: ")
|
||||||
|
if err != nil && passwordTriesLeft > 1 {
|
||||||
|
opts.password = ""
|
||||||
|
fmt.Printf("%s. Try again\n", err)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.SearchKey(opts.ctx, opts.password, maxKeys, opts.KeyHint)
|
err = s.SearchKey(opts.ctx, opts.password, maxKeys, opts.KeyHint)
|
||||||
|
if err != nil && passwordTriesLeft > 1 {
|
||||||
|
opts.password = ""
|
||||||
|
fmt.Printf("%s. Try again\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.IsFatal(err) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return nil, errors.Fatalf("%s", err)
|
||||||
|
}
|
||||||
|
|
||||||
if stdoutIsTerminal() && !opts.JSON {
|
if stdoutIsTerminal() && !opts.JSON {
|
||||||
id := s.Config().ID
|
id := s.Config().ID
|
||||||
|
|
|
@ -18,7 +18,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrNoKeyFound is returned when no key for the repository could be decrypted.
|
// ErrNoKeyFound is returned when no key for the repository could be decrypted.
|
||||||
ErrNoKeyFound = errors.Fatal("wrong password or no key found")
|
ErrNoKeyFound = errors.New("wrong password or no key found")
|
||||||
|
|
||||||
// ErrMaxKeysReached is returned when the maximum number of keys was checked and no key could be found.
|
// ErrMaxKeysReached is returned when the maximum number of keys was checked and no key could be found.
|
||||||
ErrMaxKeysReached = errors.Fatal("maximum number of keys reached")
|
ErrMaxKeysReached = errors.Fatal("maximum number of keys reached")
|
||||||
|
|
Loading…
Reference in a new issue