-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathssh_executor.go
More file actions
153 lines (130 loc) · 3.5 KB
/
Copy pathssh_executor.go
File metadata and controls
153 lines (130 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package runtime
import (
"bytes"
"fmt"
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
"net"
"strings"
"time"
)
var _ Executor = (*SSHExecutor)(nil)
// SSHExecutor
type SSHExecutor struct {
Host string
User string
Password string
IdentityFile string
}
// WithIdentityFile sets the identity file option for the ssh executor
func WithIdentityFile(identityFile string) func(e *SSHExecutor) {
return func(e *SSHExecutor) {
e.IdentityFile = identityFile
}
}
// WithPassword sets the identity file option for the ssh executor
func WithPassword(pass string) func(e *SSHExecutor) {
return func(e *SSHExecutor) {
e.Password = pass
}
}
// NewSSHExecutor creates a new executor
func NewSSHExecutor(host string, user string, opts ...func(e *SSHExecutor)) Executor {
e := SSHExecutor{
Host: host,
User: user,
}
for _, o := range opts {
o(&e)
}
return e
}
// Execute executes a command on a remote host viá SSH
func (e SSHExecutor) Execute(test TestCase) (TestResult, error) {
if test.Command.InheritEnv {
panic("Inherit env is not supported viá SSH")
}
// initialize auth methods with pass auth method as the default
authMethods := []ssh.AuthMethod{
ssh.Password(e.Password),
}
// add public key auth if identity file is given
if e.IdentityFile != "" {
signer := e.createSigner()
authMethods = append(authMethods, ssh.PublicKeys(signer))
}
// create ssh config
sshConf := &ssh.ClientConfig{
User: e.User,
Auth: authMethods,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
Timeout: 10*time.Second,
}
// create ssh connection
conn, err := ssh.Dial("tcp", e.Host, sshConf)
if err != nil {
return TestResult{}, fmt.Errorf("Failed to connect to ssh %w", err)
}
// start session
session, err := conn.NewSession()
defer session.Close()
if err != nil {
return TestResult{}, fmt.Errorf("Failed to create a new ssh session %w", err)
}
var stdoutBuffer bytes.Buffer
var stderrBuffer bytes.Buffer
session.Stdout = &stdoutBuffer
session.Stderr = &stderrBuffer
for k, v := range test.Command.Env {
err := session.Setenv(k, v)
if err != nil {
test.Result = CommandResult{
Error: fmt.Errorf("Failed setting env variables, maybe ssh server is configured to only accept LC_ prefixed env variables. %w", err),
}
return TestResult{
TestCase: test,
}, nil
}
}
dirCmd := ""
if test.Command.Dir != "" {
dirCmd = fmt.Sprintf("cd %s; ", test.Command.Dir)
}
exitCode := 0
err = session.Run(fmt.Sprintf("%s %s", dirCmd, test.Command.Cmd))
switch err.(type) {
case *ssh.ExitError:
ee, _ := err.(*ssh.ExitError)
exitCode = ee.Waitmsg.ExitStatus()
case nil:
break
default:
log.Println(test.Title, " failed ", err.Error())
test.Result = CommandResult{
Error: err,
}
return TestResult{
TestCase: test,
}, nil
}
test.Result = CommandResult{
ExitCode: exitCode,
Stdout: strings.TrimSpace(strings.Replace(stdoutBuffer.String(), "\r\n", "\n", -1)),
Stderr: strings.TrimSpace(strings.Replace(stderrBuffer.String(), "\r\n", "\n", -1)),
}
log.Println("title: '"+test.Title+"'", " ExitCode: ", test.Result.ExitCode)
log.Println("title: '"+test.Title+"'", " Stdout: ", test.Result.Stdout)
log.Println("title: '"+test.Title+"'", " Stderr: ", test.Result.Stderr)
return Validate(test), nil
}
func (e SSHExecutor) createSigner() ssh.Signer {
buffer, err := ioutil.ReadFile(e.IdentityFile)
if err != nil {
log.Fatal(err)
}
signer, err := ssh.ParsePrivateKey(buffer)
return signer
}