Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
62 changes: 62 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/AuthConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.dockerjava.api.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

public class AuthConfig {
Expand All @@ -23,6 +24,8 @@ public class AuthConfig {
@JsonProperty("serveraddress")
private String serverAddress = DEFAULT_SERVER_ADDRESS;

private String auth;

public String getUsername() {
return username;
}
Expand Down Expand Up @@ -55,6 +58,16 @@ public void setServerAddress(String serverAddress) {
this.serverAddress = serverAddress;
}

@JsonIgnore
public String getAuth() {
return auth;
}

@JsonProperty("auth")
public void setAuth(String auth) {
this.auth = auth;
}

@Override
public String toString() {
return "AuthConfig{" +
Expand All @@ -64,4 +77,53 @@ public String toString() {
", serverAddress='" + serverAddress + '\'' +
'}';
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((auth == null) ? 0 : auth.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
result = prime * result + ((serverAddress == null) ? 0 : serverAddress.hashCode());
result = prime * result + ((username == null) ? 0 : username.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AuthConfig other = (AuthConfig) obj;
if (auth == null) {
if (other.auth != null)
return false;
} else if (!auth.equals(other.auth))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (serverAddress == null) {
if (other.serverAddress != null)
return false;
} else if (!serverAddress.equals(other.serverAddress))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
}
146 changes: 146 additions & 0 deletions src/main/java/com/github/dockerjava/core/AuthConfigFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.github.dockerjava.core;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.model.AuthConfig;

public class AuthConfigFile {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final TypeReference<Map<String, AuthConfig>> CONFIG_MAP_TYPE = new TypeReference<Map<String, AuthConfig>>() {};
private final Map<String, AuthConfig> authConfigMap;

public AuthConfigFile() {
authConfigMap = new HashMap<String, AuthConfig>();
}

void addConfig(AuthConfig config) {
authConfigMap.put(config.getServerAddress(), config);
}

public AuthConfig resolveAuthConfig(String hostname) {
if (StringUtils.isEmpty(hostname) || AuthConfig.DEFAULT_SERVER_ADDRESS.equals(hostname)) {
return authConfigMap.get(AuthConfig.DEFAULT_SERVER_ADDRESS);
}
AuthConfig c = authConfigMap.get(hostname);
if (c != null) {
return c;
}

// Maybe they have a legacy config file, we will iterate the keys converting
// them to the new format and testing
String normalizedHostname = convertToHostname(hostname);
for (Map.Entry<String, AuthConfig> entry : authConfigMap.entrySet()) {
String registry = entry.getKey();
AuthConfig config = entry.getValue();
if (convertToHostname(registry).equals(normalizedHostname)) {
return config;
}
}
return null;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((authConfigMap == null) ? 0 : authConfigMap.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AuthConfigFile other = (AuthConfigFile) obj;
if (authConfigMap == null) {
if (other.authConfigMap != null)
return false;
} else if (!authConfigMap.equals(other.authConfigMap))
return false;
return true;
}

@Override
public String toString() {
return "AuthConfigFile [authConfigMap=" + authConfigMap + "]";
}


public static AuthConfigFile loadConfig(File confFile) throws IOException {
AuthConfigFile configFile = new AuthConfigFile();
if (!confFile.exists()) {
return new AuthConfigFile();
}
Map<String, AuthConfig> configMap = null;
try {
configMap = MAPPER.readValue(confFile, CONFIG_MAP_TYPE);
} catch (IOException e) {
// pass
}
if (configMap != null) {
for (Map.Entry<String, AuthConfig> entry : configMap.entrySet()) {
AuthConfig authConfig = entry.getValue();
decodeAuth(authConfig.getAuth(), authConfig);
authConfig.setAuth(null);
authConfig.setServerAddress(entry.getKey());
configFile.addConfig(authConfig);
}
} else {
List<String> authFileContent = FileUtils.readLines(confFile);
if (authFileContent.size() < 2) {
throw new IOException("The Auth Config file is empty");
}
AuthConfig config = new AuthConfig();
String[] origAuth = authFileContent.get(0).split(" = ");
if (origAuth.length != 2) {
throw new IOException("Invalid Auth config file");
}
decodeAuth(origAuth[1], config);

String[] origEmail = authFileContent.get(1).split(" = ");
if (origEmail.length != 2) {
throw new IOException("Invalid Auth config file");
}
config.setEmail(origEmail[1]);
configFile.addConfig(config);
}
return configFile;

}

static void decodeAuth(String auth, AuthConfig config) throws IOException {
String str = new String(Base64.decodeBase64(auth), Charset.forName("UTF-8"));
String[] parts = str.split(":", 2);
if (parts.length != 2) {
throw new IOException("Invalid auth configuration file");
}
config.setUsername(parts[0]);
config.setPassword(parts[1]);
}

static String convertToHostname(String server) {
String stripped = server;
if (server.startsWith("http://")) {
stripped = server.substring(7);
} else if (server.startsWith("https://")) {
stripped = server.substring(8);
}
String[] numParts = stripped.split("/", 2);
return numParts[0];
}
}
34 changes: 27 additions & 7 deletions src/main/java/com/github/dockerjava/core/DockerClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class DockerClientConfig {
// this is really confusing, as there are two ways to spell it
private static final String DOCKER_IO_ENABLE_LOGGING_FILTER_PROPERTY = "docker.io.enableLoggingFilter";
private static final String DOCKER_IO_DOCKER_CERT_PATH_PROPERTY = "docker.io.dockerCertPath";
private static final String DOCKER_IO_DOCKER_CFG_PATH_PROPERTY = "docker.io.dockerCfgPath";
/**
* A map from the environment name to the interval name.
*/
Expand All @@ -36,21 +37,23 @@ public class DockerClientConfig {
.put("DOCKER_READ_TIMEOUT", DOCKER_IO_READ_TIMEOUT_PROPERTY)
.put("DOCKER_LOGGING_FILTER_ENABLED", DOCKER_IO_ENABLE_LOGGING_FILTER_PROPERTY)
.put(DOCKER_CERT_PATH_PROPERTY, DOCKER_IO_DOCKER_CERT_PATH_PROPERTY)
.put("DOCKER_CFG_PATH", DOCKER_IO_DOCKER_CFG_PATH_PROPERTY)
.build();
private static final String DOCKER_IO_PROPERTIES_PROPERTY = "docker.io.properties";
private final URI uri;
private final String version, username, password, email, serverAddress, dockerCertPath;
private final String version, username, password, email, serverAddress, dockerCertPath, dockerCfgPath;
private final Integer readTimeout;
private final boolean loggingFilterEnabled;

DockerClientConfig(URI uri, String version, String username, String password, String email, String serverAddress, String dockerCertPath, Integer readTimeout, boolean loggingFilterEnabled) {
DockerClientConfig(URI uri, String version, String username, String password, String email, String serverAddress, String dockerCertPath, String dockerCfgPath, Integer readTimeout, boolean loggingFilterEnabled) {
this.uri = uri;
this.version = version;
this.username = username;
this.password = password;
this.email = email;
this.serverAddress = serverAddress;
this.dockerCertPath = dockerCertPath;
this.dockerCfgPath = dockerCfgPath;
this.readTimeout = readTimeout;
this.loggingFilterEnabled = loggingFilterEnabled;
}
Expand Down Expand Up @@ -153,6 +156,7 @@ private static Properties overrideDockerPropertiesWithSystemProperties(Propertie
DOCKER_IO_READ_TIMEOUT_PROPERTY,
DOCKER_IO_ENABLE_LOGGING_FILTER_PROPERTY,
DOCKER_IO_DOCKER_CERT_PATH_PROPERTY,
DOCKER_IO_DOCKER_CFG_PATH_PROPERTY,
}) {
if (systemProperties.containsKey(key)) {
overriddenProperties.setProperty(key, systemProperties.getProperty(key));
Expand Down Expand Up @@ -212,6 +216,10 @@ public String getDockerCertPath() {
return dockerCertPath;
}

public String getDockerCfgPath() {
return dockerCfgPath;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -222,6 +230,8 @@ public boolean equals(Object o) {
if (loggingFilterEnabled != that.loggingFilterEnabled) return false;
if (dockerCertPath != null ? !dockerCertPath.equals(that.dockerCertPath) : that.dockerCertPath != null)
return false;
if (dockerCfgPath != null ? !dockerCfgPath.equals(that.dockerCfgPath) : that.dockerCfgPath != null)
return false;
if (email != null ? !email.equals(that.email) : that.email != null) return false;
if (password != null ? !password.equals(that.password) : that.password != null) return false;
if (readTimeout != null ? !readTimeout.equals(that.readTimeout) : that.readTimeout != null) return false;
Expand All @@ -243,6 +253,7 @@ public int hashCode() {
result = 31 * result + (email != null ? email.hashCode() : 0);
result = 31 * result + (serverAddress != null ? serverAddress.hashCode() : 0);
result = 31 * result + (dockerCertPath != null ? dockerCertPath.hashCode() : 0);
result = 31 * result + (dockerCfgPath != null ? dockerCfgPath.hashCode() : 0);
result = 31 * result + (readTimeout != null ? readTimeout.hashCode() : 0);
result = 31 * result + (loggingFilterEnabled ? 1 : 0);
return result;
Expand All @@ -258,22 +269,23 @@ public String toString() {
", email='" + email + '\'' +
", serverAddress='" + serverAddress + '\'' +
", dockerCertPath='" + dockerCertPath + '\'' +
", dockerCfgPath='" + dockerCfgPath + '\'' +
", readTimeout=" + readTimeout +
", loggingFilterEnabled=" + loggingFilterEnabled +
'}';
}

public static class DockerClientConfigBuilder {
private URI uri;
private String version, username, password, email, serverAddress, dockerCertPath;
private String version, username, password, email, serverAddress, dockerCertPath, dockerCfgPath;
private Integer readTimeout;
private boolean loggingFilterEnabled;

/**
* This will set all fields in the builder to those contained in the Properties object. The Properties object
* should contain the following docker.io.* keys: url, version, username, password, email, and dockerCertPath. If
* docker.io.readTimeout or docker.io.enableLoggingFilter are not contained, they will be set to 1000 and true,
* respectively.
* should contain the following docker.io.* keys: url, version, username, password, email, dockerCertPath, and
* dockerCfgPath. If docker.io.readTimeout or docker.io.enableLoggingFilter are not contained, they will be set
* to 1000 and true, respectively.
*/
public DockerClientConfigBuilder withProperties(Properties p) {
return withUri(p.getProperty(DOCKER_IO_URL_PROPERTY))
Expand All @@ -284,7 +296,8 @@ public DockerClientConfigBuilder withProperties(Properties p) {
.withServerAddress(p.getProperty(DOCKER_IO_SERVER_ADDRESS_PROPERTY))
.withReadTimeout(Integer.valueOf(p.getProperty(DOCKER_IO_READ_TIMEOUT_PROPERTY, "0")))
.withLoggingFilter(Boolean.valueOf(p.getProperty(DOCKER_IO_ENABLE_LOGGING_FILTER_PROPERTY, "true")))
.withDockerCertPath(p.getProperty(DOCKER_IO_DOCKER_CERT_PATH_PROPERTY));
.withDockerCertPath(p.getProperty(DOCKER_IO_DOCKER_CERT_PATH_PROPERTY))
.withDockerCfgPath(p.getProperty(DOCKER_IO_DOCKER_CFG_PATH_PROPERTY));
}

public final DockerClientConfigBuilder withUri(String uri) {
Expand Down Expand Up @@ -333,6 +346,12 @@ public final DockerClientConfigBuilder withDockerCertPath(String dockerCertPath)
return this;
}

public final DockerClientConfigBuilder withDockerCfgPath(String dockerCfgPath) {
this.dockerCfgPath = dockerCfgPath;
return this;
}


public DockerClientConfig build() {
return new DockerClientConfig(
uri,
Expand All @@ -342,6 +361,7 @@ public DockerClientConfig build() {
email,
serverAddress,
dockerCertPath,
dockerCfgPath,
readTimeout,
loggingFilterEnabled
);
Expand Down
Loading