From 2b7865cb3b3836ba0554611f744de68c6d95affc Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Tue, 5 May 2020 16:13:18 -0400 Subject: [PATCH] Move "Enter a password" cmd instructions to shared/cmd (#5724) * Move EnterPassword CMD to cmd package * Add comment * Add tracking issue and remove log.Fatal * Add better error handling * gaz * Comment fix * Update shared/cmd/helpers.go Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: terence tsao --- shared/cmd/BUILD.bazel | 2 ++ shared/cmd/helpers.go | 17 +++++++++++++++++ validator/accounts/BUILD.bazel | 2 +- validator/accounts/account.go | 15 +++++---------- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/shared/cmd/BUILD.bazel b/shared/cmd/BUILD.bazel index ac59ef2de..9dfe1041f 100644 --- a/shared/cmd/BUILD.bazel +++ b/shared/cmd/BUILD.bazel @@ -13,9 +13,11 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/shared/cmd", visibility = ["//visibility:public"], deps = [ + "@com_github_pkg_errors//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@in_gopkg_urfave_cli_v2//:go_default_library", "@in_gopkg_urfave_cli_v2//altsrc:go_default_library", + "@org_golang_x_crypto//ssh/terminal:go_default_library", ], ) diff --git a/shared/cmd/helpers.go b/shared/cmd/helpers.go index 7a0048eaf..17fd95c13 100644 --- a/shared/cmd/helpers.go +++ b/shared/cmd/helpers.go @@ -3,10 +3,12 @@ package cmd import ( "bufio" "fmt" + "github.com/pkg/errors" "os" "strings" "github.com/sirupsen/logrus" + "golang.org/x/crypto/ssh/terminal" ) var log = logrus.WithField("prefix", "node") @@ -42,3 +44,18 @@ func ConfirmAction(actionText string, deniedText string) (bool, error) { return confirmed, nil } + +// 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) { + 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") + } + text := string(bytePassword) + passphrase = strings.Replace(text, "\n", "", -1) + return passphrase, nil +} diff --git a/validator/accounts/BUILD.bazel b/validator/accounts/BUILD.bazel index cc2102aa2..bba35cbc2 100644 --- a/validator/accounts/BUILD.bazel +++ b/validator/accounts/BUILD.bazel @@ -11,11 +11,11 @@ go_library( ], deps = [ "//contracts/deposit-contract:go_default_library", + "//shared/cmd:go_default_library", "//shared/keystore:go_default_library", "//shared/params:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", - "@org_golang_x_crypto//ssh/terminal:go_default_library", ], ) diff --git a/validator/accounts/account.go b/validator/accounts/account.go index 68ba2bd86..810423eb4 100644 --- a/validator/accounts/account.go +++ b/validator/accounts/account.go @@ -14,10 +14,10 @@ import ( "github.com/pkg/errors" contract "github.com/prysmaticlabs/prysm/contracts/deposit-contract" + "github.com/prysmaticlabs/prysm/shared/cmd" "github.com/prysmaticlabs/prysm/shared/keystore" "github.com/prysmaticlabs/prysm/shared/params" "github.com/sirupsen/logrus" - "golang.org/x/crypto/ssh/terminal" ) var log = logrus.WithField("prefix", "accounts") @@ -138,16 +138,12 @@ func Exists(keystorePath string) (bool, error) { // CreateValidatorAccount creates a validator account from the given cli context. func CreateValidatorAccount(path string, passphrase string) (string, string, error) { if passphrase == "" { - log.Info("Create a new validator account for eth2") - log.Info("Enter a password:") - bytePassword, err := terminal.ReadPassword(int(os.Stdin.Fd())) + log.Info("Creating a new validator account for eth2") + enteredPassphrase, err := cmd.EnterPassword() if err != nil { - log.Fatalf("Could not read account password: %v", err) - return path, passphrase, err + return path, enteredPassphrase, err } - text := string(bytePassword) - passphrase = strings.Replace(text, "\n", "", -1) - + passphrase = enteredPassphrase } if path == "" { @@ -156,7 +152,6 @@ func CreateValidatorAccount(path string, passphrase string) (string, string, err reader := bufio.NewReader(os.Stdin) text, err := reader.ReadString('\n') if err != nil { - log.Fatal(err) return path, passphrase, err } if text = strings.Replace(text, "\n", "", -1); text != "" {