-
Notifications
You must be signed in to change notification settings - Fork 341
/
sshtunnel.go
137 lines (116 loc) · 3.59 KB
/
sshtunnel.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
package sshtunnel
import (
"context"
"fmt"
"io"
"os"
"strings"
"github.com/loft-sh/log"
client2 "github.com/loft-sh/devpod/pkg/client"
config2 "github.com/loft-sh/devpod/pkg/devcontainer/config"
devssh "github.com/loft-sh/devpod/pkg/ssh"
devsshagent "github.com/loft-sh/devpod/pkg/ssh/agent"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type AgentInjectFunc func(context.Context, string, *os.File, *os.File, io.WriteCloser) error
type TunnelServerFunc func(ctx context.Context, stdin io.WriteCloser, stdout io.Reader) (*config2.Result, error)
// ExecuteCommand runs the command in an SSH Tunnel and returns the result.
func ExecuteCommand(
ctx context.Context,
client client2.WorkspaceClient,
agentInject AgentInjectFunc,
sshCommand,
command string,
log log.Logger,
tunnelServerFunc TunnelServerFunc,
) (*config2.Result, error) {
// create pipes
sshTunnelStdoutReader, sshTunnelStdoutWriter, err := os.Pipe()
if err != nil {
return nil, err
}
sshTunnelStdinReader, sshTunnelStdinWriter, err := os.Pipe()
if err != nil {
return nil, err
}
defer sshTunnelStdoutWriter.Close()
defer sshTunnelStdinWriter.Close()
// start machine on stdio
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()
errChan := make(chan error, 2)
go func() {
defer log.Debugf("Done executing ssh server helper command")
defer cancel()
writer := log.Writer(logrus.InfoLevel, false)
defer writer.Close()
log.Debugf("Inject and run command: %s", sshCommand)
err := agentInject(ctx, sshCommand, sshTunnelStdinReader, sshTunnelStdoutWriter, writer)
if err != nil && !errors.Is(err, context.Canceled) && !strings.Contains(err.Error(), "signal: ") {
errChan <- fmt.Errorf("executing agent command: %w", err)
} else {
errChan <- nil
}
}()
// create pipes
gRPCConnStdoutReader, gRPCConnStdoutWriter, err := os.Pipe()
if err != nil {
return nil, err
}
gRPCConnStdinReader, gRPCConnStdinWriter, err := os.Pipe()
if err != nil {
return nil, err
}
defer gRPCConnStdoutWriter.Close()
defer gRPCConnStdinWriter.Close()
// connect to container
go func() {
defer cancel()
log.Debugf("Attempting to create SSH client")
// start ssh client as root / default user
sshClient, err := devssh.StdioClient(sshTunnelStdoutReader, sshTunnelStdinWriter, false)
if err != nil {
errChan <- errors.Wrap(err, "create ssh client")
return
}
defer log.Debugf("Connection to SSH Server closed")
defer sshClient.Close()
log.Debugf("SSH client created")
sess, err := sshClient.NewSession()
if err != nil {
errChan <- errors.Wrap(err, "create ssh session")
}
defer sess.Close()
log.Debugf("SSH session created")
identityAgent := devsshagent.GetSSHAuthSocket()
if identityAgent != "" {
log.Debugf("Forwarding ssh-agent using %s", identityAgent)
err = devsshagent.ForwardToRemote(sshClient, identityAgent)
if err != nil {
errChan <- errors.Wrap(err, "forward agent")
}
err = devsshagent.RequestAgentForwarding(sess)
if err != nil {
errChan <- errors.Wrap(err, "request agent forwarding failed")
}
}
writer := log.Writer(logrus.InfoLevel, false)
defer writer.Close()
err = devssh.Run(ctx, sshClient, command, gRPCConnStdinReader, gRPCConnStdoutWriter, writer)
if err != nil {
errChan <- errors.Wrap(err, "run agent command")
} else {
errChan <- nil
}
}()
result, err := tunnelServerFunc(cancelCtx, gRPCConnStdinWriter, gRPCConnStdoutReader)
if err != nil {
return nil, fmt.Errorf("start tunnel server: %w", err)
}
// wait until command finished
if err := <-errChan; err != nil {
return result, err
}
return result, <-errChan
}