|
| 1 | +package jenkins.security; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.containsString; |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | + |
| 7 | +import hudson.model.Computer; |
| 8 | +import hudson.slaves.DumbSlave; |
| 9 | +import java.net.URL; |
| 10 | +import jenkins.model.Jenkins; |
| 11 | +import org.htmlunit.HttpMethod; |
| 12 | +import org.htmlunit.WebRequest; |
| 13 | +import org.htmlunit.WebResponse; |
| 14 | +import org.junit.Rule; |
| 15 | +import org.junit.Test; |
| 16 | +import org.jvnet.hudson.test.Issue; |
| 17 | +import org.jvnet.hudson.test.JenkinsRule; |
| 18 | +import org.jvnet.hudson.test.MockAuthorizationStrategy; |
| 19 | + |
| 20 | +public class Security3512Test { |
| 21 | + |
| 22 | + @Rule |
| 23 | + public JenkinsRule j = new JenkinsRule(); |
| 24 | + |
| 25 | + @Test |
| 26 | + @Issue("SECURITY-3512") |
| 27 | + public void copyAgentTest() throws Exception { |
| 28 | + Computer.EXTENDED_READ.setEnabled(true); |
| 29 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 30 | + MockAuthorizationStrategy mockAuthorizationStrategy = new MockAuthorizationStrategy(); |
| 31 | + mockAuthorizationStrategy.grant(Jenkins.READ, Computer.CREATE, Computer.EXTENDED_READ).everywhere().to("alice"); |
| 32 | + mockAuthorizationStrategy.grant(Jenkins.READ, Computer.CREATE).everywhere().to("bob"); |
| 33 | + j.jenkins.setAuthorizationStrategy(mockAuthorizationStrategy); |
| 34 | + |
| 35 | + DumbSlave agent = j.createOnlineSlave(); |
| 36 | + |
| 37 | + assertEquals(2, j.getInstance().getComputers().length); |
| 38 | + |
| 39 | + String agentCopyURL = j.getURL() + "/computer/createItem?mode=copy&from=" + agent.getNodeName() + "&name="; |
| 40 | + |
| 41 | + { // with ExtendedRead permission you can copy a node |
| 42 | + try (JenkinsRule.WebClient wc = j.createWebClient().withThrowExceptionOnFailingStatusCode(false).login("alice")) { |
| 43 | + WebResponse rsp = wc.getPage(wc.addCrumb(new WebRequest(new URL(agentCopyURL + "aliceAgent"), |
| 44 | + HttpMethod.POST))).getWebResponse(); |
| 45 | + |
| 46 | + assertEquals(200, rsp.getStatusCode()); |
| 47 | + assertEquals(3, j.getInstance().getComputers().length); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + { // without ExtendedRead permission you cannot copy a node |
| 52 | + try (JenkinsRule.WebClient wc = j.createWebClient().withThrowExceptionOnFailingStatusCode(false).login("bob")) { |
| 53 | + WebResponse rsp = wc.getPage(wc.addCrumb(new WebRequest(new URL(agentCopyURL + "bobAgent"), |
| 54 | + HttpMethod.POST))).getWebResponse(); |
| 55 | + |
| 56 | + assertEquals(403, rsp.getStatusCode()); |
| 57 | + assertThat(rsp.getContentAsString(), containsString("bob is missing the Agent/ExtendedRead permission")); |
| 58 | + assertEquals(3, j.getInstance().getComputers().length); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments