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 #14 from stephenc/jenkins-30574-redux
[FIXED JENKINS-30574] Support global default authorization strategy in Authorize Project
- Loading branch information
Showing
10 changed files
with
273 additions
and
15 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/org/jenkinsci/plugins/authorizeproject/GlobalQueueItemAuthenticator.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,48 @@ | ||
package org.jenkinsci.plugins.authorizeproject; | ||
|
||
import hudson.Extension; | ||
import hudson.model.Job; | ||
import hudson.model.Queue; | ||
import jenkins.security.QueueItemAuthenticator; | ||
import jenkins.security.QueueItemAuthenticatorDescriptor; | ||
import org.acegisecurity.Authentication; | ||
import org.jenkinsci.plugins.authorizeproject.strategy.AnonymousAuthorizationStrategy; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
/** | ||
* A global default authenticator to allow changing the default for all projects. | ||
* | ||
* @since 1.1.1 | ||
*/ | ||
public class GlobalQueueItemAuthenticator extends QueueItemAuthenticator { | ||
private final AuthorizeProjectStrategy strategy; | ||
|
||
@DataBoundConstructor | ||
public GlobalQueueItemAuthenticator(AuthorizeProjectStrategy strategy) { | ||
this.strategy = strategy; | ||
} | ||
|
||
public AuthorizeProjectStrategy getStrategy() { | ||
return strategy; | ||
} | ||
|
||
@Override | ||
public Authentication authenticate(Queue.Item item) { | ||
return strategy != null && item.task instanceof Job ? strategy.authenticate((Job<?, ?>) item.task, item) : null; | ||
} | ||
|
||
@Extension | ||
public static class DescriptorImpl extends QueueItemAuthenticatorDescriptor { | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public String getDisplayName() { | ||
return Messages.GlobalQueueItemAuthenticator_DisplayName(); | ||
} | ||
|
||
public AuthorizeProjectStrategy getDefaultStrategy() { | ||
return new AnonymousAuthorizationStrategy(); | ||
} | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
...esources/org/jenkinsci/plugins/authorizeproject/GlobalQueueItemAuthenticator/config.jelly
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,28 @@ | ||
<!-- | ||
The MIT License | ||
Copyright (c) 2015 Stephen Connolly | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
--> | ||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" | ||
xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:hack="/org/jenkinsci/plugins/authorizeproject/form"> | ||
<hack:dropdownDescriptorSelector field="strategy" default="${descriptor.defaultStrategy}" title="${%Strategy}"/> | ||
</j:jelly> |
8 changes: 8 additions & 0 deletions
8
...n/resources/org/jenkinsci/plugins/authorizeproject/GlobalQueueItemAuthenticator/help.html
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,8 @@ | ||
<div> | ||
<p>Specify the authorization to use for projects.</p> | ||
<p> | ||
<strong>NOTE:</strong> | ||
As this authenticator will be applied unless a previous authenticator has provided an authentication, | ||
it is best to put this authenticator last in the list as any other authenticators after it will be ignored. | ||
</p> | ||
</div> |
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
5 changes: 4 additions & 1 deletion
5
.../resources/org/jenkinsci/plugins/authorizeproject/ProjectQueueItemAuthenticator/help.html
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
<div> | ||
You can specify authorization of builds in project configuration pages. | ||
<p> | ||
Allows the authentication that a project will run as to be configured from the project configuration page. | ||
The strategies that are available to the user are controlled | ||
</p> | ||
</div> |
77 changes: 77 additions & 0 deletions
77
...in/resources/org/jenkinsci/plugins/authorizeproject/form/dropdownDescriptorSelector.jelly
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,77 @@ | ||
<!-- | ||
The MIT License | ||
Copyright (c) 2010, InfraDNA, Inc. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
--> | ||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" | ||
xmlns:t="/lib/hudson" xmlns:f="/lib/form"> | ||
<st:documentation> | ||
Renders a single <select> control for choosing a Describable. | ||
Depending on the currently selected value, its config.jelly will be | ||
rendered below <select, allowing the user to configure Describable. | ||
NOTE: This is almost the same as the built-in dropdownDescriptorSelector | ||
but allows controlling the @capture of the <renderOnDemand> tag. | ||
This tag can be restored to the built-in dropdownDescriptorSelector | ||
once the baseline Jenkins is 1.645+ | ||
|
||
<st:attribute name="field" use="required"> | ||
form field name. Used for databinding. | ||
</st:attribute> | ||
<st:attribute name="title" use="required"> | ||
Human readable title of this control. | ||
</st:attribute> | ||
<st:attribute name="lazy"> | ||
If specified, the additional the variables to be captured. See the @capture of <renderOnDemand> tag. | ||
</st:attribute> | ||
<st:attribute name="descriptors"> | ||
Collection that lists up all the valid candidate descriptors. | ||
If unspecified, inferred from the type of the field. | ||
</st:attribute> | ||
<st:attribute name="default"> | ||
If specified, this will be chosen as the default value in case the current selection is null. The default can be | ||
an specific instance or a descriptor e.g. | ||
${descriptor.defaultSettingsProvider} or ${descriptor.defaultSettingsProvider.descriptor}. In the later case, the | ||
from input fields will be empty. | ||
</st:attribute> | ||
</st:documentation> | ||
<f:prepareDatabinding/> | ||
<j:set target="${attrs}" property="descriptors" | ||
value="${attrs.descriptors ?: descriptor.getPropertyType(instance,attrs.field).getApplicableDescriptors()}"/> | ||
|
||
<f:dropdownList name="${attrs.field}" title="${attrs.title}" help="${descriptor.getHelpFile(attrs.field)}"> | ||
<d:invokeBody/> | ||
|
||
<j:set var="current" value="${instance[attrs.field]}"/> | ||
<j:set var="current" value="${current!=null ? current : (default.descriptor!=null ? default : null)}"/> | ||
<j:forEach var="descriptor" items="${attrs.descriptors}" varStatus="loop"> | ||
<f:dropdownListBlock value="${loop.index}" title="${descriptor.displayName}" | ||
selected="${current.descriptor==descriptor || (current==null and descriptor==attrs.default)}" | ||
staplerClass="${descriptor.clazz.name}" | ||
lazy="${attrs.lazy?attrs.lazy+',':''}descriptor,it"> | ||
<l:ajax> | ||
<j:set var="instance" value="${current.descriptor==descriptor ? current : null}"/> | ||
<st:include from="${descriptor}" page="${descriptor.configPage}"/> | ||
</l:ajax> | ||
</f:dropdownListBlock> | ||
</j:forEach> | ||
</f:dropdownList> | ||
</j:jelly> |
Empty file.
86 changes: 86 additions & 0 deletions
86
src/test/java/org/jenkinsci/plugins/authorizeproject/GlobalQueueItemAuthenticatorTest.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,86 @@ | ||
package org.jenkinsci.plugins.authorizeproject; | ||
|
||
import hudson.model.FreeStyleProject; | ||
import hudson.model.User; | ||
import hudson.security.ACL; | ||
import hudson.util.DescribableList; | ||
import jenkins.model.Jenkins; | ||
import jenkins.security.QueueItemAuthenticator; | ||
import jenkins.security.QueueItemAuthenticatorConfiguration; | ||
import jenkins.security.QueueItemAuthenticatorDescriptor; | ||
import org.jenkinsci.plugins.authorizeproject.strategy.AnonymousAuthorizationStrategy; | ||
import org.jenkinsci.plugins.authorizeproject.strategy.SpecificUsersAuthorizationStrategy; | ||
import org.jenkinsci.plugins.authorizeproject.testutil.AuthorizationCheckBuilder; | ||
import org.jenkinsci.plugins.authorizeproject.testutil.AuthorizeProjectJenkinsRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class GlobalQueueItemAuthenticatorTest { | ||
@Rule | ||
public JenkinsRule j = new AuthorizeProjectJenkinsRule(); | ||
|
||
|
||
@Test | ||
public void testWorkForFreeStyleProject() throws Exception { | ||
Jenkins.getInstance().setSecurityRealm(j.createDummySecurityRealm()); | ||
|
||
DescribableList<QueueItemAuthenticator, QueueItemAuthenticatorDescriptor> authenticators = | ||
QueueItemAuthenticatorConfiguration.get().getAuthenticators(); | ||
authenticators.remove(GlobalQueueItemAuthenticator.class); | ||
// if not configured, run in SYSTEM privilege. | ||
{ | ||
FreeStyleProject p = j.createFreeStyleProject(); | ||
AuthorizationCheckBuilder checker = new AuthorizationCheckBuilder(); | ||
p.getBuildersList().add(checker); | ||
|
||
j.assertBuildStatusSuccess(p.scheduleBuild2(0)); | ||
assertEquals(ACL.SYSTEM, checker.authentication); | ||
} | ||
|
||
authenticators.add(new GlobalQueueItemAuthenticator( | ||
new SpecificUsersAuthorizationStrategy(User.get("bob", true).getId(), true)) | ||
); | ||
// if configured, GlobalQueueItemAuthenticator takes effect | ||
{ | ||
FreeStyleProject p = j.createFreeStyleProject(); | ||
AuthorizationCheckBuilder checker = new AuthorizationCheckBuilder(); | ||
p.getBuildersList().add(checker); | ||
|
||
j.assertBuildStatusSuccess(p.scheduleBuild2(0)); | ||
assertEquals("bob", checker.authentication.getPrincipal()); | ||
} | ||
|
||
// if configured, AuthorizeProjectStrategy takes effect above Global as it is first in the list | ||
{ | ||
ProjectQueueItemAuthenticator pqia = authenticators.get(ProjectQueueItemAuthenticator.class); | ||
GlobalQueueItemAuthenticator gqia = authenticators.get(GlobalQueueItemAuthenticator.class); | ||
assertTrue("Project is before Global", authenticators.indexOf(pqia) < authenticators.indexOf(gqia)); | ||
} | ||
{ | ||
FreeStyleProject p = j.createFreeStyleProject(); | ||
AuthorizationCheckBuilder checker = new AuthorizationCheckBuilder(); | ||
p.getBuildersList().add(checker); | ||
|
||
p.addProperty(new AuthorizeProjectProperty(new AnonymousAuthorizationStrategy())); | ||
|
||
j.assertBuildStatusSuccess(p.scheduleBuild2(0)); | ||
assertEquals(Jenkins.ANONYMOUS, checker.authentication); | ||
} | ||
|
||
// if configured ProjectQueueItemAuthenticator wrong, run fall through to GlobalQueueItemAuthenticator. | ||
{ | ||
FreeStyleProject p = j.createFreeStyleProject(); | ||
AuthorizationCheckBuilder checker = new AuthorizationCheckBuilder(); | ||
p.getBuildersList().add(checker); | ||
|
||
p.addProperty(new AuthorizeProjectProperty(null)); | ||
|
||
j.assertBuildStatusSuccess(p.scheduleBuild2(0)); | ||
assertEquals("bob", checker.authentication.getPrincipal()); | ||
} | ||
} | ||
} |