From 0fc0fc518845cf2752057a13ee56a066428f3fc0 Mon Sep 17 00:00:00 2001 From: Rajat Chopra Date: Mon, 17 Sep 2018 17:18:03 -0400 Subject: [PATCH] pkg/asset/installconfig: New asset for custom user input for password. Loops around for double confirmation of the password. Signed-off-by: Rajat Chopra --- pkg/asset/installconfig/password.go | 60 +++++++++++++++++++++++++++++ pkg/asset/installconfig/stock.go | 3 +- 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 pkg/asset/installconfig/password.go diff --git a/pkg/asset/installconfig/password.go b/pkg/asset/installconfig/password.go new file mode 100644 index 00000000000..33f903ca4ec --- /dev/null +++ b/pkg/asset/installconfig/password.go @@ -0,0 +1,60 @@ +package installconfig + +import ( + "bufio" + "fmt" + "os" + + "github.com/openshift/installer/pkg/asset" + "golang.org/x/crypto/ssh/terminal" +) + +var ( + passwordPrompt = fmt.Sprintf("Enter password:") + reEnterPrompt = fmt.Sprintf("Re-enter password:") +) + +// password is an asset that queries the user for the password to the cluster +// +// Contents[0] is the actual string form of the password +type password struct { + InputReader *bufio.Reader +} + +var _ asset.Asset = (*password)(nil) + +// Dependencies returns no dependencies. +func (a *password) Dependencies() []asset.Asset { + return []asset.Asset{} +} + +// Generate queries for input from the user. +func (a *password) Generate(map[asset.Asset]*asset.State) (*asset.State, error) { + password, err := a.queryUserForPassword() + return assetStateForStringContents(password), err +} + +// queryUserForPassword for prompts Stdin for password without echoing anything on screen +// TODO: mask the password characters +func (a *password) queryUserForPassword() (string, error) { + for { + fmt.Printf(passwordPrompt) + input, err := terminal.ReadPassword(int(os.Stdin.Fd())) + if err != nil { + fmt.Printf("failed to read password %v", err) + return "", err + } + fmt.Println() + fmt.Printf(reEnterPrompt) + confirmInput, err := terminal.ReadPassword(int(os.Stdin.Fd())) + if err != nil { + fmt.Printf("failed to read password %v", err) + return "", err + } + fmt.Println() + if string(input) == string(confirmInput) { + return string(input), nil + } + fmt.Println("Password did not match.") + } +} diff --git a/pkg/asset/installconfig/stock.go b/pkg/asset/installconfig/stock.go index 0e33a9125b3..1aa82d8a854 100644 --- a/pkg/asset/installconfig/stock.go +++ b/pkg/asset/installconfig/stock.go @@ -54,8 +54,7 @@ func (s *StockImpl) EstablishStock(directory string, inputReader *bufio.Reader) Prompt: "Email Address:", InputReader: inputReader, } - s.password = &asset.UserProvided{ - Prompt: "Password:", + s.password = &password{ InputReader: inputReader, } s.sshKey = &sshPublicKey{