Return error when ssh key does not exist

This commit is contained in:
Dan Sosedoff 2019-11-02 11:36:31 -05:00
parent f2ab51a314
commit 994ceca1d1
2 changed files with 15 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package client
import (
"errors"
"fmt"
"io"
"io/ioutil"
@ -71,16 +72,21 @@ func makeConfig(info *shared.SSHInfo) (*ssh.ClientConfig, error) {
keyPath = expandKeyPath(keyPath)
}
if fileExists(keyPath) {
key, err := parsePrivateKey(keyPath)
if err != nil {
return nil, err
}
methods = append(methods, ssh.PublicKeys(key))
if !fileExists(keyPath) {
return nil, errors.New("ssh public key not found at " + keyPath)
}
methods = append(methods, ssh.Password(info.Password))
// Appen public key authentication method
key, err := parsePrivateKey(keyPath)
if err != nil {
return nil, err
}
methods = append(methods, ssh.PublicKeys(key))
// Append password authentication method
if info.Password != "" {
methods = append(methods, ssh.Password(info.Password))
}
cfg := &ssh.ClientConfig{
User: info.User,

View File

@ -4,6 +4,7 @@ import (
"fmt"
)
// SSHInfo contains ssh server configuration
type SSHInfo struct {
Host string `json:"host,omitempty"`
Port string `json:"port,omitempty"`