| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -441,7 +441,33 @@ public final Builder withCustomSslConfig(SSLConfig customSslConfig) { | |||
| 441 | 441 | return this; | |
| 442 | 442 | } | |
| 443 | 443 | ||
| 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 | + | ||
| 444 | 467 | public DefaultDockerClientConfig build() { | |
| 468 | + final DockerConfigFile dockerConfigFile = readDockerConfig(); | ||
| 469 | + final String context = (dockerContext != null) ? dockerContext : dockerConfigFile.getCurrentContext(); | ||
| 470 | + applyContextConfiguration(context); | ||
| 445 | 471 | ||
| 446 | 472 | SSLConfig sslConfig = null; | |
| 447 | 473 | ||
@@ -454,12 +480,9 @@ public DefaultDockerClientConfig build() { | |||
| 454 | 480 | sslConfig = customSslConfig; | |
| 455 | 481 | } | |
| 456 | 482 | ||
| 457 | - final DockerConfigFile dockerConfigFile = readDockerConfig(); | ||
| 458 | - | ||
| 459 | - final String context = (dockerContext != null) ? dockerContext : dockerConfigFile.getCurrentContext(); | ||
| 460 | 483 | URI dockerHostUri = dockerHost != null | |
| 461 | 484 | ? dockerHost | |
| 462 | - : resolveDockerHost(context); | ||
| 485 | + : URI.create(SystemUtils.IS_OS_WINDOWS ? WINDOWS_DEFAULT_DOCKER_HOST : DEFAULT_DOCKER_HOST); | ||
| 463 | 486 | ||
| 464 | 487 | return new DefaultDockerClientConfig(dockerHostUri, dockerConfigFile, dockerConfig, apiVersion, registryUrl, registryUsername, | |
| 465 | 488 | registryPassword, registryEmail, sslConfig); | |
@@ -473,14 +496,6 @@ private DockerConfigFile readDockerConfig() { | |||
| 473 | 496 | } | |
| 474 | 497 | } | |
| 475 | 498 | ||
| 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 | - | ||
| 484 | 499 | private String checkDockerCertPath(String dockerCertPath) { | |
| 485 | 500 | if (StringUtils.isEmpty(dockerCertPath)) { | |
| 486 | 501 | throw new DockerClientException( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,6 +18,9 @@ public class DockerContextMetaFile { | |||
| 18 | 18 | @JsonProperty("Endpoints") | |
| 19 | 19 | Endpoints endpoints; | |
| 20 | 20 | ||
| 21 | + @JsonProperty("Storage") | ||
| 22 | + Storage storage; | ||
| 23 | + | ||
| 21 | 24 | public static class Endpoints { | |
| 22 | 25 | @JsonProperty("docker") | |
| 23 | 26 | Docker docker; | |
@@ -31,11 +34,12 @@ public static class Docker { | |||
| 31 | 34 | } | |
| 32 | 35 | } | |
| 33 | 36 | ||
| 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; | ||
| 39 | 43 | } | |
| 40 | 44 | ||
| 41 | 45 | public static Optional<DockerContextMetaFile> resolveContextMetaFile(ObjectMapper objectMapper, File dockerConfigPath, String context) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,6 +23,7 @@ | |||
| 23 | 23 | import static org.hamcrest.core.Is.is; | |
| 24 | 24 | import static org.junit.Assert.assertEquals; | |
| 25 | 25 | import static org.junit.Assert.assertNull; | |
| 26 | + import static org.junit.Assert.assertTrue; | ||
| 26 | 27 | ||
| 27 | 28 | public class DefaultDockerClientConfigTest { | |
| 28 | 29 | ||
@@ -113,6 +114,25 @@ public void dockerContextFromEnvironmentVariable() { | |||
| 113 | 114 | assertEquals(URI.create("unix:///envvarcontext.sock"), config.getDockerHost()); | |
| 114 | 115 | } | |
| 115 | 116 | ||
| 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 | + | ||
| 116 | 136 | @Test | |
| 117 | 137 | public void environment() { | |
| 118 | 138 | ||
| Original file line number | Diff line number | Diff 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 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments