Skip to content

Commit 7eddd97

Browse files
committed
[FIXED JENKINS-33614] Add link to script approval when a rejection occurs
I don't *love* this, so feel free to critique/veto.
1 parent b3d7ea3 commit 7eddd97

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/main/java/org/jenkinsci/plugins/workflow/cps/SandboxContinuable.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
import com.cloudbees.groovy.cps.Continuable;
44
import com.cloudbees.groovy.cps.Outcome;
5+
6+
import java.io.IOException;
57
import java.util.List;
8+
9+
import hudson.console.HyperlinkNote;
610
import org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException;
711
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox;
812
import org.jenkinsci.plugins.scriptsecurity.scripts.ApprovalContext;
913
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
1014

1115
import java.util.concurrent.Callable;
16+
import java.util.logging.Level;
17+
import java.util.logging.Logger;
1218
import javax.annotation.CheckForNull;
1319

1420
/**
@@ -36,6 +42,13 @@ public Outcome call() {
3642
RejectedAccessException x = findRejectedAccessException(outcome.getAbnormal());
3743
if (x != null) {
3844
ScriptApproval.get().accessRejected(x, ApprovalContext.create());
45+
try {
46+
e.getOwner().getListener().getLogger().println(x.getMessage() + ". " +
47+
HyperlinkNote.encodeTo("/" + ScriptApproval.get().getUrlName(),
48+
Messages.SandboxContinuable_ScriptApprovalLink()));
49+
} catch (IOException ex) {
50+
LOGGER.log(Level.WARNING, null, ex);
51+
}
3952
}
4053
return outcome;
4154
}
@@ -59,4 +72,5 @@ public Outcome call() {
5972
}
6073
}
6174

75+
private static final Logger LOGGER = Logger.getLogger(SandboxContinuable.class.getName());
6276
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
Snippetizer.this_step_should_not_normally_be_used_in=This step should not normally be used in your script. Consult the inline help for details.
2+
SandboxContinuable.ScriptApprovalLink=Administrators can click here to approve or reject this signature.

src/test/java/org/jenkinsci/plugins/workflow/cps/CpsFlowDefinition2Test.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
package org.jenkinsci.plugins.workflow.cps;
2626

2727
import com.cloudbees.groovy.cps.CpsTransformer;
28+
import com.gargoylesoftware.htmlunit.TextPage;
29+
import com.gargoylesoftware.htmlunit.html.DomNodeUtil;
30+
import com.gargoylesoftware.htmlunit.html.HtmlPage;
2831
import hudson.Functions;
2932
import hudson.model.Computer;
3033
import hudson.model.Executor;
@@ -34,6 +37,8 @@
3437
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
3538
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
3639
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
40+
41+
import static org.hamcrest.Matchers.containsString;
3742
import static org.junit.Assert.*;
3843

3944
import org.junit.Assert;
@@ -177,6 +182,15 @@ public void sandboxInvokerUsed() throws Exception {
177182

178183
WorkflowRun r = jenkins.assertBuildStatus(Result.FAILURE, job.scheduleBuild2(0).get());
179184
jenkins.assertLogContains("org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance", r);
185+
jenkins.assertLogContains("Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance. " + Messages.SandboxContinuable_ScriptApprovalLink(), r);
186+
187+
// make sure we see the annotation
188+
HtmlPage rsp = jenkins.createWebClient().getPage(r, "console");
189+
assertEquals(1, DomNodeUtil.selectNodes(rsp, "//A[@href='" + jenkins.contextPath + "/scriptApproval']").size());
190+
191+
// make sure raw console output doesn't include the garbage
192+
TextPage raw = (TextPage)jenkins.createWebClient().goTo(r.getUrl()+"consoleText","text/plain");
193+
assertThat(raw.getContent(), containsString(" getInstance. " + Messages.SandboxContinuable_ScriptApprovalLink()));
180194
}
181195

182196
@Issue("SECURITY-551")

0 commit comments

Comments
 (0)