Skip to content

Commit

Permalink
Add proxy authentication
Browse files Browse the repository at this point in the history
Configuration from either Maven settings or system properties
  • Loading branch information
heeremk authored and heeremk committed Dec 12, 2018
1 parent 81e281e commit ec4c65e
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
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,36 @@ 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);
}

private static void addProxyCredentials(HttpTransport transport) {
DefaultHttpClient httpClient =
(DefaultHttpClient) ((ApacheHttpTransport) transport).getHttpClient();

if (System.getProperty("http.proxyUser") != null) {
httpClient
.getCredentialsProvider()
.setCredentials(
new AuthScope(
System.getProperty("http.proxyHost"),
Integer.parseInt(System.getProperty("http.proxyPort"))),
new UsernamePasswordCredentials(
System.getProperty("http.proxyUser"), System.getProperty("http.proxyPassword")));
} else if (System.getProperty("https.proxyUser") != null) {
httpClient
.getCredentialsProvider()
.setCredentials(
new AuthScope(
System.getProperty("https.proxyHost"),
Integer.parseInt(System.getProperty("https.proxyPort"))),
new UsernamePasswordCredentials(
System.getProperty("https.proxyUser"),
System.getProperty("https.proxyPassword")));
}
}

private HttpRequestFactory requestFactory;

@Nullable private HttpResponse httpResponse;
Expand Down
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());

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

HelpfulSuggestions helpfulSuggestions =
mavenHelpfulSuggestionsBuilder
Expand Down
@@ -0,0 +1,88 @@
/*
* 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.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Settings;

/** Initializes and retrieves proxy settings from either Maven settings or system properties * */
public class ProxyProvider {

static Settings mavenSettings = new Settings();

/**
* Initializes proxy settings based on Maven settings and system properties.
*
* @param settings - Maven settings from mojo
*/
public static void init(Settings settings) {
mavenSettings = settings;
for (Proxy proxy : mavenSettings.getProxies()) {
// Poor way to push proxy settings onto Connection instances
if (proxy != null && proxy.getProtocol().equalsIgnoreCase("http")) {
System.setProperty("http.proxyHost", proxy.getHost());
System.setProperty("http.proxyPort", String.valueOf(proxy.getPort()));
System.setProperty("http.nonProxyHosts", proxy.getNonProxyHosts());
System.setProperty("http.proxyUser", String.valueOf(proxy.getUsername()));
System.setProperty("http.proxyPassword", proxy.getPassword());
} else if (proxy != null && proxy.getProtocol().equalsIgnoreCase("https")) {
System.setProperty("https.proxyHost", proxy.getHost());
System.setProperty("https.proxyPort", String.valueOf(proxy.getPort()));
System.setProperty("http.nonProxyHosts", proxy.getNonProxyHosts());
System.setProperty("https.proxyUser", String.valueOf(proxy.getUsername()));
System.setProperty("https.proxyPassword", proxy.getPassword());
}
}
}

/**
* Attempts to retrieve proxy from either Maven settings or system properties.
*
* @return the active proxies
*/
public List<Proxy> getProxies() {
List<Proxy> proxies = new ArrayList<Proxy>();
if (mavenSettings != null) {
proxies.addAll(mavenSettings.getProxies());
} else {
if (System.getProperty("http.proxyHost") != null) {
Proxy proxy = new Proxy();
proxy.setHost(System.getProperty("http.proxyHost"));
proxy.setPort(Integer.parseInt(System.getProperty("http.proxyPort")));
proxy.setActive(true);
proxy.setUsername(System.getProperty("http.proxyUser"));
proxy.setPassword(System.getProperty("http.proxyPassword"));
proxy.setNonProxyHosts(System.getProperty("http.nonProxyHosts"));
proxies.add(proxy);
}
if (System.getProperty("https.proxyHost") != null) {
Proxy proxy = new Proxy();
proxy.setHost(System.getProperty("https.proxyHost"));
proxy.setPort(Integer.parseInt(System.getProperty("https.proxyPort")));
proxy.setActive(true);
proxy.setUsername(System.getProperty("https.proxyUser"));
proxy.setPassword(System.getProperty("https.proxyPassword"));
proxy.setNonProxyHosts(System.getProperty("http.nonProxyHosts"));
proxies.add(proxy);
}
}
return proxies.stream().filter(proxy -> proxy.isActive()).collect(Collectors.toList());
}
}

0 comments on commit ec4c65e

Please sign in to comment.