Skip to content

Commit

Permalink
Drivers: SSH allow skipping known host check
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Oct 18, 2021
1 parent 80ca338 commit 62b2f92
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
22 changes: 15 additions & 7 deletions driver/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package driver

import (
"fmt"

"github.com/melbahja/goph"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
)

// SSH : Driver for handling ssh executions
Expand All @@ -19,6 +21,8 @@ type SSH struct {
PubKeyFile string
// Pass key for public key file
PubKeyPass string
// Check known hosts (only disable for tests
CheckKnownHosts bool
}

func (d *SSH) String() string {
Expand All @@ -27,6 +31,9 @@ func (d *SSH) String() string {

// set the goph Client
func (d *SSH) Client() (*goph.Client, error) {
var err error
var client *goph.Client
var callback ssh.HostKeyCallback
port := 22
if d.Port != 0 {
port = d.Port
Expand All @@ -35,21 +42,22 @@ func (d *SSH) Client() (*goph.Client, error) {
if err != nil {
return nil, err
}
callback, err := goph.DefaultKnownHosts()
if err != nil {
return nil, err
if d.CheckKnownHosts {
callback, err = goph.DefaultKnownHosts()
if err != nil {
return nil, err
}
} else {
callback = ssh.InsecureIgnoreHostKey()
}
client, err := goph.NewConn(&goph.Config{
client, err = goph.NewConn(&goph.Config{
User: d.User,
Addr: d.Host,
Port: uint(port),
Auth: auth,
Timeout: goph.DefaultTimeout,
Callback: callback,
})
if err != nil {
return nil, err
}
return client, err
}

Expand Down
17 changes: 17 additions & 0 deletions driver/ssh_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
package driver

import "testing"

func TestSSHRunCommand(t *testing.T) {
d := SSH{
User: "root",
Host: "172.17.0.2",
Port: 2222,
PubKeyFile: "/home/deven/.ssh/id_rsa",
PubKeyPass: "",
CheckKnownHosts: false,
}
_, err := d.runCommand(`ps -A`)
if err != nil {
t.Error(err)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.9.0
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
)

0 comments on commit 62b2f92

Please sign in to comment.