diff --git a/src/main/java/com/spotify/docker/client/DockerConfigReader.java b/src/main/java/com/spotify/docker/client/DockerConfigReader.java index 25fa2b904..8ca84b8d0 100644 --- a/src/main/java/com/spotify/docker/client/DockerConfigReader.java +++ b/src/main/java/com/spotify/docker/client/DockerConfigReader.java @@ -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; @@ -73,9 +76,6 @@ private RegistryAuth parseDockerConfig(final Path configPath, final String serve checkNotNull(configPath); final Map configs = parseDockerConfig(configPath).configs(); - if (serverAddress != null && configs.containsKey(serverAddress)) { - return configs.get(serverAddress); - } if (isNullOrEmpty(serverAddress)) { if (configs.isEmpty()) { @@ -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); } diff --git a/src/test/java/com/spotify/docker/client/DockerConfigReaderTest.java b/src/test/java/com/spotify/docker/client/DockerConfigReaderTest.java index 1fd5da2e7..93b82000d 100644 --- a/src/test/java/com/spotify/docker/client/DockerConfigReaderTest.java +++ b/src/test/java/com/spotify/docker/client/DockerConfigReaderTest.java @@ -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; @@ -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); diff --git a/src/test/resources/dockerConfig/protocolMissing.json b/src/test/resources/dockerConfig/protocolMissing.json new file mode 100644 index 000000000..6fad3c4a4 --- /dev/null +++ b/src/test/resources/dockerConfig/protocolMissing.json @@ -0,0 +1,13 @@ +{ + "auths": { + "docker.example.com": { + "auth": "ZG9ja2VybWFuOnN3NGd5MGxvCg==" + }, + "https://repo.example.com": { + "auth": "ZG9ja2VybWFuOnN3NGd5MGxvCg==" + }, + "http://local.example.com": { + "auth": "ZG9ja2VybWFuOnN3NGd5MGxvCg==" + } + } +} \ No newline at end of file