Skip to content

Commit

Permalink
Handle cases where SecureGroovyScript now throws Descriptor.FormExcep…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
kinow committed Dec 26, 2024
1 parent c532efb commit da0b320
Show file tree
Hide file tree
Showing 22 changed files with 82 additions and 48 deletions.
17 changes: 13 additions & 4 deletions src/main/java/org/biouno/unochoice/model/GroovyScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import edu.umd.cs.findbugs.annotations.Nullable;

import hudson.model.Descriptor;
import org.biouno.unochoice.util.SafeHtmlExtendedMarkupFormatter;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
Expand Down Expand Up @@ -85,7 +86,7 @@ public class GroovyScript extends AbstractScript {
private SecureGroovyScript secureFallbackScript;

@Deprecated
public GroovyScript(String script, String fallbackScript) {
public GroovyScript(String script, String fallbackScript) throws Descriptor.FormException {
this(new SecureGroovyScript(script, false, null), new SecureGroovyScript(fallbackScript, false, null));
}

Expand All @@ -102,13 +103,21 @@ public GroovyScript(SecureGroovyScript script, SecureGroovyScript fallbackScript
private Object readResolve() {
if (secureScript == null) {
if (script != null) {
secureScript = new SecureGroovyScript(script, false, null).configuring(ApprovalContext.create());
try {
secureScript = new SecureGroovyScript(script, false, null).configuring(ApprovalContext.create());
} catch (Descriptor.FormException e) {
throw new RuntimeException("Failed to create SecureGroovyScript", e);
}
}
}
if (secureFallbackScript == null) {
if (fallbackScript != null) {
secureFallbackScript = new SecureGroovyScript(fallbackScript, false, null)
.configuring(ApprovalContext.create());
try {
secureFallbackScript = new SecureGroovyScript(fallbackScript, false, null)
.configuring(ApprovalContext.create());
} catch (Descriptor.FormException e) {
throw new RuntimeException("Failed to create SecureGroovyScript", e);
}
}
}
return this;
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/biouno/unochoice/model/ScriptlerScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.stream.Collectors;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.Descriptor;
import org.biouno.unochoice.util.Utils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.scriptler.ScriptlerManagement;
Expand Down Expand Up @@ -217,9 +218,9 @@ public Object eval(Map<String, String> parameters) {
/**
* Converts this scriptler script to a GroovyScript.
*
* The script will run in the Groovy Sandbox environment by default, unless approved by a
* <p>The script will run in the Groovy Sandbox environment by default, unless approved by a
* Jenkins administrator. In this case it won't use the Groovy Sandbox. This is useful if
* the Groovy script needs access to API not available in the Sandbox (e.g. Grapes).
* the Groovy script needs access to API not available in the Sandbox (e.g. Grapes).</p>
*
* @return a GroovyScript
*/
Expand All @@ -228,7 +229,11 @@ public GroovyScript toGroovyScript() {
if (scriptler == null) {
throw new RuntimeException("Missing required scriptler!");
}
return new GroovyScript(new SecureGroovyScript(scriptler.script, this.isSandboxed, null), null);
try {
return new GroovyScript(new SecureGroovyScript(scriptler.script, this.isSandboxed, null), null);
} catch (Descriptor.FormException e) {
throw new RuntimeException("Failed to create GroovyScript", e);
}
}

// --- descriptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import static org.junit.Assert.assertEquals;

import hudson.model.Descriptor;
import org.biouno.unochoice.model.GroovyScript;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void setUp() throws Exception {
}

@Test
public void testCreateValue() {
public void testCreateValue() throws Descriptor.FormException {
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
ChoiceParameter param = new ChoiceParameter("name", "description", "some-random-name", script, "choiceType",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.LinkedHashMap;
import java.util.Map;

import hudson.model.Descriptor;
import org.biouno.unochoice.model.GroovyScript;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
Expand All @@ -57,7 +58,7 @@ public void setUp() throws Exception {
}

@Test
public void testConstructor() {
public void testConstructor() throws Descriptor.FormException {
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
CascadeChoiceParameter param = new CascadeChoiceParameter("param000", "description", "some-random-name", script,
Expand All @@ -74,7 +75,7 @@ public void testConstructor() {
}

@Test
public void testParameters() {
public void testParameters() throws Descriptor.FormException {
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
CascadeChoiceParameter param = new CascadeChoiceParameter("param000", "description", "some-random-name", script,
Expand Down Expand Up @@ -105,7 +106,7 @@ public void testParameters() {
}

@Test
public void testNullFilterable() {
public void testNullFilterable() throws Descriptor.FormException {
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
CascadeChoiceParameter param = new CascadeChoiceParameter("param000", "description", "some-random-name", script,
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/biouno/unochoice/TestChoiceParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import hudson.model.Descriptor;
import org.biouno.unochoice.model.GroovyScript;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
Expand All @@ -52,7 +53,7 @@ public void setUp() throws Exception {
}

@Test
public void testConstructor() {
public void testConstructor() throws Descriptor.FormException {
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
ChoiceParameter param = new ChoiceParameter("param000", "description", "some-random-name", script,
Expand All @@ -68,7 +69,7 @@ public void testConstructor() {
}

@Test
public void testNullFilterable() {
public void testNullFilterable() throws Descriptor.FormException {
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
ChoiceParameter param = new ChoiceParameter("param000", "description", "some-random-name", script,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import hudson.model.Descriptor;
import org.biouno.unochoice.model.GroovyScript;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
Expand All @@ -52,7 +53,7 @@ public void setUp() throws Exception {
}

@Test
public void testConstructor() {
public void testConstructor() throws Descriptor.FormException {
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
DynamicReferenceParameter param = new DynamicReferenceParameter("param000", "description", "some-random-name",
Expand All @@ -68,7 +69,7 @@ public void testConstructor() {
}

@Test
public void testNullOmitValueField() {
public void testNullOmitValueField() throws Descriptor.FormException {
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
DynamicReferenceParameter param = new DynamicReferenceParameter("param000", "description", "some-random-name",
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/biouno/unochoice/TestParametersOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.LinkedHashMap;
import java.util.Map;

import hudson.model.Descriptor;
import org.biouno.unochoice.model.GroovyScript;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
Expand All @@ -54,7 +55,7 @@ public void setUp() throws Exception {
}

@Test
public void testParametersOrder() {
public void testParametersOrder() throws Descriptor.FormException {
Map<Object, Object> parameters = new LinkedHashMap<>();
parameters.put("D", "D");
parameters.put("C", "C");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.HashMap;
import java.util.Map;

import hudson.model.Descriptor;
import org.biouno.unochoice.CascadeChoiceParameter;
import org.biouno.unochoice.ChoiceParameter;
import org.biouno.unochoice.model.GroovyScript;
Expand Down Expand Up @@ -64,7 +65,7 @@ public void setUp() throws Exception {
}

@Test
public void testScriptAccessingGlobalProperties() {
public void testScriptAccessingGlobalProperties() throws Descriptor.FormException {
Map<String, String> testMap = new HashMap<>();
testMap.put("time", "20:13:13");
EnvironmentVariablesNodeProperty.Entry entry = new EnvironmentVariablesNodeProperty.Entry("NODE_TIME",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.io.IOException;

import hudson.model.Descriptor;
import org.biouno.unochoice.CascadeChoiceParameter;
import org.biouno.unochoice.ChoiceParameter;
import org.biouno.unochoice.DynamicReferenceParameter;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void setUp() throws Exception {
}

@Test
public void testEvaluationWorksEvenThoughWeUsedEqualsInParameterValues() throws IOException {
public void testEvaluationWorksEvenThoughWeUsedEqualsInParameterValues() throws IOException, Descriptor.FormException {
GroovyScript listScript = new GroovyScript(new SecureGroovyScript(SCRIPT_LIST, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT_LIST, Boolean.FALSE, null));
GroovyScript listSelectionScript = new GroovyScript(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.util.Map;

import hudson.model.Descriptor;
import org.biouno.unochoice.CascadeChoiceParameter;
import org.biouno.unochoice.ChoiceParameter;
import org.biouno.unochoice.model.GroovyScript;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void setUp() throws Exception {
}

@Test
public void testParameterObjectIsPresent() throws IOException {
public void testParameterObjectIsPresent() throws IOException, Descriptor.FormException {
GroovyScript listScript = new GroovyScript(new SecureGroovyScript(SCRIPT_LIST, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT_LIST, Boolean.FALSE, null));
ChoiceParameter listParam = new ChoiceParameter(PARAMETER_NAME, "description...", "random-name1", listScript,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.IOException;
import java.util.Map;

import hudson.model.Descriptor;
import org.biouno.unochoice.CascadeChoiceParameter;
import org.biouno.unochoice.ChoiceParameter;
import org.biouno.unochoice.model.GroovyScript;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void setUp() throws Exception {
}

@Test
public void testProjectsAreDifferent() throws IOException, SAXException {
public void testProjectsAreDifferent() throws IOException, SAXException, Descriptor.FormException {
GroovyScript listScript = new GroovyScript(new SecureGroovyScript(SCRIPT_LIST, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT_LIST, Boolean.FALSE, null));

Expand Down Expand Up @@ -103,4 +104,4 @@ public void testProjectsAreDifferent() throws IOException, SAXException {
assertTrue("Missing project full name for A!", listSelectionValueA.containsKey(FOLDER_NAME_A+"/"+PROJECT_NAME));
assertTrue("Missing project full name for B!", listSelectionValueB.containsKey(FOLDER_NAME_B+"/"+PROJECT_NAME));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.IOException;
import java.util.Map;

import hudson.model.Descriptor;
import org.biouno.unochoice.CascadeChoiceParameter;
import org.biouno.unochoice.ChoiceParameter;
import org.biouno.unochoice.model.GroovyScript;
Expand Down Expand Up @@ -72,7 +73,7 @@ public void setUp() throws Exception {
}

@Test
public void testProjectAreDifferent() throws IOException {
public void testProjectAreDifferent() throws IOException, Descriptor.FormException {

FreeStyleProject project = j.createProject(FreeStyleProject.class, PROJECT_NAME_BEFORE);

Expand Down Expand Up @@ -102,4 +103,4 @@ public void testProjectAreDifferent() throws IOException {
// Now, check full name!
assertTrue("Wrong project name after renaming: "+choicesStatus, listSelectionValueAfter.containsKey(PROJECT_NAME_AFTER));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import static org.junit.Assert.assertEquals;

import hudson.model.Descriptor;
import org.biouno.unochoice.ChoiceParameter;
import org.biouno.unochoice.model.GroovyScript;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
Expand Down Expand Up @@ -54,7 +55,7 @@ public class TestDefaultValuesOnParamBuildPage {
public JenkinsRule j = new JenkinsRule();

@Test
public void testReturnEmptyWhenNoElementsAreAvailable() {
public void testReturnEmptyWhenNoElementsAreAvailable() throws Descriptor.FormException {
ChoiceParameter parameter = createChoiceParameter(
ChoiceParameter.PARAMETER_TYPE_RADIO,
"return []"
Expand All @@ -67,7 +68,7 @@ public void testReturnEmptyWhenNoElementsAreAvailable() {
}

@Test
public void testReturnEmptyWhenOnlyEmptyElementIsDefined() {
public void testReturnEmptyWhenOnlyEmptyElementIsDefined() throws Descriptor.FormException {
ChoiceParameter parameter = createChoiceParameter(
ChoiceParameter.PARAMETER_TYPE_RADIO,
"return ['']"
Expand All @@ -80,7 +81,7 @@ public void testReturnEmptyWhenOnlyEmptyElementIsDefined() {
}

@Test
public void testReturnFirstElementWhenSelectedIsNotSet() {
public void testReturnFirstElementWhenSelectedIsNotSet() throws Descriptor.FormException {
ChoiceParameter parameter = createChoiceParameter(
ChoiceParameter.PARAMETER_TYPE_RADIO,
"return ['A', 'B', 'C', 'D']"
Expand All @@ -93,7 +94,7 @@ public void testReturnFirstElementWhenSelectedIsNotSet() {
}

@Test
public void testReturnSelectedElement() {
public void testReturnSelectedElement() throws Descriptor.FormException {
ChoiceParameter parameter = createChoiceParameter(
ChoiceParameter.PARAMETER_TYPE_RADIO,
"return ['A', 'B', 'C:selected', 'D', 'E:disabled']"
Expand All @@ -106,7 +107,7 @@ public void testReturnSelectedElement() {
}

@Test
public void testReturnSelectedElements() {
public void testReturnSelectedElements() throws Descriptor.FormException {
ChoiceParameter parameter = createChoiceParameter(
ChoiceParameter.PARAMETER_TYPE_CHECK_BOX,
"return ['A', 'B:selected', 'C', 'D:selected', 'E:disabled']"
Expand All @@ -119,7 +120,7 @@ public void testReturnSelectedElements() {
}

@Test
public void testReturnSelectedEmptyElement() {
public void testReturnSelectedEmptyElement() throws Descriptor.FormException {
ChoiceParameter parameter = createChoiceParameter(
ChoiceParameter.PARAMETER_TYPE_RADIO,
"return ['A', 'B', ':selected', 'D', 'E:disabled']"
Expand All @@ -132,7 +133,7 @@ public void testReturnSelectedEmptyElement() {
}

@Test
public void testAllSuffixesAreTrimmed() {
public void testAllSuffixesAreTrimmed() throws Descriptor.FormException {
ChoiceParameter parameter = createChoiceParameter(
ChoiceParameter.PARAMETER_TYPE_CHECK_BOX,
"return ['A:selected:disabled', 'B:disabled:selected', 'C', 'D:selected']"
Expand All @@ -144,7 +145,7 @@ public void testAllSuffixesAreTrimmed() {
assertEquals("Invalid parameter value!", "A,B,D", parameterValue.getValue());
}

private static final ChoiceParameter createChoiceParameter(String type, String script) {
private static final ChoiceParameter createChoiceParameter(String type, String script) throws Descriptor.FormException {
ScriptApproval.get().preapprove(script, GroovyLanguage.get());
ScriptApproval.get().preapprove(DEFAULT_FALLBACK_SCRIPT, GroovyLanguage.get());

Expand Down
Loading

0 comments on commit da0b320

Please sign in to comment.