|
26 | 26 |
|
27 | 27 | import org.htmlunit.html.HtmlPage;
|
28 | 28 | import org.htmlunit.html.HtmlTextArea;
|
| 29 | + |
| 30 | +import hudson.model.Descriptor; |
29 | 31 | import hudson.model.FreeStyleBuild;
|
30 | 32 | import hudson.model.FreeStyleProject;
|
31 | 33 | import hudson.model.Item;
|
@@ -209,23 +211,7 @@ public void reload() throws Exception {
|
209 | 211 |
|
210 | 212 | @Test
|
211 | 213 | public void forceSandboxTests() throws Exception {
|
212 |
| - r.jenkins.setSecurityRealm(r.createDummySecurityRealm()); |
213 |
| - |
214 |
| - ScriptApproval.get().setForceSandbox(true); |
215 |
| - |
216 |
| - MockAuthorizationStrategy mockStrategy = new MockAuthorizationStrategy(); |
217 |
| - mockStrategy.grant(Jenkins.READ).everywhere().to("devel"); |
218 |
| - for (Permission p : Item.PERMISSIONS.getPermissions()) { |
219 |
| - mockStrategy.grant(p).everywhere().to("devel"); |
220 |
| - } |
221 |
| - |
222 |
| - mockStrategy.grant(Jenkins.READ).everywhere().to("admin"); |
223 |
| - mockStrategy.grant(Jenkins.ADMINISTER).everywhere().to("admin"); |
224 |
| - for (Permission p : Item.PERMISSIONS.getPermissions()) { |
225 |
| - mockStrategy.grant(p).everywhere().to("admin"); |
226 |
| - } |
227 |
| - |
228 |
| - r.jenkins.setAuthorizationStrategy(mockStrategy); |
| 214 | + setBasicSecurity(); |
229 | 215 |
|
230 | 216 | try (ACLContext ctx = ACL.as(User.getById("devel", true))) {
|
231 | 217 | assertTrue(ScriptApproval.get().isForceSandbox());
|
@@ -299,10 +285,7 @@ public void forceSandboxScriptSignatureException() throws Exception {
|
299 | 285 |
|
300 | 286 | @Test
|
301 | 287 | public void forceSandboxFormValidation() throws Exception {
|
302 |
| - r.jenkins.setSecurityRealm(r.createDummySecurityRealm()); |
303 |
| - r.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy(). |
304 |
| - grant(Jenkins.READ, Item.READ).everywhere().to("dev"). |
305 |
| - grant(Jenkins.ADMINISTER).everywhere().to("admin")); |
| 288 | + setBasicSecurity(); |
306 | 289 |
|
307 | 290 | try (ACLContext ctx = ACL.as(User.getById("devel", true))) {
|
308 | 291 | ScriptApproval.get().setForceSandbox(true);
|
@@ -346,6 +329,98 @@ public void forceSandboxFormValidation() throws Exception {
|
346 | 329 | }
|
347 | 330 | }
|
348 | 331 |
|
| 332 | + @Test |
| 333 | + public void shouldHideSandboxTest() throws Exception { |
| 334 | + setBasicSecurity(); |
| 335 | + |
| 336 | + ScriptApproval.get().setForceSandbox(true); |
| 337 | + |
| 338 | + SecureGroovyScript testSandboxTrue = new SecureGroovyScript("jenkins.model.Jenkins.instance", true, null); |
| 339 | + SecureGroovyScript testSandboxFalse = new SecureGroovyScript("jenkins.model.Jenkins.instance", false, null); |
| 340 | + |
| 341 | + try (ACLContext ctx = ACL.as(User.getById("devel", true))) { |
| 342 | + assertTrue(ScriptApproval.shouldHideSandbox(testSandboxTrue, SecureGroovyScript::isSandbox)); |
| 343 | + assertFalse(ScriptApproval.shouldHideSandbox(testSandboxFalse, SecureGroovyScript::isSandbox)); |
| 344 | + assertTrue(ScriptApproval.shouldHideSandbox(null, SecureGroovyScript::isSandbox)); |
| 345 | + } |
| 346 | + |
| 347 | + try (ACLContext ctx = ACL.as(User.getById("admin", true))) { |
| 348 | + assertFalse(ScriptApproval.shouldHideSandbox(testSandboxTrue, SecureGroovyScript::isSandbox)); |
| 349 | + assertFalse(ScriptApproval.shouldHideSandbox(testSandboxFalse, SecureGroovyScript::isSandbox)); |
| 350 | + assertFalse(ScriptApproval.shouldHideSandbox(null, SecureGroovyScript::isSandbox)); |
| 351 | + } |
| 352 | + |
| 353 | + ScriptApproval.get().setForceSandbox(false); |
| 354 | + |
| 355 | + try (ACLContext ctx = ACL.as(User.getById("devel", true))) { |
| 356 | + assertFalse(ScriptApproval.shouldHideSandbox(testSandboxTrue, SecureGroovyScript::isSandbox)); |
| 357 | + assertFalse(ScriptApproval.shouldHideSandbox(testSandboxFalse, SecureGroovyScript::isSandbox)); |
| 358 | + assertFalse(ScriptApproval.shouldHideSandbox(null, SecureGroovyScript::isSandbox)); |
| 359 | + } |
| 360 | + |
| 361 | + try (ACLContext ctx = ACL.as(User.getById("admin", true))) { |
| 362 | + assertFalse(ScriptApproval.shouldHideSandbox(testSandboxTrue, SecureGroovyScript::isSandbox)); |
| 363 | + assertFalse(ScriptApproval.shouldHideSandbox(testSandboxFalse, SecureGroovyScript::isSandbox)); |
| 364 | + assertFalse(ScriptApproval.shouldHideSandbox(null, SecureGroovyScript::isSandbox)); |
| 365 | + } |
| 366 | + } |
| 367 | + |
| 368 | + @Test |
| 369 | + public void validateSandboxTest() throws Exception { |
| 370 | + setBasicSecurity(); |
| 371 | + |
| 372 | + ScriptApproval.get().setForceSandbox(true); |
| 373 | + |
| 374 | + try (ACLContext ctx = ACL.as(User.getById("devel", true))) { |
| 375 | + ScriptApproval.validateSandbox(true); |
| 376 | + assertThrows(Descriptor.FormException.class, |
| 377 | + () -> ScriptApproval.validateSandbox(false)); |
| 378 | + } |
| 379 | + |
| 380 | + try (ACLContext ctx = ACL.as(User.getById("admin", true))) { |
| 381 | + ScriptApproval.validateSandbox(true); |
| 382 | + ScriptApproval.validateSandbox(false); |
| 383 | + } |
| 384 | + |
| 385 | + ScriptApproval.get().setForceSandbox(false); |
| 386 | + |
| 387 | + try (ACLContext ctx = ACL.as(User.getById("devel", true))) { |
| 388 | + ScriptApproval.validateSandbox(true); |
| 389 | + ScriptApproval.validateSandbox(false); |
| 390 | + } |
| 391 | + |
| 392 | + try (ACLContext ctx = ACL.as(User.getById("admin", true))) { |
| 393 | + ScriptApproval.validateSandbox(true); |
| 394 | + ScriptApproval.validateSandbox(false); |
| 395 | + } |
| 396 | + } |
| 397 | + |
| 398 | + /** |
| 399 | + * Will configure a mock security settings with users: |
| 400 | + * Devel: overall Read and write without admin permission |
| 401 | + * admin: System administrator |
| 402 | + */ |
| 403 | + private void setBasicSecurity() |
| 404 | + { |
| 405 | + r.jenkins.setSecurityRealm(r.createDummySecurityRealm()); |
| 406 | + |
| 407 | + ScriptApproval.get().setForceSandbox(true); |
| 408 | + |
| 409 | + MockAuthorizationStrategy mockStrategy = new MockAuthorizationStrategy(); |
| 410 | + mockStrategy.grant(Jenkins.READ).everywhere().to("devel"); |
| 411 | + for (Permission p : Item.PERMISSIONS.getPermissions()) { |
| 412 | + mockStrategy.grant(p).everywhere().to("devel"); |
| 413 | + } |
| 414 | + |
| 415 | + mockStrategy.grant(Jenkins.READ).everywhere().to("admin"); |
| 416 | + mockStrategy.grant(Jenkins.ADMINISTER).everywhere().to("admin"); |
| 417 | + for (Permission p : Item.PERMISSIONS.getPermissions()) { |
| 418 | + mockStrategy.grant(p).everywhere().to("admin"); |
| 419 | + } |
| 420 | + |
| 421 | + r.jenkins.setAuthorizationStrategy(mockStrategy); |
| 422 | + } |
| 423 | + |
349 | 424 | private Script script(String groovy) {
|
350 | 425 | return new Script(groovy);
|
351 | 426 | }
|
|
0 commit comments