diff --git a/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java b/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java index 3caabb6f0d4..1d930d3a558 100644 --- a/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java +++ b/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java @@ -18,11 +18,14 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; +import java.util.Arrays; import java.util.Base64; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -36,6 +39,10 @@ public class RegistryAuthLocator { private static final String DEFAULT_REGISTRY_NAME = "https://index.docker.io/v1/"; + private static final Set DOCKER_HUB_REGISTRY_NAMES = new HashSet<>( + Arrays.asList("docker.io", "index.docker.io") + ); + private static final String DOCKER_AUTH_ENV_VAR = "DOCKER_AUTH_CONFIG"; private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); @@ -122,10 +129,13 @@ public AuthConfig lookupAuthConfig(DockerImageName dockerImageName, AuthConfig d final String registryName = effectiveRegistryName(dockerImageName); log.debug("Looking up auth config for image: {} at registry: {}", dockerImageName, registryName); - final Optional cachedAuth = cache.computeIfAbsent( - registryName, - __ -> lookupUncachedAuthConfig(registryName, dockerImageName) - ); + Optional cachedAuth = lookupCachedAuthConfig(registryName, dockerImageName); + + if (!cachedAuth.isPresent() && DOCKER_HUB_REGISTRY_NAMES.contains(dockerImageName.getRegistry())) { + final String indexServerAddress = indexServerAddress(); + log.debug("No auth config found for {}, trying Docker Hub at {}", registryName, indexServerAddress); + cachedAuth = lookupCachedAuthConfig(indexServerAddress, dockerImageName); + } if (cachedAuth.isPresent()) { log.debug("Cached auth found: [{}]", AuthConfigUtil.toSafeString(cachedAuth.get())); @@ -140,6 +150,10 @@ public AuthConfig lookupAuthConfig(DockerImageName dockerImageName, AuthConfig d } } + private Optional lookupCachedAuthConfig(String registryName, DockerImageName dockerImageName) { + return cache.computeIfAbsent(registryName, __ -> lookupUncachedAuthConfig(registryName, dockerImageName)); + } + private Optional lookupUncachedAuthConfig(String registryName, DockerImageName dockerImageName) { try { final JsonNode config = getDockerAuthConfig(); @@ -343,6 +357,10 @@ private String effectiveRegistryName(DockerImageName dockerImageName) { if (!StringUtils.isEmpty(registry)) { return registry; } + return indexServerAddress(); + } + + private String indexServerAddress() { return StringUtils.defaultString( DockerClientFactory.instance().getInfo().getIndexServerAddress(), DEFAULT_REGISTRY_NAME diff --git a/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java b/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java index 6988cfd86a8..2c1f777c2f4 100644 --- a/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java +++ b/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java @@ -51,6 +51,74 @@ void lookupAuthConfigWithBasicAuthCredentials() throws URISyntaxException, IOExc assertThat(authConfig.getPassword()).as("Password is set").isEqualTo("pass"); } + @Test + void lookupAuthConfigForDockerHubImageWithoutRegistry() throws URISyntaxException, IOException { + assertDockerHubCredentialsFound("org/repo:1.0"); + } + + @Test + void lookupAuthConfigForDockerHubImageWithDockerIoRegistry() throws URISyntaxException, IOException { + assertDockerHubCredentialsFound("docker.io/org/repo:1.0"); + } + + @Test + void lookupAuthConfigForDockerHubImageWithIndexDockerIoRegistry() throws URISyntaxException, IOException { + assertDockerHubCredentialsFound("index.docker.io/library/repo:1.0"); + } + + @Test + void lookupAuthConfigForDockerIoImageUsesDockerIoAuthEntry() throws URISyntaxException, IOException { + final RegistryAuthLocator authLocator = createTestAuthLocator("config-docker-io-auth.json"); + + final AuthConfig authConfig = authLocator.lookupAuthConfig( + DockerImageName.parse("docker.io/org/repo:1.0"), + new AuthConfig() + ); + + assertThat(authConfig.getRegistryAddress()).isEqualTo("docker.io"); + assertThat(authConfig.getUsername()).isEqualTo("hubuser"); + assertThat(authConfig.getPassword()).isEqualTo("hubpass"); + } + + @Test + void lookupAuthConfigForDockerIoImageUsesDockerIoCredHelper() throws URISyntaxException, IOException { + final RegistryAuthLocator authLocator = createTestAuthLocator("config-docker-io-helper.json"); + + final AuthConfig authConfig = authLocator.lookupAuthConfig( + DockerImageName.parse("docker.io/org/repo:1.0"), + new AuthConfig() + ); + + assertThat(authConfig.getRegistryAddress()).isEqualTo("docker.io"); + assertThat(authConfig.getUsername()).isEqualTo("helperuser"); + assertThat(authConfig.getPassword()).isEqualTo("helpersecret"); + } + + @Test + void lookupAuthConfigForDockerIoImagePrefersDockerIoAuthEntryOverDockerHub() + throws URISyntaxException, IOException { + final RegistryAuthLocator authLocator = createTestAuthLocator("config-docker-hub-and-docker-io-auth.json"); + + final AuthConfig authConfig = authLocator.lookupAuthConfig( + DockerImageName.parse("docker.io/org/repo:1.0"), + new AuthConfig() + ); + + assertThat(authConfig.getRegistryAddress()).isEqualTo("docker.io"); + assertThat(authConfig.getUsername()).isEqualTo("iouser"); + assertThat(authConfig.getPassword()).isEqualTo("iopass"); + } + + private void assertDockerHubCredentialsFound(String imageName) throws URISyntaxException, IOException { + final RegistryAuthLocator authLocator = createTestAuthLocator("config-docker-hub-auth.json"); + + final AuthConfig authConfig = authLocator.lookupAuthConfig(DockerImageName.parse(imageName), new AuthConfig()); + + assertThat(authConfig.getRegistryAddress()).isEqualTo("https://index.docker.io/v1/"); + assertThat(authConfig.getUsername()).isEqualTo("hubuser"); + assertThat(authConfig.getPassword()).isEqualTo("hubpass"); + } + @Test void lookupAuthConfigWithJsonKeyCredentials() throws URISyntaxException, IOException { final RegistryAuthLocator authLocator = createTestAuthLocator("config-with-json-key.json"); diff --git a/core/src/test/resources/auth-config/config-docker-hub-and-docker-io-auth.json b/core/src/test/resources/auth-config/config-docker-hub-and-docker-io-auth.json new file mode 100644 index 00000000000..ae6abcc6afd --- /dev/null +++ b/core/src/test/resources/auth-config/config-docker-hub-and-docker-io-auth.json @@ -0,0 +1,10 @@ +{ + "auths": { + "https://index.docker.io/v1/": { + "auth": "aHVidXNlcjpodWJwYXNz" + }, + "docker.io": { + "auth": "aW91c2VyOmlvcGFzcw==" + } + } +} diff --git a/core/src/test/resources/auth-config/config-docker-hub-auth.json b/core/src/test/resources/auth-config/config-docker-hub-auth.json new file mode 100644 index 00000000000..16b744d909a --- /dev/null +++ b/core/src/test/resources/auth-config/config-docker-hub-auth.json @@ -0,0 +1,7 @@ +{ + "auths": { + "https://index.docker.io/v1/": { + "auth": "aHVidXNlcjpodWJwYXNz" + } + } +} diff --git a/core/src/test/resources/auth-config/config-docker-io-auth.json b/core/src/test/resources/auth-config/config-docker-io-auth.json new file mode 100644 index 00000000000..4440ea4f340 --- /dev/null +++ b/core/src/test/resources/auth-config/config-docker-io-auth.json @@ -0,0 +1,7 @@ +{ + "auths": { + "docker.io": { + "auth": "aHVidXNlcjpodWJwYXNz" + } + } +} diff --git a/core/src/test/resources/auth-config/config-docker-io-helper.json b/core/src/test/resources/auth-config/config-docker-io-helper.json new file mode 100644 index 00000000000..c841b1d230a --- /dev/null +++ b/core/src/test/resources/auth-config/config-docker-io-helper.json @@ -0,0 +1,5 @@ +{ + "credHelpers": { + "docker.io": "fake" + } +} diff --git a/core/src/test/resources/auth-config/docker-credential-fake b/core/src/test/resources/auth-config/docker-credential-fake index 3eb54b35a7e..5309f5339cb 100755 --- a/core/src/test/resources/auth-config/docker-credential-fake +++ b/core/src/test/resources/auth-config/docker-credential-fake @@ -19,6 +19,14 @@ if [ "$inputLine" = "registry.example.com" ]; then '}' exit 0 fi +if [ "$inputLine" = "docker.io" ]; then + echo '{' \ + ' "ServerURL": "docker.io",' \ + ' "Username": "helperuser",' \ + ' "Secret": "helpersecret"' \ + '}' + exit 0 +fi if [ "$inputLine" = "registrytoken.example.com" ]; then echo '{' \ ' "ServerURL": "url",' \ diff --git a/core/src/test/resources/auth-config/win/docker-credential-fake.bat b/core/src/test/resources/auth-config/win/docker-credential-fake.bat index afb4c66f1c6..8cfb3d2c5bb 100644 --- a/core/src/test/resources/auth-config/win/docker-credential-fake.bat +++ b/core/src/test/resources/auth-config/win/docker-credential-fake.bat @@ -18,6 +18,14 @@ if "%inputLine%" == "registry.example.com" ( echo } exit 0 ) +if "%inputLine%" == "docker.io" ( + echo { + echo "ServerURL": "docker.io", + echo "Username": "helperuser", + echo "Secret": "helpersecret" + echo } + exit 0 +) if "%inputLine%" == "registrytoken.example.com" ( echo { echo "ServerURL": "url",