X Tutup
Skip to content

Commit a2b68c5

Browse files
committed
Removes setCredentials from DockerClient
DockerClient is now an immutable class. The only way to set credentials now is at construction of the DockerClient, using the DockerClientConfig object.
1 parent b7bbcfe commit a2b68c5

File tree

2 files changed

+16
-79
lines changed

2 files changed

+16
-79
lines changed

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

Lines changed: 13 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.dockerjava.client;
22

3+
import static com.google.common.base.Preconditions.checkNotNull;
34
import static org.apache.commons.io.IOUtils.closeQuietly;
45

56
import java.io.*;
@@ -16,36 +17,6 @@
1617
import org.apache.http.impl.client.DefaultHttpClient;
1718
import org.apache.http.impl.conn.PoolingClientConnectionManager;
1819

19-
import com.github.dockerjava.client.command.AbstrDockerCmd;
20-
import com.github.dockerjava.client.command.AttachContainerCmd;
21-
import com.github.dockerjava.client.command.AuthCmd;
22-
import com.github.dockerjava.client.command.BuildImgCmd;
23-
import com.github.dockerjava.client.command.CommitCmd;
24-
import com.github.dockerjava.client.command.ContainerDiffCmd;
25-
import com.github.dockerjava.client.command.CopyFileFromContainerCmd;
26-
import com.github.dockerjava.client.command.CreateContainerCmd;
27-
import com.github.dockerjava.client.command.CreateImageCmd;
28-
import com.github.dockerjava.client.command.InfoCmd;
29-
import com.github.dockerjava.client.command.InspectContainerCmd;
30-
import com.github.dockerjava.client.command.InspectImageCmd;
31-
import com.github.dockerjava.client.command.KillContainerCmd;
32-
import com.github.dockerjava.client.command.ListContainersCmd;
33-
import com.github.dockerjava.client.command.ListImagesCmd;
34-
import com.github.dockerjava.client.command.LogContainerCmd;
35-
import com.github.dockerjava.client.command.PingCmd;
36-
import com.github.dockerjava.client.command.PullImageCmd;
37-
import com.github.dockerjava.client.command.PushImageCmd;
38-
import com.github.dockerjava.client.command.RemoveContainerCmd;
39-
import com.github.dockerjava.client.command.RemoveImageCmd;
40-
import com.github.dockerjava.client.command.RestartContainerCmd;
41-
import com.github.dockerjava.client.command.SearchImagesCmd;
42-
import com.github.dockerjava.client.command.StartContainerCmd;
43-
import com.github.dockerjava.client.command.StopContainerCmd;
44-
import com.github.dockerjava.client.command.TagImageCmd;
45-
import com.github.dockerjava.client.command.TopContainerCmd;
46-
import com.github.dockerjava.client.command.VersionCmd;
47-
import com.github.dockerjava.client.command.WaitContainerCmd;
48-
4920
import com.github.dockerjava.client.model.AuthConfig;
5021

5122
import com.github.dockerjava.client.utils.JsonClientFilter;
@@ -62,12 +33,11 @@
6233
*/
6334
public class DockerClient implements Closeable {
6435

65-
private Client client;
36+
private final Client client;
6637

6738
private final CommandFactory cmdFactory;
6839
private final WebResource baseResource;
69-
private AuthConfig authConfig;
70-
40+
private final DockerClientConfig dockerClientConfig;
7141

7242
public DockerClient() {
7343
this(DockerClientConfig.createDefaultConfigBuilder().build());
@@ -77,20 +47,19 @@ public DockerClient(String serverUrl) {
7747
this(configWithServerUrl(serverUrl));
7848
}
7949

80-
8150
private static DockerClientConfig configWithServerUrl(String serverUrl) {
8251
return DockerClientConfig.createDefaultConfigBuilder()
8352
.withUri(serverUrl)
8453
.build();
8554
}
8655

87-
8856
public DockerClient(DockerClientConfig dockerClientConfig) {
8957
this(dockerClientConfig, new DefaultCommandFactory());
9058
}
9159

9260
public DockerClient(DockerClientConfig dockerClientConfig, CommandFactory cmdFactory) {
9361
this.cmdFactory = cmdFactory;
62+
this.dockerClientConfig = dockerClientConfig;
9463

9564
HttpClient httpClient = getPoolingHttpClient(dockerClientConfig);
9665
ClientConfig clientConfig = new DefaultClientConfig();
@@ -115,7 +84,6 @@ public DockerClient(DockerClientConfig dockerClientConfig, CommandFactory cmdFac
11584
}
11685
}
11786

118-
11987
private HttpClient getPoolingHttpClient(DockerClientConfig dockerClientConfig) {
12088
SchemeRegistry schemeRegistry = new SchemeRegistry();
12189
schemeRegistry.register(new Scheme("http", dockerClientConfig.getUri().getPort(),
@@ -129,56 +97,25 @@ private HttpClient getPoolingHttpClient(DockerClientConfig dockerClientConfig) {
12997
// Increase default max connection per route
13098
cm.setDefaultMaxPerRoute(1000);
13199

132-
133100
return new DefaultHttpClient(cm);
134101
}
135102

136-
137-
public void setCredentials(String username, String password, String email) {
138-
if (username == null) {
139-
throw new IllegalArgumentException("username is null");
140-
}
141-
if (password == null) {
142-
throw new IllegalArgumentException("password is null");
143-
}
144-
if (email == null) {
145-
throw new IllegalArgumentException("email is null");
146-
}
147-
authConfig = new AuthConfig();
148-
authConfig.setUsername(username);
149-
authConfig.setPassword(password);
150-
authConfig.setEmail(email);
151-
}
152-
153103
public <RES_T> RES_T execute(AbstrDockerCmd<?, RES_T> command)
154104
throws DockerException {
155105
return command.withBaseResource(baseResource).exec();
156106
}
157107

158108
public AuthConfig authConfig() throws DockerException {
159-
return authConfig != null ? authConfig : authConfigFromProperties();
160-
}
161-
162-
private static AuthConfig authConfigFromProperties() throws DockerException {
163-
final AuthConfig a = new AuthConfig();
164-
165-
// TODO This should probably come from the DockerClientConfig used to create the DockerClient.
166-
DockerClientConfig defaultDockerClientConfig = DockerClientConfig.createDefaultConfigBuilder().build();
167-
a.setUsername(defaultDockerClientConfig.getUsername());
168-
a.setPassword(defaultDockerClientConfig.getPassword());
169-
a.setEmail(defaultDockerClientConfig.getEmail());
109+
checkNotNull(dockerClientConfig.getUsername(), "Configured username is null.");
110+
checkNotNull(dockerClientConfig.getPassword(), "Configured password is null.");
111+
checkNotNull(dockerClientConfig.getEmail(), "Configured email is null.");
170112

171-
if (a.getUsername() == null) {
172-
throw new IllegalStateException("username is null");
173-
}
174-
if (a.getPassword() == null) {
175-
throw new IllegalStateException("password is null");
176-
}
177-
if (a.getEmail() == null) {
178-
throw new IllegalStateException("email is null");
179-
}
180-
181-
return a;
113+
AuthConfig authConfig = new AuthConfig();
114+
authConfig.setUsername(dockerClientConfig.getUsername());
115+
authConfig.setPassword(dockerClientConfig.getPassword());
116+
authConfig.setEmail(dockerClientConfig.getEmail());
117+
// TODO Make the registry address configurable
118+
return authConfig;
182119
}
183120

184121
/**
@@ -325,7 +262,6 @@ public TagImageCmd tagImageCmd(String imageId, String repository, String tag) {
325262
return cmdFactory.tagImageCmd(imageId, repository, tag).withBaseResource(baseResource);
326263
}
327264

328-
329265
// TODO This is only being used by the test code for logging. Is it really necessary?
330266
/**
331267
* @return The output slurped into a string.

src/test/java/com/github/dockerjava/client/command/AuthCmdTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.dockerjava.client.command;
22

33
import com.github.dockerjava.client.AbstractDockerClientTest;
4+
import com.github.dockerjava.client.DockerClient;
45
import com.github.dockerjava.client.DockerException;
56
import com.sun.jersey.api.client.UniformInterfaceException;
67

@@ -42,8 +43,8 @@ public void testAuth() throws Exception {
4243
public void testAuthInvalid() throws Exception {
4344
System.setProperty("docker.io.password", "garbage");
4445
try {
45-
dockerClient.authCmd().exec();
46-
fail();
46+
new DockerClient().authCmd().exec();
47+
fail("Expected a DockerException caused by a bad password.");
4748
} catch (DockerException e) {
4849
assertThat(e.getCause(), Matchers.instanceOf(UniformInterfaceException.class));
4950
assertEquals(((UniformInterfaceException) e.getCause()).getResponse().getStatus(), 401);

0 commit comments

Comments
 (0)
X Tutup