Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(docker): pass the resolved TLS config to the Docker HTTP client (#4638)

Sam Fakhreddine728e000187ef
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducer.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducer.java
index 02f0be1132..b206bc4d55 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducer.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducer.java
@@ -489,14 +489,18 @@ public class DockerClientProducer {
LOG.infov("Creating {0} DockerClient pool (maxConnections={1}) for host: {2}",
role, maxConnections, clientConfig.getDockerHost());
- ApacheDockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
+ return DockerClientImpl.getInstance(clientConfig,
+ wrapForRole(newHttpClient(clientConfig, maxConnections), role));
+ }
+
+ static ApacheDockerHttpClient newHttpClient(DefaultDockerClientConfig clientConfig, int maxConnections) {
+ return new ApacheDockerHttpClient.Builder()
.dockerHost(clientConfig.getDockerHost())
+ .sslConfig(clientConfig.getSSLConfig())
.maxConnections(maxConnections)
.connectionTimeout(Duration.ofSeconds(30))
.responseTimeout(Duration.ofMinutes(5))
.build();
-
- return DockerClientImpl.getInstance(clientConfig, wrapForRole(httpClient, role));
}
/**
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducerTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducerTest.java
index cc7756cde1..f637f781b4 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducerTest.java
@@ -3,6 +3,8 @@ package io.github.hectorvent.floci.core.common.docker;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.model.Frame;
+import com.github.dockerjava.core.DefaultDockerClientConfig;
+import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import io.github.hectorvent.floci.config.EmulatorConfig;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
@@ -723,4 +725,37 @@ class DockerClientProducerTest {
}
}
}
+
+ @Test
+ void newHttpClient_tlsVerifyConfig_loadsTheConfiguredCertificates(@TempDir Path certDir) throws IOException {
+ // Catches: TLS material resolved onto the client config but never handed to the HTTP transport
+ Files.writeString(certDir.resolve("ca.pem"), "not a certificate");
+ Files.writeString(certDir.resolve("cert.pem"), "not a certificate");
+ Files.writeString(certDir.resolve("key.pem"), "not a key");
+ DefaultDockerClientConfig tlsConfig =
+ DefaultDockerClientConfig.createDefaultConfigBuilder()
+ .withDockerHost("tcp://127.0.0.1:2376")
+ .withDockerTlsVerify(true)
+ .withDockerCertPath(certDir.toString())
+ .build();
+
+ // An HTTP client that actually uses the SSL config must read the certificates, so
+ // unreadable ones fail the build; one that ignores the config builds silently.
+ assertThrows(RuntimeException.class, () -> DockerClientProducer.newHttpClient(tlsConfig, 10));
+ }
+
+ @Test
+ void newHttpClient_plainTcpConfig_buildsWithoutTls() throws IOException {
+ // Catches: passing the SSL config breaking hosts that have no TLS material
+ DefaultDockerClientConfig plainConfig =
+ DefaultDockerClientConfig.createDefaultConfigBuilder()
+ .withDockerHost("tcp://127.0.0.1:2375")
+ .withDockerTlsVerify(false)
+ .build();
+
+ try (ApacheDockerHttpClient client = DockerClientProducer.newHttpClient(plainConfig, 10)) {
+ assertNull(plainConfig.getSSLConfig(), "a config without TLS verify carries no SSL config");
+ assertEquals("tcp://127.0.0.1:2375", plainConfig.getDockerHost().toString());
+ }
+ }
}