Skip to content

Commit

Permalink
181: Fix bug when authenticating to private registry, add "serveraddr…
Browse files Browse the repository at this point in the history
…ess" to authentication header. Signed-off-by: Jorge Davison <jdavisonc@gmail.com>
  • Loading branch information
Jorge Davison authored and jdavisonc committed Jul 24, 2015
1 parent b4ba873 commit c28007f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/main/java/org/jolokia/docker/maven/access/AuthConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@
*/
public class AuthConfig {

public final static AuthConfig EMPTY_AUTH_CONFIG = new AuthConfig("", "", "", "");
public final static AuthConfig EMPTY_AUTH_CONFIG = new AuthConfig("", "", "", "", "");

private Map<String,String> params;

public AuthConfig(Map<String,String> params) {
this.params = params;
}

public AuthConfig(String user, String password, String email,String auth) {
public AuthConfig(String user, String password, String email, String auth, String registry) {
params = new HashMap<>();
putNonNull(params, "username", user);
putNonNull(params, "password", password);
putNonNull(params, "email", email);
putNonNull(params, "auth", auth);
putNonNull(params, "serveraddress", registry);
}

public String toHeaderValue() {
Expand All @@ -38,6 +39,7 @@ public String toHeaderValue() {
add(ret,"password");
add(ret,"email");
add(ret,"auth");
add(ret,"serveraddress");
try {
return Base64.encodeBase64String(ret.toString().getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/org/jolokia/docker/maven/util/AuthConfigFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,18 @@ public AuthConfigFactory(PlexusContainer container) {
public AuthConfig createAuthConfig(Map authConfig, Settings settings, String user, String registry) throws MojoExecutionException {
Properties props = System.getProperties();
if (props.containsKey(DOCKER_USERNAME) || props.containsKey(DOCKER_PASSWORD)) {
return getAuthConfigFromProperties(props);
return getAuthConfigFromProperties(props, registry);
}
if (authConfig != null) {
return getAuthConfigFromPluginConfiguration(authConfig);
return getAuthConfigFromPluginConfiguration(authConfig,registry);
}
return getAuthConfigFromSettings(settings,user,registry);
}

// ===================================================================================================

private AuthConfig getAuthConfigFromProperties(Properties props) throws MojoExecutionException {
private AuthConfig getAuthConfigFromProperties(Properties props, String registry) throws
MojoExecutionException {
if (!props.containsKey(DOCKER_USERNAME)) {
throw new MojoExecutionException("No " + DOCKER_USERNAME + " given when using authentication");
}
Expand All @@ -92,17 +93,18 @@ private AuthConfig getAuthConfigFromProperties(Properties props) throws MojoExec
return new AuthConfig(props.getProperty(DOCKER_USERNAME),
decrypt(props.getProperty(DOCKER_PASSWORD)),
props.getProperty(DOCKER_EMAIL),
props.getProperty(DOCKER_AUTH));
props.getProperty(DOCKER_AUTH), registry);
}

private AuthConfig getAuthConfigFromPluginConfiguration(Map authConfig) throws MojoExecutionException {
private AuthConfig getAuthConfigFromPluginConfiguration(Map authConfig, String registry) throws MojoExecutionException {
for (String key : new String[] { "username", "password"}) {
if (!authConfig.containsKey(key)) {
throw new MojoExecutionException("No '" + key + "' given while using <authConfig> in configuration");
}
}
Map<String,String> cloneConfig = new HashMap<String,String>(authConfig);
cloneConfig.put("password",decrypt(cloneConfig.get("password")));
cloneConfig.put("serveraddress",registry);
return new AuthConfig(cloneConfig);
}

Expand All @@ -117,10 +119,10 @@ private AuthConfig getAuthConfigFromSettings(Settings settings, String user, Str
}
found = checkForServer(server, id, registry, user);
if (found != null) {
return createAuthConfigFromServer(found);
return createAuthConfigFromServer(found, registry);
}
}
return defaultServer != null ? createAuthConfigFromServer(defaultServer) : null;
return defaultServer != null ? createAuthConfigFromServer(defaultServer, registry) : null;
}

private Server checkForServer(Server server, String id, String registry, String user) {
Expand All @@ -147,12 +149,13 @@ private String decrypt(String password) throws MojoExecutionException {
}
}

private AuthConfig createAuthConfigFromServer(Server server) throws MojoExecutionException {
private AuthConfig createAuthConfigFromServer(Server server, String registry) throws MojoExecutionException {
return new AuthConfig(
server.getUsername(),
decrypt(server.getPassword()),
extractFromServerConfiguration(server.getConfiguration(), "email"),
extractFromServerConfiguration(server.getConfiguration(), "auth")
extractFromServerConfiguration(server.getConfiguration(), "auth"),
registry
);
}

Expand Down
11 changes: 7 additions & 4 deletions src/test/java/org/jolokia/docker/maven/util/AuthConfigTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.jolokia.docker.maven.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.jolokia.docker.maven.access.AuthConfig;
import org.json.JSONObject;
import org.junit.Test;

import static org.junit.Assert.*;

/**
* @author roland
* @since 30.07.14
Expand All @@ -23,13 +23,15 @@ public void simpleConstructor() {
map.put("username","roland");
map.put("password","secret");
map.put("email","roland@jolokia.org");
map.put("serveraddress","private.registry.com");
AuthConfig config = new AuthConfig(map);
check(config);
}

@Test
public void mapConstructor() {
AuthConfig config = new AuthConfig("roland","secret","roland@jolokia.org",null);
AuthConfig config = new AuthConfig("roland","secret","roland@jolokia.org",null,
"private.registry.com");
check(config);
}

Expand All @@ -39,6 +41,7 @@ private void check(AuthConfig config) {
assertEquals("roland",data.getString("username"));
assertEquals("secret",data.getString("password"));
assertEquals("roland@jolokia.org",data.getString("email"));
assertEquals("private.registry.com",data.getString("serveraddress"));
assertFalse(data.has("auth"));
}
}

0 comments on commit c28007f

Please sign in to comment.