-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from mawinter69/JENKINS-68841
[JENKINS-68841] fix node ownership permission problem
- Loading branch information
Showing
8 changed files
with
388 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/test/java/org/jenkinsci/plugins/rolestrategy/AuthorizeProjectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package org.jenkinsci.plugins.rolestrategy; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.jenkinsci.plugins.authorizeproject.AuthorizeProjectProperty; | ||
import org.jenkinsci.plugins.authorizeproject.ProjectQueueItemAuthenticator; | ||
import org.jenkinsci.plugins.authorizeproject.strategy.TriggeringUsersAuthorizationStrategy; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.JenkinsRule.DummySecurityRealm; | ||
import org.jvnet.hudson.test.recipes.LocalData; | ||
import org.springframework.security.core.Authentication; | ||
|
||
import hudson.Launcher; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.AbstractProject; | ||
import hudson.model.BuildListener; | ||
import hudson.model.Cause; | ||
import hudson.model.FreeStyleBuild; | ||
import hudson.model.FreeStyleProject; | ||
import hudson.model.Slave; | ||
import hudson.model.User; | ||
import hudson.model.Queue.Item; | ||
import hudson.model.Queue.WaitingItem; | ||
import hudson.security.ACL; | ||
import hudson.security.ACLContext; | ||
import hudson.tasks.BuildStepDescriptor; | ||
import hudson.tasks.Builder; | ||
import jenkins.model.Jenkins; | ||
import jenkins.security.QueueItemAuthenticatorConfiguration; | ||
|
||
public class AuthorizeProjectTest | ||
{ | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
private Slave n; | ||
private FreeStyleProject p; | ||
private AuthorizationCheckBuilder checker; | ||
|
||
@Before | ||
@LocalData | ||
public void setup() throws Exception { | ||
Authentication a = j.jenkins.getAuthentication2(); | ||
QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new ProjectQueueItemAuthenticator(Collections.emptyMap())); | ||
n = j.createSlave("TestAgent", null, null); | ||
j.waitOnline(n); | ||
p = j.createFreeStyleProject(); | ||
p.setAssignedNode(n); | ||
p.addProperty(new AuthorizeProjectProperty(new TriggeringUsersAuthorizationStrategy())); | ||
checker = new AuthorizationCheckBuilder(); | ||
p.getBuildersList().add(checker); | ||
DummySecurityRealm sr = j.createDummySecurityRealm(); | ||
j.jenkins.setSecurityRealm(sr); | ||
} | ||
|
||
@Test | ||
@LocalData | ||
public void agentBuildPermissionsAllowsToBuildOnAgent() throws Exception { | ||
try (ACLContext c = ACL.as(User.getById("tester", true))) { | ||
p.scheduleBuild2(0, new Cause.UserIdCause()); | ||
} | ||
j.waitUntilNoActivity(); | ||
FreeStyleBuild b = p.getLastBuild(); | ||
assertThat(b, is(not(nullValue()))); | ||
j.assertBuildStatusSuccess(b); | ||
assertThat(checker.userName, is("tester")); | ||
} | ||
|
||
@Test | ||
@LocalData | ||
public void missingAgentBuildPermissionsBlockBuild() throws Exception { | ||
try (ACLContext c = ACL.as(User.getById("reader", true))) { | ||
p.scheduleBuild2(0, new Cause.UserIdCause()); | ||
} | ||
TimeUnit.SECONDS.sleep(15); | ||
Item qi = p.getQueueItem(); | ||
assertThat(qi.getCauseOfBlockage().toString(), containsString("‘reader’ lacks permission to run on ‘TestAgent’")); | ||
} | ||
|
||
public static class AuthorizationCheckBuilder extends Builder { | ||
|
||
// "transient" is required for exclusion from serialization - see https://jenkins.io/redirect/class-filter/ | ||
public transient String userName = null; | ||
|
||
@Override | ||
public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) { | ||
userName = null; | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, | ||
BuildListener listener) throws InterruptedException, IOException { | ||
userName = Jenkins.getAuthentication2().getName(); | ||
return true; | ||
} | ||
|
||
public static class DescriptorImpl extends BuildStepDescriptor<Builder> { | ||
@SuppressWarnings("rawtypes") | ||
@Override | ||
public boolean isApplicable(Class<? extends AbstractProject> jobType) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "AuthorizationCheckBuilder"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/test/java/org/jenkinsci/plugins/rolestrategy/OwnershipTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.jenkinsci.plugins.rolestrategy; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.oneOf; | ||
|
||
import java.net.URL; | ||
import java.util.Collections; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule.WebClient; | ||
|
||
import com.gargoylesoftware.htmlunit.HttpMethod; | ||
import com.gargoylesoftware.htmlunit.WebRequest; | ||
import com.gargoylesoftware.htmlunit.WebResponse; | ||
import com.gargoylesoftware.htmlunit.html.HtmlPage; | ||
import com.gargoylesoftware.htmlunit.util.NameValuePair; | ||
import com.synopsys.arc.jenkins.plugins.ownership.OwnershipDescription; | ||
import com.synopsys.arc.jenkins.plugins.ownership.nodes.OwnerNodeProperty; | ||
|
||
import hudson.model.Node; | ||
import io.jenkins.plugins.casc.misc.ConfiguredWithCode; | ||
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule; | ||
|
||
public class OwnershipTest | ||
{ | ||
@Rule | ||
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule(); | ||
|
||
@Test | ||
@ConfiguredWithCode("OwnershipTest.yml") | ||
public void currentUserIsPrimaryOwnerGrantsPermissions() throws Exception { | ||
Node n = j.createOnlineSlave(); | ||
n.getNodeProperties().add(new OwnerNodeProperty(n, new OwnershipDescription(true, "nodePrimaryTester", null))); | ||
String nodeUrl = n.toComputer().getUrl(); | ||
|
||
WebClient wc = j.createWebClient(); | ||
wc.withThrowExceptionOnFailingStatusCode(false); | ||
wc.login("nodePrimaryTester", "nodePrimaryTester"); | ||
HtmlPage managePage = wc.withThrowExceptionOnFailingStatusCode(false).goTo(String.format("%sconfigure", nodeUrl)); | ||
assertThat(managePage.getWebResponse().getStatusCode(), is(200)); | ||
URL testUrl = wc.createCrumbedUrl(String.format("%sdisconnect", nodeUrl)); | ||
WebRequest request = new WebRequest(testUrl, HttpMethod.POST); | ||
NameValuePair param = new NameValuePair("offlineMessage", "Disconnect for Test"); | ||
request.setRequestParameters(Collections.singletonList(param)); | ||
WebResponse response = wc.loadWebResponse(request); | ||
assertThat(response.getStatusCode(), is(200)); | ||
|
||
testUrl = wc.createCrumbedUrl(String.format("%slaunchSlaveAgent", nodeUrl)); | ||
request = new WebRequest(testUrl, HttpMethod.POST); | ||
response = wc.loadWebResponse(request); | ||
assertThat(response.getStatusCode(), is(oneOf(200, 302))); | ||
|
||
testUrl = wc.createCrumbedUrl(String.format("%sdoDelete", nodeUrl)); | ||
request = new WebRequest(testUrl, HttpMethod.POST); | ||
response = wc.loadWebResponse(request); | ||
assertThat(response.getStatusCode(), is(200)); | ||
} | ||
|
||
@Test | ||
@ConfiguredWithCode("OwnershipTest.yml") | ||
public void currentUserIsSecondaryOwnerGrantsPermissions() throws Exception { | ||
Node n = j.createOnlineSlave(); | ||
n.getNodeProperties().add(new OwnerNodeProperty(n, new OwnershipDescription(true, "nodePrimaryTester", Collections.singleton("nodeSecondaryTester")))); | ||
String nodeUrl = n.toComputer().getUrl(); | ||
|
||
WebClient wc = j.createWebClient(); | ||
wc.withThrowExceptionOnFailingStatusCode(false); | ||
wc.login("nodeSecondaryTester", "nodeSecondaryTester"); | ||
HtmlPage managePage = wc.withThrowExceptionOnFailingStatusCode(false).goTo(String.format("%sconfigure", nodeUrl)); | ||
assertThat(managePage.getWebResponse().getStatusCode(), is(200)); | ||
URL testUrl = wc.createCrumbedUrl(String.format("%sdisconnect", nodeUrl)); | ||
WebRequest request = new WebRequest(testUrl, HttpMethod.POST); | ||
NameValuePair param = new NameValuePair("offlineMessage", "Disconnect for Test"); | ||
request.setRequestParameters(Collections.singletonList(param)); | ||
WebResponse response = wc.loadWebResponse(request); | ||
assertThat(response.getStatusCode(), is(200)); | ||
|
||
testUrl = wc.createCrumbedUrl(String.format("%slaunchSlaveAgent", nodeUrl)); | ||
request = new WebRequest(testUrl, HttpMethod.POST); | ||
response = wc.loadWebResponse(request); | ||
assertThat(response.getStatusCode(), is(oneOf(200, 302))); | ||
|
||
testUrl = wc.createCrumbedUrl(String.format("%sdoDelete", nodeUrl)); | ||
request = new WebRequest(testUrl, HttpMethod.POST); | ||
response = wc.loadWebResponse(request); | ||
assertThat(response.getStatusCode(), is(200)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/test/resources/org/jenkinsci/plugins/rolestrategy/AuthorizeProjectTest/config.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?xml version='1.1' encoding='UTF-8'?> | ||
<hudson> | ||
<disabledAdministrativeMonitors> | ||
<string>jenkins.security.QueueItemAuthenticatorMonitor</string> | ||
</disabledAdministrativeMonitors> | ||
<version>2.303.3</version> | ||
<numExecutors>2</numExecutors> | ||
<mode>NORMAL</mode> | ||
<useSecurity>true</useSecurity> | ||
<authorizationStrategy class="com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy"> | ||
<roleMap type="globalRoles"> | ||
<role name="admin" pattern=".*"> | ||
<permissions> | ||
<permission>hudson.model.Hudson.Administer</permission> | ||
</permissions> | ||
<assignedSIDs> | ||
<sid>admin</sid> | ||
</assignedSIDs> | ||
</role> | ||
<role name="builder" pattern=".*"> | ||
<permissions> | ||
<permission>hudson.model.Hudson.Read</permission> | ||
<permission>hudson.model.Item.Read</permission> | ||
<permission>hudson.model.Item.Build</permission> | ||
</permissions> | ||
<assignedSIDs> | ||
<sid>authenticated</sid> | ||
</assignedSIDs> | ||
</role> | ||
</roleMap> | ||
<roleMap type="slaveRoles"> | ||
<role name="agentbuilder" pattern="TestAgent"> | ||
<permissions> | ||
<permission>hudson.model.Computer.Build</permission> | ||
</permissions> | ||
<assignedSIDs> | ||
<sid>tester</sid> | ||
</assignedSIDs> | ||
</role> | ||
</roleMap> | ||
</authorizationStrategy> | ||
</hudson> |
Oops, something went wrong.