-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add custom asset for silent password
Signed-off-by: Rajat Chopra <rchopra@redhat.com>
- Loading branch information
Rajat Chopra
committed
Sep 14, 2018
1 parent
0ac2ed6
commit 7caf069
Showing
3 changed files
with
67 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters