Skip to content

Commit

Permalink
Merge pull request #14 from stephenc/jenkins-30574-redux
Browse files Browse the repository at this point in the history
[FIXED JENKINS-30574] Support global default authorization strategy in Authorize Project
  • Loading branch information
ikedam committed Feb 11, 2016
2 parents c6507c7 + 136fe61 commit 5f567fc
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 15 deletions.
@@ -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();
}
}
}
Expand Up @@ -22,9 +22,12 @@ 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">
<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">
<st:adjunct includes="org.jenkinsci.plugins.authorizeproject.nestedHelp"/>
<f:optionalBlock name="${descriptor.propertyName}" title="${descriptor.displayName}" checked="${instance != null}">
<f:dropdownDescriptorSelector title="${%Authorize Strategy}" field="strategy" descriptors="${descriptor.enabledAuthorizeProjectStrategyDescriptorList}" />
<!--
What fun that dropdownDescriptorSelector doesn't add 'it' to the render on demand captured variables
-->
<hack:dropdownDescriptorSelector title="${%Authorize Strategy}" field="strategy" descriptors="${descriptor.enabledAuthorizeProjectStrategyDescriptorList}" />
</f:optionalBlock>
</j:jelly>
@@ -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>
@@ -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>
Expand Up @@ -20,5 +20,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

ProjectQueueItemAuthenticator.DisplayName=Configure Build Authorizations in Project Configuration
ProjectQueueItemAuthenticator.DisplayName=Per-project configurable Build Authorization
AuthorizeProjectProperty.DisplayName=Configure Build Authorization
GlobalQueueItemAuthenticator.DisplayName=Project default Build Authorization
Expand Up @@ -24,15 +24,19 @@ 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">
<!-- configurations for AuthorizeProjectStrategy -->
<j:forEach var="d" items="${descriptor.availableDescriptorList}">
<j:scope>
<j:set var="checked" value="${instance.isStrategyEnabled(d)}" />
<j:set var="instance" value="${d}" />
<j:set var="descriptor" value="${d}" />
<f:optionalBlock name="${d.jsonSafeClassName}" checked="${checked}" help="${d.helpFile}" title="${d.displayName}">
<j:set var="instance" value="${d}" />
<st:include page="${d.globalSecurityConfigPage}" class="${d.clazz}" optional="true" />
</f:optionalBlock>
</j:scope>
</j:forEach>
<f:entry title="${%Strategies}">
<table width="100%">
<j:forEach var="d" items="${descriptor.availableDescriptorList}">
<j:scope>
<j:set var="checked" value="${instance==null?d.enabledByDefault: instance.isStrategyEnabled(d)}" />
<j:set var="instance" value="${d}" />
<j:set var="descriptor" value="${d}" />
<f:optionalBlock name="${d.jsonSafeClassName}" checked="${checked}" help="${d.helpFile}" title="${d.displayName}">
<j:set var="instance" value="${d}" />
<st:include page="${d.globalSecurityConfigPage}" class="${d.clazz}" optional="true" />
</f:optionalBlock>
</j:scope>
</j:forEach>
</table>
</f:entry>
</j:jelly>
@@ -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>
@@ -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 &lt;select> control for choosing a Describable.
Depending on the currently selected value, its config.jelly will be
rendered below &lt;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 &lt;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 &lt;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.
@@ -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());
}
}
}

0 comments on commit 5f567fc

Please sign in to comment.