Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ssh.KeyboardInteractive to ssh.AuthMethod list #49

Open
denysaleksandrov opened this issue Jan 24, 2020 · 3 comments
Open

Add ssh.KeyboardInteractive to ssh.AuthMethod list #49

denysaleksandrov opened this issue Jan 24, 2020 · 3 comments

Comments

@denysaleksandrov
Copy link

Arista switches, by default, allow only publickey and keyboard-interactive auth methods.
Gonir fails to create an ssh connection with the following error:

ssh: handshake failed: ssh: unable to authenticate, attempted methods [none], no supported methods remain

I fixed this error by adding ssh.KeyboardInteractive method to the ssh.ClientConfig:

// Run implements gornir.Task interface
func (t *SSHOpen) Run(ctx context.Context, logger gornir.Logger, host *gornir.Host) (gornir.TaskInstanceResult, error) {
sshConfig := &ssh.ClientConfig{
		User: host.Username,
		Auth: []ssh.AuthMethod{
			ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) ([]string, error) {
				answers := make([]string, len(questions))
				for i := range answers {
					answers[i] = host.Password
				}
				return answers, nil
			}),
			ssh.Password(host.Password),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	} // #nosec
...

ssh.KeyboardInteractive(...) simply answers with host.Password to all questions.

Please let me know if this is good enough to fix this issue, and I will submit a PR.

@dbarrosop
Copy link
Contributor

Yes, feel free to open the PR for review but I think we should answer with the password only to the password question, otherwise we may be leaking the password (imagine one of the answers to the questions is logged by the server because it wasn't meant to be a secret)

@denysaleksandrov
Copy link
Author

Indeed, I will use regex to check if "[p|P]assword:" in a question and answer those with a password.

@gwoodwa1
Copy link

I came across the same issue with Arista devices so I came up with this an amended version which checks for the Password prompt plus you could add further things into the switch statement for handling other custom prompts.

func GetSSHConfig(host *gornir.Host, logger gornir.Logger) (*ssh.ClientConfig, error) {
    // Handle the interactive prompting for credentials
    interactiveAuth := ssh.KeyboardInteractive(
        func(user, instruction string, questions []string, echos []bool) ([]string, error) {
            answers := make([]string, len(questions))
            for i, question := range questions {
                switch question {
                case "Password: ":
                    answers[i] = host.Password
                default:
                    answers[i] = "" // Default to empty answer
                }
            }
            return answers, nil
        },
    )
	sshConfig := &ssh.ClientConfig{
		User:            host.Username,
		Auth:            []ssh.AuthMethod{interactiveAuth},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	}
	return sshConfig, nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants