Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,18 @@ private String launchCommandWithCredentials(ArgumentListBuilder args, File workD
@NonNull URIish url) throws GitException, InterruptedException {
return launchCommandWithCredentials(args, workDir, credentials, url, TIMEOUT);
}

/**
* Provides all the no proxy hosts from proxy object
* of ProxyConfiguration
* @return proxy hosts concatenated by commas
*/
private String getNoProxyHosts(){
String noProxyHost = proxy.noProxyHost;
List <String> noProxyHosts = Lists.newArrayList(Arrays.asList(noProxyHost.split("[\t\n,|]+")));
return String.join(",",noProxyHosts);
}

private String launchCommandWithCredentials(ArgumentListBuilder args, File workDir,
StandardCredentials credentials,
@NonNull URIish url,
Expand Down Expand Up @@ -2034,6 +2046,7 @@ private String launchCommandWithCredentials(ArgumentListBuilder args, File workD
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,60 @@
package org.jenkinsci.plugins.gitclient;

import hudson.EnvVars;
import hudson.ProxyConfiguration;
import hudson.model.TaskListener;
import junit.framework.TestCase;
import org.junit.Test;
import org.objenesis.ObjenesisStd;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

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

private CliGitAPIImpl cliGit;

@Test
public void test_no_proxy_host_is_set_correctly() throws NoSuchFieldException, IllegalAccessException {
cliGit = new CliGitAPIImpl("git", new File("."), TaskListener.NULL, new EnvVars());

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

ProxyConfiguration proxyConfig = new ObjenesisStd().newInstance(ProxyConfiguration.class);

setField(ProxyConfiguration.class, "name", proxyConfig, proxyHost);
setField(ProxyConfiguration.class, "port", proxyConfig, Integer.parseInt(proxyPort));
setField(ProxyConfiguration.class, "userName", proxyConfig, null);
setField(ProxyConfiguration.class, "noProxyHost", proxyConfig, noProxyHosts);
setField(ProxyConfiguration.class, "password", proxyConfig, null);
setField(ProxyConfiguration.class, "secretPassword", proxyConfig, null);

cliGit.setProxy(proxyConfig);

try {
Method getNoProxyHosts = CliGitAPIImpl.class.getDeclaredMethod("getNoProxyHosts");
getNoProxyHosts.setAccessible(true);
String returnValue = (String)
getNoProxyHosts.invoke(cliGit, null);
assertEquals( "NO_PROXY hosts are not set correctly" , noProxyHosts, returnValue);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}

private void setField(Class<?> clazz, String fieldName, Object object, Object value)
throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
{
Field declaredField = clazz.getDeclaredField(fieldName);
declaredField.setAccessible(true);
declaredField.set(object, value);
}
}