From 33f18c470326f129930e254a1665a692524720c1 Mon Sep 17 00:00:00 2001 From: nakedpony Date: Tue, 26 Jan 2016 22:44:19 +0100 Subject: [PATCH] Check for repository with 'https://' scheme Signed-off-by: Sergey Belyakov --- .../docker/maven/util/AuthConfigFactory.java | 50 ++++++++++++------- .../maven/util/AuthConfigHandlerTest.java | 20 ++++++-- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/jolokia/docker/maven/util/AuthConfigFactory.java b/src/main/java/org/jolokia/docker/maven/util/AuthConfigFactory.java index 333281ac7..d5ab23081 100644 --- a/src/main/java/org/jolokia/docker/maven/util/AuthConfigFactory.java +++ b/src/main/java/org/jolokia/docker/maven/util/AuthConfigFactory.java @@ -1,11 +1,5 @@ package org.jolokia.docker.maven.util; -import java.io.*; -import java.lang.reflect.Method; -import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.settings.Server; @@ -19,6 +13,18 @@ import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; import org.yaml.snakeyaml.Yaml; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.Reader; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * Factory for creating docker specific authentication configuration * @@ -215,17 +221,27 @@ private AuthConfig getAuthConfigFromSettings(Settings settings, String user, Str private AuthConfig getAuthConfigFromDockerConfig(String registry) throws MojoExecutionException { JSONObject dockerConfig = readDockerConfig(); - if (dockerConfig != null && dockerConfig.has("auths")) { - JSONObject auths = dockerConfig.getJSONObject("auths"); - String registryToLookup = registry != null ? registry : DOCKER_LOGIN_DEFAULT_REGISTRY; - if (auths.has(registryToLookup)) { - JSONObject entry = auths.getJSONObject(registryToLookup); - if (entry.has("auth")) { - String auth = entry.getString("auth"); - String email = entry.has("email") ? entry.getString("email") : null; - return new AuthConfig(auth,email); - } - } + if (dockerConfig == null || !dockerConfig.has("auths")) { + return null; + } + JSONObject auths = dockerConfig.getJSONObject("auths"); + String registryToLookup = registry != null ? registry : DOCKER_LOGIN_DEFAULT_REGISTRY; + JSONObject credentials = getCredentialsNode(auths, registryToLookup); + if (credentials == null) { + return null; + } + String auth = credentials.getString("auth"); + String email = credentials.has("email") ? credentials.getString("email") : null; + return new AuthConfig(auth,email); + } + + private JSONObject getCredentialsNode(JSONObject auths, String registryToLookup) { + if (auths.has(registryToLookup)) { + return auths.getJSONObject(registryToLookup); + } + String registryWithScheme = "https://" + registryToLookup; + if (auths.has(registryWithScheme)) { + return auths.getJSONObject(registryWithScheme); } return null; } diff --git a/src/test/java/org/jolokia/docker/maven/util/AuthConfigHandlerTest.java b/src/test/java/org/jolokia/docker/maven/util/AuthConfigHandlerTest.java index cdae73f85..b5ad8dc31 100644 --- a/src/test/java/org/jolokia/docker/maven/util/AuthConfigHandlerTest.java +++ b/src/test/java/org/jolokia/docker/maven/util/AuthConfigHandlerTest.java @@ -1,10 +1,9 @@ package org.jolokia.docker.maven.util; -import java.io.*; -import java.nio.file.Files; -import java.util.*; - -import mockit.*; +import mockit.Mock; +import mockit.MockUp; +import mockit.Mocked; +import mockit.NonStrictExpectations; import mockit.integration.junit4.JMockit; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.settings.Server; @@ -21,6 +20,16 @@ import org.junit.runner.RunWith; import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import static org.junit.Assert.*; /** @@ -85,6 +94,7 @@ public void testDockerLogin() throws Exception { public void exec(File homeDir) throws IOException, MojoExecutionException { checkDockerLogin(homeDir,AuthConfigFactory.DOCKER_LOGIN_DEFAULT_REGISTRY,null); checkDockerLogin(homeDir,"localhost:5000","localhost:5000"); + checkDockerLogin(homeDir,"https://localhost:5000","localhost:5000"); } }); }