Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +39,10 @@ public class RegistryAuthLocator {

private static final String DEFAULT_REGISTRY_NAME = "https://index.docker.io/v1/";

private static final Set<String> 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();
Expand Down Expand Up @@ -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<AuthConfig> cachedAuth = cache.computeIfAbsent(
registryName,
__ -> lookupUncachedAuthConfig(registryName, dockerImageName)
);
Optional<AuthConfig> cachedAuth = lookupCachedAuthConfig(registryName, dockerImageName);

if (!cachedAuth.isPresent() && DOCKER_HUB_REGISTRY_NAMES.contains(dockerImageName.getRegistry())) {
final String indexServerAddress = indexServerAddress();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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()));
Expand All @@ -140,6 +150,10 @@ public AuthConfig lookupAuthConfig(DockerImageName dockerImageName, AuthConfig d
}
}

private Optional<AuthConfig> lookupCachedAuthConfig(String registryName, DockerImageName dockerImageName) {
return cache.computeIfAbsent(registryName, __ -> lookupUncachedAuthConfig(registryName, dockerImageName));
}

private Optional<AuthConfig> lookupUncachedAuthConfig(String registryName, DockerImageName dockerImageName) {
try {
final JsonNode config = getDockerAuthConfig();
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "aHVidXNlcjpodWJwYXNz"
},
"docker.io": {
"auth": "aW91c2VyOmlvcGFzcw=="
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "aHVidXNlcjpodWJwYXNz"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"auths": {
"docker.io": {
"auth": "aHVidXNlcjpodWJwYXNz"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"credHelpers": {
"docker.io": "fake"
}
}
8 changes: 8 additions & 0 deletions core/src/test/resources/auth-config/docker-credential-fake
Original file line number Diff line number Diff line change
Expand Up @@ -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",' \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down