From 470ea819bc0f515cee502a57e90da34ef018dece Mon Sep 17 00:00:00 2001 From: ErickGCA <92351477+ErickGCA@users.noreply.github.com> Date: Mon, 30 Nov 2026 22:02:45 -0300 Subject: [PATCH 1/2] Use Docker Hub credentials for docker.io and index.docker.io images The Docker CLI stores Docker Hub credentials under the index server address (https://index.docker.io/v1/), but images like docker.io/org/repo looked up credentials under docker.io, so they were pulled anonymously. Treat docker.io and index.docker.io as Docker Hub, like the CLI does. --- .../utility/RegistryAuthLocator.java | 9 ++++++- .../utility/RegistryAuthLocatorTest.java | 25 +++++++++++++++++++ .../auth-config/config-docker-hub-auth.json | 7 ++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/auth-config/config-docker-hub-auth.json diff --git a/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java b/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java index 3caabb6f0d4..4b2be098c3c 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(); @@ -340,7 +347,7 @@ private String getCredentialProgramName(String credHelper) { private String effectiveRegistryName(DockerImageName dockerImageName) { final String registry = dockerImageName.getRegistry(); - if (!StringUtils.isEmpty(registry)) { + if (!StringUtils.isEmpty(registry) && !DOCKER_HUB_REGISTRY_NAMES.contains(registry)) { return registry; } return StringUtils.defaultString( diff --git a/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java b/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java index 6988cfd86a8..559efd6d9a0 100644 --- a/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java +++ b/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java @@ -51,6 +51,31 @@ 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"); + } + + 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-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" + } + } +} From c37d127e6a88195b139f73e39dfda3fb0ee4b470 Mon Sep 17 00:00:00 2001 From: ErickGCA <92351477+ErickGCA@users.noreply.github.com> Date: Mon, 30 Nov 2026 22:15:57 -0300 Subject: [PATCH 2/2] Keep looking up the registry from the image name first Credentials stored under a docker.io key in auths or credHelpers were no longer found. Look up the registry from the image name as before and only fall back to the Docker Hub index server address when nothing is found. --- .../utility/RegistryAuthLocator.java | 21 ++++++--- .../utility/RegistryAuthLocatorTest.java | 43 +++++++++++++++++++ .../config-docker-hub-and-docker-io-auth.json | 10 +++++ .../auth-config/config-docker-io-auth.json | 7 +++ .../auth-config/config-docker-io-helper.json | 5 +++ .../auth-config/docker-credential-fake | 8 ++++ .../win/docker-credential-fake.bat | 8 ++++ 7 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 core/src/test/resources/auth-config/config-docker-hub-and-docker-io-auth.json create mode 100644 core/src/test/resources/auth-config/config-docker-io-auth.json create mode 100644 core/src/test/resources/auth-config/config-docker-io-helper.json diff --git a/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java b/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java index 4b2be098c3c..1d930d3a558 100644 --- a/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java +++ b/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java @@ -129,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())); @@ -147,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(); @@ -347,9 +354,13 @@ private String getCredentialProgramName(String credHelper) { private String effectiveRegistryName(DockerImageName dockerImageName) { final String registry = dockerImageName.getRegistry(); - if (!StringUtils.isEmpty(registry) && !DOCKER_HUB_REGISTRY_NAMES.contains(registry)) { + 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 559efd6d9a0..2c1f777c2f4 100644 --- a/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java +++ b/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java @@ -66,6 +66,49 @@ void lookupAuthConfigForDockerHubImageWithIndexDockerIoRegistry() throws URISynt 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"); 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-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",