Skip to content

Commit 5137472

Browse files
Use server URL from auth query if helper does not return one (#9056)
Co-authored-by: Eddú Meléndez Gonzales <[email protected]>
1 parent bcca9f3 commit 5137472

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

core/src/main/java/org/testcontainers/utility/RegistryAuthLocator.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,12 @@ private AuthConfig runCredentialProvider(String hostName, String helperOrStoreNa
318318

319319
final String username = helperResponse.at("/Username").asText();
320320
final String password = helperResponse.at("/Secret").asText();
321+
final String serverUrl = helperResponse.at("/ServerURL").asText();
322+
final AuthConfig authConfig = new AuthConfig().withRegistryAddress(serverUrl.isEmpty() ? hostName : serverUrl);
321323
if ("<token>".equals(username)) {
322-
return new AuthConfig().withIdentityToken(password);
324+
return authConfig.withIdentityToken(password);
323325
} else {
324-
return new AuthConfig()
325-
.withRegistryAddress(helperResponse.at("/ServerURL").asText())
326-
.withUsername(username)
327-
.withPassword(password);
326+
return authConfig.withUsername(username).withPassword(password);
328327
}
329328
}
330329

core/src/test/java/org/testcontainers/utility/RegistryAuthLocatorTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ public void lookupAuthConfigUsingHelperWithToken() throws URISyntaxException, IO
131131
new AuthConfig()
132132
);
133133

134+
assertThat(authConfig.getRegistryAddress())
135+
.as("Correct server URL is obtained from a credential store")
136+
.isEqualTo("url");
134137
assertThat(authConfig.getIdentitytoken())
135138
.as("Correct identitytoken is obtained from a credential store")
136139
.isEqualTo("secret");
@@ -176,6 +179,45 @@ public void lookupNonEmptyAuthWithHelper() throws URISyntaxException, IOExceptio
176179
.isEqualTo("secret");
177180
}
178181

182+
@Test
183+
public void lookupAuthConfigUsingHelperNoServerUrl() throws URISyntaxException, IOException {
184+
final RegistryAuthLocator authLocator = createTestAuthLocator("config-with-helper-no-server-url.json");
185+
186+
final AuthConfig authConfig = authLocator.lookupAuthConfig(
187+
DockerImageName.parse("registrynoserverurl.example.com/org/repo"),
188+
new AuthConfig()
189+
);
190+
191+
assertThat(authConfig.getRegistryAddress())
192+
.as("Fallback (registry) server URL is used")
193+
.isEqualTo("registrynoserverurl.example.com");
194+
assertThat(authConfig.getUsername())
195+
.as("Correct username is obtained from a credential store")
196+
.isEqualTo("username");
197+
assertThat(authConfig.getPassword())
198+
.as("Correct secret is obtained from a credential store")
199+
.isEqualTo("secret");
200+
}
201+
202+
@Test
203+
public void lookupAuthConfigUsingHelperNoServerUrlWithToken() throws URISyntaxException, IOException {
204+
final RegistryAuthLocator authLocator = createTestAuthLocator(
205+
"config-with-helper-no-server-url-using-token.json"
206+
);
207+
208+
final AuthConfig authConfig = authLocator.lookupAuthConfig(
209+
DockerImageName.parse("registrynoserverurltoken.example.com/org/repo"),
210+
new AuthConfig()
211+
);
212+
213+
assertThat(authConfig.getRegistryAddress())
214+
.as("Fallback (registry) server URL is used")
215+
.isEqualTo("registrynoserverurltoken.example.com");
216+
assertThat(authConfig.getIdentitytoken())
217+
.as("Correct identitytoken is obtained from a credential store")
218+
.isEqualTo("secret");
219+
}
220+
179221
@Test
180222
public void lookupAuthConfigWithCredentialsNotFound() throws URISyntaxException, IOException {
181223
Map<String, String> notFoundMessagesReference = new HashMap<>();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"auths": {
3+
"registrynoserverurltoken.example.com": {}
4+
},
5+
"HttpHeaders": {
6+
"User-Agent": "Docker-Client/18.03.0-ce (darwin)"
7+
},
8+
"credHelpers": {
9+
"registrynoserverurltoken.example.com": "fake"
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"auths": {
3+
"registrynoserverurl.example.com": {}
4+
},
5+
"HttpHeaders": {
6+
"User-Agent": "Docker-Client/18.03.0-ce (darwin)"
7+
},
8+
"credHelpers": {
9+
"registrynoserverurl.example.com": "fake"
10+
}
11+
}

core/src/test/resources/auth-config/docker-credential-fake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,19 @@ if [ "$inputLine" = "registrytoken.example.com" ]; then
3131
'}'
3232
exit 0
3333
fi
34+
if [ "$inputLine" = "registrynoserverurl.example.com" ]; then
35+
echo '{' \
36+
' "Username": "username",' \
37+
' "Secret": "secret"' \
38+
'}'
39+
exit 0
40+
fi
41+
if [ "$inputLine" = "registrynoserverurltoken.example.com" ]; then
42+
echo '{' \
43+
' "Username": "<token>",' \
44+
' "Secret": "secret"' \
45+
'}'
46+
exit 0
47+
fi
3448

3549
exit 1

0 commit comments

Comments
 (0)