/
ssh.go
404 lines (337 loc) · 9.83 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
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
397
398
399
400
401
402
403
404
package common
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/url"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"github.com/fatih/color"
"github.com/melbahja/goph"
"golang.org/x/crypto/ssh"
)
// SshCommandInput is the input for CallSshCommand
type SshCommandInput struct {
// Command is the command to execute. This can be the path to an executable
// or the executable with arguments.
//
// Any arguments must be given via Args
Command string
// Args are the arguments to pass to the command.
Args []string
// DisableStdioBuffer disables the stdio buffer
DisableStdioBuffer bool
// Env is a list of environment variables to add to the current environment
Env map[string]string
// AllowUknownHosts allows connecting to hosts with unknown host keys
AllowUknownHosts bool
// RemoteHost is the remote host to connect to
RemoteHost string
// Stdin is the stdin of the command
Stdin io.Reader
// StreamStdio prints stdout and stderr directly to os.Stdout/err as
// the command runs.
StreamStdio bool
// StreamStdout prints stdout directly to os.Stdout as the command runs.
StreamStdout bool
// StreamStderr prints stderr directly to os.Stderr as the command runs.
StreamStderr bool
// Sudo runs the command with sudo -n -u root
Sudo bool
}
// CallSshCommand executes a command on a remote host via ssh
func CallSshCommand(input SshCommandInput) (SshResult, error) {
return CallSshCommandWithContext(context.Background(), input)
}
// CallSshCommand executes a command on a remote host via ssh with the given context
func CallSshCommandWithContext(ctx context.Context, input SshCommandInput) (SshResult, error) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGTERM)
ctx, cancel := context.WithCancel(ctx)
go func() {
<-signals
cancel()
}()
// hack: colors do not work natively with io.MultiWriter
// as it isn't detected as a tty. If the output isn't
// being captured, then color output can be forced.
isatty := !color.NoColor
env := []string{}
if input.Env != nil {
for k, v := range input.Env {
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
}
u, err := url.Parse(input.RemoteHost)
if err != nil {
return SshResult{}, fmt.Errorf("failed to parse remote host: %w", err)
}
if u.Scheme == "" {
return SshResult{}, fmt.Errorf("missing remote host ssh scheme in remote host: %s", input.RemoteHost)
}
if u.Scheme != "ssh" {
return SshResult{}, fmt.Errorf("invalid remote host scheme: %s", u.Scheme)
}
username := ""
password := ""
if u.User != nil {
username = u.User.Username()
if pass, ok := u.User.Password(); ok {
password = pass
}
}
if username == "" {
username = os.Getenv("USER")
}
portStr := u.Port()
port := 0
if portStr != "" {
portVal, err := strconv.Atoi(portStr)
if err != nil {
return SshResult{}, fmt.Errorf("failed to parse port: %w", err)
}
port = portVal
}
if port == 0 {
port = 22
}
sshKeyPath := filepath.Join(os.Getenv("DOKKU_ROOT"), ".ssh/id_ed25519")
if !FileExists(sshKeyPath) {
sshKeyPath = filepath.Join(os.Getenv("DOKKU_ROOT"), ".ssh/id_rsa")
}
if !FileExists(sshKeyPath) {
return SshResult{}, errors.New("ssh key not found at ~/.ssh/id_ed25519 or ~/.ssh/id_rsa")
}
cmd := SshTask{
Command: input.Command,
Args: input.Args,
Env: env,
DisableStdioBuffer: input.DisableStdioBuffer,
AllowUknownHosts: input.AllowUknownHosts,
Hostname: u.Hostname(),
Port: uint(port),
Username: username,
Password: password,
SshKeyPath: sshKeyPath,
Sudo: input.Sudo,
}
if os.Getenv("DOKKU_TRACE") == "1" {
cmd.PrintCommand = true
}
if input.Stdin != nil {
cmd.Stdin = input.Stdin
} else if isatty {
cmd.Stdin = os.Stdin
}
if input.StreamStdio {
cmd.StreamStdio = true
}
if input.StreamStdout {
cmd.StdOutWriter = os.Stdout
}
if input.StreamStderr {
cmd.StdErrWriter = os.Stderr
}
res, err := cmd.Execute(ctx)
if err != nil {
return res, err
}
if res.ExitCode != 0 {
return res, errors.New(res.Stderr)
}
return res, nil
}
// SshTask is a task for executing a command on a remote host via ssh
type SshTask struct {
// Command is the command to execute. This can be the path to an executable
// or the executable with arguments.
//
// Any arguments must be given via Args
Command string
// Args are the arguments to pass to the command.
Args []string
// Shell run the command in a bash shell.
// Note that the system must have `bash` installed in the PATH or in /bin/bash
Shell bool
// Env is a list of environment variables to add to the current environment
Env []string
// Stdin connect a reader to stdin for the command
// being executed.
Stdin io.Reader
// PrintCommand prints the command before executing
PrintCommand bool
// StreamStdio prints stdout and stderr directly to os.Stdout/err as
// the command runs.
StreamStdio bool
// DisableStdioBuffer prevents any output from being saved in the
// TaskResult, which is useful for when the result is very large, or
// when you want to stream the output to another writer exclusively.
DisableStdioBuffer bool
// StdoutWriter when set will receive a copy of stdout from the command
StdOutWriter io.Writer
// StderrWriter when set will receive a copy of stderr from the command
StdErrWriter io.Writer
// AllowUknownHosts allows connecting to hosts with unknown host keys
AllowUknownHosts bool
// Hostname is the hostname to connect to
Hostname string
// Port is the port to connect to
Port uint
// Username is the username to connect with
Username string
// Password is the password to connect with
Password string
// SshKeyPath is the path to the ssh key to use
SshKeyPath string
// Sudo runs the command with sudo -n -u root
Sudo bool
}
// SshResult is the result of executing a command on a remote host via ssh
type SshResult struct {
Stdout string
Stderr string
ExitCode int
Cancelled bool
}
// Execute runs the task
func (task SshTask) Execute(ctx context.Context) (SshResult, error) {
if task.Command == "" {
return SshResult{}, errors.New("command is required")
}
if task.Hostname == "" {
return SshResult{}, errors.New("hostname is required")
}
if task.SshKeyPath == "" {
return SshResult{}, errors.New("ssh key path is required")
}
if task.Username == "" {
return SshResult{}, errors.New("username is required")
}
if task.Port == 0 {
task.Port = 22
}
if err := TouchFile(filepath.Join(os.Getenv("DOKKU_ROOT"), ".ssh", "known_hosts")); err != nil {
return SshResult{}, fmt.Errorf("failed to touch known_hosts file: %w", err)
}
auth, err := goph.Key(task.SshKeyPath, "")
if err != nil {
return SshResult{}, fmt.Errorf("failed to load ssh key: %w", err)
}
callback, err := goph.DefaultKnownHosts()
if err != nil {
return SshResult{}, fmt.Errorf("failed to load known hosts: %w", err)
}
if task.AllowUknownHosts {
callback = ssh.InsecureIgnoreHostKey()
}
connectionConf := goph.Config{
User: task.Username,
Addr: task.Hostname,
Port: task.Port,
Timeout: goph.DefaultTimeout,
Callback: callback,
Auth: auth,
}
if task.Password != "" {
connectionConf.Auth = goph.Password(task.Password)
}
client, err := goph.NewConn(&connectionConf)
if err != nil {
return SshResult{}, fmt.Errorf("failed to create ssh client: %w", err)
}
defer client.Close()
// don't try to run if the context is already cancelled
if ctx.Err() != nil {
return SshResult{
// the exec package returns -1 for cancelled commands
ExitCode: -1,
Cancelled: ctx.Err() == context.Canceled,
}, ctx.Err()
}
var command string
var commandArgs []string
if task.Shell {
command = "bash"
if len(task.Args) == 0 {
// use Split and Join to remove any extra whitespace?
startArgs := strings.Split(task.Command, " ")
script := strings.Join(startArgs, " ")
commandArgs = append([]string{"-c"}, script)
} else {
script := strings.Join(task.Args, " ")
commandArgs = append([]string{"-c"}, fmt.Sprintf("%s %s", task.Command, script))
}
} else {
command = task.Command
commandArgs = task.Args
}
if task.Sudo {
commandArgs = append([]string{"-n", "-u", "root", command}, commandArgs...)
command = "sudo"
}
if task.PrintCommand {
LogDebug(fmt.Sprintf("ssh %s@%s %s %v", task.Username, task.Hostname, command, commandArgs))
}
cmd, err := client.CommandContext(ctx, command, commandArgs...)
if err != nil {
return SshResult{}, fmt.Errorf("failed to create ssh command: %w", err)
}
if len(task.Env) > 0 {
overrides := map[string]bool{}
for _, env := range task.Env {
key := strings.Split(env, "=")[0]
overrides[key] = true
cmd.Env = append(cmd.Env, env)
}
}
if task.Stdin != nil {
cmd.Stdin = task.Stdin
}
stdoutBuff := bytes.Buffer{}
stderrBuff := bytes.Buffer{}
var stdoutWriters []io.Writer
var stderrWriters []io.Writer
if !task.DisableStdioBuffer {
stdoutWriters = append(stdoutWriters, &stdoutBuff)
stderrWriters = append(stderrWriters, &stderrBuff)
}
if task.StreamStdio {
stdoutWriters = append(stdoutWriters, os.Stdout)
stderrWriters = append(stderrWriters, os.Stderr)
}
if task.StdOutWriter != nil {
stdoutWriters = append(stdoutWriters, task.StdOutWriter)
}
if task.StdErrWriter != nil {
stderrWriters = append(stderrWriters, task.StdErrWriter)
}
cmd.Stdout = io.MultiWriter(stdoutWriters...)
cmd.Stderr = io.MultiWriter(stderrWriters...)
startErr := cmd.Start()
if startErr != nil {
return SshResult{}, startErr
}
exitCode := 0
execErr := cmd.Wait()
if execErr != nil {
if exitError, ok := execErr.(*exec.ExitError); ok {
exitCode = exitError.ExitCode()
}
}
return SshResult{
Stdout: stdoutBuff.String(),
Stderr: stderrBuff.String(),
ExitCode: exitCode,
Cancelled: ctx.Err() == context.Canceled,
}, ctx.Err()
}