Skip to content

Commit

Permalink
Add confirm password to validator accounts create (#5762)
Browse files Browse the repository at this point in the history
* Add confirm password to accounts create

* Fix build

* Update shared/cmd/helpers.go

* Update shared/cmd/helpers.go

Co-authored-by: Shay Zluf <thezluf@gmail.com>

* Change to return empty ""

Co-authored-by: terence tsao <terence@prysmaticlabs.com>
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: Shay Zluf <thezluf@gmail.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
5 people authored May 6, 2020
1 parent b2ee466 commit 4d9da25
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
19 changes: 16 additions & 3 deletions shared/cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package cmd
import (
"bufio"
"fmt"
"github.com/pkg/errors"
"os"
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"
)
Expand Down Expand Up @@ -48,14 +48,27 @@ func ConfirmAction(actionText string, deniedText string) (bool, error) {
// EnterPassword queries the user for their password through the terminal, in order to make sure it is
// not passed in a visible way to the terminal.
// TODO(#5749): This function is untested and should be tested.
func EnterPassword() (string, error) {
func EnterPassword(confirmPassword bool) (string, error) {
var passphrase string
log.Info("Enter a password:")
bytePassword, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return passphrase, errors.Wrap(err, "could not read account password")
return "", errors.Wrap(err, "could not read account password")
}
text := string(bytePassword)
passphrase = strings.Replace(text, "\n", "", -1)
if confirmPassword {
log.Info("Please re-enter your password:")
bytePassword, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", errors.Wrap(err, "could not read account password")
}
text := string(bytePassword)
confirmedPass := strings.Replace(text, "\n", "", -1)
if passphrase != confirmedPass {
log.Info("Passwords did not match, please try again")
return EnterPassword(true)
}
}
return passphrase, nil
}
4 changes: 2 additions & 2 deletions validator/accounts/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func DefaultValidatorDir() string {
}

// HandleEmptyFlags checks what the set flags are and allows the user to manually enter them if they're empty.
func HandleEmptyFlags(cliCtx *cli.Context) (string, string, error) {
func HandleEmptyFlags(cliCtx *cli.Context, confirmPassword bool) (string, string, error) {
path := cliCtx.String(flags.KeystorePathFlag.Name)
passphrase := cliCtx.String(flags.PasswordFlag.Name)

Expand All @@ -204,7 +204,7 @@ func HandleEmptyFlags(cliCtx *cli.Context) (string, string, error) {

if passphrase == "" {
log.Info("Please enter the password for your private keys")
enteredPassphrase, err := cmd.EnterPassword()
enteredPassphrase, err := cmd.EnterPassword(confirmPassword)
if err != nil {
return path, enteredPassphrase, errors.Wrap(err, "could not read entered passphrase")
}
Expand Down
4 changes: 2 additions & 2 deletions validator/accounts/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestNewValidatorAccount_CreateValidatorAccount(t *testing.T) {
}
}

func TestHandleEmptyFlags_FlagsSetF(t *testing.T) {
func TestHandleEmptyFlags_FlagsSet(t *testing.T) {
passedPath := "~/path/given"
passedPassword := "password"

Expand All @@ -62,7 +62,7 @@ func TestHandleEmptyFlags_FlagsSetF(t *testing.T) {
set.String(flags.KeystorePathFlag.Name, passedPath, "set keystore path")
set.String(flags.PasswordFlag.Name, passedPassword, "set keystore password")
ctx := cli.NewContext(app, set, nil)
path, passphrase, err := HandleEmptyFlags(ctx)
path, passphrase, err := HandleEmptyFlags(ctx, false)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions validator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ contract in order to activate the validator client`,
params.UseMinimalConfig()
}

keystorePath, passphrase, err := accounts.HandleEmptyFlags(cliCtx)
keystorePath, passphrase, err := accounts.HandleEmptyFlags(cliCtx, true /*confirmPassword*/)
if err != nil {
log.WithError(err).Error("Could not list keys")
}
Expand All @@ -125,7 +125,7 @@ contract in order to activate the validator client`,
flags.PasswordFlag,
},
Action: func(cliCtx *cli.Context) error {
keystorePath, passphrase, err := accounts.HandleEmptyFlags(cliCtx)
keystorePath, passphrase, err := accounts.HandleEmptyFlags(cliCtx, false /*confirmPassword*/)
if err != nil {
log.WithError(err).Error("Could not list keys")
}
Expand Down

0 comments on commit 4d9da25

Please sign in to comment.