From c2a81f0c63377ac47a672cc709ff78f6d23db05d Mon Sep 17 00:00:00 2001
From: Lucy Linder <lucy.derlin@gmail.com>
Date: Mon, 3 Oct 2022 14:40:24 +0200
Subject: [PATCH] Honour DOCKER_CONFIG env var in jib the same way as in
 jib-core

Support for DOCKER_CONFIG has been added in #27460. However, in jib,
the DOCKER_CONFIG should point to a directory containing a `config.json`,
while in the quarkus implementation it must point to the `config.json`
file itself. This is very confusing, especially since there is no
documentation.

This commit fixes the `JibProcessor`, so it behave exactly as described
in the jib documentation (see
https://github.com/GoogleContainerTools/jib/blob/master/jib-maven-plugin/README.md#authentication-methods).
That is, if `DOCKER_CONFIG` is set, it is expected to point to a folder
with a `config.json` file.

To keep backward compatibility, if `DOCKER_CONFIG` points to a file,
it will be left untouched.

See also `com.google.cloud.tools.jib.plugins.common.DefaultCredentialRetrievers.java`.
---
 .../container/image/jib/deployment/JibProcessor.java   | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java
index b2a716c6a2dd8d..989a4026e6306d 100644
--- a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java
+++ b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java
@@ -335,7 +335,13 @@ private RegistryImage toRegistryImage(ImageReference imageReference, Optional<St
             registryImage.addCredentialRetriever(credentialRetrieverFactory.dockerConfig());
             String dockerConfigEnv = System.getenv().get("DOCKER_CONFIG");
             if (dockerConfigEnv != null) {
-                registryImage.addCredentialRetriever(credentialRetrieverFactory.dockerConfig(Path.of(dockerConfigEnv)));
+                Path dockerConfigPath = Path.of(dockerConfigEnv);
+                if (Files.isDirectory(dockerConfigPath)) {
+                    // this matches jib's behaviour,
+                    // see https://github.com/GoogleContainerTools/jib/blob/master/jib-maven-plugin/README.md#authentication-methods
+                    dockerConfigPath = dockerConfigPath.resolve("config.json");
+                }
+                registryImage.addCredentialRetriever(credentialRetrieverFactory.dockerConfig(dockerConfigPath));
             }
         }
         return registryImage;
@@ -804,4 +810,4 @@ public boolean test(Path path) {
             return path.getFileName().toString().endsWith(".class");
         }
     }
-}
+}
\ No newline at end of file