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

Skip to content

Commit 95f65e2

Browse files
Fix support TLS configuration from docker context (#2130)
Co-authored-by: Eddú Meléndez <[email protected]>
1 parent dd0de7c commit 95f65e2

File tree

6 files changed

+18
-19
lines changed

6 files changed

+18
-19
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -446,21 +446,20 @@ private void applyContextConfiguration(final String context) {
446446
Optional.ofNullable(context)
447447
.flatMap(ctx -> DockerContextMetaFile.resolveContextMetaFile(DockerClientConfig.getDefaultObjectMapper(),
448448
new File(this.dockerConfig), ctx));
449+
final Optional<File> dockerContextTLSFile =
450+
Optional.ofNullable(context)
451+
.flatMap(ctx -> DockerContextMetaFile.resolveContextTLSFile(new File(this.dockerConfig), ctx));
449452

450453
if (dockerContextMetaFile.isPresent()) {
451454
final Optional<DockerContextMetaFile.Endpoints.Docker> dockerEndpoint =
452455
dockerContextMetaFile.map(metaFile -> metaFile.endpoints).map(endpoint -> endpoint.docker);
453456
if (this.dockerHost == null) {
454457
this.dockerHost = dockerEndpoint.map(endpoint -> endpoint.host).map(URI::create).orElse(null);
455458
}
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-
}
459+
}
460+
if (dockerContextTLSFile.isPresent() && this.dockerCertPath == null) {
461+
this.dockerCertPath = dockerContextTLSFile.get().getAbsolutePath();
462+
this.dockerTlsVerify = true;
464463
}
465464
}
466465

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

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

21-
@JsonProperty("Storage")
22-
Storage storage;
2321

2422
public static class Endpoints {
2523
@JsonProperty("docker")
@@ -34,13 +32,6 @@ public static class Docker {
3432
}
3533
}
3634

37-
public static class Storage {
38-
39-
@JsonProperty("TLSPath")
40-
String tlsPath;
41-
@JsonProperty("MetadataPath")
42-
String metadataPath;
43-
}
4435

4536
public static Optional<DockerContextMetaFile> resolveContextMetaFile(ObjectMapper objectMapper, File dockerConfigPath, String context) {
4637
final File path = dockerConfigPath.toPath()
@@ -52,6 +43,16 @@ public static Optional<DockerContextMetaFile> resolveContextMetaFile(ObjectMappe
5243
return Optional.ofNullable(loadContextMetaFile(objectMapper, path));
5344
}
5445

46+
public static Optional<File> resolveContextTLSFile(File dockerConfigPath, String context) {
47+
final File path = dockerConfigPath.toPath()
48+
.resolve("contexts")
49+
.resolve("tls")
50+
.resolve(metaHashFunction.hashString(context, StandardCharsets.UTF_8).toString())
51+
.resolve("docker")
52+
.toFile();
53+
return Optional.ofNullable(path).filter(File::exists);
54+
}
55+
5556
public static DockerContextMetaFile loadContextMetaFile(ObjectMapper objectMapper, File dockerContextMetaFile) {
5657
try {
5758
return parseContextMetaFile(objectMapper, dockerContextMetaFile);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ public void dockerContextWithDockerHostAndTLS() {
129129

130130
assertEquals(URI.create("tcp://remote:2376"), config.getDockerHost());
131131
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());
132+
assertTrue("SSL directory is set", ((LocalDirectorySSLConfig)config.getSSLConfig()).getDockerCertPath().endsWith("dockerContextHomeDir/.docker/contexts/tls/b71199ebd070b36beab7317920c2c2f1d777df8d05e5527d8458fda57cb17a7a/docker"));
134133
}
135134

136135
@Test

docker-java/src/test/resources/dockerContextHomeDir/.docker/contexts/tls/b71199ebd070b36beab7317920c2c2f1d777df8d05e5527d8458fda57cb17a7a/docker/ca.pem

Whitespace-only changes.

docker-java/src/test/resources/dockerContextHomeDir/.docker/contexts/tls/b71199ebd070b36beab7317920c2c2f1d777df8d05e5527d8458fda57cb17a7a/docker/cert.pem

Whitespace-only changes.

docker-java/src/test/resources/dockerContextHomeDir/.docker/contexts/tls/b71199ebd070b36beab7317920c2c2f1d777df8d05e5527d8458fda57cb17a7a/docker/key.pem

Whitespace-only changes.

0 commit comments

Comments
 (0)