Skip to content

Commit

Permalink
refactor: optimize password prompt
Browse files Browse the repository at this point in the history
Password prompt for login user changed from 'Password' to 'Password for zhangsan'.
  • Loading branch information
windvalley committed Jan 14, 2022
1 parent 7b34776 commit 3fcd73e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
38 changes: 22 additions & 16 deletions internal/pkg/sshtask/sshtask.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,24 @@ func (t *Task) BatchRun() {
util.CheckErr(err)
}

log.Debugf("got target hosts, count: %d", len(allHosts))

if t.configFlags.Hosts.List {
hostsCount := len(allHosts)
fmt.Printf("%s\n", strings.Join(allHosts, "\n"))
fmt.Fprintf(os.Stderr, "\nhosts (%d)\n", hostsCount)
return
}

authConf := t.configFlags.Auth
runConf := t.configFlags.Run

log.Debugf("Auth: login user: %s", authConf.User)

if runConf.Sudo {
log.Debugf("Auth: use sudo as user '%s'", runConf.AsUser)
}

switch t.taskType {
case CommandTask:
if t.command == "" {
Expand Down Expand Up @@ -445,8 +456,6 @@ func (t *Task) getSSHAuthMethods(password *string) []ssh.AuthMethod {
)

if *password != "" {
log.Debugf("Auth: received password of the login user")

auths = append(auths, ssh.Password(*password))
} else {
log.Debugf("Auth: password of the login user not provided")
Expand Down Expand Up @@ -479,7 +488,7 @@ func (t *Task) getSSHAuthMethods(password *string) []ssh.AuthMethod {
if len(auths) == 0 {
log.Debugf("Auth: no valid authentication method detected. Prompt for password of the login user")

*password = getPasswordFromPrompt()
*password = getPasswordFromPrompt(t.configFlags.Auth.User)
auths = append(auths, ssh.Password(*password))

return auths
Expand All @@ -488,7 +497,7 @@ func (t *Task) getSSHAuthMethods(password *string) []ssh.AuthMethod {
if *password == "" && t.configFlags.Run.Sudo {
log.Debugf("Auth: using sudo as other user needs password. Prompt for password of the login user")

*password = getPasswordFromPrompt()
*password = getPasswordFromPrompt(t.configFlags.Auth.User)
auths = append(auths, ssh.Password(*password))
}

Expand All @@ -502,15 +511,14 @@ func (t *Task) getProxySSHAuthMethods(password *string) []ssh.AuthMethod {
err error
)

if t.configFlags.Proxy.Password != "" {
log.Debugf("Proxy Auth: received password of the proxy user")
log.Debugf("Proxy Auth: proxy login user: %s", t.configFlags.Proxy.User)

if t.configFlags.Proxy.Password != "" {
proxyAuths = append(proxyAuths, ssh.Password(t.configFlags.Proxy.Password))
} else {
log.Debugf("Proxy Auth: received password of the proxy user")

proxyAuths = append(proxyAuths, ssh.Password(*password))
}
log.Debugf("Proxy Auth: received password of the proxy user")

proxyKeyfiles := t.getProxyItentityFiles()
if len(proxyKeyfiles) != 0 {
Expand Down Expand Up @@ -552,23 +560,21 @@ func (t *Task) getPassword() (password string, err error) {

password = strings.TrimSpace(string(passwordContent))

log.Debugf("read password from file '%s'", authFile)
log.Debugf("Auth: read password from file '%s'", authFile)
}

passwordFromFlag := t.configFlags.Auth.Password
if passwordFromFlag != "" {
password = passwordFromFlag

log.Debugf("read password from commandline flag or configuration file")
log.Debugf("Auth: received password from commandline flag or configuration file")
}

assignRealPass(&password)

if t.configFlags.Auth.AskPass {
log.Debugf("Auth: ask for password of login user by flag '-k/--auth.ask-pass'")
password = getPasswordFromPrompt()

log.Debugf("read password from terminal prompt")
password = getPasswordFromPrompt(t.configFlags.Auth.User)
}

//nolint:nakedret
Expand Down Expand Up @@ -652,8 +658,8 @@ func getSigner(keyfile, passphrase string) (ssh.Signer, string) {
return pubkey, fmt.Sprintf("parsed identity file '%s'", keyfile)
}

func getPasswordFromPrompt() string {
fmt.Fprintf(os.Stderr, "Password: ")
func getPasswordFromPrompt(loginUser string) string {
fmt.Fprintf(os.Stderr, "Password for %s: ", loginUser)

var passwordByte []byte
passwordByte, err := term.ReadPassword(0)
Expand All @@ -666,7 +672,7 @@ func getPasswordFromPrompt() string {

fmt.Println("")

log.Debugf("Auth: read password of the login user from terminal prompt")
log.Debugf("Auth: received password of the login user '%s' from terminal prompt", loginUser)

return password
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/batchssh/batchssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ func NewClient(
auths []ssh.AuthMethod,
options ...func(*Client),
) (*Client, error) {
log.Debugf("Login user: %s", user)

client := Client{
User: user,
Password: password,
Expand Down Expand Up @@ -814,8 +812,6 @@ func WithConcurrency(count int) func(*Client) {
// WithProxyServer connect remote hosts by proxy server.
func WithProxyServer(proxyServer, user string, port int, auths []ssh.AuthMethod) func(*Client) {
return func(c *Client) {
log.Debugf("Proxy login user: %s", user)

proxySSHConfig := &ssh.ClientConfig{
User: user,
Auth: auths,
Expand Down

0 comments on commit 3fcd73e

Please sign in to comment.