Skip to content

Commit 9001033

Browse files
committed
Add step that provides the global proxy settings as environment variables
1 parent 33f8055 commit 9001033

3 files changed

Lines changed: 186 additions & 0 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package org.jenkinsci.plugins.workflow.steps;
2+
3+
import com.google.common.base.Joiner;
4+
import hudson.EnvVars;
5+
import hudson.Extension;
6+
import hudson.ProxyConfiguration;
7+
import jenkins.model.Jenkins;
8+
import org.kohsuke.stapler.DataBoundConstructor;
9+
10+
import javax.annotation.Nonnull;
11+
import java.io.IOException;
12+
import java.net.InetSocketAddress;
13+
import java.net.Proxy;
14+
import java.util.ArrayList;
15+
import java.util.Collections;
16+
import java.util.List;
17+
import java.util.Set;
18+
import java.util.regex.Pattern;
19+
20+
/**
21+
* @author Robin Müller
22+
*/
23+
public class HttpProxyEnvStep extends Step {
24+
25+
@DataBoundConstructor
26+
public HttpProxyEnvStep() {
27+
}
28+
29+
@Override
30+
public StepExecution start(StepContext context) throws Exception {
31+
return new Execution(context);
32+
}
33+
34+
@Extension
35+
public static class DescriptorImpl extends StepDescriptor {
36+
37+
@Override
38+
public String getFunctionName() {
39+
return "withHttpProxyEnv";
40+
}
41+
42+
@Override
43+
public boolean takesImplicitBlockArgument() {
44+
return true;
45+
}
46+
47+
@Override
48+
public String getDisplayName() {
49+
return "Set the global proxy settings as environment variables within a block";
50+
}
51+
52+
@Override
53+
public Set<? extends Class<?>> getRequiredContext() {
54+
return Collections.emptySet();
55+
}
56+
57+
}
58+
59+
public static class Execution extends AbstractStepExecutionImpl {
60+
61+
private static final long serialVersionUID = 1;
62+
63+
Execution(StepContext context) {
64+
super(context);
65+
}
66+
67+
@Override
68+
public boolean start() throws Exception {
69+
StepContext context = getContext();
70+
context.newBodyInvoker()
71+
.withContext(EnvironmentExpander.merge(context.get(EnvironmentExpander.class), new HttpProxyEnvironmentExpander()))
72+
.withCallback(BodyExecutionCallback.wrap(context))
73+
.start();
74+
return false;
75+
}
76+
77+
@Override
78+
public void stop(Throwable cause) throws Exception {
79+
getContext().onFailure(cause);
80+
}
81+
82+
@Override
83+
public void onResume() {
84+
}
85+
86+
}
87+
88+
private static class HttpProxyEnvironmentExpander extends EnvironmentExpander {
89+
@Override
90+
public void expand(@Nonnull EnvVars envVars) throws IOException, InterruptedException {
91+
ProxyConfiguration proxyConfiguration = Jenkins.getActiveInstance().proxy;
92+
if (proxyConfiguration != null) {
93+
Proxy proxy = proxyConfiguration.createProxy(null);
94+
InetSocketAddress address = (InetSocketAddress) proxy.address();
95+
String proxyHost = "http://" + address.getHostName() + ":" + address.getPort();
96+
envVars.put("http_proxy", proxyHost);
97+
envVars.put("https_proxy", proxyHost);
98+
envVars.put("HTTP_PROXY", proxyHost);
99+
envVars.put("HTTPS_PROXY", proxyHost);
100+
List<String> noProxyHosts = new ArrayList<>();
101+
for (Pattern noProxyHost : proxyConfiguration.getNoProxyHostPatterns()) {
102+
noProxyHosts.add(noProxyHost.toString().replace("\\.", ".").replace(".*", "*"));
103+
}
104+
String noProxy = Joiner.on(',').join(noProxyHosts);
105+
envVars.put("no_proxy", noProxy);
106+
envVars.put("NO_PROXY", noProxy);
107+
}
108+
}
109+
}
110+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<?jelly escape-by-default='true'?>
3+
<j:jelly xmlns:j="jelly:core">
4+
Provides the global proxy settings as HTTP proxy environment variables<br/>
5+
<ul>
6+
<li>http_proxy/HTTP_PROXY</li>
7+
<li>https_proxy/HTTPS_PROXY</li>
8+
<li>no_proxy/NO_PROXY</li>
9+
</ul>
10+
</j:jelly>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.jenkinsci.plugins.workflow.steps;
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 HttpProxyEnvStepTest {
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+
job.setDefinition(new CpsFlowDefinition("withHttpProxyEnv { " +
24+
" echo \"proxy: $env.http_proxy\" " +
25+
"}", true));
26+
27+
WorkflowRun run = jenkinsRule.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0).get());
28+
jenkinsRule.assertLogContains("proxy: null", run);
29+
}
30+
31+
@Test
32+
public void test_proxy_set() throws Exception {
33+
jenkinsRule.getInstance().proxy = new ProxyConfiguration("test", 8080);
34+
35+
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "workflow");
36+
job.setDefinition(new CpsFlowDefinition("withHttpProxyEnv { " +
37+
" echo \"proxy: $env.http_proxy\" " +
38+
"}", true));
39+
WorkflowRun run = jenkinsRule.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0).get());
40+
jenkinsRule.assertLogContains("proxy: http://test:8080", run);
41+
}
42+
43+
@Test
44+
public void test_noProxyHosts_not_set() throws Exception {
45+
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "workflow");
46+
job.setDefinition(new CpsFlowDefinition("withHttpProxyEnv { " +
47+
" echo \"noProxyHosts: $env.no_proxy\" " +
48+
"}", true));
49+
50+
WorkflowRun run = jenkinsRule.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0).get());
51+
jenkinsRule.assertLogContains("noProxyHosts: null", run);
52+
}
53+
54+
@Test
55+
public void test_noProxyHosts_set() throws Exception {
56+
jenkinsRule.getInstance().proxy = new ProxyConfiguration("test", 8080, null, null, "test.tld\n*.test.tld");
57+
58+
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "workflow");
59+
job.setDefinition(new CpsFlowDefinition("withHttpProxyEnv { " +
60+
" echo \"noProxyHosts: $env.no_proxy\" " +
61+
"}", true));
62+
63+
WorkflowRun run = jenkinsRule.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0).get());
64+
jenkinsRule.assertLogContains("noProxyHosts: test.tld,*.test.tld", run);
65+
}
66+
}

0 commit comments

Comments
 (0)