-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh.go
More file actions
99 lines (85 loc) · 2.85 KB
/
Copy pathssh.go
File metadata and controls
99 lines (85 loc) · 2.85 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
package xshellz
import (
"context"
"errors"
"fmt"
"io"
"net"
"strconv"
"time"
"golang.org/x/crypto/ssh"
)
// commandRunner abstracts the data-plane transport (SSH in v0) so the
// Sandbox exec/file logic is unit-testable without a network.
type commandRunner interface {
// run executes cmd on the box, wiring the given streams, and returns the
// remote exit code. A non-zero exit code is NOT an error; err is reserved
// for transport failures and context cancellation.
run(ctx context.Context, cmd string, stdin io.Reader, stdout, stderr io.Writer) (int, error)
close() error
}
// dialFunc creates a commandRunner for a box; swapped out in tests.
type dialFunc func(ctx context.Context, host string, port int, signer ssh.Signer) (commandRunner, error)
// sshRunner is the real SSH data plane: one ssh.Client per sandbox, one
// session per command.
type sshRunner struct {
client *ssh.Client
}
// dialSSH connects to root@host:port using the sandbox's in-memory key.
// Host-key verification is intentionally disabled (InsecureIgnoreHostKey):
// boxes are ephemeral and generate a fresh host key on every spawn, so there
// is no stable key to pin in v0.
func dialSSH(ctx context.Context, host string, port int, signer ssh.Signer) (commandRunner, error) {
config := &ssh.ClientConfig{
User: "root",
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //nolint:gosec // ephemeral boxes, fresh host key per spawn
Timeout: 15 * time.Second,
}
addr := net.JoinHostPort(host, strconv.Itoa(port))
var d net.Dialer
conn, err := d.DialContext(ctx, "tcp", addr)
if err != nil {
return nil, fmt.Errorf("xshellz: dial %s: %w", addr, err)
}
sshConn, chans, reqs, err := ssh.NewClientConn(conn, addr, config)
if err != nil {
_ = conn.Close()
return nil, fmt.Errorf("xshellz: SSH handshake with %s: %w", addr, err)
}
return &sshRunner{client: ssh.NewClient(sshConn, chans, reqs)}, nil
}
func (r *sshRunner) run(ctx context.Context, cmd string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
session, err := r.client.NewSession()
if err != nil {
return -1, fmt.Errorf("xshellz: open SSH session: %w", err)
}
defer session.Close()
session.Stdin = stdin
session.Stdout = stdout
session.Stderr = stderr
if err := session.Start(cmd); err != nil {
return -1, fmt.Errorf("xshellz: start command: %w", err)
}
done := make(chan error, 1)
go func() { done <- session.Wait() }()
select {
case <-ctx.Done():
_ = session.Signal(ssh.SIGKILL)
_ = session.Close()
<-done
return -1, ctx.Err()
case err := <-done:
if err == nil {
return 0, nil
}
var exitErr *ssh.ExitError
if errors.As(err, &exitErr) {
return exitErr.ExitStatus(), nil
}
return -1, fmt.Errorf("xshellz: command failed: %w", err)
}
}
func (r *sshRunner) close() error {
return r.client.Close()
}