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

View File

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