Skip to content

Commit 67f029e

Browse files
committed
Don't use TLS for socket connections
Before this patch: mkdir -p ./tempconfig && touch ./tempconfig/ca.pem ./tempconfig/cert.pem ./tempconfig/key.pem DOCKER_TLS_VERIFY=1 DOCKER_CONFIG=./tempconfig DOCKER_HOST=unix:///var/run/docker.sock docker info Failed to initialize: failed to retrieve context tls info: ca.pem seems invalid With this patch: DOCKER_TLS_VERIFY=1 DOCKER_CONFIG=./tempconfig DOCKER_HOST=unix:///var/run/docker.sock docker info Client: Version: 28.1.1-25-g2dfe7b558.m Context: default ... Note that the above is just to illustrate; there's still parts in context- related code that will check for, and load TLS-related files ahead of time. We should make some of that code lazy-loading (i.e., don't load these until we're actually gonna make an API connection). For example, if the TLS files are missing; rm ./tempconfig/*.pem DOCKER_TLS_VERIFY=1 DOCKER_CONFIG=./tempconfig DOCKER_HOST=unix:///var/run/docker.sock docker info Failed to initialize: unable to resolve docker endpoint: open tempconfig/ca.pem: no such file or directory Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 77fbbc3 commit 67f029e

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

cli/context/docker/load.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/pem"
77
"net"
88
"net/http"
9+
"strings"
910
"time"
1011

1112
"github.com/docker/cli/cli/connhelper"
@@ -90,14 +91,19 @@ func (ep *Endpoint) ClientOpts() ([]client.Opt, error) {
9091
return nil, err
9192
}
9293
if helper == nil {
93-
tlsConfig, err := ep.tlsConfig()
94-
if err != nil {
95-
return nil, err
94+
// Check if we're connecting over a socket, because there's no
95+
// need to configure TLS for a socket connection.
96+
//
97+
// TODO(thaJeztah); make resolveDockerEndpoint and resolveDefaultDockerEndpoint not load TLS data,
98+
// and load TLS files lazily; see https://github.com/docker/cli/pull/1581
99+
if !isSocket(ep.Host) {
100+
tlsConfig, err := ep.tlsConfig()
101+
if err != nil {
102+
return nil, err
103+
}
104+
result = append(result, withHTTPClient(tlsConfig))
96105
}
97-
result = append(result,
98-
withHTTPClient(tlsConfig),
99-
client.WithHost(ep.Host),
100-
)
106+
result = append(result, client.WithHost(ep.Host))
101107
} else {
102108
result = append(result,
103109
client.WithHTTPClient(&http.Client{
@@ -116,6 +122,17 @@ func (ep *Endpoint) ClientOpts() ([]client.Opt, error) {
116122
return result, nil
117123
}
118124

125+
// isSocket checks if the given address is a Unix-socket (linux),
126+
// named pipe (Windows), or file-descriptor.
127+
func isSocket(addr string) bool {
128+
switch proto, _, _ := strings.Cut(addr, "://"); proto {
129+
case "unix", "npipe", "fd":
130+
return true
131+
default:
132+
return false
133+
}
134+
}
135+
119136
func withHTTPClient(tlsConfig *tls.Config) func(*client.Client) error {
120137
return func(c *client.Client) error {
121138
if tlsConfig == nil {

0 commit comments

Comments
 (0)