Skip to content
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
20 changes: 20 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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<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 @@ 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);
}
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(""));
}
}