Browse code

Fix TLS from environment variables in client

A recent change accidently caused any TLS configuration in FromEnv to be
ignored. This change alters WithHost to create a new http client only if
one doesn't already exist, and otherwise applies the logic to the
transport on the existing client. This preserves the TLS configuration
that might already be on the client.

Signed-off-by: Drew Erny <drew.erny@docker.com>

Drew Erny authored on 2018/02/10 08:10:58
Showing 1 changed files
... ...
@@ -133,23 +133,15 @@ func FromEnv(c *Client) error {
133 133
 			},
134 134
 			CheckRedirect: CheckRedirect,
135 135
 		}
136
+		WithHTTPClient(httpClient)(c)
136 137
 	}
137 138
 
138 139
 	host := os.Getenv("DOCKER_HOST")
139 140
 	if host != "" {
140
-		var err error
141
+		// WithHost will create an API client if it doesn't exist
141 142
 		if err := WithHost(host)(c); err != nil {
142 143
 			return err
143 144
 		}
144
-		httpClient, err = defaultHTTPClient(host)
145
-		if err != nil {
146
-			return err
147
-		}
148
-	}
149
-	if httpClient != nil {
150
-		if err := WithHTTPClient(httpClient)(c); err != nil {
151
-			return err
152
-		}
153 145
 	}
154 146
 	version := os.Getenv("DOCKER_API_VERSION")
155 147
 	if version != "" {
... ...
@@ -167,7 +159,8 @@ func WithVersion(version string) func(*Client) error {
167 167
 	}
168 168
 }
169 169
 
170
-// WithHost overrides the client host with the specified one
170
+// WithHost overrides the client host with the specified one, creating a new
171
+// http client if one doesn't exist
171 172
 func WithHost(host string) func(*Client) error {
172 173
 	return func(c *Client) error {
173 174
 		hostURL, err := ParseHostURL(host)
... ...
@@ -178,11 +171,17 @@ func WithHost(host string) func(*Client) error {
178 178
 		c.proto = hostURL.Scheme
179 179
 		c.addr = hostURL.Host
180 180
 		c.basePath = hostURL.Path
181
-		client, err := defaultHTTPClient(host)
182
-		if err != nil {
183
-			return err
181
+		if c.client == nil {
182
+			client, err := defaultHTTPClient(host)
183
+			if err != nil {
184
+				return err
185
+			}
186
+			return WithHTTPClient(client)(c)
187
+		}
188
+		if transport, ok := c.client.Transport.(*http.Transport); ok {
189
+			return sockets.ConfigureTransport(transport, c.proto, c.addr)
184 190
 		}
185
-		return WithHTTPClient(client)(c)
191
+		return fmt.Errorf("cannot apply host to http transport")
186 192
 	}
187 193
 }
188 194