Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ssh context not working #577

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
27 changes: 12 additions & 15 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is incomplete

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()
tunnelCloser, err := ssh.NewSSHHandler(osCommand).HandleSSHDockerHost(dockerHost)
if err != nil {
ogLog.Fatal(err)
}

// 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.FromEnv,
client.WithVersion(APIVersion),
}
// For an ssh connection the DOCKER_HOST env variable has been overridden.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain the rationale behind these changes? I'm not clear on the reasoning

// Discard the previously determined dockerHost
if !strings.HasPrefix(dockerHost, "ssh://") {
clientOpts = append(clientOpts, 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 Down
7 changes: 4 additions & 3 deletions pkg/commands/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@ func NewSSHHandler(oSCommand CmdKiller) *SSHHandler {

// 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) {
func (self *SSHHandler) HandleSSHDockerHost(dockerHost string) (io.Closer, error) {
const key = "DOCKER_HOST"
ctx := context.Background()
u, err := url.Parse(self.getenv(key))

u, err := url.Parse(dockerHost)
if err != nil {
// if no or an invalid docker host is specified, continue nominally
return 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)
tunnel, err := self.createDockerHostTunnel(ctx, u.String())
if err != nil {
return noopCloser{}, fmt.Errorf("tunnel ssh docker host: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/ssh/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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