8000 Merge branch 'main' into dont-trash-ioexception · docker-java/docker-java@a36141d · GitHub
[go: up one dir, main page]

Skip to content

Commit a36141d

Browse files
authored
Merge branch 'main' into dont-trash-ioexception
2 parents c2e8329 + e92c30e commit a36141d

File tree

18 files changed

+153
-26
lines changed

18 files changed

+153
-26
lines changed

docker-java-api/src/main/java/com/github/dockerjava/api/command/BuildImageResultCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void onNext(BuildResponseItem item) {
3131
} else if (item.isErrorIndicated()) {
3232
this.error = item.getError();
3333
}
34-
LOGGER.debug(item.toString());
34+
LOGGER.debug("{}", item);
3535
}
3636

3737
/**

docker-java-api/src/main/java/com/github/dockerjava/api/command/LoadImageCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void onNext(LoadResponseItem item) {
2222
this.error = item.getError();
2323
}
2424

25-
LOGGER.debug(item.toString());
25+
LOGGER.debug("{}", item);
2626
}
2727

2828
public String awaitMessage() {

docker-java-api/src/main/java/com/github/dockerjava/api/command/PullImageResultCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void onNext(PullResponseItem item) {
4141
handleDockerClientResponse(item);
4242
}
4343

44-
LOGGER.debug(item.toString());
44+
LOGGER.debug("{}", item);
4545
}
4646

4747
private void checkForDockerSwarmResponse(PullResponseItem item) {

docker-java-api/src/main/java/com/github/dockerjava/api/command/WaitContainerResultCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class WaitContainerResultCallback extends ResultCallbackTemplate<WaitCont
2727
@Override
2828
public void onNext(WaitResponse waitResponse) {
2929
this.waitResponse = waitResponse;
30-
LOGGER.debug(waitResponse.toString());
30+
LOGGER.debug("{}", waitResponse);
3131
}
3232

3333
/**

docker-java-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
<dependency>
7171
<groupId>org.bouncycastle</groupId>
72-
<artifactId>bcpkix-jdk15on</artifactId>
72+
<artifactId>bcpkix-jdk18on</artifactId>
7373
<version>${bouncycastle.version}</version>
7474
</dependency>
7575

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,32 @@ 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+
final Optional<File> dockerContextTLSFile =
450+
Optional.ofNullable(context)
451+
.flatMap(ctx -> DockerContextMetaFile.resolveContextTLSFile(new File(this.dockerConfig), ctx));
452+
453+
if (dockerContextMetaFile.isPresent()) {
454+
final Optional<DockerContextMetaFile.Endpoints.Docker> dockerEndpoint =
455+
dockerContextMetaFile.map(metaFile -> metaFile.endpoints).map(endpoint -> endpoint.docker);
456+
if (this.dockerHost == null) {
457+
this.dockerHost = dockerEndpoint.map(endpoint -> endpoint.host).map(URI::create).orElse(null);
458+
}
459+
}
460+
if (dockerContextTLSFile.isPresent() && this.dockerCertPath == null) {
461+
this.dockerCertPath = dockerCo 10000 ntextTLSFile.get().getAbsolutePath();
462+
this.dockerTlsVerify = true;
463+
}
464+
}
465+
444466
public DefaultDockerClientConfig build() {
467+
final DockerConfigFile dockerConfigFile = readDockerConfig();
468+
final String context = (dockerContext != null) ? dockerContext : dockerConfigFile.getCurrentContext();
469+
applyContextConfiguration(context);
445470

446471
SSLConfig sslConfig = null;
447472

@@ -454,12 +479,9 @@ public DefaultDockerClientConfig build() {
454479
sslConfig = customSslConfig;
455480
}
456481

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

464486
return new DefaultDockerClientConfig(dockerHostUri, dockerConfigFile, dockerConfig, apiVersion, registryUrl, registryUsername,
465487
registryPassword, registryEmail, sslConfig);
@@ -473,14 +495,6 @@ private DockerConfigFile readDockerConfig() {
473495
}
474496
}
475497

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-
484498
private String checkDockerCertPath(String dockerCertPath) {
485499
if (StringUtils.isEmpty(dockerCertPath)) {
486500
throw new DockerClientException(

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.dockerjava.core;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.fasterxml.jackson.annotation.JsonSetter;
45
import com.fasterxml.jackson.core.type.TypeReference;
56
import com.fasterxml.jackson.databind.ObjectMapper;
67
import com.github.dockerjava.api.model.AuthConfig;
@@ -28,7 +29,7 @@ public class DockerConfigFile {
2829
};
2930

3031
@JsonProperty
31-
private final Map<String, AuthConfig> auths;
32+
private Map<String, AuthConfig> auths;
3233

3334
@JsonProperty
3435
private String currentContext;
@@ -46,6 +47,11 @@ public Map<String, AuthConfig> getAuths() {
4647
return auths;
4748
}
4849

50+
@JsonSetter
51+
public void setAuths(Map<String, AuthConfig> authConfigMap) {
52+
auths = (authConfigMap == null || authConfigMap.size() == 0) ? new HashMap<>() : authConfigMap;
53+
}
54+
4955
void addAuthConfig(AuthConfig config) {
5056
auths.put(config.getRegistryAddress(), config);
5157
}

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

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

21+
2122
public static class Endpoints {
2223
@JsonProperty("docker")
2324
Docker docker;
@@ -31,12 +32,6 @@ public static class Docker {
3132
}
3233
}
3334

34-
public Optional<String> host() {
35- F438
if (endpoints != null && endpoints.docker != null) {
36-
return Optional.ofNullable(endpoints.docker.host);
37-
}
38-
return Optional.empty();
39-
}
4035

4136
public static Optional<DockerContextMetaFile> resolveContextMetaFile(ObjectMapper objectMapper, File dockerConfigPath, String context) {
4237
final File path = dockerConfigPath.toPath()
@@ -48,6 +43,16 @@ public static Optional<DockerContextMetaFile> resolveContextMetaFile(ObjectMappe
4843
return Optional.ofNullable(loadContextMetaFile(objectMapper, path));
4944
}
5045

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+
5156
public static DockerContextMetaFile loadContextMetaFile(ObjectMapper objectMapper, File dockerContextMetaFile) {
5257
try {
5358
return parseContextMetaFile(objectMapper, dockerContextMetaFile);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ private NameParser() {
2222
private static final int RepositoryNameTotalLengthMax = 255;
2323

2424
private static final String SHA256_SEPARATOR = "@sha256:";
25+
private static final String COLON_SEPARATOR = ":";
2526

2627
private static final Pattern RepositoryNameComponentRegexp = Pattern.compile("[a-z0-9]+(?:[._-][a-z0-9]+)*");
2728

@@ -106,6 +107,13 @@ public static HostnameReposName resolveRepositoryName(String reposName) {
106107
String[] nameParts = reposName.split("/", 2);
107108
if (nameParts.length == 1
108109
|| (!nameParts[0].contains(".") && !nameParts[0].contains(":") && !nameParts[0].equals("localhost"))) {
110+
if (StringUtils.containsIgnoreCase(reposName, SHA256_SEPARATOR)) {
111+
reposName = StringUtils.substringBeforeLast(reposName, SHA256_SEPARATOR);
112+
}
113+
114+
if (StringUtils.contains(reposName, COLON_SEPARATOR)) {
115+
reposName = StringUtils.substringBeforeLast(reposName, COLON_SEPARATOR);
116+
}
109117
return new HostnameReposName(AuthConfig.DEFAULT_SERVER_ADDRESS, reposName);
110118
}
111119

@@ -119,6 +127,10 @@ public static HostnameReposName resolveRepositoryName(String reposName) {
119127
reposName = StringUtils.substringBeforeLast(reposName, SHA256_SEPARATOR);
120128
}
121129

130+
if (StringUtils.contains(reposName, COLON_SEPARATOR)) {
131+
reposName = StringUtils.substringBeforeLast(reposName, COLON_SEPARATOR);
132+
}
133+
122134
validateRepoName(reposName);
123135
return new HostnameReposName(hostname, reposName);
124136
}

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

Lines changed: 19 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,24 @@ 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+
assertTrue("SSL directory is set", ((LocalDirectorySSLConfig)config.getSSLConfig()).getDockerCertPath().endsWith("dockerContextHomeDir/.docker/contexts/tls/b71199ebd070b36beab7317920c2c2f1d777df8d05e5527d8458fda57cb17a7a/docker"));
133+
}
134+
116135
@Test
117136
public void environment() {
118137

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ public void nonExistent() throws IOException {
164164
assertThat(runTest("idontexist"), is(expected));
165165
}
166166

167+
@Test
168+
public void validJsonAuthsNull() throws IOException {
169+
DockerConfigFile expected = new DockerConfigFile();
170+
assertThat(runTest("validJsonAuthsNull"), is(expected));
171+
}
172+
167173
private DockerConfigFile runTest(String testFileName) throws IOException {
168174
return DockerConfigFile.loadConfig(JSONTestHelper.getMapper(), new File(FILESROOT, testFileName).getAbsolutePath());
169175
}

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ public void testResolveSimpleRepositoryName() {
8383
assertEquals(new HostnameReposName(AuthConfig.DEFAULT_SERVER_ADDRESS, "repository"), resolved);
8484
}
8585

86+
@Test
87+
public void testResolveRepositoryNameWithTag() {
88+
HostnameReposName resolved = NameParser.resolveRepositoryName("repository:tag");
89+
assertEquals(new HostnameReposName(AuthConfig.DEFAULT_SERVER_ADDRESS, "repository"), resolved);
90+
}
91+
92+
@Test
93+
public void testResolveRepositoryNameWithSHA256() {
94+
HostnameReposName resolved = NameParser.resolveRepositoryName("repository@sha256:sha256");
95+
assertEquals(new HostnameReposName(AuthConfig.DEFAULT_SERVER_ADDRESS, "repository"), resolved);
96+
}
97+
98+
@Test
99+
public void testResolveRepositoryNameWithTagAndSHA256() {
100+
HostnameReposName resolved = NameParser.resolveRepositoryName("repository:tag@sha256:sha256");
101+
assertEquals(new HostnameReposName(AuthConfig.DEFAULT_SERVER_ADDRESS, "repository"), resolved);
102+
}
103+
86104
@Test
87105
public void testResolveRepositoryNameWithNamespace() {
88106
HostnameReposName resolved = NameParser.resolveRepositoryName("namespace/repository");
@@ -92,7 +110,7 @@ public void testResolveRepositoryNameWithNamespace() {
92110
@Test
93111
public void testResolveRepositoryNameWithNamespaceAndSHA256() {
94112
HostnameReposName resolved = NameParser.resolveRepositoryName("namespace/repository@sha256:sha256");
95-
assertEquals(new HostnameReposName(AuthConfig.DEFAULT_SERVER_ADDRESS, "namespace/repository@sha256:sha256"), resolved);
113+
assertEquals(new HostnameReposName(AuthConfig.DEFAULT_SERVER_ADDRESS, "namespace/repository"), resolved);
96114
}
97115

98116
@Test
@@ -107,6 +125,17 @@ public void testResolveRepositoryNameWithNamespaceAndHostnameAndSHA256() {
107125
assertEquals(new HostnameReposName("localhost:5000", "namespace/repository"), resolved);
108126
}
109127

128+
@Test
129+
public void testResolveRepositoryNameWithNamespaceAndHostnameAndTag() {
130+
HostnameReposName resolved = NameParser.resolveRepositoryName("localhost:5000/namespace/repository:tag");
131+
assertEquals(new HostnameReposName("localhost:5000", "namespace/repository"), resolved);
132+
}
133+
@Test
134+
public void testResolveRepositoryNameWithNamespaceAndHostnameAndTagAndSHA256() {
135+
HostnameReposName resolved = NameParser.resolveRepositoryName("localhost:5000/namespace/repository:tag@sha256:sha256");
136+
assertEquals(new HostnameReposName("localhost:5000", "namespace/repository"), resolved);
137+
}
138+
110139
@Test(expected = InvalidRepositoryNameException.class)
111140
public void testResolveRepositoryNameWithIndex() {
112141
NameParser.resolveRepositoryName("index.docker.io/repository");
@@ -147,4 +176,16 @@ public void testResolveReposTagWithSHA256() {
147176
resolved = NameParser.parseRepositoryTag("localhost:5000/namespace/repository@sha256:sha256");
148177
assertEquals(new ReposTag("localhost:5000/namespace/repository@sha256:sha256", ""), resolved);
149178
}
179+
180+
@Test
181+
public void testResolveReposTagWithTagAndSHA256() {
182+
ReposTag resolved = NameParser.parseRepositoryTag("repository:tag@sha256:sha256");
183+
assertEquals(new ReposTag("repository:tag@sha256:sha256", ""), resolved);
184+
185+
resolved = NameParser.parseRepositoryTag("namespace/repository:tag@sha256:sha256");
186+
assertEquals(new ReposTag("namespace/repository:tag@sha256:sha256", ""), resolved);
187+
188+
resolved = NameParser.parseRepositoryTag("localhost:5000/namespace/repository:tag@sha256:sha256");
189+
assertEquals(new ReposTag("localhost:5000/namespace/repository:tag@sha256:sha256", ""), resolved);
190+
}
150191
}
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+
}

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.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"auths": null,
3+
"credsStore": "desktop",
4+
"plugins": {
5+
"-x-cli-hints": {
6+
"enabled": "true"
7+
}
8+
}
9+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
<commons-lang3.version>3.12.0</commons-lang3.version>
6767
<slf4j-api.version>1.7.30</slf4j-api.version>
6868

69-
<bouncycastle.version>1.64</bouncycastle.version>
69+
<bouncycastle.version>1.75</bouncycastle.version>
7070
<junixsocket.version>2.6.1</junixsocket.version>
7171
<guava.version>19.0</guava.version> <!-- todo remove from project -->
7272

0 commit comments

Comments
 (0)
0