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 <terence@prysmaticlabs.com>
This commit is contained in:
Ivan Martinez 2020-05-05 16:13:18 -04:00 committed by GitHub
parent 3a677ef342
commit 2b7865cb3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 11 deletions

View File

@ -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",
],
)

View File

@ -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
}

View File

@ -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",
],
)

View File

@ -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 != "" {