Skip to content

Commit

Permalink
[SECURITY-3103] test adaptation
Browse files Browse the repository at this point in the history
  • Loading branch information
jtnord authored and julieheard committed Aug 7, 2023
1 parent bab8f75 commit 7738605
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.361.x</artifactId>
<!-- TODO when updating the bom check for script-security update and remove version override below -->
<version>2025.v816d28f1e04f</version>
<type>pom</type>
<scope>import</scope>
Expand All @@ -57,6 +58,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>1265.va_fb_290b_4b_d34</version>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
Expand Down
41 changes: 39 additions & 2 deletions src/test/java/hudson/slaves/CommandLauncher2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@
import edu.umd.cs.findbugs.annotations.CheckForNull;
import jenkins.model.Jenkins;
import org.apache.tools.ant.filters.StringInputStream;
import org.hamcrest.CustomTypeSafeMatcher;
import org.hamcrest.Description;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval.PendingScript;
import org.jenkinsci.plugins.scriptsecurity.scripts.languages.SystemCommandLanguage;
import static org.junit.Assert.*;
import org.junit.Rule;
Expand All @@ -54,12 +58,14 @@
import org.jvnet.hudson.test.RestartableJenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;

import static hudson.slaves.CommandLauncher2Test.PendingScriptApprovalMatcher.pendingScript;

public class CommandLauncher2Test {

@Rule
public RestartableJenkinsRule rr = new RestartableJenkinsRule();

@Issue("SECURITY-478")
@Issue({"SECURITY-478", "SECURITY-3103"})
@Test
public void requireApproval() throws Exception {
rr.addStep(new Statement() { // TODO .then, when using sufficiently new jenkins-test-harness
Expand All @@ -82,6 +88,9 @@ public void evaluate() throws Throwable {
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertEquals("echo configured by GUI", ((CommandLauncher) s.getLauncher()).getCommand());
assertSerialForm(s, "echo configured by GUI");
assertThat(ScriptApproval.get().getPendingScripts(), contains(pendingScript("echo configured by GUI")));
ScriptApproval.get().getPendingScripts().clear(); // reset

// Then by REST.
String configDotXml = s.toComputer().getUrl() + "config.xml";
String xml = wc.goTo(configDotXml, "application/xml").getWebResponse().getContentAsString();
Expand All @@ -93,14 +102,19 @@ public void evaluate() throws Throwable {
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertEquals("echo configured by REST", ((CommandLauncher) s.getLauncher()).getCommand());
assertSerialForm(s, "echo configured by REST");
assertThat(ScriptApproval.get().getPendingScripts(), contains(pendingScript("echo configured by REST")));
ScriptApproval.get().getPendingScripts().clear(); // reset

// 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());
assertEquals(Collections.emptySet(), ScriptApproval.get().getPendingScripts());
assertSerialForm(s, "echo configured by CLI");
assertThat(ScriptApproval.get().getPendingScripts(), contains(pendingScript("echo configured by CLI")));
ScriptApproval.get().getPendingScripts().clear(); // reset

// Now verify that all modes failed as dev. First as GUI.
ScriptApproval.get().preapprove("echo configured by admin", SystemCommandLanguage.get());
s.setLauncher(new CommandLauncher("echo configured by admin"));
Expand Down Expand Up @@ -187,4 +201,27 @@ public void evaluate() throws Throwable {
});
}

static class PendingScriptApprovalMatcher extends CustomTypeSafeMatcher<PendingScript> {

private final String expectedScript;

private PendingScriptApprovalMatcher(String expectedScript) {
super("PendingScript with script " + expectedScript);
this.expectedScript = expectedScript;
}

@Override
protected boolean matchesSafely(PendingScript item) {
return expectedScript.equals(item.script);
}

@Override
public void describeMismatchSafely(PendingScript item, Description mismatchDescription) {
mismatchDescription.appendText("has script ").appendText(item.script);
}

public static PendingScriptApprovalMatcher pendingScript(String expectedScript) {
return new PendingScriptApprovalMatcher(expectedScript);
}
}
}

0 comments on commit 7738605

Please sign in to comment.