Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proxy authentication #1337

Merged
merged 6 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,6 +65,7 @@ public static Function<URL, Connection> getConnectionFactory() {
* href="https://github.com/google/google-http-java-client/issues/39">https://github.com/google/google-http-java-client/issues/39</a>
*/
HttpTransport transport = new ApacheHttpTransport();
addProxyCredentials(transport);
return url -> new Connection(url, transport);
}

Expand All @@ -75,9 +79,51 @@ public static Function<URL, Connection> 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per #1337 (comment), I think the logic should be

    if (httpProxy && httpCreds) {
      httpClient.getCredentialsProvider().setCredentials(
          new AuthScope(http.proxyHost, http.proxyPort),
          new UsernamePasswordCredentials(http.proxyUser, http.proxyPassword));
    }
    if (httpsProxy && httpsCreds) {
      httpClient.getCredentialsProvider().setCredentials(
          new AuthScope(https.proxyHost, https.proxyPort),
          new UsernamePasswordCredentials(https.proxyUser, https.proxyPassword));
    }

http.proxyHost and https.proxyHost are independent, and I don't think it makes sense to set http.proxyUser for https.proxyHost. Agreed?

httpClient
.getCredentialsProvider()
.setCredentials(
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public void execute() throws MojoExecutionException {
MojoCommon.getExtraDirectoryPath(this),
MojoCommon.convertPermissionsList(getExtraDirectoryPermissions()),
appRoot);

EventDispatcher eventDispatcher =
new DefaultEventDispatcher(projectProperties.getEventHandlers());

Expand All @@ -90,6 +91,7 @@ public void execute() throws MojoExecutionException {
null,
null,
mavenHelpfulSuggestionsBuilder.build());
ProxyProvider.init(getSession().getSettings());
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved

ImageReference targetImageReference = pluginConfigurationProcessor.getTargetImageReference();
HelpfulSuggestions helpfulSuggestions =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void execute() throws MojoExecutionException {
projectProperties,
tarOutputPath,
mavenHelpfulSuggestionsBuilder.build());
ProxyProvider.init(getSession().getSettings());

HelpfulSuggestions helpfulSuggestions =
mavenHelpfulSuggestionsBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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, <code>
* nonProxyHosts</code> is excluded as it can only be set with <code>http</code>.
*
* @param protocol protocol
*/
private static boolean proxyPropertiesSet(String protocol) {
return PROXY_PROPERTIES.stream().anyMatch(p -> System.getProperty(protocol + "." + p) != null);
}
}