Skip to content

Commit

Permalink
Fix handling of DOCKER_HOST that begins with unix://
Browse files Browse the repository at this point in the history
Fixes gh-22300
  • Loading branch information
wilkinsona committed Jul 10, 2020
1 parent 794ded5 commit f18b657
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Expand Up @@ -50,6 +50,8 @@
*/
final class LocalHttpClientTransport extends HttpClientTransport {

private static final String UNIX_SOCKET_PREFIX = "unix://";

private static final String DOCKER_HOST = "DOCKER_HOST";

private static final HttpHost LOCAL_DOCKER_HOST = HttpHost.create("docker://localhost");
Expand All @@ -60,11 +62,17 @@ private LocalHttpClientTransport(CloseableHttpClient client) {

static LocalHttpClientTransport create(Environment environment) {
HttpClientBuilder builder = HttpClients.custom();
builder.setConnectionManager(new LocalConnectionManager(environment.get(DOCKER_HOST)));
builder.setConnectionManager(new LocalConnectionManager(socketFilePath(environment)));
builder.setSchemePortResolver(new LocalSchemePortResolver());
return new LocalHttpClientTransport(builder.build());
}

private static String socketFilePath(Environment environment) {
String host = environment.get(DOCKER_HOST);
return (host != null && host.startsWith(UNIX_SOCKET_PREFIX)) ? host.substring(UNIX_SOCKET_PREFIX.length())
: host;
}

/**
* {@link HttpClientConnectionManager} for local Docker.
*/
Expand Down
Expand Up @@ -40,6 +40,8 @@
*/
final class RemoteHttpClientTransport extends HttpClientTransport {

private static final String UNIX_SOCKET_PREFIX = "unix://";

private static final String DOCKER_HOST = "DOCKER_HOST";

private static final String DOCKER_TLS_VERIFY = "DOCKER_TLS_VERIFY";
Expand All @@ -63,8 +65,9 @@ static RemoteHttpClientTransport createIfPossible(Environment environment, SslCo
}

private static boolean isLocalFileReference(String host) {
String filePath = host.startsWith(UNIX_SOCKET_PREFIX) ? host.substring(UNIX_SOCKET_PREFIX.length()) : host;
try {
return Files.exists(Paths.get(host));
return Files.exists(Paths.get(filePath));
}
catch (Exception ex) {
return false;
Expand Down
Expand Up @@ -50,6 +50,15 @@ void createWhenDockerHostVariableIsFileReturnsLocal(@TempDir Path tempDir) throw
assertThat(transport).isInstanceOf(LocalHttpClientTransport.class);
}

@Test
void createWhenDockerHostVariableIsUnixSchemePrefixedFileReturnsLocal(@TempDir Path tempDir) throws IOException {
String dummySocketFilePath = "unix://"
+ Files.createTempFile(tempDir, "http-transport", null).toAbsolutePath().toString();
Map<String, String> environment = Collections.singletonMap("DOCKER_HOST", dummySocketFilePath);
HttpTransport transport = HttpTransport.create(environment::get);
assertThat(transport).isInstanceOf(LocalHttpClientTransport.class);
}

@Test
void createWhenDoesNotHaveDockerHostVariableReturnsLocal() {
HttpTransport transport = HttpTransport.create((name) -> null);
Expand Down

0 comments on commit f18b657

Please sign in to comment.