From 67c985d8f9c2b3960b4e367a6da5ac492d6faaaf Mon Sep 17 00:00:00 2001 From: Chanseok Oh Date: Fri, 13 Jul 2018 16:41:20 -0400 Subject: [PATCH] WIP --- .../DockerConfigCredentialRetriever.java | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerConfigCredentialRetriever.java b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerConfigCredentialRetriever.java index 4670214931..02e9c679e0 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerConfigCredentialRetriever.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerConfigCredentialRetriever.java @@ -21,10 +21,13 @@ import com.google.cloud.tools.jib.json.JsonTemplateMapper; import com.google.cloud.tools.jib.registry.credentials.json.DockerConfigTemplate; import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableList; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.LinkedList; +import java.util.Optional; import javax.annotation.Nullable; /** @@ -50,6 +53,11 @@ public class DockerConfigCredentialRetriever { private static final Path DOCKER_CONFIG_FILE = Paths.get(System.getProperty("user.home")).resolve(".docker").resolve("config.json"); + private static final ImmutableList> REGISTRY_ALIAS_GROUPS = ImmutableList.of( + // Docker Hub alias group + ImmutableList.of("registry.hub.docker.com", "index.docker.io/v1/") + ); + private final String registry; private final Path dockerConfigFile; private final DockerCredentialHelperFactory dockerCredentialHelperFactory; @@ -62,7 +70,7 @@ public DockerConfigCredentialRetriever(String registry) { DockerConfigCredentialRetriever(String registry, Path dockerConfigFile) { this.registry = registry; this.dockerConfigFile = dockerConfigFile; - this.dockerCredentialHelperFactory = new DockerCredentialHelperFactory(registry); + dockerCredentialHelperFactory = new DockerCredentialHelperFactory(registry); } @VisibleForTesting @@ -75,6 +83,26 @@ public DockerConfigCredentialRetriever(String registry) { this.dockerCredentialHelperFactory = dockerCredentialHelperFactory; } + @VisibleForTesting + static ImmutableList getAllRegistryAliases(String registry) { + Optional> registryGroup = + REGISTRY_ALIAS_GROUPS.stream().filter(aliasGroup -> hasRegistry(aliasGroup, registry)).findAny(); + if (!registryGroup.isPresent()) { + // No known aliases for the given registry. Just try the registry alone. + return ImmutableList.of(registry); + } + + LinkedList registryAliases = new LinkedList<>(registryGroup.get()); + registryAliases.remove(registry); + registryAliases.addFirst(registry); + return ImmutableList.copyOf(registryAliases); + } + + @VisibleForTesting + static boolean hasRegistry(ImmutableList registries, String registry) { + return registries.stream().anyMatch(registry::equals); + } + /** * @return {@link Authorization} found for {@code registry}, or {@code null} if not found * @throws IOException if failed to parse the config JSON @@ -86,6 +114,16 @@ public Authorization retrieve() throws IOException { return null; } + for (String registry : getAllRegistryAliases(registry)) { + Authorization authorization = retrieve(dockerConfigTemplate, registry); + if (authorization != null) { + return authorization; + } + } + return null; + } + + private Authorization retrieve(DockerConfigTemplate dockerConfigTemplate, String registry) throws IOException { // First, tries to find defined auth. String auth = dockerConfigTemplate.getAuthFor(registry); if (auth != null) {