Fix ssh context not working

This commit is contained in:
Clément PÉAU 2024-09-02 22:21:54 +02:00
parent 80af149b02
commit 9bdc0cc27d
2 changed files with 21 additions and 10 deletions

View file

@ -69,19 +69,29 @@ func (c *DockerCommand) NewCommandObject(obj CommandObject) CommandObject {
return defaultObj
}
// NewDockerCommand it runs docker commands
// NewDockerCommand creates a DockerCommand struct that will be able to run docker commands.
func NewDockerCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.TranslationSet, config *config.AppConfig, errorChan chan error) (*DockerCommand, error) {
tunnelCloser, err := ssh.NewSSHHandler(osCommand).HandleSSHDockerHost()
if err != nil {
ogLog.Fatal(err)
}
dockerHost, err := determineDockerHost()
if err != nil {
ogLog.Printf("> could not determine host %v", err)
}
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(APIVersion), client.WithHost(dockerHost))
tunnelCloser, err := ssh.NewSSHHandler(osCommand).HandleSSHDockerHost(dockerHost)
if err != nil {
ogLog.Fatal(err)
}
clientOpts := []client.Opt{
client.FromEnv,
client.WithVersion(APIVersion),
}
// For an ssh connection the DOCKER_HOST env variable has been overridden.
// Discard the previously determined dockerHost
if !strings.HasPrefix(dockerHost, "ssh://") {
clientOpts = append(clientOpts, client.WithHost(dockerHost))
}
cli, err := client.NewClientWithOpts(clientOpts...)
if err != nil {
ogLog.Fatal(err)
}

View file

@ -44,10 +44,11 @@ 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
@ -55,7 +56,7 @@ func (self *SSHHandler) HandleSSHDockerHost() (io.Closer, error) {
// 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)
}