X Tutup
Skip to content

Commit c6507bd

Browse files
author
Stephan Classen
committed
support DOCKER_TLS_VERIFY to detect ssl
1 parent b637b59 commit c6507bd

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ In your application, e.g.
122122
##### System Environment
123123

124124
export DOCKER_URL=http://localhost:2376
125-
126-
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.
127125

128-
##### File System
126+
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.
127+
128+
##### File System
129129

130130
In `$HOME/.docker.io.properties`
131131

src/main/java/com/github/dockerjava/core/DockerClientConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class DockerClientConfig implements Serializable {
2121

2222
private static final String DOCKER_HOST_PROPERTY = "DOCKER_HOST";
2323
private static final String DOCKER_CERT_PATH_PROPERTY = "DOCKER_CERT_PATH";
24+
private static final String DOCKER_VERIFY_TLS_PROPERTY = "DOCKER_TLS_VERIFY";
2425
private static final String DOCKER_IO_URL_PROPERTY = "docker.io.url";
2526
private static final String DOCKER_IO_VERSION_PROPERTY = "docker.io.version";
2627
private static final String DOCKER_IO_USERNAME_PROPERTY = "docker.io.username";
@@ -142,7 +143,7 @@ private static Properties overrideDockerPropertiesWithEnv(Properties properties,
142143

143144
private static String protocol(Map<String, String> env) {
144145
// if this is set, we assume we need SSL
145-
return env.containsKey(DOCKER_CERT_PATH_PROPERTY) ? "https" : "http";
146+
return env.containsKey(DOCKER_CERT_PATH_PROPERTY) || "1".equals(env.get(DOCKER_VERIFY_TLS_PROPERTY)) ? "https" : "http";
146147
}
147148

148149
/**

src/test/java/com/github/dockerjava/core/DockerClientConfigTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void environmentDockerHost() throws Exception {
4545
}
4646

4747
@Test
48-
public void environmentDockerHostHttpsAutoDetect() throws Exception {
48+
public void environmentDockerHostHttpsAutoDetectByCertPath() throws Exception {
4949

5050
// given docker host in env
5151
Map<String, String> env = new HashMap<String, String>(System.getenv());
@@ -60,6 +60,38 @@ public void environmentDockerHostHttpsAutoDetect() throws Exception {
6060
assertEquals(config.getUri(), URI.create("https://bar:8768"));
6161
}
6262

63+
@Test
64+
public void environmentDockerHostHttpsAutoDetectByTlsVerify() throws Exception {
65+
66+
// given docker host in env
67+
Map<String, String> env = new HashMap<String, String>(System.getenv());
68+
env.put("DOCKER_HOST", "tcp://bar:8768");
69+
// and it looks to be SSL enabled
70+
env.put("DOCKER_TLS_VERIFY", "1");
71+
72+
// when you build a config
73+
DockerClientConfig config = buildConfig(env, new Properties());
74+
75+
// then the URL is that value with "tcp" changed to "https"
76+
assertEquals(config.getUri(), URI.create("https://bar:8768"));
77+
}
78+
79+
@Test
80+
public void environmentDockerHostWithInvalidTlsVerify() throws Exception {
81+
82+
// given docker host in env
83+
Map<String, String> env = new HashMap<String, String>(System.getenv());
84+
env.put("DOCKER_HOST", "tcp://bar:8768");
85+
// and it looks to be SSL enabled
86+
env.put("DOCKER_TLS_VERIFY", "any value different from '1'");
87+
88+
// when you build a config
89+
DockerClientConfig config = buildConfig(env, new Properties());
90+
91+
// then the URL is that value with "tcp" changed to "https"
92+
assertEquals(config.getUri(), URI.create("http://bar:8768"));
93+
}
94+
6395
@Test
6496
public void environment() throws Exception {
6597

0 commit comments

Comments
 (0)
X Tutup