Skip to content

Commit 60dee43

Browse files
committed
AuthCmd now returns AuthResponse so you can tell if you need to authorise you newly registered account
1 parent 1543b0b commit 60dee43

File tree

7 files changed

+83
-47
lines changed

7 files changed

+83
-47
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ Developer forum for [docker-java](https://groups.google.com/forum/?hl=de#!forum/
1919

2020
You'll need to be running a local private registry, as per [the quick start instructions](https://github.com/docker/docker-registry):
2121

22-
docker run -p 5000:5000 registry
22+
$ docker run -p 5000:5000 registry
23+
24+
If you're using boot2docker, set-up a port forward:
25+
26+
$ VBoxManage controlvm boot2docker-vm natpf1 "5000,tcp,127.0.0.1,5000,,5000"
27+
28+
You can remove this forward later using:
29+
30+
$ VBoxManage controlvm boot2docker-vm natpf1 delete 5000
2331

2432
The Maven build includes integration tests which are using a localhost instance of Docker and require manual setup. Make sure you have a local Docker daemon running and then provide your https://registry.hub.docker.com/account/login/ information via system properties:
2533

26-
$ mvn clean install -Ddocker.io.username=... -Ddocker.io.password=... -Ddocker.io.email=...
27-
28-
_If your Docker server is remote, add its URL like this: `-Ddocker.io.url=https://...:2376`._
34+
$ mvn clean install
2935

3036
If you do not have access to a Docker server or just want to execute the build quickly, you can run the build without the integration tests:
3137

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@
22

33
import com.github.dockerjava.api.UnauthorizedException;
44
import com.github.dockerjava.api.model.AuthConfig;
5+
import com.github.dockerjava.api.model.AuthResponse;
56

67
/**
78
*
89
* Authenticate with the server, useful for checking authentication.
910
*
1011
*/
11-
public interface AuthCmd extends DockerCmd<Void> {
12+
public interface AuthCmd extends DockerCmd<AuthResponse> {
1213

1314
public AuthConfig getAuthConfig();
1415

1516
public AuthCmd withAuthConfig(AuthConfig authConfig);
16-
17+
18+
/**
19+
* @return The status. Based on it's value you may mean you need to authorise your account, e.g.:
20+
* "Account created. Please see the documentation of the registry http://localhost:5000/v1/ for instructions how to activate it."
21+
* @throws UnauthorizedException If you're not authorised (e.g. bad password).
22+
*/
1723
@Override
18-
public Void exec() throws UnauthorizedException;
24+
public AuthResponse exec() throws UnauthorizedException;
1925

20-
public static interface Exec extends DockerCmdExec<AuthCmd, Void> {
26+
public static interface Exec extends DockerCmdExec<AuthCmd, AuthResponse> {
2127
}
2228

2329
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
public class AuthResponse {
6+
@JsonProperty("Status")
7+
private String status;
8+
9+
public String getStatus() {
10+
return status;
11+
}
12+
}

src/main/java/com/github/dockerjava/core/command/AuthCmdImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@
33
import com.github.dockerjava.api.UnauthorizedException;
44
import com.github.dockerjava.api.command.AuthCmd;
55
import com.github.dockerjava.api.model.AuthConfig;
6+
import com.github.dockerjava.api.model.AuthResponse;
67

78
/**
89
*
910
* Authenticate with the server, useful for checking authentication.
1011
*
1112
*/
12-
public class AuthCmdImpl extends AbstrAuthCfgDockerCmd<AuthCmd, Void> implements AuthCmd {
13+
public class AuthCmdImpl extends AbstrAuthCfgDockerCmd<AuthCmd, AuthResponse> implements AuthCmd {
1314

1415
public AuthCmdImpl(AuthCmd.Exec exec, AuthConfig authConfig) {
1516
super(exec);
1617
withAuthConfig(authConfig);
1718
}
18-
19+
1920
@Override
20-
public Void exec() throws UnauthorizedException {
21+
public AuthResponse exec() throws UnauthorizedException {
2122
return super.exec();
2223
}
23-
24+
2425
@Override
2526
public String toString() {
2627
return "authenticate using " + this.getAuthConfig();
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.github.dockerjava.jaxrs;
22

3-
import static javax.ws.rs.client.Entity.entity;
3+
import com.github.dockerjava.api.UnauthorizedException;
4+
import com.github.dockerjava.api.command.AuthCmd;
5+
import com.github.dockerjava.api.model.AuthResponse;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
48

59
import javax.ws.rs.client.WebTarget;
610
import javax.ws.rs.core.MediaType;
711
import javax.ws.rs.core.Response;
812

9-
import org.slf4j.Logger;
10-
import org.slf4j.LoggerFactory;
11-
12-
import com.github.dockerjava.api.UnauthorizedException;
13-
import com.github.dockerjava.api.command.AuthCmd;
13+
import static javax.ws.rs.client.Entity.entity;
1414

15-
public class AuthCmdExec extends AbstrDockerCmdExec<AuthCmd, Void> implements AuthCmd.Exec {
15+
public class AuthCmdExec extends AbstrDockerCmdExec<AuthCmd,AuthResponse> implements AuthCmd.Exec {
1616

1717
private static final Logger LOGGER = LoggerFactory
1818
.getLogger(AuthCmdExec.class);
@@ -22,16 +22,18 @@ public AuthCmdExec(WebTarget baseResource) {
2222
}
2323

2424
@Override
25-
protected Void execute(AuthCmd command) {
25+
protected AuthResponse execute(AuthCmd command) {
2626
WebTarget webResource = getBaseResource().path("/auth");
2727
LOGGER.trace("POST: {}", webResource);
28-
Response response = webResource.request().accept(MediaType.APPLICATION_JSON).post(entity(command.getAuthConfig(), MediaType.APPLICATION_JSON));
28+
Response response = webResource
29+
.request()
30+
.accept(MediaType.APPLICATION_JSON).post(entity(command.getAuthConfig(), MediaType.APPLICATION_JSON));
2931

3032
if(response.getStatus() == 401) {
3133
throw new UnauthorizedException("Unauthorized");
32-
};
33-
34-
return null;
34+
}
35+
36+
return response.readEntity(AuthResponse.class);
3537
}
3638

3739
}

src/test/java/com/github/dockerjava/client/AbstractDockerClientTest.java

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

3-
import java.io.IOException;
4-
import java.io.InputStream;
5-
import java.io.StringWriter;
6-
import java.lang.reflect.Method;
7-
import java.net.DatagramSocket;
8-
import java.net.ServerSocket;
9-
3+
import com.github.dockerjava.api.DockerClient;
4+
import com.github.dockerjava.api.DockerException;
5+
import com.github.dockerjava.core.DockerClientBuilder;
6+
import com.github.dockerjava.core.DockerClientConfig;
7+
import com.github.dockerjava.core.TestDockerCmdExecFactory;
108
import org.apache.commons.io.IOUtils;
119
import org.apache.commons.io.LineIterator;
1210
import org.slf4j.Logger;
1311
import org.slf4j.LoggerFactory;
1412
import org.testng.Assert;
1513
import org.testng.ITestResult;
1614

17-
import com.github.dockerjava.api.DockerClient;
18-
import com.github.dockerjava.api.DockerException;
19-
import com.github.dockerjava.core.DockerClientBuilder;
20-
import com.github.dockerjava.core.TestDockerCmdExecFactory;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.io.StringWriter;
18+
import java.lang.reflect.Method;
19+
import java.net.DatagramSocket;
20+
import java.net.ServerSocket;
2121

2222
public abstract class AbstractDockerClientTest extends Assert {
2323

@@ -31,7 +31,14 @@ public abstract class AbstractDockerClientTest extends Assert {
3131
public void beforeTest() {
3232
LOG.info("======================= BEFORETEST =======================");
3333
LOG.info("Connecting to Docker server");
34-
dockerClient = DockerClientBuilder.getInstance()
34+
dockerClient = DockerClientBuilder.getInstance(
35+
DockerClientConfig.createDefaultConfigBuilder()
36+
.withServerAddress("http://localhost:5000")
37+
.withUsername("docker-java")
38+
.withPassword("docker-java")
39+
.withEmail("docker-java@github.com")
40+
.build()
41+
)
3542
.withDockerCmdExecFactory(dockerCmdExecFactory)
3643
.build();
3744

src/test/java/com/github/dockerjava/core/command/AuthCmdImplTest.java

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

3-
import java.lang.reflect.Method;
4-
5-
import org.testng.ITestResult;
6-
import org.testng.annotations.AfterMethod;
7-
import org.testng.annotations.AfterTest;
8-
import org.testng.annotations.BeforeMethod;
9-
import org.testng.annotations.BeforeTest;
10-
import org.testng.annotations.Test;
11-
123
import com.github.dockerjava.api.DockerClient;
134
import com.github.dockerjava.api.DockerException;
145
import com.github.dockerjava.api.UnauthorizedException;
6+
import com.github.dockerjava.api.model.AuthResponse;
157
import com.github.dockerjava.client.AbstractDockerClientTest;
168
import com.github.dockerjava.core.DockerClientBuilder;
179
import com.github.dockerjava.core.DockerClientConfig;
10+
import org.testng.ITestResult;
11+
import org.testng.annotations.*;
12+
13+
import java.lang.reflect.Method;
14+
15+
import static org.hamcrest.MatcherAssert.assertThat;
16+
import static org.hamcrest.core.IsNot.not;
17+
import static org.hamcrest.core.StringContains.containsString;
1818

1919
@Test(groups = "integration")
2020
public class AuthCmdImplTest extends AbstractDockerClientTest {
@@ -41,8 +41,10 @@ public void afterMethod(ITestResult result) {
4141

4242
@Test
4343
public void testAuth() throws Exception {
44-
dockerClient.authCmd().exec();
45-
}
44+
AuthResponse response = dockerClient.authCmd().exec();
45+
46+
assertThat(response.getStatus(), not(containsString("Account created")));
47+
}
4648

4749
@Test
4850
public void testAuthInvalid() throws Exception {

0 commit comments

Comments
 (0)