2018-02-19 04:31:29 -05:00
|
|
|
// Package connhelper provides helpers for connecting to a remote daemon host with custom logic.
|
|
|
|
package connhelper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2024-11-16 15:41:47 -05:00
|
|
|
"os"
|
2023-04-24 04:57:16 -04:00
|
|
|
"strings"
|
2018-02-19 04:31:29 -05:00
|
|
|
|
2019-03-01 09:34:36 -05:00
|
|
|
"github.com/docker/cli/cli/connhelper/commandconn"
|
2018-02-19 04:31:29 -05:00
|
|
|
"github.com/docker/cli/cli/connhelper/ssh"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2024-11-16 15:41:47 -05:00
|
|
|
const (
|
|
|
|
// DockerSSHRemoteBinaryEnv is the environment variable that can be used to
|
|
|
|
// override the default Docker binary called over SSH
|
|
|
|
DockerSSHRemoteBinaryEnv = "DOCKER_SSH_REMOTE_BINARY"
|
|
|
|
)
|
|
|
|
|
2018-02-19 04:31:29 -05:00
|
|
|
// ConnectionHelper allows to connect to a remote host with custom stream provider binary.
|
|
|
|
type ConnectionHelper struct {
|
|
|
|
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
|
|
|
|
Host string // dummy URL used for HTTP requests. e.g. "http://docker"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConnectionHelper returns Docker-specific connection helper for the given URL.
|
|
|
|
// GetConnectionHelper returns nil without error when no helper is registered for the scheme.
|
2019-03-01 09:34:36 -05:00
|
|
|
//
|
|
|
|
// ssh://<user>@<host> URL requires Docker 18.09 or later on the remote host.
|
2018-02-19 04:31:29 -05:00
|
|
|
func GetConnectionHelper(daemonURL string) (*ConnectionHelper, error) {
|
2020-05-22 05:47:24 -04:00
|
|
|
return getConnectionHelper(daemonURL, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConnectionHelperWithSSHOpts returns Docker-specific connection helper for
|
|
|
|
// the given URL, and accepts additional options for ssh connections. It returns
|
|
|
|
// nil without error when no helper is registered for the scheme.
|
|
|
|
//
|
|
|
|
// Requires Docker 18.09 or later on the remote host.
|
|
|
|
func GetConnectionHelperWithSSHOpts(daemonURL string, sshFlags []string) (*ConnectionHelper, error) {
|
|
|
|
return getConnectionHelper(daemonURL, sshFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConnectionHelper(daemonURL string, sshFlags []string) (*ConnectionHelper, error) {
|
2018-02-19 04:31:29 -05:00
|
|
|
u, err := url.Parse(daemonURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-20 07:54:53 -05:00
|
|
|
if u.Scheme == "ssh" {
|
2019-03-01 09:34:36 -05:00
|
|
|
sp, err := ssh.ParseURL(daemonURL)
|
2018-02-19 04:31:29 -05:00
|
|
|
if err != nil {
|
2019-03-01 09:34:36 -05:00
|
|
|
return nil, errors.Wrap(err, "ssh host connection is not valid")
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
2024-08-12 11:28:32 -04:00
|
|
|
sshFlags = addSSHTimeout(sshFlags)
|
|
|
|
sshFlags = disablePseudoTerminalAllocation(sshFlags)
|
2018-02-19 04:31:29 -05:00
|
|
|
return &ConnectionHelper{
|
|
|
|
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
2024-11-16 15:41:47 -05:00
|
|
|
args := []string{dockerSSHRemoteBinary()}
|
2023-03-04 07:53:25 -05:00
|
|
|
if sp.Path != "" {
|
|
|
|
args = append(args, "--host", "unix://"+sp.Path)
|
|
|
|
}
|
|
|
|
args = append(args, "system", "dial-stdio")
|
|
|
|
return commandconn.New(ctx, "ssh", append(sshFlags, sp.Args(args...)...)...)
|
2018-02-19 04:31:29 -05:00
|
|
|
},
|
2021-04-30 03:31:41 -04:00
|
|
|
Host: "http://docker.example.com",
|
2018-02-19 04:31:29 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
// Future version may support plugins via ~/.docker/config.json. e.g. "dind"
|
|
|
|
// See docker/cli#889 for the previous discussion.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-01 09:34:36 -05:00
|
|
|
// GetCommandConnectionHelper returns Docker-specific connection helper constructed from an arbitrary command.
|
2019-01-31 12:50:58 -05:00
|
|
|
func GetCommandConnectionHelper(cmd string, flags ...string) (*ConnectionHelper, error) {
|
|
|
|
return &ConnectionHelper{
|
|
|
|
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
2019-03-01 09:34:36 -05:00
|
|
|
return commandconn.New(ctx, cmd, flags...)
|
2019-01-31 12:50:58 -05:00
|
|
|
},
|
2021-04-30 03:31:41 -04:00
|
|
|
Host: "http://docker.example.com",
|
2019-01-31 12:50:58 -05:00
|
|
|
}, nil
|
|
|
|
}
|
2023-04-24 04:57:16 -04:00
|
|
|
|
|
|
|
func addSSHTimeout(sshFlags []string) []string {
|
|
|
|
if !strings.Contains(strings.Join(sshFlags, ""), "ConnectTimeout") {
|
|
|
|
sshFlags = append(sshFlags, "-o ConnectTimeout=30")
|
|
|
|
}
|
|
|
|
return sshFlags
|
|
|
|
}
|
2024-08-06 19:30:06 -04:00
|
|
|
|
|
|
|
// disablePseudoTerminalAllocation disables pseudo-terminal allocation to
|
|
|
|
// prevent SSH from executing as a login shell
|
|
|
|
func disablePseudoTerminalAllocation(sshFlags []string) []string {
|
|
|
|
for _, flag := range sshFlags {
|
|
|
|
if flag == "-T" {
|
|
|
|
return sshFlags
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(sshFlags, "-T")
|
|
|
|
}
|
2024-11-16 15:41:47 -05:00
|
|
|
|
|
|
|
// dockerSSHRemoteBinary returns the binary to use when executing Docker
|
|
|
|
// commands over SSH. It defaults to "docker" if the DOCKER_SSH_REMOTE_BINARY
|
|
|
|
// environment variable is not set.
|
|
|
|
func dockerSSHRemoteBinary() string {
|
|
|
|
value := os.Getenv(DockerSSHRemoteBinaryEnv)
|
|
|
|
if value == "" {
|
|
|
|
return "docker"
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|