diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/http/Connection.java b/jib-core/src/main/java/com/google/cloud/tools/jib/http/Connection.java index 5602b53622..922b5ce693 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/http/Connection.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/http/Connection.java @@ -31,6 +31,9 @@ import java.security.GeneralSecurityException; import java.util.function.Function; import javax.annotation.Nullable; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.impl.client.DefaultHttpClient; /** * Sends an HTTP {@link Request} and stores the {@link Response}. Clients should not send more than @@ -62,6 +65,7 @@ public static Function getConnectionFactory() { * href="https://github.com/google/google-http-java-client/issues/39">https://github.com/google/google-http-java-client/issues/39 */ HttpTransport transport = new ApacheHttpTransport(); + addProxyCredentials(transport); return url -> new Connection(url, transport); } @@ -75,9 +79,51 @@ public static Function getInsecureConnectionFactory() throws GeneralSecurityException { // Do not use {@link NetHttpTransport}. See {@link getConnectionFactory} for details. HttpTransport transport = new ApacheHttpTransport.Builder().doNotValidateCertificate().build(); + addProxyCredentials(transport); return url -> new Connection(url, transport); } + /** + * Registers proxy credentials onto transport client, in order to deal with proxies that require + * basic authentication. + * + * @param transport + */ + private static void addProxyCredentials(HttpTransport transport) { + DefaultHttpClient httpClient = + (DefaultHttpClient) ((ApacheHttpTransport) transport).getHttpClient(); + + boolean httpProxy = System.getProperty("http.proxyHost") != null; + boolean httpCreds = + System.getProperty("http.proxyUser") != null + && System.getProperty("http.proxyPassword") != null; + if (httpProxy && httpCreds) { + httpClient + .getCredentialsProvider() + .setCredentials( + new AuthScope( + System.getProperty("http.proxyHost"), + Integer.parseInt(System.getProperty("http.proxyPort", "8080"))), + new UsernamePasswordCredentials( + System.getProperty("http.proxyUser"), System.getProperty("http.proxyPassword"))); + } + boolean httpsProxy = System.getProperty("https.proxyHost") != null; + boolean httpsCreds = + System.getProperty("https.proxyUser") != null + && System.getProperty("https.proxyPassword") != null; + if (httpsProxy && httpsCreds) { + httpClient + .getCredentialsProvider() + .setCredentials( + new AuthScope( + System.getProperty("https.proxyHost"), + Integer.parseInt(System.getProperty("https.proxyPort", "443"))), + new UsernamePasswordCredentials( + System.getProperty("https.proxyUser"), + System.getProperty("https.proxyPassword"))); + } + } + private HttpRequestFactory requestFactory; @Nullable private HttpResponse httpResponse; diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java index f02ba36913..f519548956 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java @@ -75,6 +75,7 @@ public void execute() throws MojoExecutionException { MojoCommon.getExtraDirectoryPath(this), MojoCommon.convertPermissionsList(getExtraDirectoryPermissions()), appRoot); + EventDispatcher eventDispatcher = new DefaultEventDispatcher(projectProperties.getEventHandlers()); @@ -90,6 +91,7 @@ public void execute() throws MojoExecutionException { null, null, mavenHelpfulSuggestionsBuilder.build()); + ProxyProvider.init(getSession().getSettings()); ImageReference targetImageReference = pluginConfigurationProcessor.getTargetImageReference(); HelpfulSuggestions helpfulSuggestions = diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java index 51711c6c7f..e5abba7d8e 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java @@ -104,6 +104,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { new MavenSettingsServerCredentials( getSession().getSettings(), getSettingsDecrypter(), eventDispatcher), projectProperties); + ProxyProvider.init(getSession().getSettings()); ImageReference targetImageReference = pluginConfigurationProcessor.getTargetImageReference(); HelpfulSuggestions helpfulSuggestions = diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java index a88e60d926..c1ef9145d8 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java @@ -86,6 +86,7 @@ public void execute() throws MojoExecutionException { projectProperties, tarOutputPath, mavenHelpfulSuggestionsBuilder.build()); + ProxyProvider.init(getSession().getSettings()); HelpfulSuggestions helpfulSuggestions = mavenHelpfulSuggestionsBuilder diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/ProxyProvider.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/ProxyProvider.java new file mode 100644 index 0000000000..94955345ff --- /dev/null +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/ProxyProvider.java @@ -0,0 +1,84 @@ +/* + * Copyright 2018 Google LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.tools.jib.maven; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import org.apache.maven.settings.Proxy; +import org.apache.maven.settings.Settings; + +/** Propagates proxy configuration from Maven settings to system properties * */ +public class ProxyProvider { + + private static final List PROXY_PROPERTIES = + Arrays.asList("proxyHost,proxyPort,proxyUser,proxyPassword".split(",")); + + /** + * Initializes proxy settings based on Maven settings. + * + * @param settings - Maven settings from mojo + */ + public static void init(Settings settings) { + settings + .getProxies() + .stream() + .filter(proxy -> proxy.isActive()) + .collect(Collectors.toList()) + .forEach(proxy -> setProxyProperties(proxy)); + } + + /** + * Set proxy system properties based on Maven proxy configuration. These system properties will be + * picked up by {@link java.net.ProxySelector} used in {@link + * com.google.cloud.tools.jib.http.Connection}, while connecting to container image registries. + * + * @param proxy Maven proxy + */ + private static void setProxyProperties(Proxy proxy) { + String protocol = proxy.getProtocol(); + if (protocol != null && !proxyPropertiesSet(protocol)) { + setProxyProperty(protocol + ".proxyHost", proxy.getHost()); + setProxyProperty(protocol + ".proxyPort", String.valueOf(proxy.getPort())); + setProxyProperty(protocol + ".proxyUser", proxy.getUsername()); + setProxyProperty(protocol + ".proxyPassword", proxy.getPassword()); + setProxyProperty("http.nonProxyHosts", proxy.getNonProxyHosts()); + } + } + + /** + * Set proxy system property if it has a proper value + * + * @param property property name + * @param value property value + */ + private static void setProxyProperty(String property, String value) { + if (property != null && value != null) { + System.setProperty(property, value); + } + } + + /** + * Check if any proxy system properties are already set for a given protocol. Note, + * nonProxyHosts is excluded as it can only be set with http. + * + * @param protocol protocol + */ + private static boolean proxyPropertiesSet(String protocol) { + return PROXY_PROPERTIES.stream().anyMatch(p -> System.getProperty(protocol + "." + p) != null); + } +}