From 29aab0cc2a375f13b51f88f8eaff9dca627702a2 Mon Sep 17 00:00:00 2001 From: Tim Jacomb Date: Fri, 30 Apr 2021 13:11:56 +0100 Subject: [PATCH] Extend proxy support for agents --- .../plugins/azuresdk/HttpClientRetriever.java | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/azuresdk/HttpClientRetriever.java b/src/main/java/io/jenkins/plugins/azuresdk/HttpClientRetriever.java index 5b1263f..530a900 100644 --- a/src/main/java/io/jenkins/plugins/azuresdk/HttpClientRetriever.java +++ b/src/main/java/io/jenkins/plugins/azuresdk/HttpClientRetriever.java @@ -4,6 +4,7 @@ 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; @@ -11,19 +12,36 @@ 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(); } }