-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
/
CommandLauncher2Test.java
157 lines (151 loc) · 8.44 KB
/
CommandLauncher2Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* The MIT License
*
* Copyright 2017 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.slaves;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import hudson.XmlFile;
import hudson.cli.CLICommand;
import hudson.cli.CLICommandInvoker;
import hudson.cli.UpdateNodeCommand;
import hudson.model.Computer;
import hudson.model.User;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import javax.annotation.CheckForNull;
import jenkins.model.Jenkins;
import org.apache.tools.ant.filters.StringInputStream;
import static org.hamcrest.Matchers.*;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockAuthorizationStrategy;
import org.jvnet.hudson.test.RestartableJenkinsRule;
public class CommandLauncher2Test {
@Rule
public RestartableJenkinsRule rr = new RestartableJenkinsRule();
@Issue("SECURITY-478")
@Test
public void requireRunScripts() throws Exception {
rr.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
rr.j.jenkins.setSecurityRealm(rr.j.createDummySecurityRealm());
rr.j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().
grant(Jenkins.ADMINISTER).everywhere().to("admin").
grant(Jenkins.READ, Computer.CONFIGURE).everywhere().to("dev"));
DumbSlave s = new DumbSlave("s", "/", new CommandLauncher("echo unconfigured"));
rr.j.jenkins.addNode(s);
// First, reconfigure using GUI.
JenkinsRule.WebClient wc = rr.j.createWebClient().login("admin");
HtmlForm form = wc.getPage(s, "configure").getFormByName("config");
HtmlTextInput input = form.getInputByName("_.command");
assertEquals("echo unconfigured", input.getText());
input.setText("echo configured by GUI");
rr.j.submit(form);
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertEquals("echo configured by GUI", ((CommandLauncher) s.getLauncher()).getCommand());
assertSerialForm(s, "echo configured by GUI");
// Then by REST.
String configDotXml = s.toComputer().getUrl() + "config.xml";
String xml = wc.goTo(configDotXml, "application/xml").getWebResponse().getContentAsString();
assertThat(xml, containsString("echo configured by GUI"));
WebRequest req = new WebRequest(wc.createCrumbedUrl(configDotXml), HttpMethod.POST);
req.setEncodingType(null);
req.setRequestBody(xml.replace("echo configured by GUI", "echo configured by REST"));
wc.getPage(req);
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertEquals("echo configured by REST", ((CommandLauncher) s.getLauncher()).getCommand());
assertSerialForm(s, "echo configured by REST");
// Then by CLI.
CLICommand cmd = new UpdateNodeCommand();
cmd.setTransportAuth(User.get("admin").impersonate());
assertThat(new CLICommandInvoker(rr.j, cmd).withStdin(new StringInputStream(xml.replace("echo configured by GUI", "echo configured by CLI"))).invokeWithArgs("s"), CLICommandInvoker.Matcher.succeededSilently());
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertEquals("echo configured by CLI", ((CommandLauncher) s.getLauncher()).getCommand());
assertSerialForm(s, "echo configured by CLI");
// Now verify that all modes failed as dev. First as GUI.
s.setLauncher(new CommandLauncher("echo configured by admin"));
s.save();
wc = rr.j.createWebClient().login("dev");
form = wc.getPage(s, "configure").getFormByName("config");
input = form.getInputByName("_.command");
assertEquals("echo configured by admin", input.getText());
input.setText("echo ATTACK");
try {
rr.j.submit(form);
fail();
} catch (FailingHttpStatusCodeException x) {
assertEquals("403 would be more natural but Descriptor.newInstance wraps AccessDeniedException2 in Error", 500, x.getStatusCode());
}
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertEquals("echo configured by admin", ((CommandLauncher) s.getLauncher()).getCommand());
assertSerialForm(s, "echo configured by admin");
// Then by REST.
req = new WebRequest(wc.createCrumbedUrl(configDotXml), HttpMethod.POST);
req.setEncodingType(null);
req.setRequestBody(xml.replace("echo configured by GUI", "echo ATTACK"));
try {
wc.getPage(req);
} catch (FailingHttpStatusCodeException x) {
assertEquals(HttpURLConnection.HTTP_FORBIDDEN, x.getStatusCode());
}
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertNotEquals(CommandLauncher.class, s.getLauncher().getClass()); // currently seems to reset it to JNLPLauncher, whatever
assertSerialForm(s, null);
s.setLauncher(new CommandLauncher("echo configured by admin"));
// Then by CLI.
cmd = new UpdateNodeCommand();
cmd.setTransportAuth(User.get("dev").impersonate());
assertThat(new CLICommandInvoker(rr.j, cmd).withStdin(new StringInputStream(xml.replace("echo configured by GUI", "echo ATTACK"))).invokeWithArgs("s"),
CLICommandInvoker.Matcher./* gets swallowed by RobustReflectionConverter, hmm*/succeededSilently());
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertNotEquals(CommandLauncher.class, s.getLauncher().getClass());
assertSerialForm(s, null);
// Now also check that SYSTEM deserialization works after a restart.
s.setLauncher(new CommandLauncher("echo configured by admin"));
s.save();
}
private void assertSerialForm(DumbSlave s, @CheckForNull String expectedCommand) throws IOException {
// cf. private methods in Nodes
File nodesDir = new File(rr.j.jenkins.getRootDir(), "nodes");
XmlFile configXml = new XmlFile(Jenkins.XSTREAM, new File(new File(nodesDir, s.getNodeName()), "config.xml"));
assertThat(configXml.asString(), expectedCommand != null ? containsString("<agentCommand>" + expectedCommand + "</agentCommand>") : not(containsString("<agentCommand>")));
}
});
rr.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
DumbSlave s = (DumbSlave) rr.j.jenkins.getNode("s");
assertEquals("echo configured by admin", ((CommandLauncher) s.getLauncher()).getCommand());
}
});
}
}