Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions pkg/commands/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,30 @@ func (c *DockerCommand) NewCommandObject(obj CommandObject) CommandObject {
return defaultObj
}

// NewDockerCommand it runs docker commands
// NewDockerCommand creates a DockerCommand struct that wraps the docker client.
// Able to run docker commands and handles SSH docker hosts
func NewDockerCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.TranslationSet, config *config.AppConfig, errorChan chan error) (*DockerCommand, error) {
dockerHost, err := determineDockerHost()
if err != nil {
ogLog.Printf("> could not determine host %v", err)
}

// NOTE: Inject the determined docker host to the environment. This allows the
// `SSHHandler.HandleSSHDockerHost()` to create a local unix socket tunneled
// over SSH to the specified ssh host.
if strings.HasPrefix(dockerHost, "ssh://") {
os.Setenv(dockerHostEnvKey, dockerHost)
}

tunnelCloser, err := ssh.NewSSHHandler(osCommand).HandleSSHDockerHost()
tunnelResult, err := ssh.NewSSHHandler(osCommand).HandleSSHDockerHost(dockerHost)
if err != nil {
ogLog.Fatal(err)
}
// If we created a tunnel to the remote ssh host, we then override the dockerhost to point to the tunnel
if tunnelResult.Created {
dockerHost = tunnelResult.SocketPath
}

// Retrieve the docker host from the environment which could have been set by
// the `SSHHandler.HandleSSHDockerHost()` and override `dockerHost`.
dockerHostFromEnv := os.Getenv(dockerHostEnvKey)
if dockerHostFromEnv != "" {
dockerHost = dockerHostFromEnv
clientOpts := []client.Opt{
client.WithTLSClientConfigFromEnv(),
client.WithVersion(APIVersion),
client.WithHost(dockerHost),
}

cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(APIVersion), client.WithHost(dockerHost))
cli, err := client.NewClientWithOpts(clientOpts...)
if err != nil {
ogLog.Fatal(err)
}
Expand All @@ -110,7 +107,7 @@ func NewDockerCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Translat
Client: cli,
ErrorChan: errorChan,
InDockerComposeProject: true,
Closers: []io.Closer{tunnelCloser},
Closers: []io.Closer{tunnelResult.Closer},
}

dockerCommand.setDockerComposeCommand(config)
Expand Down
33 changes: 19 additions & 14 deletions pkg/commands/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,36 @@ func NewSSHHandler(oSCommand CmdKiller) *SSHHandler {
}
}

type TunnelResult struct {
Closer io.Closer
SocketPath string
Created bool
}

// HandleSSHDockerHost overrides the DOCKER_HOST environment variable
// to point towards a local unix socket tunneled over SSH to the specified ssh host.
func (self *SSHHandler) HandleSSHDockerHost() (io.Closer, error) {
const key = "DOCKER_HOST"
ctx := context.Background()
u, err := url.Parse(self.getenv(key))
func (self *SSHHandler) HandleSSHDockerHost(dockerHost string) (TunnelResult, error) {
u, err := url.Parse(dockerHost)
if err != nil {
// if no or an invalid docker host is specified, continue nominally
return noopCloser{}, nil
return TunnelResult{Closer: noopCloser{}}, nil
}

// if the docker host scheme is "ssh", forward the docker socket before creating the client
if u.Scheme == "ssh" {
tunnel, err := self.createDockerHostTunnel(ctx, u.Host)
if err != nil {
return noopCloser{}, fmt.Errorf("tunnel ssh docker host: %w", err)
}
err = self.setenv(key, tunnel.socketPath)
ctx := context.Background()
tunnel, err := self.createDockerHostTunnel(ctx, u.String())
if err != nil {
return noopCloser{}, fmt.Errorf("override DOCKER_HOST to tunneled socket: %w", err)
return TunnelResult{Closer: noopCloser{}}, fmt.Errorf("tunnel ssh docker host: %w", err)
}

return tunnel, nil
return TunnelResult{
Closer: tunnel,
SocketPath: tunnel.socketPath,
Created: true,
}, nil
}
return noopCloser{}, nil
return TunnelResult{Closer: noopCloser{}}, nil
}

type noopCloser struct{}
Expand All @@ -86,7 +91,7 @@ func (t *tunneledDockerHost) Close() error {
}

func (self *SSHHandler) createDockerHostTunnel(ctx context.Context, remoteHost string) (*tunneledDockerHost, error) {
socketDir, err := self.tempDir("/tmp", "lazydocker-sshtunnel-")
socketDir, err := self.tempDir("/tmp", "lazydocker-ssh-tunnel-")
if err != nil {
return nil, fmt.Errorf("create ssh tunnel tmp file: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/commands/ssh/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestSSHHandlerHandleSSHDockerHost(t *testing.T) {

tempDir := func(dir string, pattern string) (string, error) {
assert.Equal(t, "/tmp", dir)
assert.Equal(t, "lazydocker-sshtunnel-", pattern)
assert.Equal(t, "lazydocker-ssh-tunnel-", pattern)

return "/tmp/lazydocker-ssh-tunnel-12345", nil
}
Expand All @@ -64,7 +64,7 @@ func TestSSHHandlerHandleSSHDockerHost(t *testing.T) {

startCmdCount := 0
startCmd := func(cmd *exec.Cmd) error {
assert.EqualValues(t, []string{"ssh", "-L", "/tmp/lazydocker-ssh-tunnel-12345/dockerhost.sock:/var/run/docker.sock", "192.168.5.178", "-N"}, cmd.Args)
assert.EqualValues(t, []string{"ssh", "-L", "/tmp/lazydocker-ssh-tunnel-12345/dockerhost.sock:/var/run/docker.sock", s.envVarValue, "-N"}, cmd.Args)

startCmdCount++

Expand All @@ -91,7 +91,7 @@ func TestSSHHandlerHandleSSHDockerHost(t *testing.T) {
setenv: setenv,
}

_, err := handler.HandleSSHDockerHost()
_, err := handler.HandleSSHDockerHost(s.envVarValue)
assert.NoError(t, err)

assert.Equal(t, s.expectedDialContextCount, dialContextCount)
Expand Down
Loading