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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -231,14 +233,16 @@ public String getRegistryUrl() {
return registryUrl;
}

@CheckForNull
public String getDockerConfigPath() {
return dockerConfigPath;
}

@Nonnull
public DockerConfigFile getDockerConfig() {
if (dockerConfig == null) {
try {
dockerConfig = DockerConfigFile.loadConfig(new File(getDockerConfigPath()));
dockerConfig = DockerConfigFile.loadConfig(getDockerConfigPath());
} catch (IOException e) {
throw new DockerClientException("Failed to parse docker configuration file", e);
}
Expand Down
27 changes: 20 additions & 7 deletions src/main/java/com/github/dockerjava/core/DockerConfigFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand All @@ -27,7 +29,7 @@ public class DockerConfigFile {
private static final TypeReference<Map<String, AuthConfig>> CONFIG_MAP_TYPE = new TypeReference<Map<String, AuthConfig>>() {
};

@JsonProperty()
@JsonProperty
private final Map<String, AuthConfig> auths;

public DockerConfigFile() {
Expand All @@ -38,6 +40,7 @@ private DockerConfigFile(Map<String, AuthConfig> authConfigMap) {
auths = authConfigMap;
}

@Nonnull
public Map<String, AuthConfig> getAuths() {
return auths;
}
Expand All @@ -46,7 +49,8 @@ void addAuthConfig(AuthConfig config) {
auths.put(config.getRegistryAddress(), config);
}

public AuthConfig resolveAuthConfig(String hostname) {
@CheckForNull
public AuthConfig resolveAuthConfig(@CheckForNull String hostname) {
if (StringUtils.isEmpty(hostname) || AuthConfig.DEFAULT_SERVER_ADDRESS.equals(hostname)) {
return auths.get(AuthConfig.DEFAULT_SERVER_ADDRESS);
}
Expand All @@ -70,6 +74,7 @@ public AuthConfig resolveAuthConfig(String hostname) {
return null;
}

@Nonnull
public AuthConfigurations getAuthConfigurations() {
final AuthConfigurations authConfigurations = new AuthConfigurations();
for (Map.Entry<String, AuthConfig> authConfigEntry : auths.entrySet()) {
Expand Down Expand Up @@ -112,7 +117,13 @@ public String toString() {
return "DockerConfigFile [auths=" + auths + "]";
}

public static DockerConfigFile loadConfig(File dockerConfigPath) throws IOException {
@Nonnull
public static DockerConfigFile loadConfig(@CheckForNull String dockerConfigPath) throws IOException {
// no any configs, but for empty auths return non null object
if (dockerConfigPath == null) {
return new DockerConfigFile();
}

//parse new docker config file format
DockerConfigFile dockerConfig = loadCurrentConfig(dockerConfigPath);

Expand All @@ -136,8 +147,9 @@ public static DockerConfigFile loadConfig(File dockerConfigPath) throws IOExcept
return dockerConfig;
}

private static DockerConfigFile loadCurrentConfig(File dockerConfigPath) throws IOException {
File dockerCfgFile = new File(dockerConfigPath, File.separator + DOCKER_CFG);
@CheckForNull
private static DockerConfigFile loadCurrentConfig(@CheckForNull String dockerConfigPath) throws IOException {
File dockerCfgFile = new File(dockerConfigPath, DOCKER_CFG);

if (!dockerCfgFile.exists() || !dockerCfgFile.isFile()) {
return null;
Expand All @@ -150,8 +162,9 @@ private static DockerConfigFile loadCurrentConfig(File dockerConfigPath) throws
}
}

private static DockerConfigFile loadLegacyConfig(File dockerConfigPath) throws IOException {
File dockerLegacyCfgFile = new File(dockerConfigPath, File.separator + DOCKER_LEGACY_CFG);
@CheckForNull
private static DockerConfigFile loadLegacyConfig(String dockerConfigPath) throws IOException {
File dockerLegacyCfgFile = new File(dockerConfigPath, DOCKER_LEGACY_CFG);

if (!dockerLegacyCfgFile.exists() || !dockerLegacyCfgFile.isFile()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.AuthConfigurations;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.RemoteApiVersion;
import com.github.dockerjava.core.InvocationBuilder;
import com.github.dockerjava.core.RemoteApiVersion;
import com.github.dockerjava.core.WebTarget;
import org.apache.commons.codec.binary.Base64;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;

import static com.github.dockerjava.core.RemoteApiVersion.UNKNOWN_VERSION;
Expand All @@ -33,19 +35,21 @@ protected WebTarget getBaseResource() {
return baseResource;
}

@CheckForNull
protected AuthConfigurations getBuildAuthConfigs() {
return dockerClientConfig.getAuthConfigurations();
}

protected String registryAuth(AuthConfig authConfig) {
protected String registryAuth(@Nonnull AuthConfig authConfig) {
try {
return Base64.encodeBase64String(new ObjectMapper().writeValueAsString(authConfig).getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

protected String registryConfigs(AuthConfigurations authConfigs) {
@Nonnull
protected String registryConfigs(@Nonnull AuthConfigurations authConfigs) {
try {
final String json;
final ObjectMapper objectMapper = new ObjectMapper();
Expand All @@ -68,11 +72,15 @@ protected String registryConfigs(AuthConfigurations authConfigs) {
}
}

protected InvocationBuilder resourceWithAuthConfig(AuthConfig authConfig, InvocationBuilder request) {
@Nonnull
protected InvocationBuilder resourceWithAuthConfig(@Nonnull AuthConfig authConfig,
@Nonnull InvocationBuilder request) {
return request.header("X-Registry-Auth", registryAuth(authConfig));
}

protected InvocationBuilder resourceWithOptionalAuthConfig(AuthConfig authConfig, InvocationBuilder request) {
@Nonnull
protected InvocationBuilder resourceWithOptionalAuthConfig(@CheckForNull AuthConfig authConfig,
@Nonnull InvocationBuilder request) {
if (authConfig != null) {
request = resourceWithAuthConfig(authConfig, request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.CheckForNull;

import static com.github.dockerjava.core.util.CacheFromEncoder.jsonEncode;
import static org.apache.commons.lang.StringUtils.isNotBlank;

Expand All @@ -31,8 +33,9 @@ private InvocationBuilder resourceWithOptionalAuthConfig(BuildImageCmd command,
return request;
}

private static AuthConfigurations firstNonNull(final AuthConfigurations fromCommand,
final AuthConfigurations fromConfig) {
@CheckForNull
private static AuthConfigurations firstNonNull(@CheckForNull final AuthConfigurations fromCommand,
@CheckForNull final AuthConfigurations fromConfig) {
if (fromCommand != null) {
return fromCommand;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ protected Void execute0(PullImageCmd command, ResultCallback<PullResponseItem> r
}

LOGGER.trace("POST: {}", webResource);
resourceWithOptionalAuthConfig(command.getAuthConfig(), webResource.request()).accept(MediaType.APPLICATION_OCTET_STREAM).post(
null, new TypeReference<PullResponseItem>() {
resourceWithOptionalAuthConfig(command.getAuthConfig(), webResource.request())
.accept(MediaType.APPLICATION_OCTET_STREAM)
.post(null, new TypeReference<PullResponseItem>() {
}, resultCallback);

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void nonExistent() throws IOException {
}

private DockerConfigFile runTest(String testFileName) throws IOException {
return DockerConfigFile.loadConfig(new File(FILESROOT, testFileName));
return DockerConfigFile.loadConfig(new File(FILESROOT, testFileName).getAbsolutePath());
}

}