From aaffef9a8256249aeb1b031a9f66210620586128 Mon Sep 17 00:00:00 2001 From: Chris Eager Date: Thu, 1 Aug 2024 15:52:27 -0500 Subject: [PATCH 1/2] Use server URL from auth query if helper does not return one --- .../utility/RegistryAuthLocator.java | 9 +++-- .../utility/RegistryAuthLocatorTest.java | 40 +++++++++++++++++++ ...with-helper-no-server-url-using-token.json | 11 +++++ .../config-with-helper-no-server-url.json | 11 +++++ .../auth-config/docker-credential-fake | 14 +++++++ 5 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 core/src/test/resources/auth-config/config-with-helper-no-server-url-using-token.json create mode 100644 core/src/test/resources/auth-config/config-with-helper-no-server-url.json diff --git a/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java b/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java index af2db247eb8..211b23a0081 100644 --- a/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java +++ b/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java @@ -318,12 +318,13 @@ private AuthConfig runCredentialProvider(String hostName, String helperOrStoreNa final String username = helperResponse.at("/Username").asText(); final String password = helperResponse.at("/Secret").asText(); + final String serverUrl = helperResponse.at("/ServerURL").asText(); + final AuthConfig authConfig = new AuthConfig() + .withRegistryAddress(serverUrl.isEmpty() ? hostName : serverUrl); if ("".equals(username)) { - return new AuthConfig().withIdentityToken(password); + return authConfig.withIdentityToken(password); } else { - return new AuthConfig() - .withRegistryAddress(helperResponse.at("/ServerURL").asText()) - .withUsername(username) + return authConfig.withUsername(username) .withPassword(password); } } diff --git a/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java b/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java index 835c1dff101..49709ee9107 100644 --- a/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java +++ b/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java @@ -131,6 +131,9 @@ public void lookupAuthConfigUsingHelperWithToken() throws URISyntaxException, IO new AuthConfig() ); + assertThat(authConfig.getRegistryAddress()) + .as("Correct server URL is obtained from a credential store") + .isEqualTo("url"); assertThat(authConfig.getIdentitytoken()) .as("Correct identitytoken is obtained from a credential store") .isEqualTo("secret"); @@ -176,6 +179,43 @@ public void lookupNonEmptyAuthWithHelper() throws URISyntaxException, IOExceptio .isEqualTo("secret"); } + @Test + public void lookupAuthConfigUsingHelperNoServerUrl() throws URISyntaxException, IOException { + final RegistryAuthLocator authLocator = createTestAuthLocator("config-with-helper-no-server-url.json"); + + final AuthConfig authConfig = authLocator.lookupAuthConfig( + DockerImageName.parse("registrynoserverurl.example.com/org/repo"), + new AuthConfig() + ); + + assertThat(authConfig.getRegistryAddress()) + .as("Fallback (registry) server URL is used") + .isEqualTo("registrynoserverurl.example.com"); + assertThat(authConfig.getUsername()) + .as("Correct username is obtained from a credential store") + .isEqualTo("username"); + assertThat(authConfig.getPassword()) + .as("Correct secret is obtained from a credential store") + .isEqualTo("secret"); + } + + @Test + public void lookupAuthConfigUsingHelperNoServerUrlWithToken() throws URISyntaxException, IOException { + final RegistryAuthLocator authLocator = createTestAuthLocator("config-with-helper-no-server-url-using-token.json"); + + final AuthConfig authConfig = authLocator.lookupAuthConfig( + DockerImageName.parse("registrynoserverurltoken.example.com/org/repo"), + new AuthConfig() + ); + + assertThat(authConfig.getRegistryAddress()) + .as("Fallback (registry) server URL is used") + .isEqualTo("registrynoserverurltoken.example.com"); + assertThat(authConfig.getIdentitytoken()) + .as("Correct identitytoken is obtained from a credential store") + .isEqualTo("secret"); + } + @Test public void lookupAuthConfigWithCredentialsNotFound() throws URISyntaxException, IOException { Map notFoundMessagesReference = new HashMap<>(); diff --git a/core/src/test/resources/auth-config/config-with-helper-no-server-url-using-token.json b/core/src/test/resources/auth-config/config-with-helper-no-server-url-using-token.json new file mode 100644 index 00000000000..836204cd7fb --- /dev/null +++ b/core/src/test/resources/auth-config/config-with-helper-no-server-url-using-token.json @@ -0,0 +1,11 @@ +{ + "auths": { + "registrynoserverurltoken.example.com": {} + }, + "HttpHeaders": { + "User-Agent": "Docker-Client/18.03.0-ce (darwin)" + }, + "credHelpers": { + "registrynoserverurltoken.example.com": "fake" + } +} diff --git a/core/src/test/resources/auth-config/config-with-helper-no-server-url.json b/core/src/test/resources/auth-config/config-with-helper-no-server-url.json new file mode 100644 index 00000000000..2ebe514d547 --- /dev/null +++ b/core/src/test/resources/auth-config/config-with-helper-no-server-url.json @@ -0,0 +1,11 @@ +{ + "auths": { + "registrynoserverurl.example.com": {} + }, + "HttpHeaders": { + "User-Agent": "Docker-Client/18.03.0-ce (darwin)" + }, + "credHelpers": { + "registrynoserverurl.example.com": "fake" + } +} diff --git a/core/src/test/resources/auth-config/docker-credential-fake b/core/src/test/resources/auth-config/docker-credential-fake index 753e62ca347..4a851d93322 100755 --- a/core/src/test/resources/auth-config/docker-credential-fake +++ b/core/src/test/resources/auth-config/docker-credential-fake @@ -31,5 +31,19 @@ if [ "$inputLine" = "registrytoken.example.com" ]; then '}' exit 0 fi +if [ "$inputLine" = "registrynoserverurl.example.com" ]; then + echo '{' \ + ' "Username": "username",' \ + ' "Secret": "secret"' \ + '}' + exit 0 +fi +if [ "$inputLine" = "registrynoserverurltoken.example.com" ]; then + echo '{' \ + ' "Username": "",' \ + ' "Secret": "secret"' \ + '}' + exit 0 +fi exit 1 From b9d7bacf7481e602e499820d55c8c53f4ed10ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Mon, 14 Oct 2024 22:56:10 -0600 Subject: [PATCH 2/2] Fix format --- .../org/testcontainers/utility/RegistryAuthLocator.java | 6 ++---- .../org/testcontainers/utility/RegistryAuthLocatorTest.java | 4 +++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java b/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java index 211b23a0081..600a800c0df 100644 --- a/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java +++ b/core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java @@ -319,13 +319,11 @@ private AuthConfig runCredentialProvider(String hostName, String helperOrStoreNa final String username = helperResponse.at("/Username").asText(); final String password = helperResponse.at("/Secret").asText(); final String serverUrl = helperResponse.at("/ServerURL").asText(); - final AuthConfig authConfig = new AuthConfig() - .withRegistryAddress(serverUrl.isEmpty() ? hostName : serverUrl); + final AuthConfig authConfig = new AuthConfig().withRegistryAddress(serverUrl.isEmpty() ? hostName : serverUrl); if ("".equals(username)) { return authConfig.withIdentityToken(password); } else { - return authConfig.withUsername(username) - .withPassword(password); + return authConfig.withUsername(username).withPassword(password); } } diff --git a/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java b/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java index 49709ee9107..2e6f87d08b7 100644 --- a/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java +++ b/core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java @@ -201,7 +201,9 @@ public void lookupAuthConfigUsingHelperNoServerUrl() throws URISyntaxException, @Test public void lookupAuthConfigUsingHelperNoServerUrlWithToken() throws URISyntaxException, IOException { - final RegistryAuthLocator authLocator = createTestAuthLocator("config-with-helper-no-server-url-using-token.json"); + final RegistryAuthLocator authLocator = createTestAuthLocator( + "config-with-helper-no-server-url-using-token.json" + ); final AuthConfig authConfig = authLocator.lookupAuthConfig( DockerImageName.parse("registrynoserverurltoken.example.com/org/repo"),