Skip to content

Commit d338f97

Browse files
authored
Add support for KeyboardInteractiveChallenge (#27)
* Add support for KeyboardInteractiveChallenge Some setups don't have `PasswordAuthentication` enabled but do have `KbdInteractiveAuthentication`. This patch introduces support for using this method with a password.
1 parent 1fcc71d commit d338f97

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

collector/collector.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -98,30 +98,42 @@ func (c *Collector) collect() Metric {
9898
c1 := make(chan int, 1)
9999
timeout := false
100100
var metric Metric
101-
var auth ssh.AuthMethod
102-
var sessionerror, autherror, commanderror error
101+
var auth []ssh.AuthMethod
102+
var sessionerror, commanderror error
103103

104104
if c.target.Certificate != "" {
105-
auth, autherror = getCertificateAuth(c.target.PrivateKey, c.target.Certificate)
105+
authMethod, autherror := getCertificateAuth(c.target.PrivateKey, c.target.Certificate)
106106
if autherror != nil {
107107
metric.FailureReason = "error"
108108
level.Error(c.logger).Log("msg", "Error setting up certificate auth", "err", autherror)
109109
return metric
110110
}
111+
auth = []ssh.AuthMethod{authMethod}
111112
} else if c.target.PrivateKey != "" {
112-
auth, autherror = getPrivateKeyAuth(c.target.PrivateKey)
113+
authMethod, autherror := getPrivateKeyAuth(c.target.PrivateKey)
113114
if autherror != nil {
114115
metric.FailureReason = "error"
115116
level.Error(c.logger).Log("msg", "Error setting up private key auth", "err", autherror)
116117
return metric
117118
}
119+
auth = []ssh.AuthMethod{authMethod}
118120
} else {
119-
auth = ssh.Password(c.target.Password)
121+
auth = []ssh.AuthMethod{
122+
ssh.KeyboardInteractiveChallenge(func(name, instruction string, questions []string, echos []bool) ([]string, error) {
123+
// assumes password is the only answer to everything
124+
answers := make([]string, len(questions))
125+
for i := range answers {
126+
answers[i] = c.target.Password
127+
}
128+
return answers, nil
129+
}),
130+
ssh.Password(c.target.Password),
131+
}
120132
}
121133

122134
sshConfig := &ssh.ClientConfig{
123135
User: c.target.User,
124-
Auth: []ssh.AuthMethod{auth},
136+
Auth: auth,
125137
HostKeyCallback: hostKeyCallback(&metric, c.target, c.logger),
126138
HostKeyAlgorithms: c.target.HostKeyAlgorithms,
127139
Timeout: time.Duration(c.target.Timeout) * time.Second,

0 commit comments

Comments
 (0)