From 05dddc2ee117dd3bbe4c70afbbe7c2430025ee27 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Sun, 3 May 2020 15:50:49 -0400 Subject: [PATCH 1/7] Move EnterPassword CMD to cmd package --- shared/cmd/BUILD.bazel | 1 + shared/cmd/helpers.go | 14 ++++++++++++++ validator/accounts/BUILD.bazel | 2 +- validator/accounts/account.go | 15 ++++++--------- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/shared/cmd/BUILD.bazel b/shared/cmd/BUILD.bazel index d7823be161b5..3302c74d6e07 100644 --- a/shared/cmd/BUILD.bazel +++ b/shared/cmd/BUILD.bazel @@ -15,6 +15,7 @@ go_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 7a0048eaf64a..eff6c8f950cf 100644 --- a/shared/cmd/helpers.go +++ b/shared/cmd/helpers.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/sirupsen/logrus" + "golang.org/x/crypto/ssh/terminal" ) var log = logrus.WithField("prefix", "node") @@ -42,3 +43,16 @@ func ConfirmAction(actionText string, deniedText string) (bool, error) { return confirmed, nil } + +func EnterPassword() (string, error) { + var passphrase string + log.Info("Enter a password:") + bytePassword, err := terminal.ReadPassword(int(os.Stdin.Fd())) + if err != nil { + log.Fatalf("Could not read account password: %v", err) + return passphrase, err + } + 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 266eacc20f01..829edec45a09 100644 --- a/validator/accounts/BUILD.bazel +++ b/validator/accounts/BUILD.bazel @@ -10,11 +10,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 a527ec40ca9b..b041a44a4d4c 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") @@ -136,16 +136,13 @@ 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 + log.Fatal(err) + return path, enteredPassphrase, err } - text := string(bytePassword) - passphrase = strings.Replace(text, "\n", "", -1) - + passphrase = enteredPassphrase } if path == "" { From 81d4fb4814e8b7be257e4c88c94e926d121d6384 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Sun, 3 May 2020 15:55:55 -0400 Subject: [PATCH 2/7] Add comment --- shared/cmd/helpers.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shared/cmd/helpers.go b/shared/cmd/helpers.go index eff6c8f950cf..2e93658d295e 100644 --- a/shared/cmd/helpers.go +++ b/shared/cmd/helpers.go @@ -44,6 +44,8 @@ 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. func EnterPassword() (string, error) { var passphrase string log.Info("Enter a password:") From b501642e2cc08f3034e891d41a367ee5eb29c39f Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Tue, 5 May 2020 15:05:53 -0400 Subject: [PATCH 3/7] Add tracking issue and remove log.Fatal --- shared/cmd/helpers.go | 1 + validator/accounts/account.go | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/cmd/helpers.go b/shared/cmd/helpers.go index 2e93658d295e..4c7f7dbea1f0 100644 --- a/shared/cmd/helpers.go +++ b/shared/cmd/helpers.go @@ -46,6 +46,7 @@ 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, would be a good candidate for unit test. func EnterPassword() (string, error) { var passphrase string log.Info("Enter a password:") diff --git a/validator/accounts/account.go b/validator/accounts/account.go index 4c45e2dd4150..0b13b6a7aa27 100644 --- a/validator/accounts/account.go +++ b/validator/accounts/account.go @@ -141,7 +141,6 @@ func CreateValidatorAccount(path string, passphrase string) (string, string, err log.Info("Creating a new validator account for eth2") enteredPassphrase, err := cmd.EnterPassword() if err != nil { - log.Fatal(err) return path, enteredPassphrase, err } passphrase = enteredPassphrase From 1c703b4b591ef5257455592c39eed1cd387cde1f Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Tue, 5 May 2020 15:18:40 -0400 Subject: [PATCH 4/7] Add better error handling --- shared/cmd/helpers.go | 4 ++-- validator/accounts/account.go | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/shared/cmd/helpers.go b/shared/cmd/helpers.go index 4c7f7dbea1f0..62f1cfca3303 100644 --- a/shared/cmd/helpers.go +++ b/shared/cmd/helpers.go @@ -3,6 +3,7 @@ package cmd import ( "bufio" "fmt" + "github.com/pkg/errors" "os" "strings" @@ -52,8 +53,7 @@ func EnterPassword() (string, error) { log.Info("Enter a password:") bytePassword, err := terminal.ReadPassword(int(os.Stdin.Fd())) if err != nil { - log.Fatalf("Could not read account password: %v", err) - return passphrase, err + return passphrase, errors.Wrap(err, "could not read account password") } text := string(bytePassword) passphrase = strings.Replace(text, "\n", "", -1) diff --git a/validator/accounts/account.go b/validator/accounts/account.go index 0b13b6a7aa27..810423eb4cad 100644 --- a/validator/accounts/account.go +++ b/validator/accounts/account.go @@ -152,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 != "" { From 605c37d70211bd3b7448e270baf6a665ff71d9ff Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Tue, 5 May 2020 15:21:41 -0400 Subject: [PATCH 5/7] gaz --- shared/cmd/BUILD.bazel | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/cmd/BUILD.bazel b/shared/cmd/BUILD.bazel index 34aa64da50cc..9dfe1041ff9d 100644 --- a/shared/cmd/BUILD.bazel +++ b/shared/cmd/BUILD.bazel @@ -13,6 +13,7 @@ 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", From 24deb655379740ed09b85b7dd510ffaf01cca20c Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Tue, 5 May 2020 15:28:44 -0400 Subject: [PATCH 6/7] Comment fix --- shared/cmd/helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/cmd/helpers.go b/shared/cmd/helpers.go index 62f1cfca3303..cbeac2136ef8 100644 --- a/shared/cmd/helpers.go +++ b/shared/cmd/helpers.go @@ -47,7 +47,7 @@ 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, would be a good candidate for unit test. +// TODO(#5749) This function is untested, would be a good candidate for a unit test. func EnterPassword() (string, error) { var passphrase string log.Info("Enter a password:") From e6c88c8cddd73b407b9f059b36fa4d8ec10a1d4a Mon Sep 17 00:00:00 2001 From: terence tsao Date: Tue, 5 May 2020 12:42:03 -0700 Subject: [PATCH 7/7] Update shared/cmd/helpers.go --- shared/cmd/helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/cmd/helpers.go b/shared/cmd/helpers.go index cbeac2136ef8..17fd95c13444 100644 --- a/shared/cmd/helpers.go +++ b/shared/cmd/helpers.go @@ -47,7 +47,7 @@ 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, would be a good candidate for a unit test. +// TODO(#5749): This function is untested and should be tested. func EnterPassword() (string, error) { var passphrase string log.Info("Enter a password:")