Skip to content

Commit f92068e

Browse files
committed
Add global variable for accessing the proxy settings in pipelines
1 parent a3978f9 commit f92068e

4 files changed

Lines changed: 160 additions & 1 deletion

File tree

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
<groupId>org.jenkins-ci.plugins.workflow</groupId>
9898
<artifactId>workflow-cps</artifactId>
9999
<version>2.22</version>
100-
<scope>test</scope>
101100
</dependency>
102101
<dependency>
103102
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.jenkinsci.plugins.workflow.vars;
2+
3+
4+
import groovy.lang.Binding;
5+
import hudson.Extension;
6+
import hudson.ProxyConfiguration;
7+
import jenkins.model.Jenkins;
8+
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;
9+
import org.jenkinsci.plugins.workflow.cps.CpsScript;
10+
import org.jenkinsci.plugins.workflow.cps.GlobalVariable;
11+
12+
import javax.annotation.Nonnull;
13+
import java.io.Serializable;
14+
import java.net.InetSocketAddress;
15+
import java.net.Proxy;
16+
import java.util.ArrayList;
17+
import java.util.Collections;
18+
import java.util.List;
19+
import java.util.regex.Pattern;
20+
21+
/**
22+
* @author Robin Müller
23+
*/
24+
@Extension
25+
public class ProxyVar extends GlobalVariable {
26+
27+
private static final String PROXY_VAR_NAME = "proxy";
28+
29+
@Nonnull
30+
@Override
31+
public String getName() {
32+
return PROXY_VAR_NAME;
33+
}
34+
35+
@Nonnull
36+
@Override
37+
public Object getValue(@Nonnull CpsScript cpsScript) throws Exception {
38+
Binding binding = cpsScript.getBinding();
39+
if (binding.hasVariable(PROXY_VAR_NAME)) {
40+
return binding.getVariable(PROXY_VAR_NAME);
41+
} else {
42+
ProxySettings proxy = new ProxySettings(Jenkins.getActiveInstance().proxy);
43+
binding.setVariable(PROXY_VAR_NAME, proxy);
44+
return proxy;
45+
}
46+
}
47+
48+
public static class ProxySettings implements Serializable {
49+
50+
private final String proxyHost;
51+
private final Integer proxyPort;
52+
private final List<String> noProxyHosts;
53+
54+
public ProxySettings(ProxyConfiguration proxyConfiguration) {
55+
if (proxyConfiguration != null) {
56+
Proxy proxy = proxyConfiguration.createProxy(null);
57+
InetSocketAddress address = (InetSocketAddress) proxy.address();
58+
this.proxyHost = address.getHostName();
59+
this.proxyPort = address.getPort();
60+
this.noProxyHosts = new ArrayList<>();
61+
for (Pattern noProxyHost : proxyConfiguration.getNoProxyHostPatterns()) {
62+
noProxyHosts.add(noProxyHost.toString().replace("\\.", ".").replace(".*", "*"));
63+
}
64+
} else {
65+
this.proxyHost = null;
66+
this.proxyPort = null;
67+
this.noProxyHosts = Collections.emptyList();
68+
}
69+
}
70+
71+
@Whitelisted
72+
public String getProxyHost() {
73+
return proxyHost;
74+
}
75+
76+
@Whitelisted
77+
public Integer getProxyPort() {
78+
return proxyPort;
79+
}
80+
81+
@Whitelisted
82+
public List<String> getNoProxyHosts() {
83+
return noProxyHosts;
84+
}
85+
}
86+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<?jelly escape-by-default='true'?>
3+
<j:jelly xmlns:j="jelly:core">
4+
Represents the Jenkins proxy configuration.<br/>
5+
Available properties:
6+
<ul>
7+
<li>proxyHost (String)</li>
8+
<li>proxyPort (Integer)</li>
9+
<li>noProxyHosts (List&amp;lt;String&gt;)</li>
10+
</ul>
11+
</j:jelly>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.jenkinsci.plugins.workflow.vars;
2+
3+
import hudson.ProxyConfiguration;
4+
import hudson.model.Result;
5+
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
6+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
7+
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
import org.jvnet.hudson.test.JenkinsRule;
11+
12+
/**
13+
* @author Robin Müller
14+
*/
15+
public class ProxyVarTest {
16+
17+
@Rule
18+
public JenkinsRule jenkinsRule = new JenkinsRule();
19+
20+
@Test
21+
public void test_proxy_not_set() throws Exception {
22+
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "workflow");
23+
// leave out the subject
24+
job.setDefinition(new CpsFlowDefinition("echo \"proxy: http://$proxy.proxyHost:$proxy.proxyPort\"", true));
25+
26+
WorkflowRun run = jenkinsRule.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0).get());
27+
jenkinsRule.assertLogContains("proxy: http://null:null", run);
28+
}
29+
30+
@Test
31+
public void test_proxy_set() throws Exception {
32+
jenkinsRule.getInstance().proxy = new ProxyConfiguration("test", 8080);
33+
34+
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "workflow");
35+
// leave out the subject
36+
job.setDefinition(new CpsFlowDefinition("echo \"proxy: http://$proxy.proxyHost:$proxy.proxyPort\"", true));
37+
38+
WorkflowRun run = jenkinsRule.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0).get());
39+
jenkinsRule.assertLogContains("proxy: http://test:8080", run);
40+
}
41+
42+
@Test
43+
public void test_noProxyHosts_not_set() throws Exception {
44+
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "workflow");
45+
// leave out the subject
46+
job.setDefinition(new CpsFlowDefinition("echo \"noProxyHosts: ${proxy.noProxyHosts.join(',')};\"", true));
47+
48+
WorkflowRun run = jenkinsRule.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0).get());
49+
jenkinsRule.assertLogContains("noProxyHosts: ;", run);
50+
}
51+
52+
@Test
53+
public void test_noProxyHosts_set() throws Exception {
54+
jenkinsRule.getInstance().proxy = new ProxyConfiguration("test", 8080, null, null, "test.tld\n*.test.tld");
55+
56+
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "workflow");
57+
// leave out the subject
58+
job.setDefinition(new CpsFlowDefinition("echo \"noProxyHosts: ${proxy.noProxyHosts.join(',')};\"", true));
59+
60+
WorkflowRun run = jenkinsRule.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0).get());
61+
jenkinsRule.assertLogContains("noProxyHosts: test.tld,*.test.tld;", run);
62+
}
63+
}

0 commit comments

Comments
 (0)