Skip to content

Commit

Permalink
Merge pull request #1708 from svanellewee/issue-1608-password-hiding-…
Browse files Browse the repository at this point in the history
…again

added AskForPasswordValue and 2 tests
  • Loading branch information
r2d4 committed Jul 20, 2017
2 parents c98ac20 + ed58437 commit afcee9a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
27 changes: 27 additions & 0 deletions cmd/minikube/cmd/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config

import (
"bytes"
"fmt"
"testing"
)

Expand Down Expand Up @@ -57,6 +58,32 @@ var configTestCases = []configTestCase{
},
}

func TestHiddenPrint(t *testing.T) {
testString := "gabbagabbahey"
b := new(bytes.Buffer)
_, err := b.WriteString(fmt.Sprintf("%s\r\n", testString)) // you need the \r!
if err != nil {
t.Errorf("Could not prepare bytestring")
}
result, err := concealableAskForStaticValue(b, "hello", true)
if result != testString {
t.Errorf("Result %s not match %s", result, testString)
}
}

func TestVerbosePrint(t *testing.T) {
testString := "gabbagabbahey"
b := new(bytes.Buffer)
_, err := b.WriteString(fmt.Sprintf("%s\r\n", testString)) // you need the \r!
if err != nil {
t.Errorf("Could not prepare bytestring")
}
result, err := concealableAskForStaticValue(b, "hello", false)
if result != testString {
t.Errorf("Result %s not match %s", result, testString)
}
}

func TestWriteConfig(t *testing.T) {
var b bytes.Buffer
for _, tt := range configTestCases {
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/config/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var addonsConfigureCmd = &cobra.Command{
if enableDR {
dockerServer = AskForStaticValue("-- Enter docker registry server url: ")
dockerUser = AskForStaticValue("-- Enter docker registry username: ")
dockerPass = AskForStaticValue("-- Enter docker registry password: ")
dockerPass = AskForPasswordValue("-- Enter docker registry password: ")
}

// Create ECR Secret
Expand Down
46 changes: 46 additions & 0 deletions cmd/minikube/cmd/config/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package config
import (
"bufio"
"fmt"
"golang.org/x/crypto/ssh/terminal"
"io"
"log"
"os"
"strings"
Expand Down Expand Up @@ -75,6 +77,50 @@ func AskForStaticValue(s string) string {
}
}

func concealableAskForStaticValue(readWriter io.ReadWriter, promptString string, hidden bool) (string, error) {
for {
var (
response string
err error
term *terminal.Terminal
)

if hidden {
term = terminal.NewTerminal(readWriter, "")
response, err = term.ReadPassword(promptString)
} else {
term = terminal.NewTerminal(readWriter, promptString)
response, err = term.ReadLine()
}

if err != nil {
return "", err
}
response = strings.TrimSpace(response)
if len(response) == 0 {
fmt.Println("--Error, please enter a value:")
return concealableAskForStaticValue(readWriter, promptString, hidden)
}
return response, nil
}
}

func AskForPasswordValue(s string) string {

stdInFd := int(os.Stdin.Fd())
oldState, err := terminal.MakeRaw(stdInFd)
if err != nil {
log.Fatal(err)
}
defer terminal.Restore(stdInFd, oldState)

result, err := concealableAskForStaticValue(os.Stdin, s, true)
if err != nil {
log.Fatal(err)
}
return result
}

// posString returns the first index of element in slice.
// If slice does not contain element, returns -1.
func posString(slice []string, element string) int {
Expand Down

0 comments on commit afcee9a

Please sign in to comment.