Skip to content

Commit

Permalink
Extend proxy support for agents (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
timja authored Apr 30, 2021
1 parent a053b07 commit cb202d9
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/main/java/io/jenkins/plugins/azuresdk/HttpClientRetriever.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,44 @@
import com.azure.core.http.ProxyOptions;
import com.azure.core.http.netty.NettyAsyncHttpClientBuilder;
import hudson.ProxyConfiguration;
import hudson.Util;
import jenkins.model.Jenkins;
import jenkins.util.JenkinsJVM;

import java.net.InetSocketAddress;

public class HttpClientRetriever {

/**
* Jenkins class loader prevents the built in auto-detection from working
* need to pass an explicit http client.
*
* If this is running on an agent then use {@link #get(ProxyConfiguration)}
*/
public static HttpClient get() {
// Jenkins class loader prevents the built in auto-detection from working
// need to pass an explicit http client
ProxyOptions proxyOptions = null;
if (JenkinsJVM.isJenkinsJVM()) {
ProxyConfiguration proxy = Jenkins.get().proxy;
if (proxy != null) {
proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress(proxy.name, proxy.port));
if (proxy.getSecretPassword() != null && !proxy.getSecretPassword().getPlainText().equals("")) {
proxyOptions.setCredentials(proxy.getUserName(), proxy.getSecretPassword().getPlainText());
}
return get(proxy);
}
}
return new NettyAsyncHttpClientBuilder().build();
}

public static HttpClient get(ProxyConfiguration proxy) {
ProxyOptions proxyOptions = null;

if (proxy != null) {
proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress(proxy.name, proxy.port));
if (proxy.getSecretPassword() != null && !proxy.getSecretPassword().getPlainText().equals("")) {
proxyOptions.setCredentials(proxy.getUserName(), proxy.getSecretPassword().getPlainText());
}
String noProxyHost = Util.fixEmpty(proxy.getNoProxyHost());
if (noProxyHost != null) {
proxyOptions.setNonProxyHosts(noProxyHost);
}
}

return new NettyAsyncHttpClientBuilder().proxy(proxyOptions).build();
}
}

0 comments on commit cb202d9

Please sign in to comment.