Skip to content

Commit

Permalink
pkg/asset/installconfig:
Browse files Browse the repository at this point in the history
  New asset for custom user input for password. Loops around for double confirmation of the password.

Signed-off-by: Rajat Chopra <rchopra@redhat.com>
  • Loading branch information
Rajat Chopra committed Sep 17, 2018
1 parent ea51ea6 commit e6f490a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
64 changes: 64 additions & 0 deletions pkg/asset/installconfig/password.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package installconfig

import (
"bufio"
"fmt"
"os"

"github.com/openshift/installer/pkg/asset"
"golang.org/x/crypto/ssh/terminal"
)

var (
passwordPrompt = fmt.Sprintf("Enter password (6-16 characters):")
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()
if len(input) < 6 || len(input) > 16 {
fmt.Println("Password length should be 6-16 characters.")
continue
}
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.")
}
}
3 changes: 1 addition & 2 deletions pkg/asset/installconfig/stock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit e6f490a

Please sign in to comment.