Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
AuthCmd now returns AuthResponse so you can tell if you need to autho…
…rise you newly registered account
  • Loading branch information
alexec committed Nov 2, 2014
commit 60dee43ad1f03baaf846048e7b14dccac204092d
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ Developer forum for [docker-java](https://groups.google.com/forum/?hl=de#!forum/

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

docker run -p 5000:5000 registry
$ docker run -p 5000:5000 registry

If you're using boot2docker, set-up a port forward:

$ VBoxManage controlvm boot2docker-vm natpf1 "5000,tcp,127.0.0.1,5000,,5000"

You can remove this forward later using:

$ VBoxManage controlvm boot2docker-vm natpf1 delete 5000

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:

$ mvn clean install -Ddocker.io.username=... -Ddocker.io.password=... -Ddocker.io.email=...

_If your Docker server is remote, add its URL like this: `-Ddocker.io.url=https://...:2376`._
$ mvn clean install

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:

Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/github/dockerjava/api/command/AuthCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@

import com.github.dockerjava.api.UnauthorizedException;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.AuthResponse;

/**
*
* Authenticate with the server, useful for checking authentication.
*
*/
public interface AuthCmd extends DockerCmd<Void> {
public interface AuthCmd extends DockerCmd<AuthResponse> {

public AuthConfig getAuthConfig();

public AuthCmd withAuthConfig(AuthConfig authConfig);


/**
* @return The status. Based on it's value you may mean you need to authorise your account, e.g.:
* "Account created. Please see the documentation of the registry http://localhost:5000/v1/ for instructions how to activate it."
* @throws UnauthorizedException If you're not authorised (e.g. bad password).
*/
@Override
public Void exec() throws UnauthorizedException;
public AuthResponse exec() throws UnauthorizedException;

public static interface Exec extends DockerCmdExec<AuthCmd, Void> {
public static interface Exec extends DockerCmdExec<AuthCmd, AuthResponse> {
}

}
12 changes: 12 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/AuthResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.dockerjava.api.model;

import com.fasterxml.jackson.annotation.JsonProperty;

public class AuthResponse {
@JsonProperty("Status")
private String status;

public String getStatus() {
return status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
import com.github.dockerjava.api.UnauthorizedException;
import com.github.dockerjava.api.command.AuthCmd;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.AuthResponse;

/**
*
* Authenticate with the server, useful for checking authentication.
*
*/
public class AuthCmdImpl extends AbstrAuthCfgDockerCmd<AuthCmd, Void> implements AuthCmd {
public class AuthCmdImpl extends AbstrAuthCfgDockerCmd<AuthCmd, AuthResponse> implements AuthCmd {

public AuthCmdImpl(AuthCmd.Exec exec, AuthConfig authConfig) {
super(exec);
withAuthConfig(authConfig);
}

@Override
public Void exec() throws UnauthorizedException {
public AuthResponse exec() throws UnauthorizedException {
return super.exec();
}

@Override
public String toString() {
return "authenticate using " + this.getAuthConfig();
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/com/github/dockerjava/jaxrs/AuthCmdExec.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.github.dockerjava.jaxrs;

import static javax.ws.rs.client.Entity.entity;
import com.github.dockerjava.api.UnauthorizedException;
import com.github.dockerjava.api.command.AuthCmd;
import com.github.dockerjava.api.model.AuthResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.dockerjava.api.UnauthorizedException;
import com.github.dockerjava.api.command.AuthCmd;
import static javax.ws.rs.client.Entity.entity;

public class AuthCmdExec extends AbstrDockerCmdExec<AuthCmd, Void> implements AuthCmd.Exec {
public class AuthCmdExec extends AbstrDockerCmdExec<AuthCmd,AuthResponse> implements AuthCmd.Exec {

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

@Override
protected Void execute(AuthCmd command) {
protected AuthResponse execute(AuthCmd command) {
WebTarget webResource = getBaseResource().path("/auth");
LOGGER.trace("POST: {}", webResource);
Response response = webResource.request().accept(MediaType.APPLICATION_JSON).post(entity(command.getAuthConfig(), MediaType.APPLICATION_JSON));
Response response = webResource
.request()
.accept(MediaType.APPLICATION_JSON).post(entity(command.getAuthConfig(), MediaType.APPLICATION_JSON));

if(response.getStatus() == 401) {
throw new UnauthorizedException("Unauthorized");
};
return null;
}

return response.readEntity(AuthResponse.class);
}

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package com.github.dockerjava.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.net.DatagramSocket;
import java.net.ServerSocket;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.DockerException;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.TestDockerCmdExecFactory;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.ITestResult;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.DockerException;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.TestDockerCmdExecFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.net.DatagramSocket;
import java.net.ServerSocket;

public abstract class AbstractDockerClientTest extends Assert {

Expand All @@ -31,7 +31,14 @@ public abstract class AbstractDockerClientTest extends Assert {
public void beforeTest() {
LOG.info("======================= BEFORETEST =======================");
LOG.info("Connecting to Docker server");
dockerClient = DockerClientBuilder.getInstance()
dockerClient = DockerClientBuilder.getInstance(
DockerClientConfig.createDefaultConfigBuilder()
.withServerAddress("http://localhost:5000")
.withUsername("docker-java")
.withPassword("docker-java")
.withEmail("docker-java@github.com")
.build()
)
.withDockerCmdExecFactory(dockerCmdExecFactory)
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.github.dockerjava.core.command;

import java.lang.reflect.Method;

import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.DockerException;
import com.github.dockerjava.api.UnauthorizedException;
import com.github.dockerjava.api.model.AuthResponse;
import com.github.dockerjava.client.AbstractDockerClientTest;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import org.testng.ITestResult;
import org.testng.annotations.*;

import java.lang.reflect.Method;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.StringContains.containsString;

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

@Test
public void testAuth() throws Exception {
dockerClient.authCmd().exec();
}
AuthResponse response = dockerClient.authCmd().exec();

assertThat(response.getStatus(), not(containsString("Account created")));
}

@Test
public void testAuthInvalid() throws Exception {
Expand Down