Skip to content

Commit

Permalink
chore: allow passing password as stdin (#325)
Browse files Browse the repository at this point in the history
* chore: allow passing password as stdin
  • Loading branch information
moul committed Sep 22, 2022
1 parent 9dfb6c7 commit 3cc5ae8
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 39 deletions.
42 changes: 22 additions & 20 deletions cmd/gnofaucet/gnofaucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,29 @@ func main() {
// serveApp

type serveOptions struct {
client.BaseOptions // home, ...
ChainID string `flag:"chain-id" help:"chain id"`
GasWanted int64 `flag:"gas-wanted" help:"gas requested for tx"`
GasFee string `flag:"gas-fee" help:"gas payment fee"`
Memo string `flag:"memo" help:"any descriptive text"`
TestTo string `flag:"test-to" help:"test addr (optional)"`
Send string `flag:"send" help:"send coins"`
CaptchaSecret string `flag:"captcha-secret" help:"recaptcha secret key (if empty, captcha are disabled)"`
IsBehindProxy bool `flag:"is-behind-proxy" help:"use X-Forwarded-For IP for throttling."`
client.BaseOptions // home, ...
ChainID string `flag:"chain-id" help:"chain id"`
GasWanted int64 `flag:"gas-wanted" help:"gas requested for tx"`
GasFee string `flag:"gas-fee" help:"gas payment fee"`
Memo string `flag:"memo" help:"any descriptive text"`
TestTo string `flag:"test-to" help:"test addr (optional)"`
Send string `flag:"send" help:"send coins"`
CaptchaSecret string `flag:"captcha-secret" help:"recaptcha secret key (if empty, captcha are disabled)"`
IsBehindProxy bool `flag:"is-behind-proxy" help:"use X-Forwarded-For IP for throttling"`
InsecurePasswordStdin bool `flag:"insecure-password-stdin" help:"WARNING! take password from stdin"`
}

var DefaultServeOptions = serveOptions{
BaseOptions: client.DefaultBaseOptions,
ChainID: "", // must override
GasWanted: 50000,
GasFee: "1000000ugnot",
Memo: "",
TestTo: "",
Send: "1000000ugnot",
CaptchaSecret: "",
IsBehindProxy: false,
BaseOptions: client.DefaultBaseOptions,
ChainID: "", // must override
GasWanted: 50000,
GasFee: "1000000ugnot",
Memo: "",
TestTo: "",
Send: "1000000ugnot",
CaptchaSecret: "",
IsBehindProxy: false,
InsecurePasswordStdin: false,
}

func serveApp(cmd *command.Command, args []string, iopts interface{}) error {
Expand Down Expand Up @@ -167,9 +169,9 @@ func serveApp(cmd *command.Command, args []string, iopts interface{}) error {
const dummy = "test"
var pass string
if opts.Quiet {
pass, err = cmd.GetPassword("")
pass, err = cmd.GetPassword("", opts.InsecurePasswordStdin)
} else {
pass, err = cmd.GetPassword("Enter password.")
pass, err = cmd.GetPassword("Enter password.", opts.InsecurePasswordStdin)
}
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/gnokey/gnokeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,9 @@ func signAndBroadcast(cmd *command.Command, args []string, tx std.Tx, baseopts c
}
sopts.Home = baseopts.Home
if baseopts.Quiet {
sopts.Pass, err = cmd.GetPassword("")
sopts.Pass, err = cmd.GetPassword("", baseopts.InsecurePasswordStdin)
} else {
sopts.Pass, err = cmd.GetPassword("Enter password.")
sopts.Pass, err = cmd.GetPassword("Enter password.", baseopts.InsecurePasswordStdin)
}
if err != nil {
return err
Expand Down
14 changes: 10 additions & 4 deletions pkgs/command/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ import (

// GetPassword will prompt for a password one-time (to sign a tx).
// Passwords may be blank; user must validate.
func (cmd *Command) GetPassword(prompt string) (pass string, err error) {
func (cmd *Command) GetPassword(prompt string, insecureUseStdin bool) (pass string, err error) {
if prompt != "" {
// On stderr so it isn't part of bash output.
cmd.ErrPrintln(prompt)
}
pass, err = cmd.readPasswordFromInBuf()

// insecure stdin.
if insecureUseStdin {
return cmd.readLineFromInBuf()
}

// secure prompt.
pass, err = cmd.readPasswordFromInBuf()
if err != nil {
return "", err
}
Expand All @@ -30,11 +36,11 @@ func (cmd *Command) GetPassword(prompt string) (pass string, err error) {
// It enforces the password length. Only parses password once if
// input is piped in.
func (cmd *Command) GetCheckPassword(prompt, prompt2 string) (string, error) {
pass, err := cmd.GetPassword(prompt)
pass, err := cmd.GetPassword(prompt, false)
if err != nil {
return "", err
}
pass2, err := cmd.GetPassword(prompt2)
pass2, err := cmd.GetPassword(prompt2, false)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions pkgs/crypto/keys/client/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func deleteApp(cmd *command.Command, args []string, iopts interface{}) error {
skipPass := opts.Force
var oldpass string
if !skipPass {
if oldpass, err = cmd.GetPassword(
"DANGER - enter password to permanently delete key:"); err != nil {
msg := "DANGER - enter password to permanently delete key:"
if oldpass, err = cmd.GetPassword(msg, false); err != nil {
return err
}
}
Expand Down
19 changes: 10 additions & 9 deletions pkgs/crypto/keys/client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ import (
)

type BaseOptions struct {
Home string `flag:"home" help:"home directory"`
Remote string `flag:"remote" help:"remote node URL (default 127.0.0.1:26657)"`
Quiet bool `flag:"quiet" help:"for parsing output"`
Home string `flag:"home" help:"home directory"`
Remote string `flag:"remote" help:"remote node URL (default 127.0.0.1:26657)"`
Quiet bool `flag:"quiet" help:"for parsing output"`
InsecurePasswordStdin bool `flag:"insecure-password-stdin" help:"WARNING! take password from stdin"`
}

var DefaultBaseOptions = BaseOptions{
Home: homeDir(),
Remote: "127.0.0.1:26657",
Quiet: false,
Home: homeDir(),
Remote: "127.0.0.1:26657",
Quiet: false,
InsecurePasswordStdin: false,
}

func homeDir() string {
// if environment set, always use that.
hd := os.Getenv("GNO_HOME")
if hd != "" {
return fmt.Sprintf("%s/.gno", hd)
return hd
}
// look for dir in local directory.
// XXX

// look for dir in home directory.
hd, err := os.UserHomeDir()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkgs/crypto/keys/client/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func signApp(cmd *command.Command, args []string, iopts interface{}) error {
}

if opts.Quiet {
opts.Pass, err = cmd.GetPassword("")
opts.Pass, err = cmd.GetPassword("", opts.InsecurePasswordStdin)
} else {
opts.Pass, err = cmd.GetPassword("Enter password.")
opts.Pass, err = cmd.GetPassword("Enter password.", opts.InsecurePasswordStdin)
}
if err != nil {
return err
Expand Down

0 comments on commit 3cc5ae8

Please sign in to comment.