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

Honor system proxy settings #510

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
@@ -1,5 +1,6 @@
package org.jfrog.gradle.plugin.artifactory.task;

import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.lang.StringUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
@@ -178,7 +179,7 @@ private void deployArtifacts(ArtifactoryClientConfiguration accRoot, Map<String,
IncludeExcludePatterns patterns = new IncludeExcludePatterns(
publisher.getIncludePatterns(),
publisher.getExcludePatterns());
configureProxy(accRoot, artifactoryManager);
configureProxy(contextUrl, accRoot, artifactoryManager);
configConnectionTimeout(accRoot, artifactoryManager);
configRetriesParams(accRoot, artifactoryManager);
deployArtifacts(artifactoryTask.deployDetails, artifactoryManager, patterns, logPrefix, publisher.getMinChecksumDeploySizeKb());
@@ -201,19 +202,88 @@ private void deployArtifacts(ArtifactoryClientConfiguration accRoot, Map<String,
}
}

private void configureProxy(ArtifactoryClientConfiguration clientConf, ArtifactoryManager artifactoryManager) {
@VisibleForTesting
static void configureProxy(String contextUrl, ArtifactoryClientConfiguration clientConf, ArtifactoryManager artifactoryManager) {
ArtifactoryClientConfiguration.ProxyHandler proxy = clientConf.proxy;
String proxyHost = proxy.getHost();
if (StringUtils.isNotBlank(proxyHost) && proxy.getPort() != null) {
Integer proxyPort = proxy.getPort();

boolean isHttps = !contextUrl.startsWith("http://");

// If no proxyHost is explicitly set, check for the JVM system proxyHost property:
if (StringUtils.isBlank(proxyHost)) {
// Note: "http.nonProxyHosts" is used for both http and https, despite its prefix
String systemNonProxyHostsString = System.getProperty("http.nonProxyHosts");
String[] systemNonProxyHosts = StringUtils.split(systemNonProxyHostsString, '|');

String contextUrlHost = getHost(contextUrl);
boolean isContextUrlOnNonProxyList = false;
for (String nonProxyHost : systemNonProxyHosts) {
if (matchesProxyHostWildcardPattern(contextUrlHost, nonProxyHost)) {
isContextUrlOnNonProxyList = true;
break;
}
}

if (!isContextUrlOnNonProxyList) {
String systemPropertyName = isHttps ? "https.proxyHost" : "http.proxyHost";
proxyHost = System.getProperty(systemPropertyName);
}
}

// If no proxyPort is explicitly set, check for the JVM system proxyPort property:
if (proxyPort == null) {
String systemPropertyName = isHttps ? "https.proxyPort" : "http.proxyPort";
String systemProxyPort = System.getProperty(systemPropertyName);
if (StringUtils.isNotBlank(systemProxyPort)) {
proxyPort = Integer.valueOf(systemProxyPort);
}
}

if (StringUtils.isNotBlank(proxyHost) && proxyPort != null) {
log.debug("Found proxy host '{}'", proxyHost);
String proxyUserName = proxy.getUsername();
if (StringUtils.isNotBlank(proxyUserName)) {
log.debug("Found proxy user name '{}'", proxyUserName);
artifactoryManager.setProxyConfiguration(proxyHost, proxy.getPort(), proxyUserName, proxy.getPassword());
artifactoryManager.setProxyConfiguration(proxyHost, proxyPort, proxyUserName, proxy.getPassword());
} else {
log.debug("No proxy user name and password found, using anonymous proxy");
artifactoryManager.setProxyConfiguration(proxyHost, proxy.getPort());
artifactoryManager.setProxyConfiguration(proxyHost, proxyPort);
}
}
}

private static String getHost(String url) {
return url.contains("://") ? StringUtils.substringAfter(url, "://") : url;
}

/**
* Checks whether a string matches a pattern with * wildcards. * is the only supported wildcard character.
*
* @param string The string to check.
* @param pattern The wildcard pattern to match with the string.
* @return True if the string matches the wildcard pattern, or if the pattern contains no wildcards and is equal to
* the string. False otherwise.
*/
private static boolean matchesProxyHostWildcardPattern(@Nonnull String string, @Nonnull String pattern) {
String[] patternParts = Arrays.stream(StringUtils.split(pattern, "*"))
// Drop leading '.', so that string with "example.com" will match pattern part ".example.com":
.map(it -> it.startsWith(".") ? it.substring(1) : it)
.toArray(String[]::new);

if (!pattern.startsWith("*") && !string.startsWith(patternParts[0])) {
// Shortcut loop if string doesn't start with an exact match:
return false;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

This "else" clause is redundant, because of the above "return".

String remainingString = string;
for (String patternPart : patternParts) {
if (remainingString.contains(patternPart)) {
remainingString = StringUtils.substringAfter(remainingString, patternPart);
} else {
return false;
}
}
return true;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
package org.jfrog.gradle.plugin.artifactory.task;

import org.jfrog.build.api.util.Log;
import org.jfrog.build.api.util.NullLog;
import org.jfrog.build.client.ProxyConfiguration;
import org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.ArtifactoryManager;
import org.testng.annotations.*;

import javax.annotation.Nullable;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;

@Test
public class DeployTaskProxyTest {

private static final String HTTPS_PROXY_HOST_SETTING = "https.proxyHost";
private static final String HTTPS_PROXY_PORT_SETTING = "https.proxyPort";
private static final String HTTP_PROXY_HOST_SETTING = "http.proxyHost";
private static final String HTTP_PROXY_PORT_SETTING = "http.proxyPort";
private static final String HTTP_NON_PROXY_HOSTS_SETTING = "http.nonProxyHosts";

private final Log log = new NullLog();

private String systemHttpsProxyHost = null;
private String systemHttpsProxyPort = null;
private String systemHttpProxyHost = null;
private String systemHttpProxyPort = null;
private String systemHttpNonProxyHosts = null;

@BeforeMethod
public void copySystemProperties() {
systemHttpsProxyHost = System.getProperty(HTTPS_PROXY_HOST_SETTING);
systemHttpsProxyPort = System.getProperty(HTTPS_PROXY_PORT_SETTING);
systemHttpProxyHost = System.getProperty(HTTP_PROXY_HOST_SETTING);
systemHttpProxyPort = System.getProperty(HTTP_PROXY_PORT_SETTING);
systemHttpNonProxyHosts = System.getProperty(HTTP_NON_PROXY_HOSTS_SETTING);
}

@AfterMethod
public void restoreSystemProperties() {
setOrClearSystemProperty(HTTPS_PROXY_HOST_SETTING, systemHttpsProxyHost);
setOrClearSystemProperty(HTTPS_PROXY_PORT_SETTING, systemHttpsProxyPort);
setOrClearSystemProperty(HTTP_PROXY_HOST_SETTING, systemHttpProxyHost);
setOrClearSystemProperty(HTTP_PROXY_PORT_SETTING, systemHttpProxyPort);
setOrClearSystemProperty(HTTP_NON_PROXY_HOSTS_SETTING, systemHttpNonProxyHosts);
}

private static void setOrClearSystemProperty(String key, @Nullable String value) {
if (value == null)
System.clearProperty(key);
else
System.setProperty(key, value);
}

@Test
public void givenExplicitProxyHostAndPort_usesExplicitProxyHostAndPort() {
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log);
clientConfig.proxy.setHost("proxy-host");
clientConfig.proxy.setPort(9999);

ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log);

DeployTask.configureProxy("", clientConfig, artifactoryManager);

ProxyConfiguration result = artifactoryManager.getProxyConfiguration();
assertEquals(result.host, "proxy-host");
assertEquals(result.port, 9999);
}

@Test
public void givenExplicitProxyHostButNoPort_doesNotUseProxy() {
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log);
clientConfig.proxy.setHost("proxy-host");

ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log);

DeployTask.configureProxy("", clientConfig, artifactoryManager);

assertNull(artifactoryManager.getProxyConfiguration());
}

@Test
public void givenExplicitProxyPortButNoHost_doesNotUseProxy() {
ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log);
clientConfig.proxy.setPort(9999);

ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log);

DeployTask.configureProxy("", clientConfig, artifactoryManager);

assertNull(artifactoryManager.getProxyConfiguration());
}

@Test
public void givenExplicitAndSystemProxyHostAndPort_usesExplicitProxyHostAndPort() {
System.setProperty(HTTPS_PROXY_HOST_SETTING, "wrong-proxy-host");
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(4444));

ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log);
clientConfig.proxy.setHost("proxy-host");
clientConfig.proxy.setPort(9999);

ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log);

DeployTask.configureProxy("", clientConfig, artifactoryManager);

ProxyConfiguration result = artifactoryManager.getProxyConfiguration();
assertEquals(result.host, "proxy-host");
assertEquals(result.port, 9999);
}

@Test
public void givenSystemProxyHostAndPort_usesSystemProxyHostAndPort() {
System.setProperty(HTTPS_PROXY_HOST_SETTING, "proxy-host");
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(9999));

ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log);
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log);

DeployTask.configureProxy("", clientConfig, artifactoryManager);

ProxyConfiguration result = artifactoryManager.getProxyConfiguration();
assertEquals(result.host, "proxy-host");
assertEquals(result.port, 9999);
}

@Test
public void givenSystemProxyHostAndPort_whenContextUrlIsHttp_usesSystemHttpProxyHostAndPort() {
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host");
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111));
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host");
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222));
Copy link
Contributor

Choose a reason for hiding this comment

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

The above line are duplicated many times in the code. I suggest we move them into a method and reuse it.


ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log);
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log);

DeployTask.configureProxy("http://example.com/artifacts", clientConfig, artifactoryManager);

ProxyConfiguration result = artifactoryManager.getProxyConfiguration();
assertEquals(result.host, "http-proxy-host");
assertEquals(result.port, 2222);
}

@Test
public void givenSystemProxyHostAndPort_whenContextUrlIsHttps_usesSystemHttpsProxyHostAndPort() {
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host");
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111));
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host");
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222));

ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log);
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log);

DeployTask.configureProxy("https://example.com/artifacts", clientConfig, artifactoryManager);

ProxyConfiguration result = artifactoryManager.getProxyConfiguration();
assertEquals(result.host, "https-proxy-host");
assertEquals(result.port, 1111);
}

@Test
public void givenSystemProxySettings_whenContextUrlMatchesNonProxyHostsExactly_doesNotUseProxy() {
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host");
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111));
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host");
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222));
System.setProperty(HTTP_NON_PROXY_HOSTS_SETTING, "example.com|other");

ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log);
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log);

DeployTask.configureProxy("https://example.com/artifacts", clientConfig, artifactoryManager);

assertNull(artifactoryManager.getProxyConfiguration());
}

@Test
public void givenSystemProxySettings_whenContextUrlMatchesNonProxyHostsWithDot_doesNotUseProxy() {
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host");
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111));
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host");
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222));
System.setProperty(HTTP_NON_PROXY_HOSTS_SETTING, ".example.com|other");

ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log);
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log);

DeployTask.configureProxy("https://example.com/artifacts", clientConfig, artifactoryManager);

assertNull(artifactoryManager.getProxyConfiguration());
}

@Test
public void givenSystemProxySettings_whenContextUrlMatchesNonProxyHostsByLeadingWildcard_doesNotUseProxy() {
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host");
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111));
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host");
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222));
System.setProperty(HTTP_NON_PROXY_HOSTS_SETTING, "other|*.example.com");

ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log);
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log);

DeployTask.configureProxy("https://example.com/artifacts", clientConfig, artifactoryManager);

assertNull(artifactoryManager.getProxyConfiguration());
}

@Test
public void givenSystemProxySettings_whenContextUrlMatchesNonProxyHostsByCentralWildcard_doesNotUseProxy() {
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host");
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111));
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host");
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222));
System.setProperty(HTTP_NON_PROXY_HOSTS_SETTING, "other|ex*ple.com");

ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log);
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log);

DeployTask.configureProxy("https://example.com/artifacts", clientConfig, artifactoryManager);

assertNull(artifactoryManager.getProxyConfiguration());
}

@Test
public void givenSystemProxySettings_whenContextUrlMatchesNonProxyHostsWithSlash_doesNotUseProxy() {
System.setProperty(HTTPS_PROXY_HOST_SETTING, "https-proxy-host");
System.setProperty(HTTPS_PROXY_PORT_SETTING, String.valueOf(1111));
System.setProperty(HTTP_PROXY_HOST_SETTING, "http-proxy-host");
System.setProperty(HTTP_PROXY_PORT_SETTING, String.valueOf(2222));
System.setProperty(HTTP_NON_PROXY_HOSTS_SETTING, "other|example.com/artifacts");

ArtifactoryClientConfiguration clientConfig = new ArtifactoryClientConfiguration(log);
ArtifactoryManager artifactoryManager = new ArtifactoryManager("", log);

DeployTask.configureProxy("https://example.com/artifacts", clientConfig, artifactoryManager);

assertNull(artifactoryManager.getProxyConfiguration());
}
}