forked from k0sproject/rig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openssh.go
396 lines (333 loc) · 10.1 KB
/
openssh.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package rig
import (
"bufio"
"errors"
"fmt"
"io"
"os"
goexec "os/exec"
"strconv"
"strings"
"sync"
"github.com/k0sproject/rig/exec"
"github.com/k0sproject/rig/log"
)
// ErrControlPathNotSet is returned when the controlpath is not set when disconnecting from a multiplexed connection
var ErrControlPathNotSet = errors.New("controlpath not set")
// OpenSSH is a rig.Connection implementation that uses the system openssh client "ssh" to connect to remote hosts.
// The connection is multiplexec over a control master, so that subsequent connections don't need to re-authenticate.
type OpenSSH struct {
Address string `yaml:"address" validate:"required"`
User *string `yaml:"user"`
Port *int `yaml:"port"`
KeyPath *string `yaml:"keyPath,omitempty"`
ConfigPath *string `yaml:"configPath,omitempty"`
Options OpenSSHOptions `yaml:"options,omitempty"`
DisableMultiplexing bool `yaml:"disableMultiplexing,omitempty"`
isConnected bool
controlMutex sync.Mutex
isWindows *bool
name string
}
func boolPtr(b bool) *bool {
return &b
}
// Protocol returns the protocol name
func (c *OpenSSH) Protocol() string {
return "OpenSSH"
}
// IPAddress returns the IP address of the remote host
func (c *OpenSSH) IPAddress() string {
return c.Address
}
// IsWindows returns true if the remote host is windows
func (c *OpenSSH) IsWindows() bool {
// Implement your logic here
if c.isWindows != nil {
return *c.isWindows
}
c.isWindows = boolPtr(c.Exec("cmd.exe /c exit 0") == nil)
log.Debugf("%s: host is windows: %t", c, *c.isWindows)
return *c.isWindows
}
// OpenSSHOptions are options for the OpenSSH client. For example StrictHostkeyChecking: false becomes -o StrictHostKeyChecking=no
type OpenSSHOptions map[string]any
// Copy returns a copy of the options
func (o OpenSSHOptions) Copy() OpenSSHOptions {
dup := make(OpenSSHOptions, len(o))
for k, v := range o {
dup[k] = v
}
return dup
}
// Set sets an option key to value
func (o OpenSSHOptions) Set(key string, value any) {
o[key] = value
}
// SetIfUnset sets the option if it's not already set
func (o OpenSSHOptions) SetIfUnset(key string, value any) {
if o.IsSet(key) {
return
}
o.Set(key, value)
}
// IsSet returns true if the option is set
func (o OpenSSHOptions) IsSet(key string) bool {
_, ok := o[key]
return ok
}
// ToArgs converts the options to command line arguments
func (o OpenSSHOptions) ToArgs() []string {
args := make([]string, 0, len(o)*2)
for k, v := range o {
if b, ok := v.(bool); ok {
if b {
args = append(args, "-o", fmt.Sprintf("%s=yes", k))
} else {
args = append(args, "-o", fmt.Sprintf("%s=no", k))
}
continue
}
args = append(args, "-o", fmt.Sprintf("%s=%v", k, v))
}
return args
}
// DefaultOpenSSHOptions are the default options for the OpenSSH client
var DefaultOpenSSHOptions = OpenSSHOptions{
// It's easy to end up with control paths that are too long for unix sockets (104 chars?)
// with the default ~/.ssh/master-%r@%h:%p, for example something like:
// /Users/user/.ssh/master-ec2-xx-xx-xx-xx.eu-central-1.compute.amazonaws.com-centos.AAZFTHkT5....
// so, using %C here for hash instead.
//
// Note that openssh client does not respect $HOME so this will always be in the actual home dir
// that the ssh client digs from /etc/passwd.
"ControlPath": "~/.ssh/ctrl-%C",
"ControlMaster": false,
"ServerAliveInterval": "60",
"ServerAliveCountMax": "3",
"StrictHostKeyChecking": false,
"Compression": false,
"ConnectTimeout": "10",
}
// SetDefaults sets default values
func (c *OpenSSH) SetDefaults() {
if c.Options == nil {
c.Options = make(OpenSSHOptions)
}
for k, v := range DefaultOpenSSHOptions {
if v == nil {
delete(c.Options, k)
continue
}
c.Options.SetIfUnset(k, v)
}
if c.DisableMultiplexing {
delete(c.Options, "ControlMaster")
delete(c.Options, "ControlPath")
}
}
func (c *OpenSSH) userhost() string {
if c.User != nil {
return fmt.Sprintf("%s@%s", *c.User, c.Address)
}
return c.Address
}
func (c *OpenSSH) args() []string {
args := []string{}
if c.KeyPath != nil && *c.KeyPath != "" {
args = append(args, "-i", *c.KeyPath)
}
if c.Port != nil {
args = append(args, "-p", strconv.Itoa(*c.Port))
}
if c.ConfigPath != nil && *c.ConfigPath != "" {
args = append(args, "-F", *c.ConfigPath)
}
args = append(args, c.userhost())
return args
}
// Connect connects to the remote host. If multiplexing is enabled, this will start a control master. If multiplexing is disabled, this will just run a noop command to check connectivity.
func (c *OpenSSH) Connect() error {
if c.isConnected {
return nil
}
if c.DisableMultiplexing {
if err := c.Exec("exit 0", exec.StreamOutput()); err != nil {
return fmt.Errorf("failed to connect: %w", err)
}
c.isConnected = true
return nil
}
c.controlMutex.Lock()
defer c.controlMutex.Unlock()
opts := c.Options.Copy()
opts.Set("ControlMaster", true)
opts.Set("ControlPersist", 600)
opts.Set("TCPKeepalive", true)
args := []string{"-N", "-f"}
args = append(args, opts.ToArgs()...)
args = append(args, c.args()...)
cmd := goexec.Command("ssh", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
log.Debugf("%s: starting ssh control master", c)
err := cmd.Run()
if err != nil {
c.isConnected = false
return fmt.Errorf("failed to start ssh multiplexing control master: %w", err)
}
c.isConnected = true
log.Debugf("%s: started ssh multipliexing control master", c)
return nil
}
func (c *OpenSSH) closeControl() error {
c.controlMutex.Lock()
defer c.controlMutex.Unlock()
if !c.isConnected {
return nil
}
controlPath, ok := c.Options["ControlPath"].(string)
if !ok {
return ErrControlPathNotSet
}
args := []string{"-O", "exit", "-S", controlPath}
args = append(args, c.args()...)
args = append(args, c.userhost())
log.Debugf("%s: closing ssh multiplexing control master", c)
cmd := goexec.Command("ssh", args...)
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to close control master: %w", err)
}
c.isConnected = false
return nil
}
// Exec executes a command on the remote host
func (c *OpenSSH) Exec(cmdStr string, opts ...exec.Option) error { //nolint:cyclop
if !c.DisableMultiplexing && !c.isConnected {
return ErrNotConnected
}
execOpts := exec.Build(opts...)
command, err := execOpts.Command(cmdStr)
if err != nil {
return fmt.Errorf("failed to build command: %w", err)
}
args := c.Options.ToArgs()
// use BatchMode (no password prompts) for non-interactive commands
args = append(args, "-o", "BatchMode=yes")
args = append(args, c.args()...)
args = append(args, "--", command)
cmd := goexec.Command("ssh", args...)
if execOpts.Stdin != "" {
execOpts.LogStdin(c.String())
cmd.Stdin = strings.NewReader(execOpts.Stdin)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("failed to get stdout pipe: %w", err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
return fmt.Errorf("failed to get stderr pipe: %w", err)
}
execOpts.LogCmd(c.String(), cmd.String())
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start command: %w", err)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if execOpts.Writer == nil {
outputScanner := bufio.NewScanner(stdout)
for outputScanner.Scan() {
execOpts.AddOutput(c.String(), outputScanner.Text()+"\n", "")
}
if err := outputScanner.Err(); err != nil {
execOpts.LogErrorf("%s: failed to scan stdout: %v", c, err)
}
} else {
if _, err := io.Copy(execOpts.Writer, stdout); err != nil {
execOpts.LogErrorf("%s: failed to stream stdout: %v", c, err)
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
outputScanner := bufio.NewScanner(stderr)
for outputScanner.Scan() {
execOpts.AddOutput(c.String(), "", outputScanner.Text()+"\n")
}
if err := outputScanner.Err(); err != nil {
execOpts.LogErrorf("%s: failed to scan stderr: %v", c, err)
}
}()
wg.Wait()
err = cmd.Wait()
if err != nil {
return fmt.Errorf("%w: command wait: %w", ErrCommandFailed, err)
}
return nil
}
// ExecStreams executes a command on the remote host, streaming stdin, stdout and stderr
func (c *OpenSSH) ExecStreams(cmdStr string, stdin io.ReadCloser, stdout, stderr io.Writer, opts ...exec.Option) (exec.Waiter, error) {
if !c.DisableMultiplexing && !c.isConnected {
return nil, ErrNotConnected
}
execOpts := exec.Build(opts...)
command, err := execOpts.Command(cmdStr)
if err != nil {
return nil, fmt.Errorf("%w: failed to build command: %w", ErrCommandFailed, err)
}
args := c.Options.ToArgs()
args = append(args, "-o", "BatchMode=yes")
args = append(args, c.args()...)
args = append(args, "--", command)
cmd := goexec.Command("ssh", args...)
cmd.Stdin = stdin
cmd.Stdout = stdout
cmd.Stderr = stderr
execOpts.LogCmd(c.String(), cmd.String())
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("%w: failed to start: %w", ErrCommandFailed, err)
}
return cmd, nil
}
// ExecInteractive executes an interactive command on the remote host, streaming stdin, stdout and stderr
func (c *OpenSSH) ExecInteractive(cmdStr string) error {
cmd, err := c.ExecStreams(cmdStr, os.Stdin, os.Stdout, os.Stderr)
if err != nil {
return err
}
if err := cmd.Wait(); err != nil {
return fmt.Errorf("%w: command wait: %w", ErrCommandFailed, err)
}
return nil
}
func (c *OpenSSH) String() string {
if c.name != "" {
return c.name
}
c.name = fmt.Sprintf("[OpenSSH] %s", c.userhost())
if c.Port != nil {
c.name = fmt.Sprintf("%s:%d", c.name, *c.Port)
}
return c.name
}
// IsConnected returns true if the connection is connected
func (c *OpenSSH) IsConnected() bool {
return c.isConnected
}
// Disconnect disconnects from the remote host. If multiplexing is enabled, this will close the control master.
// If multiplexing is disabled, this will do nothing.
func (c *OpenSSH) Disconnect() {
if c.DisableMultiplexing {
// nothing to do
return
}
if err := c.closeControl(); err != nil {
log.Warnf("%s: failed to close control master: %v", c, err)
}
}