Skip to content

Commit

Permalink
Resolve registry auth URL by registry ID. Fixes issue #1688.
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Vayda <[email protected]>
  • Loading branch information
wajda authored and rohanKanojia committed Jul 24, 2023
1 parent 6eeb78a commit 68b9371
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/main/java/io/fabric8/maven/docker/access/AuthConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonObject;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.Map;
Expand All @@ -24,6 +25,10 @@ public class AuthConfig {
public static final String AUTH_AUTH = "auth";
public static final String AUTH_IDENTITY_TOKEN = "identityToken";

public static final String REGISTRY_DOCKER_IO = "docker.io";
public static final String REGISTRY_DOCKER_IO_URL = "https://index.docker.io/v1/";
public static final String REGISTRY_DEFAULT = REGISTRY_DOCKER_IO;

private final String username;
private final String password;
private final String email;
Expand Down Expand Up @@ -109,7 +114,7 @@ public String toJson() {
JsonObject creds = new JsonObject();
creds.addProperty("auth", encodeBase64(username + ":" + password));
JsonObject auths = new JsonObject();
auths.add(registry != null ? registry : "https://index.docker.io/v1/", creds);
auths.add(getRegistryUrl(registry), creds);
JsonObject root = new JsonObject();
root.add("auths", auths);
return root.toString();
Expand Down Expand Up @@ -153,4 +158,20 @@ private static void putNonNull(JsonObject ret, String key, String value) {
ret.addProperty(key, value);
}
}
}

/**
* This method returns registry authentication URL.
* In most cases it will be the same as registry, but for some it could be different.
* For example for "docker.io" the authentication URL should be exactly "https://index.docker.io/v1/"
*
* @param registry registry or null. If registry is null the default one is used (see REGISTRY_DEFAULT).
* @return authentication URL for the given registry.
*/
private static String getRegistryUrl(final String registry) {
final String reg = registry != null ? registry : REGISTRY_DEFAULT;
if (REGISTRY_DOCKER_IO.equals(StringUtils.substringBefore(reg, "/"))) {
return REGISTRY_DOCKER_IO_URL;
}
return reg;
}
}

0 comments on commit 68b9371

Please sign in to comment.