X Tutup
Skip to content

Commit 138b11e

Browse files
committed
Change the client configuration so that it can optionally take an
SSLConfig. Signed-off-by: Nigel Magnay <nigel.magnay@gmail.com>
1 parent 20a72fc commit 138b11e

File tree

4 files changed

+33
-52
lines changed

4 files changed

+33
-52
lines changed

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
import java.io.File;
77
import java.io.FileInputStream;
88
import java.io.IOException;
9+
import java.io.Serializable;
910
import java.net.URI;
1011
import java.util.Map;
1112
import java.util.Properties;
1213

13-
public class DockerClientConfig {
14+
public class DockerClientConfig implements Serializable {
1415
private static final String DOCKER_HOST_PROPERTY = "DOCKER_HOST";
1516
private static final String DOCKER_CERT_PATH_PROPERTY = "DOCKER_CERT_PATH";
1617
private static final String DOCKER_IO_URL_PROPERTY = "docker.io.url";
@@ -41,21 +42,22 @@ public class DockerClientConfig {
4142
.build();
4243
private static final String DOCKER_IO_PROPERTIES_PROPERTY = "docker.io.properties";
4344
private final URI uri;
44-
private final String version, username, password, email, serverAddress, dockerCertPath, dockerCfgPath;
45+
private final String version, username, password, email, serverAddress, dockerCfgPath;
4546
private final Integer readTimeout;
4647
private final boolean loggingFilterEnabled;
48+
private final SSLConfig sslConfig;
4749

48-
DockerClientConfig(URI uri, String version, String username, String password, String email, String serverAddress, String dockerCertPath, String dockerCfgPath, Integer readTimeout, boolean loggingFilterEnabled) {
50+
DockerClientConfig(URI uri, String version, String username, String password, String email, String serverAddress, String dockerCfgPath, Integer readTimeout, boolean loggingFilterEnabled, SSLConfig sslConfig) {
4951
this.uri = uri;
5052
this.version = version;
5153
this.username = username;
5254
this.password = password;
5355
this.email = email;
5456
this.serverAddress = serverAddress;
55-
this.dockerCertPath = dockerCertPath;
5657
this.dockerCfgPath = dockerCfgPath;
5758
this.readTimeout = readTimeout;
5859
this.loggingFilterEnabled = loggingFilterEnabled;
60+
this.sslConfig = sslConfig;
5961
}
6062

6163
private static Properties loadIncludedDockerProperties(Properties systemProperties) {
@@ -212,23 +214,23 @@ public boolean isLoggingFilterEnabled() {
212214
return loggingFilterEnabled;
213215
}
214216

215-
public String getDockerCertPath() {
216-
return dockerCertPath;
217+
public SSLConfig getSslConfig() {
218+
return sslConfig;
217219
}
218220

219221
public String getDockerCfgPath() {
220222
return dockerCfgPath;
221223
}
222224

223-
@Override
225+
@Override
224226
public boolean equals(Object o) {
225227
if (this == o) return true;
226228
if (o == null || getClass() != o.getClass()) return false;
227229

228230
DockerClientConfig that = (DockerClientConfig) o;
229231

230232
if (loggingFilterEnabled != that.loggingFilterEnabled) return false;
231-
if (dockerCertPath != null ? !dockerCertPath.equals(that.dockerCertPath) : that.dockerCertPath != null)
233+
if (sslConfig != null ? !sslConfig.equals(that.sslConfig) : that.sslConfig != null)
232234
return false;
233235
if (dockerCfgPath != null ? !dockerCfgPath.equals(that.dockerCfgPath) : that.dockerCfgPath != null)
234236
return false;
@@ -252,8 +254,8 @@ public int hashCode() {
252254
result = 31 * result + (password != null ? password.hashCode() : 0);
253255
result = 31 * result + (email != null ? email.hashCode() : 0);
254256
result = 31 * result + (serverAddress != null ? serverAddress.hashCode() : 0);
255-
result = 31 * result + (dockerCertPath != null ? dockerCertPath.hashCode() : 0);
256257
result = 31 * result + (dockerCfgPath != null ? dockerCfgPath.hashCode() : 0);
258+
result = 31 * result + (sslConfig != null ? sslConfig.hashCode() : 0);
257259
result = 31 * result + (readTimeout != null ? readTimeout.hashCode() : 0);
258260
result = 31 * result + (loggingFilterEnabled ? 1 : 0);
259261
return result;
@@ -268,18 +270,19 @@ public String toString() {
268270
", password='" + password + '\'' +
269271
", email='" + email + '\'' +
270272
", serverAddress='" + serverAddress + '\'' +
271-
", dockerCertPath='" + dockerCertPath + '\'' +
272273
", dockerCfgPath='" + dockerCfgPath + '\'' +
274+
", sslConfig='" + sslConfig + '\'' +
273275
", readTimeout=" + readTimeout +
274276
", loggingFilterEnabled=" + loggingFilterEnabled +
275277
'}';
276278
}
277279

278280
public static class DockerClientConfigBuilder {
279281
private URI uri;
280-
private String version, username, password, email, serverAddress, dockerCertPath, dockerCfgPath;
282+
private String version, username, password, email, serverAddress, dockerCfgPath;
281283
private Integer readTimeout;
282284
private boolean loggingFilterEnabled;
285+
private SSLConfig sslConfig;
283286

284287
/**
285288
* This will set all fields in the builder to those contained in the Properties object. The Properties object
@@ -342,7 +345,7 @@ public final DockerClientConfigBuilder withLoggingFilter(boolean loggingFilterEn
342345
}
343346

344347
public final DockerClientConfigBuilder withDockerCertPath(String dockerCertPath) {
345-
this.dockerCertPath = dockerCertPath;
348+
this.sslConfig = new LocalDirectorySSLConfig(dockerCertPath);
346349
return this;
347350
}
348351

@@ -352,6 +355,11 @@ public final DockerClientConfigBuilder withDockerCfgPath(String dockerCfgPath) {
352355
}
353356

354357

358+
public final DockerClientConfigBuilder withSSLConfig(SSLConfig config) {
359+
this.sslConfig = config;
360+
return this;
361+
}
362+
355363
public DockerClientConfig build() {
356364
return new DockerClientConfig(
357365
uri,
@@ -360,10 +368,10 @@ public DockerClientConfig build() {
360368
password,
361369
email,
362370
serverAddress,
363-
dockerCertPath,
364371
dockerCfgPath,
365372
readTimeout,
366-
loggingFilterEnabled
373+
loggingFilterEnabled,
374+
sslConfig
367375
);
368376
}
369377
}

src/main/java/com/github/dockerjava/jaxrs/DockerCmdExecFactoryImpl.java

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,43 +52,16 @@ public void init(DockerClientConfig dockerClientConfig) {
5252

5353
ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig(clientConfig);
5454

55-
String dockerCertPath = dockerClientConfig.getDockerCertPath();
56-
57-
if (dockerCertPath != null) {
58-
boolean certificatesExist = CertificateUtils.verifyCertificatesExist(dockerCertPath);
59-
60-
if (certificatesExist) {
61-
62-
try {
63-
64-
Security.addProvider(new BouncyCastleProvider());
65-
66-
KeyStore keyStore = CertificateUtils.createKeyStore(dockerCertPath);
67-
KeyStore trustStore = CertificateUtils.createTrustStore(dockerCertPath);
68-
69-
// properties acrobatics not needed for java > 1.6
70-
String httpProtocols = System.getProperty("https.protocols");
71-
System.setProperty("https.protocols", "TLSv1");
72-
SslConfigurator sslConfig = SslConfigurator.newInstance(true);
73-
if (httpProtocols != null) System.setProperty("https.protocols", httpProtocols);
74-
75-
sslConfig.keyStore(keyStore);
76-
sslConfig.keyStorePassword("docker");
77-
sslConfig.trustStore(trustStore);
78-
79-
SSLContext sslContext = sslConfig.createSSLContext();
80-
81-
82-
clientBuilder.sslContext(sslContext);
83-
84-
} catch (Exception e) {
85-
throw new DockerClientException(e.getMessage(), e);
86-
}
87-
88-
}
55+
try {
56+
SSLContext ssl = dockerClientConfig.getSslConfig().getSSLContext();
8957

58+
if (ssl != null)
59+
clientBuilder.sslContext(ssl);
60+
} catch(Exception ex) {
61+
throw new DockerClientException("Error in SSL Configuration", ex);
9062
}
9163

64+
9265
client = clientBuilder.build();
9366

9467
WebTarget webResource = client.target(dockerClientConfig.getUri());

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public class DockerClientConfigTest {
1616
public static final DockerClientConfig EXAMPLE_CONFIG = newExampleConfig();
1717

1818
private static DockerClientConfig newExampleConfig() {
19-
return new DockerClientConfig(URI.create("http://foo"), "bar", "baz", "qux", "blam", "wham", "flim", "flam", 877, false);
19+
return new DockerClientConfig(URI.create("http://foo"), "bar", "baz", "qux", "blam", "wham", "flam", 877, false, new LocalDirectorySSLConfig("flim"));
2020
}
2121

2222
@Test
2323
public void string() throws Exception {
24-
assertEquals("DockerClientConfig{uri=http://foo, version='bar', username='baz', password='qux', email='blam', serverAddress='wham', dockerCertPath='flim', dockerCfgPath='flam', readTimeout=877, loggingFilterEnabled=false}",
24+
assertEquals("DockerClientConfig{uri=http://foo, version='bar', username='baz', password='qux', email='blam', serverAddress='wham', dockerCfgPath='flam', sslConfig='LocalDirectorySSLConfig{dockerCertPath=flim}', readTimeout=877, loggingFilterEnabled=false}",
2525
EXAMPLE_CONFIG.toString());
2626
}
2727

@@ -104,8 +104,8 @@ public void defaults() throws Exception {
104104
assertEquals(config.getServerAddress(), AuthConfig.DEFAULT_SERVER_ADDRESS);
105105
assertEquals(config.getVersion(), null);
106106
assertEquals(config.isLoggingFilterEnabled(), true);
107-
assertEquals(config.getDockerCertPath(), "someHomeDir/.docker");
108107
assertEquals(config.getDockerCfgPath(), "someHomeDir/.dockercfg");
108+
assertEquals( ((LocalDirectorySSLConfig)config.getSslConfig()).getDockerCertPath(), "someHomeDir/.docker");
109109
}
110110

111111
@Test

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class DockerClientImplTest {
1010
@Test
1111
public void configuredInstanceAuthConfig() throws Exception {
1212
// given a config with null serverAddress
13-
DockerClientConfig dockerClientConfig = new DockerClientConfig(null, null, "", "", "", null, null, null, 0, false);
13+
DockerClientConfig dockerClientConfig = new DockerClientConfig(null, null, "", "", "", null, null, 0, false, null);
1414
DockerClientImpl dockerClient = DockerClientImpl.getInstance(dockerClientConfig);
1515

1616
// when we get the auth config

0 commit comments

Comments
 (0)
X Tutup