Merge pull request #45345 from nosan

* pr/45345:
  Use serverUrl from the image as a fallback for the Credentials helper

Closes gh-45345
This commit is contained in:
Stéphane Nicoll 2025-05-02 15:24:37 +02:00
commit 3a51197082
2 changed files with 33 additions and 4 deletions

View File

@ -85,14 +85,13 @@ class DockerRegistryConfigAuthentication implements DockerRegistryAuthentication
private DockerRegistryAuthentication getAuthentication(String serverUrl) {
Credential credentialsFromHelper = getCredentialsFromHelper(serverUrl);
Map.Entry<String, Auth> authConfigEntry = getAuthConfigEntry(serverUrl);
serverUrl = (authConfigEntry != null) ? authConfigEntry.getKey() : serverUrl;
Auth authConfig = (authConfigEntry != null) ? authConfigEntry.getValue() : null;
if (credentialsFromHelper != null) {
return getAuthentication(credentialsFromHelper, authConfig, serverUrl);
}
if (authConfigEntry != null) {
return DockerRegistryAuthentication.user(authConfig.getUsername(), authConfig.getPassword(), serverUrl,
authConfig.getEmail());
if (authConfig != null) {
return DockerRegistryAuthentication.user(authConfig.getUsername(), authConfig.getPassword(),
authConfigEntry.getKey(), authConfig.getEmail());
}
return this.fallback;
}

View File

@ -348,6 +348,36 @@ class DockerRegistryConfigAuthenticationTests {
then(desktopHelper).should(never()).get(any(String.class));
}
@WithResource(name = "config.json", content = """
{
"auths": {
"https://my-registry.example.com": {
"email": "test@gmail.com"
}
},
"credsStore": "desktop"
}
""")
@WithResource(name = "credentials.json", content = """
{
"Username": "username",
"Secret": "secret"
}
""")
@Test
void getAuthHeaderWhenUsingHelperFromCredHelpersUsesImageReferenceServerUrlAsFallback(@ResourcesRoot Path directory)
throws Exception {
this.environment.put("DOCKER_CONFIG", directory.toString());
mockHelper("desktop", "my-registry.example.com", "credentials.json");
ImageReference imageReference = ImageReference.of("my-registry.example.com/ubuntu:latest");
String authHeader = getAuthHeader(imageReference);
assertThat(decode(authHeader)).hasSize(4)
.containsEntry("serveraddress", "my-registry.example.com")
.containsEntry("username", "username")
.containsEntry("password", "secret")
.containsEntry("email", "test@gmail.com");
}
private String getAuthHeader(ImageReference imageReference) {
return getAuthHeader(imageReference, null);
}