Skip to content

Commit cc0b2e0

Browse files
committed
Add more config and auth tests for push / pull and container creation in swarm
1 parent 4fd7b8a commit cc0b2e0

File tree

22 files changed

+390
-169
lines changed

22 files changed

+390
-169
lines changed

src/main/java/com/github/dockerjava/api/DockerClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public interface DockerClient extends Closeable {
236236

237237
TopContainerCmd topContainerCmd(String containerId);
238238

239-
TagImageCmd tagImageCmd(String imageId, String repository, String tag);
239+
TagImageCmd tagImageCmd(String imageId, String imageNameWithRepository, String tag);
240240

241241
PauseContainerCmd pauseContainerCmd(String containerId);
242242

src/main/java/com/github/dockerjava/api/command/CreateContainerCmd.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ public interface CreateContainerCmd extends SyncDockerCmd<CreateContainerRespons
200200
@CheckForNull
201201
Boolean isTty();
202202

203+
/**
204+
* While using swarm classic, you can provide an optional auth config which will be used to pull images from a private registry,
205+
* if the swarm node does not already have the docker image.
206+
* Note: This option does not have any effect in normal docker
207+
* @param authConfig The optional auth config
208+
*/
203209
CreateContainerCmd withAuthConfig(AuthConfig authConfig);
204210

205211
/**

src/main/java/com/github/dockerjava/core/DockerClientImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,8 @@ public TopContainerCmd topContainerCmd(String containerId) {
431431
}
432432

433433
@Override
434-
public TagImageCmd tagImageCmd(String imageId, String repository, String tag) {
435-
return new TagImageCmdImpl(getDockerCmdExecFactory().createTagImageCmdExec(), imageId, repository, tag);
434+
public TagImageCmd tagImageCmd(String imageId, String imageNameWithRepository, String tag) {
435+
return new TagImageCmdImpl(getDockerCmdExecFactory().createTagImageCmdExec(), imageId, imageNameWithRepository, tag);
436436
}
437437

438438
@Override

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ private static DockerConfigFile loadLegacyConfig(File dockerConfigPath) throws I
187187
}
188188

189189
private static void decodeAuth(AuthConfig config) throws IOException {
190+
if (config.getAuth() == null) {
191+
return;
192+
}
193+
190194
String str = new String(Base64.decodeBase64(config.getAuth()), StandardCharsets.UTF_8);
191195
String[] parts = str.split(":", 2);
192196
if (parts.length != 2) {

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
package com.github.dockerjava.jaxrs;
22

3-
import static com.github.dockerjava.core.RemoteApiVersion.UNKNOWN_VERSION;
4-
import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_19;
5-
import static com.google.common.base.Preconditions.checkNotNull;
6-
7-
import java.io.IOException;
8-
9-
import javax.ws.rs.client.WebTarget;
10-
11-
import com.fasterxml.jackson.databind.node.ObjectNode;
12-
import org.apache.commons.codec.binary.Base64;
13-
143
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.node.ObjectNode;
155
import com.github.dockerjava.api.model.AuthConfig;
166
import com.github.dockerjava.api.model.AuthConfigurations;
177
import com.github.dockerjava.core.DockerClientConfig;
188
import com.github.dockerjava.core.RemoteApiVersion;
9+
import org.apache.commons.codec.binary.Base64;
10+
11+
import javax.ws.rs.client.Invocation;
12+
import javax.ws.rs.client.WebTarget;
13+
import java.io.IOException;
14+
15+
import static com.github.dockerjava.core.RemoteApiVersion.UNKNOWN_VERSION;
16+
import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_19;
17+
import static com.google.common.base.Preconditions.checkNotNull;
1918

2019
public abstract class AbstrDockerCmdExec {
2120

@@ -46,6 +45,18 @@ protected String registryAuth(AuthConfig authConfig) {
4645
}
4746
}
4847

48+
protected Invocation.Builder resourceWithAuthConfig(AuthConfig authConfig, Invocation.Builder request) {
49+
request = request.header("X-Registry-Auth", registryAuth(authConfig));
50+
return request;
51+
}
52+
53+
protected Invocation.Builder resourceWithOptionalAuthConfig(AuthConfig authConfig, Invocation.Builder request) {
54+
if (authConfig != null) {
55+
request = resourceWithAuthConfig(authConfig, request);
56+
}
57+
return request;
58+
}
59+
4960
protected String registryConfigs(AuthConfigurations authConfigs) {
5061
try {
5162
final String json;

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import com.github.dockerjava.api.command.CreateContainerCmd;
44
import com.github.dockerjava.api.command.CreateContainerResponse;
5-
import com.github.dockerjava.api.model.AuthConfig;
65
import com.github.dockerjava.core.DockerClientConfig;
76
import org.slf4j.Logger;
87
import org.slf4j.LoggerFactory;
98

10-
import javax.ws.rs.client.Invocation;
119
import javax.ws.rs.client.WebTarget;
1210
import javax.ws.rs.core.MediaType;
1311

@@ -22,14 +20,6 @@ public CreateContainerCmdExec(WebTarget baseResource, DockerClientConfig dockerC
2220
super(baseResource, dockerClientConfig);
2321
}
2422

25-
private Invocation.Builder resourceWithOptionalAuthConfig(CreateContainerCmd command, Invocation.Builder request) {
26-
AuthConfig authConfig = command.getAuthConfig();
27-
if (authConfig != null) {
28-
request = request.header("X-Registry-Auth", registryAuth(authConfig));
29-
}
30-
return request;
31-
}
32-
3323
@Override
3424
protected CreateContainerResponse execute(CreateContainerCmd command) {
3525
WebTarget webResource = getBaseResource().path("/containers/create");
@@ -39,7 +29,7 @@ protected CreateContainerResponse execute(CreateContainerCmd command) {
3929
}
4030

4131
LOGGER.trace("POST: {} ", webResource);
42-
return resourceWithOptionalAuthConfig(command, webResource.request()).accept(MediaType.APPLICATION_JSON)
32+
return resourceWithOptionalAuthConfig(command.getAuthConfig(), webResource.request()).accept(MediaType.APPLICATION_JSON)
4333
.post(entity(command, MediaType.APPLICATION_JSON), CreateContainerResponse.class);
4434
}
4535

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

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
package com.github.dockerjava.jaxrs;
22

3-
import static javax.ws.rs.client.Entity.entity;
4-
5-
import javax.ws.rs.client.Invocation;
6-
import javax.ws.rs.client.Invocation.Builder;
7-
import javax.ws.rs.client.WebTarget;
8-
import javax.ws.rs.core.MediaType;
9-
10-
import org.slf4j.Logger;
11-
import org.slf4j.LoggerFactory;
12-
133
import com.github.dockerjava.api.async.ResultCallback;
144
import com.github.dockerjava.api.command.PullImageCmd;
15-
import com.github.dockerjava.api.model.AuthConfig;
165
import com.github.dockerjava.api.model.PullResponseItem;
176
import com.github.dockerjava.core.DockerClientConfig;
187
import com.github.dockerjava.core.async.JsonStreamProcessor;
198
import com.github.dockerjava.jaxrs.async.AbstractCallbackNotifier;
209
import com.github.dockerjava.jaxrs.async.POSTCallbackNotifier;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import javax.ws.rs.client.Invocation.Builder;
14+
import javax.ws.rs.client.WebTarget;
15+
import javax.ws.rs.core.MediaType;
16+
17+
import static javax.ws.rs.client.Entity.entity;
2118

2219
public class PullImageCmdExec extends AbstrAsyncDockerCmdExec<PullImageCmd, PullResponseItem> implements
2320
PullImageCmd.Exec {
@@ -28,14 +25,6 @@ public PullImageCmdExec(WebTarget baseResource, DockerClientConfig dockerClientC
2825
super(baseResource, dockerClientConfig);
2926
}
3027

31-
private Invocation.Builder resourceWithOptionalAuthConfig(PullImageCmd command, Invocation.Builder request) {
32-
AuthConfig authConfig = command.getAuthConfig();
33-
if (authConfig != null) {
34-
request = request.header("X-Registry-Auth", registryAuth(authConfig));
35-
}
36-
return request;
37-
}
38-
3928
@Override
4029
protected AbstractCallbackNotifier<PullResponseItem> callbackNotifier(PullImageCmd command,
4130
ResultCallback<PullResponseItem> resultCallback) {
@@ -44,7 +33,7 @@ protected AbstractCallbackNotifier<PullResponseItem> callbackNotifier(PullImageC
4433
.queryParam("fromImage", command.getRepository()).queryParam("registry", command.getRegistry());
4534

4635
LOGGER.trace("POST: {}", webResource);
47-
Builder builder = resourceWithOptionalAuthConfig(command, webResource.request()).accept(
36+
Builder builder = resourceWithOptionalAuthConfig(command.getAuthConfig(), webResource.request()).accept(
4837
MediaType.APPLICATION_OCTET_STREAM_TYPE);
4938

5039
return new POSTCallbackNotifier<PullResponseItem>(new JsonStreamProcessor<PullResponseItem>(

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

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

3-
import static javax.ws.rs.client.Entity.entity;
4-
5-
import javax.ws.rs.client.Invocation.Builder;
6-
import javax.ws.rs.client.WebTarget;
7-
import javax.ws.rs.core.MediaType;
8-
9-
import org.slf4j.Logger;
10-
import org.slf4j.LoggerFactory;
11-
123
import com.github.dockerjava.api.async.ResultCallback;
134
import com.github.dockerjava.api.command.PushImageCmd;
145
import com.github.dockerjava.api.model.AuthConfig;
@@ -17,6 +8,14 @@
178
import com.github.dockerjava.core.async.JsonStreamProcessor;
189
import com.github.dockerjava.jaxrs.async.AbstractCallbackNotifier;
1910
import com.github.dockerjava.jaxrs.async.POSTCallbackNotifier;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
14+
import javax.ws.rs.client.Invocation.Builder;
15+
import javax.ws.rs.client.WebTarget;
16+
import javax.ws.rs.core.MediaType;
17+
18+
import static javax.ws.rs.client.Entity.entity;
2019

2120
public class PushImageCmdExec extends AbstrAsyncDockerCmdExec<PushImageCmd, PushResponseItem> implements
2221
PushImageCmd.Exec {
@@ -40,10 +39,9 @@ protected AbstractCallbackNotifier<PushResponseItem> callbackNotifier(PushImageC
4039
WebTarget webResource = getBaseResource().path("/images/" + name(command) + "/push").queryParam("tag",
4140
command.getTag());
4241

43-
final String registryAuth = registryAuth(command.getAuthConfig());
4442
LOGGER.trace("POST: {}", webResource);
4543

46-
Builder builder = webResource.request().header("X-Registry-Auth", registryAuth)
44+
Builder builder = resourceWithAuthConfig(command.getAuthConfig(), webResource.request())
4745
.accept(MediaType.APPLICATION_JSON);
4846

4947
return new POSTCallbackNotifier<PushResponseItem>(new JsonStreamProcessor<PushResponseItem>(

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
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;
5-
import static com.google.common.base.Preconditions.checkNotNull;
6-
7-
import java.io.IOException;
8-
9-
import com.fasterxml.jackson.databind.node.ObjectNode;
10-
import org.apache.commons.codec.binary.Base64;
11-
123
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.node.ObjectNode;
135
import com.github.dockerjava.api.model.AuthConfig;
146
import com.github.dockerjava.api.model.AuthConfigurations;
157
import com.github.dockerjava.core.DockerClientConfig;
168
import com.github.dockerjava.core.RemoteApiVersion;
9+
import com.github.dockerjava.netty.InvocationBuilder;
1710
import com.github.dockerjava.netty.WebTarget;
11+
import org.apache.commons.codec.binary.Base64;
12+
13+
import java.io.IOException;
14+
15+
import static com.github.dockerjava.core.RemoteApiVersion.UNKNOWN_VERSION;
16+
import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_19;
17+
import static com.google.common.base.Preconditions.checkNotNull;
1818

1919
public abstract class AbstrDockerCmdExec {
2020

@@ -68,6 +68,17 @@ protected String registryConfigs(AuthConfigurations authConfigs) {
6868
}
6969
}
7070

71+
protected InvocationBuilder resourceWithAuthConfig(AuthConfig authConfig, InvocationBuilder request) {
72+
return request.header("X-Registry-Auth", registryAuth(authConfig));
73+
}
74+
75+
protected InvocationBuilder resourceWithOptionalAuthConfig(AuthConfig authConfig, InvocationBuilder request) {
76+
if (authConfig != null) {
77+
request = resourceWithAuthConfig(authConfig, request);
78+
}
79+
return request;
80+
}
81+
7182
protected boolean bool(Boolean bool) {
7283
return bool != null && bool;
7384
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ protected CreateContainerResponse execute(CreateContainerCmd command) {
2828
}
2929

3030
LOGGER.trace("POST: {} ", webResource);
31-
return webResource.request().accept(MediaType.APPLICATION_JSON)
32-
.post(command, new TypeReference<CreateContainerResponse>() {
33-
});
31+
return resourceWithOptionalAuthConfig(command.getAuthConfig(), webResource.request())
32+
.accept(MediaType.APPLICATION_JSON)
33+
.post(command, new TypeReference<CreateContainerResponse>() { });
3434
}
3535
}

0 commit comments

Comments
 (0)