diff --git a/pom.xml b/pom.xml index bee178a6d..3f9caa55a 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.github.docker-java docker-java jar - 3.0.1-SNAPSHOT + 3.0.0-kostyasha-3 docker-java https://github.com/docker-java/docker-java diff --git a/src/main/java/com/github/dockerjava/api/command/DockerCmdExecFactory.java b/src/main/java/com/github/dockerjava/api/command/DockerCmdExecFactory.java index ae090e434..0de3afe38 100644 --- a/src/main/java/com/github/dockerjava/api/command/DockerCmdExecFactory.java +++ b/src/main/java/com/github/dockerjava/api/command/DockerCmdExecFactory.java @@ -3,8 +3,6 @@ import java.io.Closeable; import java.io.IOException; -import javax.net.ssl.SSLContext; - import com.github.dockerjava.core.DockerClientConfig; import com.github.dockerjava.core.RemoteApiVersion; @@ -117,8 +115,6 @@ public interface DockerCmdExecFactory extends Closeable { DisconnectFromNetworkCmd.Exec createDisconnectFromNetworkCmdExec(); - DockerCmdExecFactory withSSLContext(SSLContext sslContext); - @Override void close() throws IOException; diff --git a/src/main/java/com/github/dockerjava/core/DefaultDockerClientConfig.java b/src/main/java/com/github/dockerjava/core/DefaultDockerClientConfig.java new file mode 100644 index 000000000..4641f9d3c --- /dev/null +++ b/src/main/java/com/github/dockerjava/core/DefaultDockerClientConfig.java @@ -0,0 +1,455 @@ +package com.github.dockerjava.core; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Serializable; +import java.net.URI; +import java.util.HashSet; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; + +import com.github.dockerjava.api.exception.DockerClientException; +import com.github.dockerjava.api.model.AuthConfig; +import com.github.dockerjava.api.model.AuthConfigurations; +import com.github.dockerjava.core.NameParser.HostnameReposName; +import com.github.dockerjava.core.NameParser.ReposTag; + +/** + * Respects some of the docker CLI options. See https://docs.docker.com/engine/reference/commandline/cli/#environment-variables + */ +public class DefaultDockerClientConfig implements Serializable, DockerClientConfig { + + private static final long serialVersionUID = -4307357472441531489L; + + public static final String DOCKER_HOST = "DOCKER_HOST"; + + public static final String DOCKER_TLS_VERIFY = "DOCKER_TLS_VERIFY"; + + public static final String DOCKER_CONFIG = "DOCKER_CONFIG"; + + public static final String DOCKER_CERT_PATH = "DOCKER_CERT_PATH"; + + public static final String API_VERSION = "api.version"; + + public static final String REGISTRY_USERNAME = "registry.username"; + + public static final String REGISTRY_PASSWORD = "registry.password"; + + public static final String REGISTRY_EMAIL = "registry.email"; + + public static final String REGISTRY_URL = "registry.url"; + + private static final String DOCKER_JAVA_PROPERTIES = "docker-java.properties"; + + private static final String DOCKER_CFG = ".dockercfg"; + + private static final Set CONFIG_KEYS = new HashSet(); + + static { + CONFIG_KEYS.add(DOCKER_HOST); + CONFIG_KEYS.add(DOCKER_TLS_VERIFY); + CONFIG_KEYS.add(DOCKER_CONFIG); + CONFIG_KEYS.add(DOCKER_CERT_PATH); + CONFIG_KEYS.add(API_VERSION); + CONFIG_KEYS.add(REGISTRY_USERNAME); + CONFIG_KEYS.add(REGISTRY_PASSWORD); + CONFIG_KEYS.add(REGISTRY_EMAIL); + CONFIG_KEYS.add(REGISTRY_URL); + } + + private final URI dockerHost; + + private final String registryUsername, registryPassword, registryEmail, registryUrl, dockerConfig; + + private final SSLConfig sslConfig; + + private final RemoteApiVersion apiVersion; + + DefaultDockerClientConfig(URI dockerHost, String dockerConfig, String apiVersion, String registryUrl, + String registryUsername, String registryPassword, String registryEmail, SSLConfig sslConfig) { + this.dockerHost = checkDockerHostScheme(dockerHost); + this.dockerConfig = dockerConfig; + this.apiVersion = RemoteApiVersion.parseConfigWithDefault(apiVersion); + this.sslConfig = sslConfig; + this.registryUsername = registryUsername; + this.registryPassword = registryPassword; + this.registryEmail = registryEmail; + this.registryUrl = registryUrl; + } + + private URI checkDockerHostScheme(URI dockerHost) { + if ("tcp".equals(dockerHost.getScheme()) || "unix".equals(dockerHost.getScheme())) { + return dockerHost; + } else { + throw new DockerClientException("Unsupported protocol scheme found: '" + dockerHost + + "'. Only 'tcp://' or 'unix://' supported."); + } + } + + private static Properties loadIncludedDockerProperties(Properties systemProperties) { + try (InputStream is = DefaultDockerClientConfig.class.getResourceAsStream("/" + DOCKER_JAVA_PROPERTIES)) { + Properties p = new Properties(); + p.load(is); + replaceProperties(p, systemProperties); + return p; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static void replaceProperties(Properties properties, Properties replacements) { + for (Object objectKey : properties.keySet()) { + String key = objectKey.toString(); + properties.setProperty(key, replaceProperties(properties.getProperty(key), replacements)); + } + } + + private static String replaceProperties(String s, Properties replacements) { + for (Map.Entry entry : replacements.entrySet()) { + String key = "${" + entry.getKey() + "}"; + while (s.contains(key)) { + s = s.replace(key, String.valueOf(entry.getValue())); + } + } + return s; + } + + /** + * Creates a new Properties object containing values overridden from ${user.home}/.docker.io.properties + * + * @param p + * The original set of properties to override + * @return A copy of the original Properties with overridden values + */ + private static Properties overrideDockerPropertiesWithSettingsFromUserHome(Properties p, Properties systemProperties) { + Properties overriddenProperties = new Properties(); + overriddenProperties.putAll(p); + + final File usersDockerPropertiesFile = new File(systemProperties.getProperty("user.home"), + "." + DOCKER_JAVA_PROPERTIES); + if (usersDockerPropertiesFile.isFile()) { + try (FileInputStream in = new FileInputStream(usersDockerPropertiesFile)) { + overriddenProperties.load(in); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + return overriddenProperties; + } + + private static Properties overrideDockerPropertiesWithEnv(Properties properties, Map env) { + Properties overriddenProperties = new Properties(); + overriddenProperties.putAll(properties); + + // special case which is a sensible default + if (env.containsKey(DOCKER_HOST)) { + overriddenProperties.setProperty(DOCKER_HOST, env.get(DOCKER_HOST)); + } + + for (Map.Entry envEntry : env.entrySet()) { + String envKey = envEntry.getKey(); + if (CONFIG_KEYS.contains(envKey)) { + overriddenProperties.setProperty(envKey, envEntry.getValue()); + } + } + + return overriddenProperties; + } + + /** + * Creates a new Properties object containing values overridden from the System properties + * + * @param p + * The original set of properties to override + * @return A copy of the original Properties with overridden values + */ + private static Properties overrideDockerPropertiesWithSystemProperties(Properties p, Properties systemProperties) { + Properties overriddenProperties = new Properties(); + overriddenProperties.putAll(p); + + for (String key : CONFIG_KEYS) { + if (systemProperties.containsKey(key)) { + overriddenProperties.setProperty(key, systemProperties.getProperty(key)); + } + } + return overriddenProperties; + } + + public static Builder createDefaultConfigBuilder() { + return createDefaultConfigBuilder(System.getenv(), System.getProperties()); + } + + /** + * Allows you to build the config without system environment interfering for more robust testing + */ + static Builder createDefaultConfigBuilder(Map env, Properties systemProperties) { + Properties properties = loadIncludedDockerProperties(systemProperties); + properties = overrideDockerPropertiesWithSettingsFromUserHome(properties, systemProperties); + properties = overrideDockerPropertiesWithEnv(properties, env); + properties = overrideDockerPropertiesWithSystemProperties(properties, systemProperties); + return new Builder().withProperties(properties); + } + + @Override + public URI getDockerHost() { + return dockerHost; + } + + @Override + public RemoteApiVersion getApiVersion() { + return apiVersion; + } + + @Override + public String getRegistryUsername() { + return registryUsername; + } + + @Override + public String getRegistryPassword() { + return registryPassword; + } + + @Override + public String getRegistryEmail() { + return registryEmail; + } + + @Override + public String getRegistryUrl() { + return registryUrl; + } + + public String getDockerConfig() { + return dockerConfig; + } + + private AuthConfig getAuthConfig() { + AuthConfig authConfig = null; + if (getRegistryUsername() != null && getRegistryPassword() != null && getRegistryEmail() != null + && getRegistryUrl() != null) { + authConfig = new AuthConfig() + .withUsername(getRegistryUsername()) + .withPassword(getRegistryPassword()) + .withEmail(getRegistryEmail()) + .withRegistryAddress(getRegistryUrl()); + } + return authConfig; + } + + @Override + public AuthConfig effectiveAuthConfig(String imageName) { + AuthConfig authConfig = null; + + File dockerCfgFile = new File(getDockerConfig() + File.separator + DOCKER_CFG); + + if (dockerCfgFile.exists() && dockerCfgFile.isFile() && imageName != null) { + AuthConfigFile authConfigFile; + try { + authConfigFile = AuthConfigFile.loadConfig(dockerCfgFile); + } catch (IOException e) { + throw new DockerClientException("Failed to parse dockerCfgFile", e); + } + ReposTag reposTag = NameParser.parseRepositoryTag(imageName); + HostnameReposName hostnameReposName = NameParser.resolveRepositoryName(reposTag.repos); + + authConfig = authConfigFile.resolveAuthConfig(hostnameReposName.hostname); + } + + AuthConfig otherAuthConfig = getAuthConfig(); + + if (otherAuthConfig != null) { + authConfig = otherAuthConfig; + } + + return authConfig; + } + + @Override + public AuthConfigurations getAuthConfigurations() { + File dockerCfgFile = new File(getDockerConfig() + File.separator + DOCKER_CFG); + if (dockerCfgFile.exists() && dockerCfgFile.isFile()) { + AuthConfigFile authConfigFile; + try { + authConfigFile = AuthConfigFile.loadConfig(dockerCfgFile); + } catch (IOException e) { + throw new DockerClientException("Failed to parse dockerCfgFile", e); + } + + return authConfigFile.getAuthConfigurations(); + } + + return new AuthConfigurations(); + } + + @Override + public SSLConfig getSSLConfig() { + return sslConfig; + } + + @Override + public boolean equals(Object o) { + return EqualsBuilder.reflectionEquals(this, o); + } + + @Override + public int hashCode() { + return HashCodeBuilder.reflectionHashCode(this); + } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); + } + + public static class Builder { + private URI dockerHost; + + private String apiVersion, registryUsername, registryPassword, registryEmail, registryUrl, dockerConfig, + dockerCertPath; + + private boolean dockerTlsVerify; + + private SSLConfig customSslConfig = null; + + /** + * This will set all fields in the builder to those contained in the Properties object. The Properties object should contain the + * following docker-java configuration keys: DOCKER_HOST, DOCKER_TLS_VERIFY, api.version, registry.username, registry.password, + * registry.email, DOCKER_CERT_PATH, and DOCKER_CONFIG. + */ + public Builder withProperties(Properties p) { + return withDockerHost(p.getProperty(DOCKER_HOST)) + .withDockerTlsVerify(p.getProperty(DOCKER_TLS_VERIFY)) + .withDockerConfig(p.getProperty(DOCKER_CONFIG)) + .withDockerCertPath(p.getProperty(DOCKER_CERT_PATH)) + .withApiVersion(p.getProperty(API_VERSION)) + .withRegistryUsername(p.getProperty(REGISTRY_USERNAME)) + .withRegistryPassword(p.getProperty(REGISTRY_PASSWORD)) + .withRegistryEmail(p.getProperty(REGISTRY_EMAIL)) + .withRegistryUrl(p.getProperty(REGISTRY_URL)); + } + + /** + * configure DOCKER_HOST + */ + public final Builder withDockerHost(String dockerHost) { + checkNotNull(dockerHost, "uri was not specified"); + this.dockerHost = URI.create(dockerHost); + return this; + } + + public final Builder withApiVersion(RemoteApiVersion apiVersion) { + this.apiVersion = apiVersion.getVersion(); + return this; + } + + public final Builder withApiVersion(String apiVersion) { + this.apiVersion = apiVersion; + return this; + } + + public final Builder withRegistryUsername(String registryUsername) { + this.registryUsername = registryUsername; + return this; + } + + public final Builder withRegistryPassword(String registryPassword) { + this.registryPassword = registryPassword; + return this; + } + + public final Builder withRegistryEmail(String registryEmail) { + this.registryEmail = registryEmail; + return this; + } + + public Builder withRegistryUrl(String registryUrl) { + this.registryUrl = registryUrl; + return this; + } + + public final Builder withDockerCertPath(String dockerCertPath) { + this.dockerCertPath = dockerCertPath; + return this; + } + + public final Builder withDockerConfig(String dockerConfig) { + this.dockerConfig = dockerConfig; + return this; + } + + public final Builder withDockerTlsVerify(String dockerTlsVerify) { + if (dockerTlsVerify != null) { + String trimmed = dockerTlsVerify.trim(); + this.dockerTlsVerify = "true".equalsIgnoreCase(trimmed) || "1".equals(trimmed); + } else { + this.dockerTlsVerify = false; + } + return this; + } + + public final Builder withDockerTlsVerify(Boolean dockerTlsVerify) { + this.dockerTlsVerify = dockerTlsVerify; + return this; + } + + /** + * Overrides the default {@link SSLConfig} that is used when calling {@link Builder#withDockerTlsVerify(java.lang.Boolean)} and + * {@link Builder#withDockerCertPath(String)}. This way it is possible to pass a custom {@link SSLConfig} to the resulting + * {@link DockerClientConfig} that may be created by other means than the local file system. + */ + public final Builder withCustomSslConfig(SSLConfig customSslConfig) { + this.customSslConfig = customSslConfig; + return this; + } + + public DefaultDockerClientConfig build() { + + SSLConfig sslConfig = null; + + if (customSslConfig == null) { + if (dockerTlsVerify) { + dockerCertPath = checkDockerCertPath(dockerCertPath); + sslConfig = new LocalDirectorySSLConfig(dockerCertPath); + } + } else { + sslConfig = customSslConfig; + } + + return new DefaultDockerClientConfig(dockerHost, dockerConfig, apiVersion, registryUrl, registryUsername, + registryPassword, registryEmail, sslConfig); + } + + private String checkDockerCertPath(String dockerCertPath) { + if (StringUtils.isEmpty(dockerCertPath)) { + throw new DockerClientException( + "Enabled TLS verification (DOCKER_TLS_VERIFY=1) but certifate path (DOCKER_CERT_PATH) is not defined."); + } + + File certPath = new File(dockerCertPath); + + if (!certPath.exists()) { + throw new DockerClientException( + "Enabled TLS verification (DOCKER_TLS_VERIFY=1) but certificate path (DOCKER_CERT_PATH) '" + + dockerCertPath + "' doesn't exist."); + } else if (!certPath.isDirectory()) { + throw new DockerClientException( + "Enabled TLS verification (DOCKER_TLS_VERIFY=1) but certificate path (DOCKER_CERT_PATH) '" + + dockerCertPath + "' doesn't point to a directory."); + } + + return dockerCertPath; + } + } +} diff --git a/src/main/java/com/github/dockerjava/core/DockerClientBuilder.java b/src/main/java/com/github/dockerjava/core/DockerClientBuilder.java index 6aed6c358..743b8c2ef 100644 --- a/src/main/java/com/github/dockerjava/core/DockerClientBuilder.java +++ b/src/main/java/com/github/dockerjava/core/DockerClientBuilder.java @@ -2,7 +2,7 @@ import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.command.DockerCmdExecFactory; -import com.github.dockerjava.core.DockerClientConfig.DockerClientConfigBuilder; +import com.github.dockerjava.core.DefaultDockerClientConfig.Builder; import com.github.dockerjava.jaxrs.DockerCmdExecFactoryImpl; public class DockerClientBuilder { @@ -19,7 +19,7 @@ public static DockerClientBuilder getInstance() { return new DockerClientBuilder(DockerClientImpl.getInstance()); } - public static DockerClientBuilder getInstance(DockerClientConfigBuilder dockerClientConfigBuilder) { + public static DockerClientBuilder getInstance(Builder dockerClientConfigBuilder) { return getInstance(dockerClientConfigBuilder.build()); } diff --git a/src/main/java/com/github/dockerjava/core/DockerClientConfig.java b/src/main/java/com/github/dockerjava/core/DockerClientConfig.java index 31cfc0c58..532fe6b43 100644 --- a/src/main/java/com/github/dockerjava/core/DockerClientConfig.java +++ b/src/main/java/com/github/dockerjava/core/DockerClientConfig.java @@ -1,440 +1,40 @@ +/* + * Created on 08.06.2016 + */ package com.github.dockerjava.core; -import static com.google.common.base.Preconditions.checkNotNull; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.Serializable; import java.net.URI; -import java.util.HashSet; -import java.util.Map; -import java.util.Properties; -import java.util.Set; - -import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang.builder.EqualsBuilder; -import org.apache.commons.lang.builder.HashCodeBuilder; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; -import com.github.dockerjava.api.exception.DockerClientException; import com.github.dockerjava.api.model.AuthConfig; import com.github.dockerjava.api.model.AuthConfigurations; -import com.github.dockerjava.core.NameParser.HostnameReposName; -import com.github.dockerjava.core.NameParser.ReposTag; /** - * Respects some of the docker CLI options. See https://docs.docker.com/engine/reference/commandline/cli/#environment-variables + * Interface that describes the docker client configuration. + * + * @author Marcus Linke + * */ -public class DockerClientConfig implements Serializable { - - private static final long serialVersionUID = -4307357472441531489L; - - public static final String DOCKER_HOST = "DOCKER_HOST"; - - public static final String DOCKER_TLS_VERIFY = "DOCKER_TLS_VERIFY"; - - public static final String DOCKER_CONFIG = "DOCKER_CONFIG"; - - public static final String DOCKER_CERT_PATH = "DOCKER_CERT_PATH"; - - public static final String API_VERSION = "api.version"; - - public static final String REGISTRY_USERNAME = "registry.username"; - - public static final String REGISTRY_PASSWORD = "registry.password"; - - public static final String REGISTRY_EMAIL = "registry.email"; - - public static final String REGISTRY_URL = "registry.url"; - - private static final String DOCKER_JAVA_PROPERTIES = "docker-java.properties"; - - private static final String DOCKER_CFG = ".dockercfg"; - - private static final Set CONFIG_KEYS = new HashSet(); - - static { - CONFIG_KEYS.add(DOCKER_HOST); - CONFIG_KEYS.add(DOCKER_TLS_VERIFY); - CONFIG_KEYS.add(DOCKER_CONFIG); - CONFIG_KEYS.add(DOCKER_CERT_PATH); - CONFIG_KEYS.add(API_VERSION); - CONFIG_KEYS.add(REGISTRY_USERNAME); - CONFIG_KEYS.add(REGISTRY_PASSWORD); - CONFIG_KEYS.add(REGISTRY_EMAIL); - CONFIG_KEYS.add(REGISTRY_URL); - } - - private final URI dockerHost; - - private final String registryUsername, registryPassword, registryEmail, registryUrl, dockerConfig, dockerCertPath; +public interface DockerClientConfig { - private final boolean dockerTlsVerify; + URI getDockerHost(); - private final RemoteApiVersion apiVersion; + RemoteApiVersion getApiVersion(); - DockerClientConfig(URI dockerHost, String dockerConfig, String apiVersion, String registryUrl, - String registryUsername, String registryPassword, String registryEmail, String dockerCertPath, - boolean dockerTslVerify) { - this.dockerHost = checkDockerHostScheme(dockerHost); - this.dockerTlsVerify = dockerTslVerify; - this.dockerCertPath = checkDockerCertPath(dockerTslVerify, dockerCertPath); - this.dockerConfig = dockerConfig; - this.apiVersion = RemoteApiVersion.parseConfigWithDefault(apiVersion); - this.registryUsername = registryUsername; - this.registryPassword = registryPassword; - this.registryEmail = registryEmail; - this.registryUrl = registryUrl; - } + String getRegistryUsername(); - private URI checkDockerHostScheme(URI dockerHost) { - if ("tcp".equals(dockerHost.getScheme()) || "unix".equals(dockerHost.getScheme())) { - return dockerHost; - } else { - throw new DockerClientException("Unsupported protocol scheme found: '" + dockerHost - + "'. Only 'tcp://' or 'unix://' supported."); - } - } + String getRegistryPassword(); - private String checkDockerCertPath(boolean dockerTlsVerify, String dockerCertPath) { - if (dockerTlsVerify) { - if (StringUtils.isEmpty(dockerCertPath)) { - throw new DockerClientException( - "Enabled TLS verification (DOCKER_TLS_VERIFY=1) but certifate path (DOCKER_CERT_PATH) is not defined."); - } else { - File certPath = new File(dockerCertPath); + String getRegistryEmail(); - if (!certPath.exists()) { - throw new DockerClientException( - "Enabled TLS verification (DOCKER_TLS_VERIFY=1) but certificate path (DOCKER_CERT_PATH) '" + dockerCertPath - + "' doesn't exist."); - } + String getRegistryUrl(); - if (certPath.isDirectory()) { - return dockerCertPath; - } else { - throw new DockerClientException( - "Enabled TLS verification (DOCKER_TLS_VERIFY=1) but certificate path (DOCKER_CERT_PATH) '" + dockerCertPath - + "' doesn't point to a directory."); - } - } - } else { - return dockerCertPath; - } - } + AuthConfig effectiveAuthConfig(String imageName); - private static Properties loadIncludedDockerProperties(Properties systemProperties) { - try (InputStream is = DockerClientConfig.class.getResourceAsStream("/" + DOCKER_JAVA_PROPERTIES)) { - Properties p = new Properties(); - p.load(is); - replaceProperties(p, systemProperties); - return p; - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - private static void replaceProperties(Properties properties, Properties replacements) { - for (Object objectKey : properties.keySet()) { - String key = objectKey.toString(); - properties.setProperty(key, replaceProperties(properties.getProperty(key), replacements)); - } - } - - private static String replaceProperties(String s, Properties replacements) { - for (Map.Entry entry : replacements.entrySet()) { - String key = "${" + entry.getKey() + "}"; - while (s.contains(key)) { - s = s.replace(key, String.valueOf(entry.getValue())); - } - } - return s; - } - - /** - * Creates a new Properties object containing values overridden from ${user.home}/.docker.io.properties - * - * @param p - * The original set of properties to override - * @return A copy of the original Properties with overridden values - */ - private static Properties overrideDockerPropertiesWithSettingsFromUserHome(Properties p, Properties systemProperties) { - Properties overriddenProperties = new Properties(); - overriddenProperties.putAll(p); - - final File usersDockerPropertiesFile = new File(systemProperties.getProperty("user.home"), - "." + DOCKER_JAVA_PROPERTIES); - if (usersDockerPropertiesFile.isFile()) { - try (FileInputStream in = new FileInputStream(usersDockerPropertiesFile)) { - overriddenProperties.load(in); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - return overriddenProperties; - } - - private static Properties overrideDockerPropertiesWithEnv(Properties properties, Map env) { - Properties overriddenProperties = new Properties(); - overriddenProperties.putAll(properties); - - // special case which is a sensible default - if (env.containsKey(DOCKER_HOST)) { - overriddenProperties.setProperty(DOCKER_HOST, env.get(DOCKER_HOST)); - } - - for (Map.Entry envEntry : env.entrySet()) { - String envKey = envEntry.getKey(); - if (CONFIG_KEYS.contains(envKey)) { - overriddenProperties.setProperty(envKey, envEntry.getValue()); - } - } - - return overriddenProperties; - } + AuthConfigurations getAuthConfigurations(); /** - * Creates a new Properties object containing values overridden from the System properties - * - * @param p - * The original set of properties to override - * @return A copy of the original Properties with overridden values + * Returns an {@link SSLConfig} when secure connection is configured or null if not. */ - private static Properties overrideDockerPropertiesWithSystemProperties(Properties p, Properties systemProperties) { - Properties overriddenProperties = new Properties(); - overriddenProperties.putAll(p); - - for (String key : CONFIG_KEYS) { - if (systemProperties.containsKey(key)) { - overriddenProperties.setProperty(key, systemProperties.getProperty(key)); - } - } - return overriddenProperties; - } - - public static DockerClientConfigBuilder createDefaultConfigBuilder() { - return createDefaultConfigBuilder(System.getenv(), System.getProperties()); - } - - /** - * Allows you to build the config without system environment interfering for more robust testing - */ - static DockerClientConfigBuilder createDefaultConfigBuilder(Map env, Properties systemProperties) { - Properties properties = loadIncludedDockerProperties(systemProperties); - properties = overrideDockerPropertiesWithSettingsFromUserHome(properties, systemProperties); - properties = overrideDockerPropertiesWithEnv(properties, env); - properties = overrideDockerPropertiesWithSystemProperties(properties, systemProperties); - return new DockerClientConfigBuilder().withProperties(properties); - } - - public URI getDockerHost() { - return dockerHost; - } - - public RemoteApiVersion getApiVersion() { - return apiVersion; - } - - public String getRegistryUsername() { - return registryUsername; - } - - public String getRegistryPassword() { - return registryPassword; - } - - public String getRegistryEmail() { - return registryEmail; - } - - public String getRegistryUrl() { - return registryUrl; - } - - public String getDockerConfig() { - return dockerConfig; - } - - public String getDockerCertPath() { - return dockerCertPath; - } - - public boolean getDockerTlsVerify() { - return dockerTlsVerify; - } - - private AuthConfig getAuthConfig() { - AuthConfig authConfig = null; - if (getRegistryUsername() != null && getRegistryPassword() != null && getRegistryEmail() != null - && getRegistryUrl() != null) { - authConfig = new AuthConfig() - .withUsername(getRegistryUsername()) - .withPassword(getRegistryPassword()) - .withEmail(getRegistryEmail()) - .withRegistryAddress(getRegistryUrl()); - } - return authConfig; - } - - public AuthConfig effectiveAuthConfig(String imageName) { - AuthConfig authConfig = null; - - File dockerCfgFile = new File(getDockerConfig() + File.separator + DOCKER_CFG); - - if (dockerCfgFile.exists() && dockerCfgFile.isFile() && imageName != null) { - AuthConfigFile authConfigFile; - try { - authConfigFile = AuthConfigFile.loadConfig(dockerCfgFile); - } catch (IOException e) { - throw new DockerClientException("Failed to parse dockerCfgFile", e); - } - ReposTag reposTag = NameParser.parseRepositoryTag(imageName); - HostnameReposName hostnameReposName = NameParser.resolveRepositoryName(reposTag.repos); - - authConfig = authConfigFile.resolveAuthConfig(hostnameReposName.hostname); - } - - AuthConfig otherAuthConfig = getAuthConfig(); - - if (otherAuthConfig != null) { - authConfig = otherAuthConfig; - } - - return authConfig; - } - - public AuthConfigurations getAuthConfigurations() { - File dockerCfgFile = new File(getDockerConfig() + File.separator + DOCKER_CFG); - if (dockerCfgFile.exists() && dockerCfgFile.isFile()) { - AuthConfigFile authConfigFile; - try { - authConfigFile = AuthConfigFile.loadConfig(dockerCfgFile); - } catch (IOException e) { - throw new DockerClientException("Failed to parse dockerCfgFile", e); - } - - return authConfigFile.getAuthConfigurations(); - } - - return new AuthConfigurations(); - } - - // CHECKSTYLE:OFF - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - - DockerClientConfig that = (DockerClientConfig) o; - - return EqualsBuilder.reflectionEquals(this, that); - } - - @Override - public int hashCode() { - return HashCodeBuilder.reflectionHashCode(this); - } - - // CHECKSTYLE:ON - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); - } - - public static class DockerClientConfigBuilder { - private URI dockerHost; - - private String apiVersion, registryUsername, registryPassword, registryEmail, registryUrl, dockerConfig, - dockerCertPath; - - private boolean dockerTlsVerify; - - /** - * This will set all fields in the builder to those contained in the Properties object. The Properties object should contain the - * following docker-java configuration keys: DOCKER_HOST, DOCKER_TLS_VERIFY, api.version, registry.username, registry.password, - * registry.email, DOCKER_CERT_PATH, and DOCKER_CONFIG. - */ - public DockerClientConfigBuilder withProperties(Properties p) { - return withDockerHost(p.getProperty(DOCKER_HOST)).withDockerTlsVerify(p.getProperty(DOCKER_TLS_VERIFY)) - .withDockerConfig(p.getProperty(DOCKER_CONFIG)).withDockerCertPath(p.getProperty(DOCKER_CERT_PATH)) - .withApiVersion(p.getProperty(API_VERSION)).withRegistryUsername(p.getProperty(REGISTRY_USERNAME)) - .withRegistryPassword(p.getProperty(REGISTRY_PASSWORD)) - .withRegistryEmail(p.getProperty(REGISTRY_EMAIL)).withRegistryUrl(p.getProperty(REGISTRY_URL)); - } - - /** - * configure DOCKER_HOST - */ - public final DockerClientConfigBuilder withDockerHost(String dockerHost) { - checkNotNull(dockerHost, "uri was not specified"); - this.dockerHost = URI.create(dockerHost); - return this; - } - - public final DockerClientConfigBuilder withApiVersion(RemoteApiVersion apiVersion) { - this.apiVersion = apiVersion.getVersion(); - return this; - } - - public final DockerClientConfigBuilder withApiVersion(String apiVersion) { - this.apiVersion = apiVersion; - return this; - } - - public final DockerClientConfigBuilder withRegistryUsername(String registryUsername) { - this.registryUsername = registryUsername; - return this; - } - - public final DockerClientConfigBuilder withRegistryPassword(String registryPassword) { - this.registryPassword = registryPassword; - return this; - } - - public final DockerClientConfigBuilder withRegistryEmail(String registryEmail) { - this.registryEmail = registryEmail; - return this; - } - - public DockerClientConfigBuilder withRegistryUrl(String registryUrl) { - this.registryUrl = registryUrl; - return this; - } - - public final DockerClientConfigBuilder withDockerCertPath(String dockerCertPath) { - this.dockerCertPath = dockerCertPath; - return this; - } - - public final DockerClientConfigBuilder withDockerConfig(String dockerConfig) { - this.dockerConfig = dockerConfig; - return this; - } - - public final DockerClientConfigBuilder withDockerTlsVerify(String dockerTlsVerify) { - if (dockerTlsVerify != null) { - String trimmed = dockerTlsVerify.trim(); - this.dockerTlsVerify = "true".equalsIgnoreCase(trimmed) || "1".equals(trimmed); - } else { - this.dockerTlsVerify = false; - } - return this; - } - - public final DockerClientConfigBuilder withDockerTlsVerify(Boolean dockerTlsVerify) { - this.dockerTlsVerify = dockerTlsVerify; - return this; - } + SSLConfig getSSLConfig(); - public DockerClientConfig build() { - return new DockerClientConfig(dockerHost, dockerConfig, apiVersion, registryUrl, registryUsername, - registryPassword, registryEmail, dockerCertPath, dockerTlsVerify); - } - } } diff --git a/src/main/java/com/github/dockerjava/core/DockerClientImpl.java b/src/main/java/com/github/dockerjava/core/DockerClientImpl.java index 765f562bd..e11ca4f30 100644 --- a/src/main/java/com/github/dockerjava/core/DockerClientImpl.java +++ b/src/main/java/com/github/dockerjava/core/DockerClientImpl.java @@ -125,7 +125,7 @@ public class DockerClientImpl implements Closeable, DockerClient { private DockerCmdExecFactory dockerCmdExecFactory; private DockerClientImpl() { - this(DockerClientConfig.createDefaultConfigBuilder().build()); + this(DefaultDockerClientConfig.createDefaultConfigBuilder().build()); } private DockerClientImpl(String serverUrl) { @@ -138,7 +138,7 @@ private DockerClientImpl(DockerClientConfig dockerClientConfig) { } private static DockerClientConfig configWithServerUrl(String serverUrl) { - return DockerClientConfig.createDefaultConfigBuilder().withDockerHost(serverUrl).build(); + return DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost(serverUrl).build(); } public static DockerClientImpl getInstance() { diff --git a/src/main/java/com/github/dockerjava/jaxrs/DockerCmdExecFactoryImpl.java b/src/main/java/com/github/dockerjava/jaxrs/DockerCmdExecFactoryImpl.java index 5d489bbc5..d71cc33ae 100644 --- a/src/main/java/com/github/dockerjava/jaxrs/DockerCmdExecFactoryImpl.java +++ b/src/main/java/com/github/dockerjava/jaxrs/DockerCmdExecFactoryImpl.java @@ -18,6 +18,7 @@ import javax.ws.rs.client.WebTarget; import com.github.dockerjava.api.command.UpdateContainerCmd; + import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; @@ -83,7 +84,6 @@ import com.github.dockerjava.api.command.RenameContainerCmd; import com.github.dockerjava.api.exception.DockerClientException; import com.github.dockerjava.core.DockerClientConfig; -import com.github.dockerjava.core.LocalDirectorySSLConfig; import com.github.dockerjava.jaxrs.connector.ApacheConnectorProvider; import com.github.dockerjava.jaxrs.filter.JsonClientFilter; import com.github.dockerjava.jaxrs.filter.ResponseStatusExceptionFilter; @@ -114,8 +114,6 @@ public class DockerCmdExecFactoryImpl implements DockerCmdExecFactory { private DockerClientConfig dockerClientConfig; - SSLContext sslContext = null; - @Override public void init(DockerClientConfig dockerClientConfig) { checkNotNull(dockerClientConfig, "config was not specified"); @@ -160,18 +158,16 @@ public void init(DockerClientConfig dockerClientConfig) { String protocol = null; - if (dockerClientConfig.getDockerTlsVerify()) { - protocol = "https"; + SSLContext sslContext = null; - try { - - if (sslContext == null) { - sslContext = new LocalDirectorySSLConfig(dockerClientConfig.getDockerCertPath()).getSSLContext(); - } + try { + sslContext = dockerClientConfig.getSSLConfig().getSSLContext(); + } catch (Exception ex) { + throw new DockerClientException("Error in SSL Configuration", ex); + } - } catch (Exception ex) { - throw new DockerClientException("Error in SSL Configuration", ex); - } + if (sslContext != null) { + protocol = "https"; } else { protocol = "http"; } @@ -529,12 +525,6 @@ public void close() throws IOException { client.close(); } - @Override - public DockerCmdExecFactoryImpl withSSLContext(SSLContext sslContext) { - this.sslContext = sslContext; - return this; - } - public DockerCmdExecFactoryImpl withReadTimeout(Integer readTimeout) { this.readTimeout = readTimeout; return this; diff --git a/src/main/java/com/github/dockerjava/netty/DockerCmdExecFactoryImpl.java b/src/main/java/com/github/dockerjava/netty/DockerCmdExecFactoryImpl.java index 8f704c7af..1f55c61a8 100644 --- a/src/main/java/com/github/dockerjava/netty/DockerCmdExecFactoryImpl.java +++ b/src/main/java/com/github/dockerjava/netty/DockerCmdExecFactoryImpl.java @@ -53,7 +53,7 @@ import com.github.dockerjava.api.command.RenameContainerCmd; import com.github.dockerjava.core.DockerClientConfig; import com.github.dockerjava.core.DockerClientImpl; -import com.github.dockerjava.core.LocalDirectorySSLConfig; +import com.github.dockerjava.core.SSLConfig; import com.github.dockerjava.netty.exec.AttachContainerCmdExec; import com.github.dockerjava.netty.exec.AuthCmdExec; import com.github.dockerjava.netty.exec.BuildImageCmdExec; @@ -123,7 +123,6 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider; -import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLParameters; @@ -168,8 +167,6 @@ public class DockerCmdExecFactoryImpl implements DockerCmdExecFactory { private NettyInitializer nettyInitializer; - private SSLContext sslContext = null; - private ChannelProvider channelProvider = new ChannelProvider() { @Override public DuplexChannel getChannel() { @@ -270,12 +267,10 @@ public DuplexChannel connect(Bootstrap bootstrap) throws InterruptedException { DuplexChannel channel = (DuplexChannel) bootstrap.connect(host, port).sync().channel(); - if (dockerClientConfig.getDockerTlsVerify()) { - final SslHandler ssl = initSsl(dockerClientConfig); + final SslHandler ssl = initSsl(dockerClientConfig); - if (ssl != null) { - channel.pipeline().addFirst(ssl); - } + if (ssl != null) { + channel.pipeline().addFirst(ssl); } return channel; @@ -288,18 +283,19 @@ private SslHandler initSsl(DockerClientConfig dockerClientConfig) { String host = dockerClientConfig.getDockerHost().getHost(); int port = dockerClientConfig.getDockerHost().getPort(); - if (sslContext == null) { - sslContext = new LocalDirectorySSLConfig(dockerClientConfig.getDockerCertPath()).getSSLContext(); - } + final SSLConfig sslConfig = dockerClientConfig.getSSLConfig(); + + if (sslConfig != null && sslConfig.getSSLContext() != null) { - SSLEngine engine = sslContext.createSSLEngine(host, port); - engine.setUseClientMode(true); - engine.setSSLParameters(enableHostNameVerification(engine.getSSLParameters())); + SSLEngine engine = sslConfig.getSSLContext().createSSLEngine(host, port); + engine.setUseClientMode(true); + engine.setSSLParameters(enableHostNameVerification(engine.getSSLParameters())); - // in the future we may use HostnameVerifier like here: - // https://github.com/AsyncHttpClient/async-http-client/blob/1.8.x/src/main/java/com/ning/http/client/providers/netty/NettyConnectListener.java#L76 + // in the future we may use HostnameVerifier like here: + // https://github.com/AsyncHttpClient/async-http-client/blob/1.8.x/src/main/java/com/ning/http/client/providers/netty/NettyConnectListener.java#L76 - ssl = new SslHandler(engine); + ssl = new SslHandler(engine); + } } catch (Exception e) { throw new RuntimeException(e); @@ -577,12 +573,6 @@ public void close() throws IOException { eventLoopGroup.shutdownGracefully(); } - @Override - public DockerCmdExecFactory withSSLContext(SSLContext sslContext) { - this.sslContext = sslContext; - return this; - } - private WebTarget getBaseResource() { return new WebTarget(channelProvider); } diff --git a/src/test/java/com/github/dockerjava/client/AbstractDockerClientTest.java b/src/test/java/com/github/dockerjava/client/AbstractDockerClientTest.java index 231e709c1..6174e0332 100644 --- a/src/test/java/com/github/dockerjava/client/AbstractDockerClientTest.java +++ b/src/test/java/com/github/dockerjava/client/AbstractDockerClientTest.java @@ -29,7 +29,7 @@ import com.github.dockerjava.api.model.Network; import com.github.dockerjava.api.model.Volume; import com.github.dockerjava.core.DockerClientBuilder; -import com.github.dockerjava.core.DockerClientConfig; +import com.github.dockerjava.core.DefaultDockerClientConfig; import com.github.dockerjava.core.TestDockerCmdExecFactory; import com.github.dockerjava.core.command.BuildImageResultCallback; import com.github.dockerjava.core.command.LogContainerResultCallback; @@ -68,12 +68,12 @@ public void beforeTest() throws Exception { LOG.info("======================= END OF BEFORETEST =======================\n\n"); } - private DockerClientConfig config() { + private DefaultDockerClientConfig config() { return config(null); } - protected DockerClientConfig config(String password) { - DockerClientConfig.DockerClientConfigBuilder builder = DockerClientConfig.createDefaultConfigBuilder() + protected DefaultDockerClientConfig config(String password) { + DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder() .withRegistryUrl("https://index.docker.io/v1/"); if (password != null) { builder = builder.withRegistryPassword(password); diff --git a/src/test/java/com/github/dockerjava/core/DockerClientConfigTest.java b/src/test/java/com/github/dockerjava/core/DefaultDockerClientConfigTest.java similarity index 54% rename from src/test/java/com/github/dockerjava/core/DockerClientConfigTest.java rename to src/test/java/com/github/dockerjava/core/DefaultDockerClientConfigTest.java index ee679a0af..55ec38015 100644 --- a/src/test/java/com/github/dockerjava/core/DockerClientConfigTest.java +++ b/src/test/java/com/github/dockerjava/core/DefaultDockerClientConfigTest.java @@ -19,16 +19,16 @@ import com.github.dockerjava.api.exception.DockerClientException; import com.github.dockerjava.api.model.AuthConfig; -public class DockerClientConfigTest { +public class DefaultDockerClientConfigTest { - public static final DockerClientConfig EXAMPLE_CONFIG = newExampleConfig(); + public static final DefaultDockerClientConfig EXAMPLE_CONFIG = newExampleConfig(); - private static DockerClientConfig newExampleConfig() { + private static DefaultDockerClientConfig newExampleConfig() { String dockerCertPath = dockerCertPath(); - return new DockerClientConfig(URI.create("tcp://foo"), "dockerConfig", "apiVersion", "registryUrl", "registryUsername", "registryPassword", "registryEmail", - dockerCertPath, true); + return new DefaultDockerClientConfig(URI.create("tcp://foo"), "dockerConfig", "apiVersion", "registryUrl", "registryUsername", "registryPassword", "registryEmail", + new LocalDirectorySSLConfig(dockerCertPath)); } private static String homeDir() { @@ -49,7 +49,7 @@ public void environmentDockerHost() throws Exception { // given docker host in env Map env = new HashMap(); - env.put(DockerClientConfig.DOCKER_HOST, "tcp://baz:8768"); + env.put(DefaultDockerClientConfig.DOCKER_HOST, "tcp://baz:8768"); // and it looks to be SSL disabled env.remove("DOCKER_CERT_PATH"); @@ -59,7 +59,7 @@ public void environmentDockerHost() throws Exception { systemProperties.setProperty("user.home", homeDir()); // when you build a config - DockerClientConfig config = buildConfig(env, systemProperties); + DefaultDockerClientConfig config = buildConfig(env, systemProperties); assertEquals(config.getDockerHost(), URI.create("tcp://baz:8768")); } @@ -69,25 +69,25 @@ public void environment() throws Exception { // given a default config in env properties Map env = new HashMap(); - env.put(DockerClientConfig.DOCKER_HOST, "tcp://foo"); - env.put(DockerClientConfig.API_VERSION, "apiVersion"); - env.put(DockerClientConfig.REGISTRY_USERNAME, "registryUsername"); - env.put(DockerClientConfig.REGISTRY_PASSWORD, "registryPassword"); - env.put(DockerClientConfig.REGISTRY_EMAIL, "registryEmail"); - env.put(DockerClientConfig.REGISTRY_URL, "registryUrl"); - env.put(DockerClientConfig.DOCKER_CONFIG, "dockerConfig"); - env.put(DockerClientConfig.DOCKER_CERT_PATH, dockerCertPath()); - env.put(DockerClientConfig.DOCKER_TLS_VERIFY, "1"); + env.put(DefaultDockerClientConfig.DOCKER_HOST, "tcp://foo"); + env.put(DefaultDockerClientConfig.API_VERSION, "apiVersion"); + env.put(DefaultDockerClientConfig.REGISTRY_USERNAME, "registryUsername"); + env.put(DefaultDockerClientConfig.REGISTRY_PASSWORD, "registryPassword"); + env.put(DefaultDockerClientConfig.REGISTRY_EMAIL, "registryEmail"); + env.put(DefaultDockerClientConfig.REGISTRY_URL, "registryUrl"); + env.put(DefaultDockerClientConfig.DOCKER_CONFIG, "dockerConfig"); + env.put(DefaultDockerClientConfig.DOCKER_CERT_PATH, dockerCertPath()); + env.put(DefaultDockerClientConfig.DOCKER_TLS_VERIFY, "1"); // when you build a config - DockerClientConfig config = buildConfig(env, new Properties()); + DefaultDockerClientConfig config = buildConfig(env, new Properties()); // then we get the example object assertEquals(config, EXAMPLE_CONFIG); } - private DockerClientConfig buildConfig(Map env, Properties systemProperties) { - return DockerClientConfig.createDefaultConfigBuilder(env, systemProperties).build(); + private DefaultDockerClientConfig buildConfig(Map env, Properties systemProperties) { + return DefaultDockerClientConfig.createDefaultConfigBuilder(env, systemProperties).build(); } @Test @@ -99,7 +99,7 @@ public void defaults() throws Exception { systemProperties.setProperty("user.home", homeDir()); // when you build config - DockerClientConfig config = buildConfig(Collections. emptyMap(), systemProperties); + DefaultDockerClientConfig config = buildConfig(Collections. emptyMap(), systemProperties); // then the cert path is as expected assertEquals(config.getDockerHost(), URI.create("unix:///var/run/docker.sock")); @@ -107,7 +107,7 @@ public void defaults() throws Exception { assertEquals(config.getRegistryUrl(), AuthConfig.DEFAULT_SERVER_ADDRESS); assertEquals(config.getApiVersion(), RemoteApiVersion.unknown()); assertEquals(config.getDockerConfig(), homeDir() + "/.docker"); - assertNull(config.getDockerCertPath()); + assertNull(config.getSSLConfig()); } @Test @@ -115,18 +115,18 @@ public void systemProperties() throws Exception { // given system properties based on the example Properties systemProperties = new Properties(); - systemProperties.put(DockerClientConfig.DOCKER_HOST, "tcp://foo"); - systemProperties.put(DockerClientConfig.API_VERSION, "apiVersion"); - systemProperties.put(DockerClientConfig.REGISTRY_USERNAME, "registryUsername"); - systemProperties.put(DockerClientConfig.REGISTRY_PASSWORD, "registryPassword"); - systemProperties.put(DockerClientConfig.REGISTRY_EMAIL, "registryEmail"); - systemProperties.put(DockerClientConfig.REGISTRY_URL, "registryUrl"); - systemProperties.put(DockerClientConfig.DOCKER_CONFIG, "dockerConfig"); - systemProperties.put(DockerClientConfig.DOCKER_CERT_PATH, dockerCertPath()); - systemProperties.put(DockerClientConfig.DOCKER_TLS_VERIFY, "1"); + systemProperties.put(DefaultDockerClientConfig.DOCKER_HOST, "tcp://foo"); + systemProperties.put(DefaultDockerClientConfig.API_VERSION, "apiVersion"); + systemProperties.put(DefaultDockerClientConfig.REGISTRY_USERNAME, "registryUsername"); + systemProperties.put(DefaultDockerClientConfig.REGISTRY_PASSWORD, "registryPassword"); + systemProperties.put(DefaultDockerClientConfig.REGISTRY_EMAIL, "registryEmail"); + systemProperties.put(DefaultDockerClientConfig.REGISTRY_URL, "registryUrl"); + systemProperties.put(DefaultDockerClientConfig.DOCKER_CONFIG, "dockerConfig"); + systemProperties.put(DefaultDockerClientConfig.DOCKER_CERT_PATH, dockerCertPath()); + systemProperties.put(DefaultDockerClientConfig.DOCKER_TLS_VERIFY, "1"); // when you build new config - DockerClientConfig config = buildConfig(Collections. emptyMap(), systemProperties); + DefaultDockerClientConfig config = buildConfig(Collections. emptyMap(), systemProperties); // then it is the same as the example assertEquals(config, EXAMPLE_CONFIG); @@ -136,50 +136,46 @@ public void systemProperties() throws Exception { @Test public void serializableTest() { final byte[] serialized = SerializationUtils.serialize(EXAMPLE_CONFIG); - final DockerClientConfig deserialized = (DockerClientConfig) SerializationUtils.deserialize(serialized); + final DefaultDockerClientConfig deserialized = (DefaultDockerClientConfig) SerializationUtils.deserialize(serialized); assertThat("Deserialized object mush match source object", deserialized, equalTo(EXAMPLE_CONFIG)); } - @Test(expectedExceptions = DockerClientException.class) - public void testTlsVerifyAndCertPathNull() throws Exception { - new DockerClientConfig(URI.create("tcp://foo"), "dockerConfig", "apiVersion", "registryUrl", "registryUsername", "registryPassword", "registryEmail", - null, true); + @Test() + public void testSslContextEmpty() throws Exception { + new DefaultDockerClientConfig(URI.create("tcp://foo"), "dockerConfig", "apiVersion", "registryUrl", "registryUsername", "registryPassword", "registryEmail", + null); } - @Test(expectedExceptions = DockerClientException.class) - public void testTlsVerifyAndCertPathEmpty() throws Exception { - new DockerClientConfig(URI.create("tcp://foo"), "dockerConfig", "apiVersion", "registryUrl", "registryUsername", "registryPassword", "registryEmail", - "", true); - } + @Test() public void testTlsVerifyAndCertPath() throws Exception { - new DockerClientConfig(URI.create("tcp://foo"), "dockerConfig", "apiVersion", "registryUrl", "registryUsername", "registryPassword", "registryEmail", - dockerCertPath(), true); + new DefaultDockerClientConfig(URI.create("tcp://foo"), "dockerConfig", "apiVersion", "registryUrl", "registryUsername", "registryPassword", "registryEmail", + new LocalDirectorySSLConfig(dockerCertPath())); } @Test(expectedExceptions = DockerClientException.class) public void testWrongHostScheme() throws Exception { - new DockerClientConfig(URI.create("http://foo"), "dockerConfig", "apiVersion", "registryUrl", "registryUsername", "registryPassword", "registryEmail", - null, false); + new DefaultDockerClientConfig(URI.create("http://foo"), "dockerConfig", "apiVersion", "registryUrl", "registryUsername", "registryPassword", "registryEmail", + null); } @Test() public void testTcpHostScheme() throws Exception { - new DockerClientConfig(URI.create("tcp://foo"), "dockerConfig", "apiVersion", "registryUrl", "registryUsername", "registryPassword", "registryEmail", - null, false); + new DefaultDockerClientConfig(URI.create("tcp://foo"), "dockerConfig", "apiVersion", "registryUrl", "registryUsername", "registryPassword", "registryEmail", + null); } @Test() public void testUnixHostScheme() throws Exception { - new DockerClientConfig(URI.create("unix://foo"), "dockerConfig", "apiVersion", "registryUrl", "registryUsername", "registryPassword", "registryEmail", - null, false); + new DefaultDockerClientConfig(URI.create("unix://foo"), "dockerConfig", "apiVersion", "registryUrl", "registryUsername", "registryPassword", "registryEmail", + null); } @Test public void withDockerTlsVerify() throws Exception { - DockerClientConfig.DockerClientConfigBuilder builder = new DockerClientConfig.DockerClientConfigBuilder(); + DefaultDockerClientConfig.Builder builder = new DefaultDockerClientConfig.Builder(); Field field = builder.getClass().getDeclaredField("dockerTlsVerify"); field.setAccessible(true); diff --git a/src/test/java/com/github/dockerjava/core/DockerClientImplTest.java b/src/test/java/com/github/dockerjava/core/DockerClientImplTest.java index 7ea8bace5..1df4636ba 100644 --- a/src/test/java/com/github/dockerjava/core/DockerClientImplTest.java +++ b/src/test/java/com/github/dockerjava/core/DockerClientImplTest.java @@ -12,7 +12,7 @@ public class DockerClientImplTest { @Test public void configuredInstanceAuthConfig() throws Exception { // given a config with null serverAddress - DockerClientConfig dockerClientConfig = new DockerClientConfig(URI.create("tcp://foo"), null, null, null, "", "", "", null, false); + DefaultDockerClientConfig dockerClientConfig = new DefaultDockerClientConfig(URI.create("tcp://foo"), null, null, null, "", "", "", null); DockerClientImpl dockerClient = DockerClientImpl.getInstance(dockerClientConfig); // when we get the auth config diff --git a/src/test/java/com/github/dockerjava/core/TestDockerCmdExecFactory.java b/src/test/java/com/github/dockerjava/core/TestDockerCmdExecFactory.java index aee8a60a8..48ea1e164 100644 --- a/src/test/java/com/github/dockerjava/core/TestDockerCmdExecFactory.java +++ b/src/test/java/com/github/dockerjava/core/TestDockerCmdExecFactory.java @@ -58,7 +58,6 @@ import com.github.dockerjava.api.command.WaitContainerCmd; import com.github.dockerjava.api.model.BuildResponseItem; -import javax.net.ssl.SSLContext; import java.io.IOException; import java.security.SecureRandom; import java.util.ArrayList; @@ -431,9 +430,4 @@ public List getVolumeNames() { public List getNetworkIds() { return new ArrayList<>(networkIds); } - - @Override - public DockerCmdExecFactory withSSLContext(SSLContext sslContext) { - return delegate.withSSLContext(sslContext); - } }