3131import hudson .Functions ;
3232import hudson .model .Computer ;
3333import hudson .model .Executor ;
34+ import hudson .model .Item ;
3435import hudson .model .Result ;
36+
3537import java .util .logging .Level ;
38+
39+ import hudson .security .GlobalMatrixAuthorizationStrategy ;
40+ import jenkins .model .Jenkins ;
3641import org .jenkinsci .plugins .workflow .flow .FlowExecutionOwner ;
3742import org .jenkinsci .plugins .workflow .job .WorkflowJob ;
3843import org .jenkinsci .plugins .workflow .job .WorkflowRun ;
4954import org .junit .Test ;
5055import org .jvnet .hudson .test .BuildWatcher ;
5156import org .jvnet .hudson .test .Issue ;
57+ import org .jvnet .hudson .test .JenkinsRule ;
5258import org .jvnet .hudson .test .LoggerRule ;
5359
5460public class CpsFlowDefinition2Test extends AbstractCpsFlowTest {
@@ -174,6 +180,15 @@ public void superCallsSandboxed() throws Exception {
174180
175181 @ Test
176182 public void sandboxInvokerUsed () throws Exception {
183+ jenkins .jenkins .setSecurityRealm (jenkins .createDummySecurityRealm ());
184+ GlobalMatrixAuthorizationStrategy gmas = new GlobalMatrixAuthorizationStrategy ();
185+ // Set up a user with RUN_SCRIPTS and one without..
186+ gmas .add (Jenkins .RUN_SCRIPTS , "runScriptsUser" );
187+ gmas .add (Jenkins .READ , "runScriptsUser" );
188+ gmas .add (Item .READ , "runScriptsUser" );
189+ gmas .add (Jenkins .READ , "otherUser" );
190+ gmas .add (Item .READ , "otherUser" );
191+ jenkins .jenkins .setAuthorizationStrategy (gmas );
177192 WorkflowJob job = jenkins .jenkins .createProject (WorkflowJob .class , "p" );
178193 job .setDefinition (new CpsFlowDefinition ("[a: 1, b: 2].collectEntries { k, v ->\n " +
179194 " Jenkins.getInstance()\n " +
@@ -184,13 +199,25 @@ public void sandboxInvokerUsed() throws Exception {
184199 jenkins .assertLogContains ("org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance" , r );
185200 jenkins .assertLogContains ("Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance. " + Messages .SandboxContinuable_ScriptApprovalLink (), r );
186201
187- // make sure we see the annotation
188- HtmlPage rsp = jenkins .createWebClient ().getPage (r , "console" );
202+ JenkinsRule .WebClient wc = jenkins .createWebClient ();
203+
204+ wc .login ("runScriptsUser" );
205+ // make sure we see the annotation for the RUN_SCRIPTS user.
206+ HtmlPage rsp = wc .getPage (r , "console" );
189207 assertEquals (1 , DomNodeUtil .selectNodes (rsp , "//A[@href='" + jenkins .contextPath + "/scriptApproval']" ).size ());
190208
191- // make sure raw console output doesn't include the garbage
192- TextPage raw = (TextPage )jenkins . createWebClient () .goTo (r .getUrl ()+"consoleText" ,"text/plain" );
209+ // make sure raw console output doesn't include the garbage and has the right message.
210+ TextPage raw = (TextPage )wc .goTo (r .getUrl ()+"consoleText" ,"text/plain" );
193211 assertThat (raw .getContent (), containsString (" getInstance. " + Messages .SandboxContinuable_ScriptApprovalLink ()));
212+
213+ wc .login ("otherUser" );
214+ // make sure we don't see the link for the other user.
215+ HtmlPage rsp2 = wc .getPage (r , "console" );
216+ assertEquals (0 , DomNodeUtil .selectNodes (rsp2 , "//A[@href='" + jenkins .contextPath + "/scriptApproval']" ).size ());
217+
218+ // make sure raw console output doesn't include the garbage and has the right message.
219+ TextPage raw2 = (TextPage )wc .goTo (r .getUrl ()+"consoleText" ,"text/plain" );
220+ assertThat (raw2 .getContent (), containsString (" getInstance. " + Messages .SandboxContinuable_ScriptApprovalLink ()));
194221 }
195222
196223 @ Issue ("SECURITY-551" )
0 commit comments