diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index ae0e73766e..a383a9dc26 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -332,6 +332,7 @@ protected CliGitAPIImpl(String gitExe, File workspace, TaskListener listener, En this.listener = listener; this.gitExe = gitExe; this.environment = environment; + this.proxy = null; if (isZos() && System.getProperty("ibm.system.encoding") != null) { this.encoding = @@ -2079,6 +2080,24 @@ private String launchCommandWithCredentials( 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()) { + return ""; + } + List noProxyHosts = new ArrayList<>(Arrays.asList(noProxyHost.split("[\t\n,|]+"))); + return String.join(",", noProxyHosts); + } + private String launchCommandWithCredentials( ArgumentListBuilder args, File workDir, @@ -2175,6 +2194,7 @@ private String launchCommandWithCredentials( 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()); } catch (URISyntaxException ex) { throw new GitException("Failed to create http proxy uri", ex); } diff --git a/src/test/java/org/jenkinsci/plugins/gitclient/CliGitAPIImplNoProxyHostTest.java b/src/test/java/org/jenkinsci/plugins/gitclient/CliGitAPIImplNoProxyHostTest.java new file mode 100644 index 0000000000..1d62d7112f --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/gitclient/CliGitAPIImplNoProxyHostTest.java @@ -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("")); + } +}