forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
215 lines (178 loc) · 6.06 KB
/
ssh.go
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package ssh
import (
"golang.org/x/crypto/ssh"
"net"
"time"
"testing"
)
type Host struct {
Hostname string
SshUserName string
SshKeyPair *KeyPair
}
// Check that you can connect via SSH to the given host and fail the test if the connection fails.
func CheckSshConnection(t *testing.T, host Host) {
err := CheckSshConnectionE(t, host)
if err != nil {
t.Fatal(err)
}
}
// Check that you can connect via SSH to the given host and return an error if the connection fails
func CheckSshConnectionE(t *testing.T, host Host) error {
_, err := CheckSshCommandE(t, host, "'exit'")
return err
}
// Check that you can connect via SSH to the given host and run the given command. Returns the stdout/stderr.
func CheckSshCommand(t *testing.T, host Host, command string) string {
out, err := CheckSshCommandE(t, host, command)
if err != nil {
t.Fatal(err)
}
return out
}
// Check that you can connect via SSH to the given host and run the given command. Returns the stdout/stderr.
func CheckSshCommandE(t *testing.T, host Host, command string) (string, error) {
authMethods, err := createAuthMethodsForHost(host)
if err != nil {
return "", err
}
hostOptions := SshConnectionOptions{
Username: host.SshUserName,
Address: host.Hostname,
Port: 22,
Command: command,
AuthMethods: authMethods,
}
sshSession := &SshSession{
Options: &hostOptions,
JumpHost: &JumpHostSession{},
}
defer sshSession.Cleanup(t)
return runSshCommand(sshSession)
}
// CheckPrivateSshConnection attempts to connect to privateHost (which is not addressable from the Internet) via a
// separate publicHost (which is addressable from the Internet) and then executes "command" on privateHost and returns
// its output. It is useful for checking that it's possible to SSH from a Bastion Host to a private instance.
func CheckPrivateSshConnection(t *testing.T, publicHost Host, privateHost Host, command string) string {
out, err := CheckPrivateSshConnectionE(t, publicHost, privateHost, command)
if err != nil {
t.Fatal(err)
}
return out
}
// CheckPrivateSshConnection attempts to connect to privateHost (which is not addressable from the Internet) via a
// separate publicHost (which is addressable from the Internet) and then executes "command" on privateHost and returns
// its output. It is useful for checking that it's possible to SSH from a Bastion Host to a private instance.
func CheckPrivateSshConnectionE(t *testing.T, publicHost Host, privateHost Host, command string) (string, error) {
jumpHostAuthMethods, err := createAuthMethodsForHost(publicHost)
if err != nil {
return "", err
}
jumpHostOptions := SshConnectionOptions{
Username: publicHost.SshUserName,
Address: publicHost.Hostname,
Port: 22,
AuthMethods: jumpHostAuthMethods,
}
hostAuthMethods, err := createAuthMethodsForHost(privateHost)
if err != nil {
return "", err
}
hostOptions := SshConnectionOptions{
Username: privateHost.SshUserName,
Address: privateHost.Hostname,
Port: 22,
Command: command,
AuthMethods: hostAuthMethods,
JumpHost: &jumpHostOptions,
}
sshSession := &SshSession{
Options: &hostOptions,
JumpHost: &JumpHostSession{},
}
defer sshSession.Cleanup(t)
return runSshCommand(sshSession)
}
func runSshCommand(sshSession *SshSession) (string, error) {
if err := setupSshClient(sshSession); err != nil {
return "", err
}
if err := setupSshSession(sshSession); err != nil {
return "", err
}
bytes, err := sshSession.Session.CombinedOutput(sshSession.Options.Command)
if err != nil {
return "", err
}
return string(bytes), nil
}
func setupSshClient(sshSession *SshSession) error {
if sshSession.Options.JumpHost == nil {
return fillSshClientForHost(sshSession)
} else {
return fillSshClientForJumpHost(sshSession)
}
}
func fillSshClientForHost(sshSession *SshSession) error {
client, err := createSshClient(sshSession.Options)
if err != nil {
return err
}
sshSession.Client = client
return nil
}
func fillSshClientForJumpHost(sshSession *SshSession) error {
jumpHostClient, err := createSshClient(sshSession.Options.JumpHost)
if err != nil {
return err
}
sshSession.JumpHost.JumpHostClient = jumpHostClient
hostVirtualConn, err := jumpHostClient.Dial("tcp", sshSession.Options.ConnectionString())
if err != nil {
return err
}
sshSession.JumpHost.HostVirtualConnection = hostVirtualConn
hostConn, hostIncomingChannels, hostIncomingRequests, err := ssh.NewClientConn(hostVirtualConn, sshSession.Options.ConnectionString(), createSshClientConfig(sshSession.Options))
if err != nil {
return err
}
sshSession.JumpHost.HostConnection = hostConn
sshSession.Client = ssh.NewClient(hostConn, hostIncomingChannels, hostIncomingRequests)
return nil
}
func setupSshSession(sshSession *SshSession) error {
session, err := sshSession.Client.NewSession()
if err != nil {
return err
}
sshSession.Session = session
return nil
}
func createSshClient(options *SshConnectionOptions) (*ssh.Client, error) {
sshClientConfig := createSshClientConfig(options)
return ssh.Dial("tcp", options.ConnectionString(), sshClientConfig)
}
func createSshClientConfig(hostOptions *SshConnectionOptions) *ssh.ClientConfig {
clientConfig := &ssh.ClientConfig{
User: hostOptions.Username,
Auth: hostOptions.AuthMethods,
// Do not do a host key check, as Terratest is only used for testing, not prod
HostKeyCallback: NoOpHostKeyCallback,
// By default, Go does not impose a timeout, so a SSH connection attempt can hang for a LONG time.
Timeout: 10 * time.Second,
}
clientConfig.SetDefaults()
return clientConfig
}
// An ssh.HostKeyCallback that does nothing. Only use this when you're sure you don't want to check the host key at all
// (e.g., only for testing and non-production use cases).
func NoOpHostKeyCallback(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
}
func createAuthMethodsForHost(host Host) ([]ssh.AuthMethod, error) {
signer, err := ssh.ParsePrivateKey([]byte(host.SshKeyPair.PrivateKey))
if err != nil {
return []ssh.AuthMethod{}, err
}
return []ssh.AuthMethod{ssh.PublicKeys(signer)}, nil
}