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

[JENKINS-61193] Export no_proxy hosts when using HTTP proxy #516

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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 @@ -332,6 +332,7 @@
this.listener = listener;
this.gitExe = gitExe;
this.environment = environment;
this.proxy = null;

if (isZos() && System.getProperty("ibm.system.encoding") != null) {
this.encoding =
Expand Down Expand Up @@ -2079,6 +2080,24 @@
return launchCommandWithCredentials(args, workDir, credentials, url, TIMEOUT);
}

/**
* Provides all the no proxy hosts from proxy object
* of ProxyConfiguration. Package protected for testing.
* @return hosts not intended to be proxied, concatenated by commas
*/
@NonNull
String getNoProxyHosts() {
if (proxy == null) {
return "";
}
String noProxyHost = proxy.getNoProxyHost();
if (noProxyHost == null || noProxyHost.isEmpty()) {

Check warning on line 2094 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 2094 is only partially covered, 2 branches are missing
return "";

Check warning on line 2095 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 2095 is not covered by tests
}
List<String> noProxyHosts = new ArrayList<>(Arrays.asList(noProxyHost.split("[\t\n,|]+")));
return String.join(",", noProxyHosts);
}

private String launchCommandWithCredentials(
ArgumentListBuilder args,
File workDir,
Expand Down Expand Up @@ -2175,6 +2194,7 @@
URI http_proxy = new URI("http", userInfo, proxy.name, proxy.port, null, null, null);
env.put("http_proxy", http_proxy.toString());
env.put("https_proxy", http_proxy.toString());
env.put("no_proxy", getNoProxyHosts());

Check warning on line 2197 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 2197 is not covered by tests
} catch (URISyntaxException ex) {
throw new GitException("Failed to create http proxy uri", ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.jenkinsci.plugins.gitclient;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

import hudson.EnvVars;
import hudson.ProxyConfiguration;
import hudson.model.TaskListener;
import java.io.File;
import org.junit.Before;
import org.junit.Test;

/**
* Test that checks all the no proxy hosts are added or not.
*/
public class CliGitAPIImplNoProxyHostTest {

private CliGitAPIImpl cliGit;

@Before
public void createCliGit() {
cliGit = new CliGitAPIImpl("git", new File("."), TaskListener.NULL, new EnvVars());
}

@Test
public void test_no_proxy_host_is_set_correctly() throws NoSuchFieldException, IllegalAccessException {

final String proxyHost = "172.16.1.13";
final int proxyPort = 3128;
final String proxyUser = null;
final String proxyPassword = null;
final String noProxyHosts = "169.254.169.254";

ProxyConfiguration proxyConfig =
new ProxyConfiguration(proxyHost, proxyPort, proxyUser, proxyPassword, noProxyHosts);
cliGit.setProxy(proxyConfig);
assertThat(cliGit.getNoProxyHosts(), is(noProxyHosts));
}

@Test
public void test_default_value() {
assertThat(cliGit.getNoProxyHosts(), is(""));
}
}