Skip to content
This repository has been archived by the owner on Mar 21, 2022. It is now read-only.

Support loading auth config for repos with no scheme in their URL #910

Merged
merged 2 commits into from
Nov 15, 2017
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
27 changes: 24 additions & 3 deletions src/main/java/com/spotify/docker/client/DockerConfigReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
import com.spotify.docker.client.messages.RegistryConfigs;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -73,9 +76,6 @@ private RegistryAuth parseDockerConfig(final Path configPath, final String serve
checkNotNull(configPath);

final Map<String, RegistryAuth> configs = parseDockerConfig(configPath).configs();
if (serverAddress != null && configs.containsKey(serverAddress)) {
return configs.get(serverAddress);
}

if (isNullOrEmpty(serverAddress)) {
if (configs.isEmpty()) {
Expand All @@ -86,6 +86,27 @@ private RegistryAuth parseDockerConfig(final Path configPath, final String serve
return configs.values().iterator().next();
}

if (configs.containsKey(serverAddress)) {
return configs.get(serverAddress);
}

// If the given server address didn't have a protocol try adding a protocol to the address.
// This handles cases where older versions of Docker included the protocol when writing
// auth tokens to config.json.
try {
final URI serverAddressUri = new URI(serverAddress);
if (serverAddressUri.getScheme() == null) {
for (String proto : Arrays.asList("https://", "http://")) {
final String addrWithProto = proto + serverAddress;
if (configs.containsKey(addrWithProto)) {
return configs.get(addrWithProto);
}
}
}
} catch (URISyntaxException e) {
// Nothing to do, just let this fall through below
}

throw new IllegalArgumentException(
"serverAddress=" + serverAddress + " does not appear in config file at " + configPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.spotify.docker.client.messages.RegistryAuth;
import com.spotify.docker.client.messages.RegistryConfigs;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -160,6 +161,23 @@ public void testFromDockerConfig_MultiConfig() throws Exception {
assertThat(dockerIoParsed, equalTo(DOCKER_AUTH_CONFIG));
}

@Test
public void testFromDockerConfig_AddressProtocol() throws IOException {
final Path path = getTestFilePath("dockerConfig/protocolMissing.json");

// Server address matches exactly what's in the config file
final RegistryAuth noProto = reader.fromConfig(path, "docker.example.com");
assertThat(noProto.serverAddress(), equalTo("docker.example.com"));

// Server address doesn't have a protocol but the entry in the config file does (https)
final RegistryAuth httpsProto = reader.fromConfig(path, "repo.example.com");
assertThat(httpsProto.serverAddress(), equalTo("https://repo.example.com"));

// Server address doesn't have a protocol but the entry in the config file does (http)
final RegistryAuth httpProto = reader.fromConfig(path, "local.example.com");
assertThat(httpProto.serverAddress(), equalTo("http://local.example.com"));
}

private static Path getTestFilePath(final String path) {
if (OsUtils.isLinux() || OsUtils.isOsX()) {
return getLinuxPath(path);
Expand Down
13 changes: 13 additions & 0 deletions src/test/resources/dockerConfig/protocolMissing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"auths": {
"docker.example.com": {
"auth": "ZG9ja2VybWFuOnN3NGd5MGxvCg=="
},
"https://repo.example.com": {
"auth": "ZG9ja2VybWFuOnN3NGd5MGxvCg=="
},
"http://local.example.com": {
"auth": "ZG9ja2VybWFuOnN3NGd5MGxvCg=="
}
}
}