Skip to content

Commit

Permalink
add custom asset for silent password
Browse files Browse the repository at this point in the history
Signed-off-by: Rajat Chopra <rchopra@redhat.com>
  • Loading branch information
Rajat Chopra committed Sep 14, 2018
1 parent 0ac2ed6 commit 7caf069
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
63 changes: 63 additions & 0 deletions pkg/asset/installconfig/password.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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("error in getting pasword %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())) //reEnterPrompt)
if err != nil {
return "", err
}
fmt.Println()
if string(input) == string(confirmInput) {
return string(input), nil
}
fmt.Println("Password did not match.")
}
}
1 change: 0 additions & 1 deletion pkg/asset/installconfig/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func (a *Platform) awsPlatform() (*asset.State, error) {
func (a *Platform) libvirtPlatform() (*asset.State, error) {
return assetStateForStringContents(
LibvirtPlatformType,
// TODO(yifan): Set the default URI.
asset.QueryUser(a.InputReader, "URI:", "qemu:///system"),
), nil
}
Expand Down
11 changes: 4 additions & 7 deletions pkg/asset/installconfig/stock.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,11 @@ func (s *StockImpl) EstablishStock(directory string, inputReader *bufio.Reader)
}
s.clusterID = &clusterID{}
s.emailAddress = &asset.UserProvided{
Prompt: "Email Address:",
DefaultValue: "a@b.c",
InputReader: inputReader,
Prompt: "Email Address:",
InputReader: inputReader,
}
s.password = &asset.UserProvided{
Prompt: "Password:",
DefaultValue: "verysecure",
InputReader: inputReader,
s.password = &password{
InputReader: inputReader,
}
s.sshKey = &sshPublicKey{
inputReader: inputReader,
Expand Down

0 comments on commit 7caf069

Please sign in to comment.