Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dddd439

Browse files
Support TLS configuration from docker context (#2105)
Co-authored-by: Eddú Meléndez <[email protected]>
1 parent 97579e5 commit dddd439

File tree

4 files changed

+71
-17
lines changed

4 files changed

+71
-17
lines changed

docker-java-core/src/main/java/com/github/dockerjava/core/DefaultDockerClientConfig.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,33 @@ public final Builder withCustomSslConfig(SSLConfig customSslConfig) {
441441
return this;
442442
}
443443

444+
private void applyContextConfiguration(final String context) {
445+
final Optional<DockerContextMetaFile> dockerContextMetaFile =
446+
Optional.ofNullable(context)
447+
.flatMap(ctx -> DockerContextMetaFile.resolveContextMetaFile(DockerClientConfig.getDefaultObjectMapper(),
448+
new File(this.dockerConfig), ctx));
449+
450+
if (dockerContextMetaFile.isPresent()) {
451+
final Optional<DockerContextMetaFile.Endpoints.Docker> dockerEndpoint =
452+
dockerContextMetaFile.map(metaFile -> metaFile.endpoints).map(endpoint -> endpoint.docker);
453+
if (this.dockerHost == null) {
454+
this.dockerHost = dockerEndpoint.map(endpoint -> endpoint.host).map(URI::create).orElse(null);
455+
}
456+
if (this.dockerCertPath == null) {
457+
this.dockerCertPath = dockerContextMetaFile.map(metaFile -> metaFile.storage)
458+
.map(storage -> storage.tlsPath)
459+
.filter(file -> new File(file).exists()).orElse(null);
460+
if (this.dockerCertPath != null) {
461+
this.dockerTlsVerify = dockerEndpoint.map(endpoint -> !endpoint.skipTLSVerify).orElse(true);
462+
}
463+
}
464+
}
465+
}
466+
444467
public DefaultDockerClientConfig build() {
468+
final DockerConfigFile dockerConfigFile = readDockerConfig();
469+
final String context = (dockerContext != null) ? dockerContext : dockerConfigFile.getCurrentContext();
470+
applyContextConfiguration(context);
445471

446472
SSLConfig sslConfig = null;
447473

@@ -454,12 +480,9 @@ public DefaultDockerClientConfig build() {
454480
sslConfig = customSslConfig;
455481
}
456482

457-
final DockerConfigFile dockerConfigFile = readDockerConfig();
458-
459-
final String context = (dockerContext != null) ? dockerContext : dockerConfigFile.getCurrentContext();
460483
URI dockerHostUri = dockerHost != null
461484
? dockerHost
462-
: resolveDockerHost(context);
485+
: URI.create(SystemUtils.IS_OS_WINDOWS ? WINDOWS_DEFAULT_DOCKER_HOST : DEFAULT_DOCKER_HOST);
463486

464487
return new DefaultDockerClientConfig(dockerHostUri, dockerConfigFile, dockerConfig, apiVersion, registryUrl, registryUsername,
465488
registryPassword, registryEmail, sslConfig);
@@ -473,14 +496,6 @@ private DockerConfigFile readDockerConfig() {
473496
}
474497
}
475498

476-
private URI resolveDockerHost(String dockerContext) {
477-
return URI.create(Optional.ofNullable(dockerContext)
478-
.flatMap(context -> DockerContextMetaFile.resolveContextMetaFile(
479-
DockerClientConfig.getDefaultObjectMapper(), new File(dockerConfig), context))
480-
.flatMap(DockerContextMetaFile::host)
481-
.orElse(SystemUtils.IS_OS_WINDOWS ? WINDOWS_DEFAULT_DOCKER_HOST : DEFAULT_DOCKER_HOST));
482-
}
483-
484499
private String checkDockerCertPath(String dockerCertPath) {
485500
if (StringUtils.isEmpty(dockerCertPath)) {
486501
throw new DockerClientException(

docker-java-core/src/main/java/com/github/dockerjava/core/DockerContextMetaFile.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class DockerContextMetaFile {
1818
@JsonProperty("Endpoints")
1919
Endpoints endpoints;
2020

21+
@JsonProperty("Storage")
22+
Storage storage;
23+
2124
public static class Endpoints {
2225
@JsonProperty("docker")
2326
Docker docker;
@@ -31,11 +34,12 @@ public static class Docker {
3134
}
3235
}
3336

34-
public Optional<String> host() {
35-
if (endpoints != null && endpoints.docker != null) {
36-
return Optional.ofNullable(endpoints.docker.host);
37-
}
38-
return Optional.empty();
37+
public static class Storage {
38+
39+
@JsonProperty("TLSPath")
40+
String tlsPath;
41+
@JsonProperty("MetadataPath")
42+
String metadataPath;
3943
}
4044

4145
public static Optional<DockerContextMetaFile> resolveContextMetaFile(ObjectMapper objectMapper, File dockerConfigPath, String context) {

docker-java/src/test/java/com/github/dockerjava/core/DefaultDockerClientConfigTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.hamcrest.core.Is.is;
2424
import static org.junit.Assert.assertEquals;
2525
import static org.junit.Assert.assertNull;
26+
import static org.junit.Assert.assertTrue;
2627

2728
public class DefaultDockerClientConfigTest {
2829

@@ -113,6 +114,25 @@ public void dockerContextFromEnvironmentVariable() {
113114
assertEquals(URI.create("unix:///envvarcontext.sock"), config.getDockerHost());
114115
}
115116

117+
@Test
118+
public void dockerContextWithDockerHostAndTLS() {
119+
// given home directory with docker contexts
120+
Properties systemProperties = new Properties();
121+
systemProperties.setProperty("user.home", "target/test-classes/dockerContextHomeDir");
122+
123+
// and an environment variable that overrides docker context
124+
Map<String, String> env = new HashMap<>();
125+
env.put(DefaultDockerClientConfig.DOCKER_CONTEXT, "remote");
126+
127+
// when you build a config
128+
DefaultDockerClientConfig config = buildConfig(env, systemProperties);
129+
130+
assertEquals(URI.create("tcp://remote:2376"), config.getDockerHost());
131+
assertTrue("SSL config is set", config.getSSLConfig() instanceof LocalDirectorySSLConfig);
132+
assertEquals("target/test-classes/com/github/dockerjava/core/util/CertificateUtilsTest/allFilesExist",
133+
((LocalDirectorySSLConfig)config.getSSLConfig()).getDockerCertPath());
134+
}
135+
116136
@Test
117137
public void environment() {
118138

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"Name": "remote",
3+
"Metadata": {
4+
"Description": "remote"
5+
},
6+
"Endpoints": {
7+
"docker": {
8+
"Host": "tcp://remote:2376",
9+
"SkipTLSVerify": false
10+
}
11+
},
12+
"Storage": {
13+
"TLSPath": "target/test-classes/com/github/dockerjava/core/util/CertificateUtilsTest/allFilesExist"
14+
}
15+
}

0 commit comments

Comments
 (0)