X Tutup
Skip to content

Commit 241257c

Browse files
committed
Fix netty auth.
1 parent a0ec66f commit 241257c

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public BuildImageCmdExec(WebTarget baseResource, DockerClientConfig dockerClient
3737

3838
private Invocation.Builder resourceWithOptionalAuthConfig(BuildImageCmd command, Invocation.Builder request) {
3939
final AuthConfigurations authConfigs = firstNonNull(command.getBuildAuthConfigs(), getBuildAuthConfigs());
40-
if (authConfigs != null) {
40+
if (authConfigs != null && !authConfigs.getConfigs().isEmpty()) {
4141
request = request.header("X-Registry-Config", registryConfigs(authConfigs));
4242
}
4343
return request;
@@ -113,8 +113,10 @@ protected AbstractCallbackNotifier<BuildResponseItem> callbackNotifier(BuildImag
113113

114114
LOGGER.trace("POST: {}", webTarget);
115115

116-
return new POSTCallbackNotifier<>(new JsonStreamProcessor<>(BuildResponseItem.class), resultCallback,
117-
resourceWithOptionalAuthConfig(command, webTarget.request()).accept(MediaType.TEXT_PLAIN), entity(
118-
command.getTarInputStream(), "application/tar"));
116+
return new POSTCallbackNotifier<>(new JsonStreamProcessor<>(BuildResponseItem.class),
117+
resultCallback,
118+
resourceWithOptionalAuthConfig(command, webTarget.request()).accept(MediaType.TEXT_PLAIN),
119+
entity(command.getTarInputStream(), "application/tar")
120+
);
119121
}
120122
}

src/main/java/com/github/dockerjava/netty/exec/AbstrDockerCmdExec.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.github.dockerjava.netty.exec;
22

3+
import static com.github.dockerjava.core.RemoteApiVersion.UNKNOWN_VERSION;
4+
import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_19;
35
import static com.google.common.base.Preconditions.checkNotNull;
46

57
import java.io.IOException;
68

9+
import com.fasterxml.jackson.databind.node.ObjectNode;
710
import org.apache.commons.codec.binary.Base64;
811

912
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -45,10 +48,18 @@ protected String registryAuth(AuthConfig authConfig) {
4548
protected String registryConfigs(AuthConfigurations authConfigs) {
4649
try {
4750
final String json;
48-
if (dockerClientConfig.getApiVersion().isGreaterOrEqual(RemoteApiVersion.VERSION_1_19)) {
49-
json = new ObjectMapper().writeValueAsString(authConfigs.getConfigs());
51+
final ObjectMapper objectMapper = new ObjectMapper();
52+
final RemoteApiVersion apiVersion = dockerClientConfig.getApiVersion();
53+
54+
if (apiVersion.equals(UNKNOWN_VERSION)) {
55+
ObjectNode rootNode = objectMapper.valueToTree(authConfigs.getConfigs()); // all registries
56+
final ObjectNode authNodes = objectMapper.valueToTree(authConfigs); // wrapped in "configs":{}
57+
rootNode.setAll(authNodes); // merge 2 variants
58+
json = rootNode.toString();
59+
} else if (apiVersion.isGreaterOrEqual(VERSION_1_19)) {
60+
json = objectMapper.writeValueAsString(authConfigs.getConfigs());
5061
} else {
51-
json = new ObjectMapper().writeValueAsString(authConfigs);
62+
json = objectMapper.writeValueAsString(authConfigs);
5263
}
5364

5465
return Base64.encodeBase64String(json.getBytes());

src/main/java/com/github/dockerjava/netty/exec/BuildImageCmdExec.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public BuildImageCmdExec(WebTarget baseResource, DockerClientConfig dockerClient
2828

2929
private InvocationBuilder resourceWithOptionalAuthConfig(BuildImageCmd command, InvocationBuilder request) {
3030
final AuthConfigurations authConfigs = firstNonNull(command.getBuildAuthConfigs(), getBuildAuthConfigs());
31-
if (authConfigs != null) {
31+
if (authConfigs != null && !authConfigs.getConfigs().isEmpty()) {
3232
request = request.header("X-Registry-Config", registryConfigs(authConfigs));
3333
}
3434
return request;
@@ -96,7 +96,8 @@ protected Void execute0(BuildImageCmd command, ResultCallback<BuildResponseItem>
9696
LOGGER.trace("POST: {}", webTarget);
9797

9898
InvocationBuilder builder = resourceWithOptionalAuthConfig(command, webTarget.request())
99-
.accept(MediaType.APPLICATION_JSON).header("Content-Type", "application/tar")
99+
.accept(MediaType.APPLICATION_JSON)
100+
.header("Content-Type", "application/tar")
100101
.header("encoding", "gzip");
101102

102103
builder.post(new TypeReference<BuildResponseItem>() {

src/test/java/com/github/dockerjava/netty/exec/BuildImageCmdExecTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,11 @@ public void testBuildFromPrivateRegistry() throws Exception {
287287
AuthConfigurations authConfigurations = new AuthConfigurations();
288288
authConfigurations.addConfig(authConfig);
289289

290-
imageId = dockerClient.buildImageCmd(baseDir).withNoCache(true).withBuildAuthConfigs(authConfigurations)
291-
.exec(new BuildImageResultCallback()).awaitImageId();
290+
imageId = dockerClient.buildImageCmd(baseDir)
291+
.withNoCache(true)
292+
.withBuildAuthConfigs(authConfigurations)
293+
.exec(new BuildImageResultCallback())
294+
.awaitImageId();
292295

293296
inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec();
294297
assertThat(inspectImageResponse, not(nullValue()));

0 commit comments

Comments
 (0)
X Tutup