Skip to content

Commit

Permalink
Move "Enter a password" cmd instructions to shared/cmd (#5724)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored May 5, 2020
1 parent 3a677ef commit 2b7865c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 2 additions & 0 deletions shared/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand Down
17 changes: 17 additions & 0 deletions shared/cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion validator/accounts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand Down
15 changes: 5 additions & 10 deletions validator/accounts/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 == "" {
Expand All @@ -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 != "" {
Expand Down

0 comments on commit 2b7865c

Please sign in to comment.