Files
kopia/cli/password.go
Jarek Kowalski 207009939f cli: only fetch the persisted password from keychain if one was not provided on the command line (#744)
This also fixed a test bug where the test was incorrectly passing
password via environment variable and it was (incorrectly) expected
to be ignored.

Password is determined in the following order:

- flag/environment variable (highest priority)
- persistent storage
- asking user (lowest priority)
2020-12-24 22:39:02 -08:00

89 lines
2.0 KiB
Go

package cli
import (
"context"
"fmt"
"strings"
"github.com/bgentry/speakeasy"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo"
)
var password = app.Flag("password", "Repository password.").Envar("KOPIA_PASSWORD").Short('p').String()
func askForNewRepositoryPassword() (string, error) {
for {
p1, err := askPass("Enter password to create new repository: ")
if err != nil {
return "", errors.Wrap(err, "password entry")
}
p2, err := askPass("Re-enter password for verification: ")
if err != nil {
return "", errors.Wrap(err, "password verification")
}
if p1 != p2 {
fmt.Println("Passwords don't match!")
} else {
return p1, nil
}
}
}
func askForExistingRepositoryPassword() (string, error) {
p1, err := askPass("Enter password to open repository: ")
if err != nil {
return "", err
}
fmt.Println()
return p1, nil
}
var passwordFromToken string
func getPasswordFromFlags(ctx context.Context, isNew, allowPersistent bool) (string, error) {
switch {
case passwordFromToken != "":
// password extracted from connection token
return passwordFromToken, nil
case *password != "":
// password provided via --password flag or KOPIA_PASSWORD environment variable
return strings.TrimSpace(*password), nil
case isNew:
// this is a new repository, ask for password
return askForNewRepositoryPassword()
case allowPersistent:
// try fetching the password from persistent storage specific to the configuration file.
pass, ok := repo.GetPersistedPassword(ctx, repositoryConfigFileName())
if ok {
return pass, nil
}
}
// fall back to asking for existing password
return askForExistingRepositoryPassword()
}
// askPass presents a given prompt and asks the user for password.
func askPass(prompt string) (string, error) {
for i := 0; i < 5; i++ {
p, err := speakeasy.Ask(prompt)
if err != nil {
return "", errors.Wrap(err, "password prompt error")
}
if p == "" {
continue
}
return p, nil
}
return "", errors.New("can't get password")
}