diff --git a/README.md b/README.md index 7380f1db3..ec3a92a45 100644 --- a/README.md +++ b/README.md @@ -122,10 +122,10 @@ In your application, e.g. ##### System Environment export DOCKER_URL=http://localhost:2376 - -Note: we also auto-detect defaults. If you use `DOCKER_HOST` we use that value, and if `DOCKER_CERT_PATH` is set, we switch to SSL. -##### File System +Note: we also auto-detect defaults. If you use `DOCKER_HOST` we use that value, and if `DOCKER_CERT_PATH` or `DOCKER_TLS_VERIFY=1` is set, we switch to SSL. + +##### File System In `$HOME/.docker.io.properties` diff --git a/src/main/java/com/github/dockerjava/core/DockerClientConfig.java b/src/main/java/com/github/dockerjava/core/DockerClientConfig.java index 8506d437a..c90c62cb1 100644 --- a/src/main/java/com/github/dockerjava/core/DockerClientConfig.java +++ b/src/main/java/com/github/dockerjava/core/DockerClientConfig.java @@ -21,6 +21,7 @@ public class DockerClientConfig implements Serializable { private static final String DOCKER_HOST_PROPERTY = "DOCKER_HOST"; private static final String DOCKER_CERT_PATH_PROPERTY = "DOCKER_CERT_PATH"; + private static final String DOCKER_VERIFY_TLS_PROPERTY = "DOCKER_TLS_VERIFY"; private static final String DOCKER_IO_URL_PROPERTY = "docker.io.url"; private static final String DOCKER_IO_VERSION_PROPERTY = "docker.io.version"; private static final String DOCKER_IO_USERNAME_PROPERTY = "docker.io.username"; @@ -142,7 +143,7 @@ private static Properties overrideDockerPropertiesWithEnv(Properties properties, private static String protocol(Map env) { // if this is set, we assume we need SSL - return env.containsKey(DOCKER_CERT_PATH_PROPERTY) ? "https" : "http"; + return env.containsKey(DOCKER_CERT_PATH_PROPERTY) || "1".equals(env.get(DOCKER_VERIFY_TLS_PROPERTY)) ? "https" : "http"; } /** diff --git a/src/test/java/com/github/dockerjava/core/DockerClientConfigTest.java b/src/test/java/com/github/dockerjava/core/DockerClientConfigTest.java index 7b5e4e1f2..e452e300d 100644 --- a/src/test/java/com/github/dockerjava/core/DockerClientConfigTest.java +++ b/src/test/java/com/github/dockerjava/core/DockerClientConfigTest.java @@ -45,7 +45,7 @@ public void environmentDockerHost() throws Exception { } @Test - public void environmentDockerHostHttpsAutoDetect() throws Exception { + public void environmentDockerHostHttpsAutoDetectByCertPath() throws Exception { // given docker host in env Map env = new HashMap(System.getenv()); @@ -60,6 +60,38 @@ public void environmentDockerHostHttpsAutoDetect() throws Exception { assertEquals(config.getUri(), URI.create("https://bar:8768")); } + @Test + public void environmentDockerHostHttpsAutoDetectByTlsVerify() throws Exception { + + // given docker host in env + Map env = new HashMap(System.getenv()); + env.put("DOCKER_HOST", "tcp://bar:8768"); + // and it looks to be SSL enabled + env.put("DOCKER_TLS_VERIFY", "1"); + + // when you build a config + DockerClientConfig config = buildConfig(env, new Properties()); + + // then the URL is that value with "tcp" changed to "https" + assertEquals(config.getUri(), URI.create("https://bar:8768")); + } + + @Test + public void environmentDockerHostWithInvalidTlsVerify() throws Exception { + + // given docker host in env + Map env = new HashMap(System.getenv()); + env.put("DOCKER_HOST", "tcp://bar:8768"); + // and it looks to be SSL enabled + env.put("DOCKER_TLS_VERIFY", "any value different from '1'"); + + // when you build a config + DockerClientConfig config = buildConfig(env, new Properties()); + + // then the URL is that value with "tcp" changed to "https" + assertEquals(config.getUri(), URI.create("http://bar:8768")); + } + @Test public void environment() throws Exception {