diff --git a/core/src/main/java/hudson/model/AbstractProject.java b/core/src/main/java/hudson/model/AbstractProject.java
index 65c9ab9888..2a1ba110c0 100644
--- a/core/src/main/java/hudson/model/AbstractProject.java
+++ b/core/src/main/java/hudson/model/AbstractProject.java
@@ -2155,7 +2155,7 @@ public abstract class AbstractProject
,R extends A
* Finds a {@link AbstractProject} that has the name closest to the given name.
* @see Items#findNearest
*/
- public static AbstractProject findNearest(String name) {
+ public static @CheckForNull AbstractProject findNearest(String name) {
return findNearest(name,Jenkins.getInstance());
}
@@ -2165,7 +2165,7 @@ public abstract class AbstractProject
,R extends A
* @since 1.419
* @see Items#findNearest
*/
- public static AbstractProject findNearest(String name, ItemGroup context) {
+ public static @CheckForNull AbstractProject findNearest(String name, ItemGroup context) {
return Items.findNearest(AbstractProject.class, name, context);
}
diff --git a/core/src/main/java/hudson/model/DependencyGraph.java b/core/src/main/java/hudson/model/DependencyGraph.java
index 5b1ca1d11e..bfbaa4e999 100644
--- a/core/src/main/java/hudson/model/DependencyGraph.java
+++ b/core/src/main/java/hudson/model/DependencyGraph.java
@@ -415,6 +415,10 @@ public class DependencyGraph implements Comparator {
* Decide whether build should be triggered and provide any Actions for the build.
* Default implementation always returns true (for backward compatibility), and
* adds no Actions. Subclasses may override to control how/if the build is triggered.
+ * The authentication in effect ({@link Jenkins#getAuthentication}) will be that of the upstream build.
+ * An implementation is expected to perform any relevant access control checks:
+ * that an upstream project can both see and build a downstream project,
+ * or that a downstream project can see an upstream project.
* @param build Build of upstream project that just completed
* @param listener For any error/log output
* @param actions Add Actions for the triggered build to this list; never null
diff --git a/core/src/main/java/hudson/model/queue/Tasks.java b/core/src/main/java/hudson/model/queue/Tasks.java
index 2031ca5b32..c24c45db85 100644
--- a/core/src/main/java/hudson/model/queue/Tasks.java
+++ b/core/src/main/java/hudson/model/queue/Tasks.java
@@ -29,6 +29,9 @@ import org.acegisecurity.Authentication;
import java.util.Collection;
import java.util.Collections;
+import javax.annotation.Nonnull;
+import jenkins.security.QueueItemAuthenticator;
+import jenkins.security.QueueItemAuthenticatorConfiguration;
/**
* Convenience methods around {@link Task} and {@link SubTask}.
@@ -107,4 +110,22 @@ public class Tasks {
}
}
+ /**
+ * Finds what authentication a task is likely to be run under when scheduled.
+ * The actual authentication after scheduling ({@link hudson.model.Queue.Item#authenticate}) might differ,
+ * in case some {@link QueueItemAuthenticator#authenticate(hudson.model.Queue.Item)} takes (for example) actions into consideration.
+ * @param t a task
+ * @return an authentication as specified by some {@link QueueItemAuthenticator#authenticate(hudson.model.Queue.Task)}; else {@link #getDefaultAuthenticationOf}
+ * @since TODO
+ */
+ public static @Nonnull Authentication getAuthenticationOf(@Nonnull Task t) {
+ for (QueueItemAuthenticator qia : QueueItemAuthenticatorConfiguration.get().getAuthenticators()) {
+ Authentication a = qia.authenticate(t);
+ if (a != null) {
+ return a;
+ }
+ }
+ return getDefaultAuthenticationOf(t);
+ }
+
}
diff --git a/core/src/main/java/hudson/tasks/BuildTrigger.java b/core/src/main/java/hudson/tasks/BuildTrigger.java
index e422cc8cc6..1e8437a783 100644
--- a/core/src/main/java/hudson/tasks/BuildTrigger.java
+++ b/core/src/main/java/hudson/tasks/BuildTrigger.java
@@ -44,6 +44,7 @@ import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.ItemListener;
+import hudson.model.queue.Tasks;
import hudson.security.ACL;
import hudson.util.FormValidation;
import java.io.IOException;
@@ -63,6 +64,8 @@ import jenkins.security.QueueItemAuthenticatorConfiguration;
import jenkins.security.QueueItemAuthenticatorDescriptor;
import net.sf.json.JSONObject;
import org.acegisecurity.Authentication;
+import org.acegisecurity.context.SecurityContext;
+import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
@@ -231,22 +234,21 @@ public class BuildTrigger extends Recorder implements DependencyDeclarer {
}
for (Dependency dep : downstreamProjects) {
- AbstractProject p = dep.getDownstreamProject();
- // TODO do we need to separately check READ on all parents?
- // For example, by impersonating auth (if ANONYMOUS) and then checking Jenkins.instance.getItemByFullName(p.fullName) == p?
- if (!p.getACL().hasPermission(auth, Item.READ)) {
- continue; // do not even issue a warning (could do so if have DISCOVER)
- }
- if (!p.getACL().hasPermission(auth, Item.BUILD)) {
- logger.println(Messages.BuildTrigger_you_have_no_permission_to_build_(ModelHyperlinkNote.encodeTo(p)));
- continue;
- }
- if (p.isDisabled()) {
- logger.println(Messages.BuildTrigger_Disabled(ModelHyperlinkNote.encodeTo(p)));
- continue;
- }
List buildActions = new ArrayList();
- if (dep.shouldTriggerBuild(build, listener, buildActions)) {
+ boolean result;
+ SecurityContext orig = ACL.impersonate(auth);
+ try {
+ result = dep.shouldTriggerBuild(build, listener, buildActions);
+ } finally {
+ SecurityContextHolder.setContext(orig);
+ }
+ if (result) {
+ AbstractProject p = dep.getDownstreamProject();
+ // Allow shouldTriggerBuild to return false first, in case it is skipping because of a lack of Item.READ/DISCOVER permission:
+ if (p.isDisabled()) {
+ logger.println(Messages.BuildTrigger_Disabled(ModelHyperlinkNote.encodeTo(p)));
+ continue;
+ }
// this is not completely accurate, as a new build might be triggered
// between these calls
String name = ModelHyperlinkNote.encodeTo(p)+" #"+p.getNextBuildNumber();
@@ -268,6 +270,14 @@ public class BuildTrigger extends Recorder implements DependencyDeclarer {
@Override
public boolean shouldTriggerBuild(AbstractBuild build, TaskListener listener,
List actions) {
+ AbstractProject downstream = getDownstreamProject();
+ if (Jenkins.getInstance().getItemByFullName(downstream.getFullName()) != downstream) { // this checks Item.READ also on parent folders
+ return false; // do not even issue a warning
+ }
+ if (!downstream.hasPermission(Item.BUILD)) {
+ listener.getLogger().println(Messages.BuildTrigger_you_have_no_permission_to_build_(ModelHyperlinkNote.encodeTo(downstream)));
+ return false;
+ }
return build.getResult().isBetterOrEqualTo(threshold);
}
});
@@ -352,7 +362,7 @@ public class BuildTrigger extends Recorder implements DependencyDeclarer {
/**
* Form validation method.
*/
- public FormValidation doCheck(@AncestorInPath Item project, @QueryParameter String value, @QueryParameter boolean upstream) {
+ public FormValidation doCheck(@AncestorInPath AbstractProject project, @QueryParameter String value, @QueryParameter boolean upstream) {
// Require CONFIGURE permission on this project
if(!project.hasPermission(Item.CONFIGURE)) return FormValidation.ok();
@@ -362,12 +372,22 @@ public class BuildTrigger extends Recorder implements DependencyDeclarer {
String projectName = tokens.nextToken().trim();
if (StringUtils.isNotBlank(projectName)) {
Item item = Jenkins.getInstance().getItem(projectName,project,Item.class);
- if(item==null)
- return FormValidation.error(Messages.BuildTrigger_NoSuchProject(projectName,
- AbstractProject.findNearest(projectName,project.getParent()).getRelativeNameFrom(project)));
+ if (item == null) {
+ AbstractProject nearest = AbstractProject.findNearest(projectName,project.getParent());
+ String alternative = nearest != null ? nearest.getRelativeNameFrom(project) : "?";
+ return FormValidation.error(Messages.BuildTrigger_NoSuchProject(projectName, alternative));
+ }
if(!(item instanceof AbstractProject))
return FormValidation.error(Messages.BuildTrigger_NotBuildable(projectName));
- // Will require Item.BUILD on project (if upstream) or item (if !upstream) but we cannot predict what QueueItemAuthenticator will produce
+ if (!upstream) { // check whether the supposed user is expected to be able to build
+ Authentication auth = Tasks.getAuthenticationOf(project);
+ if (auth.equals(ACL.SYSTEM) && !QueueItemAuthenticatorConfiguration.get().getAuthenticators().isEmpty()) {
+ auth = Jenkins.ANONYMOUS; // compare behavior in execute, above
+ }
+ if (!item.getACL().hasPermission(auth, Item.BUILD)) {
+ return FormValidation.error(Messages.BuildTrigger_you_have_no_permission_to_build_(projectName));
+ }
+ }
hasProjects = true;
}
}
diff --git a/core/src/main/java/jenkins/security/QueueItemAuthenticator.java b/core/src/main/java/jenkins/security/QueueItemAuthenticator.java
index 22922bf866..c256cb2f6b 100644
--- a/core/src/main/java/jenkins/security/QueueItemAuthenticator.java
+++ b/core/src/main/java/jenkins/security/QueueItemAuthenticator.java
@@ -1,6 +1,7 @@
package jenkins.security;
import hudson.ExtensionPoint;
+import hudson.Util;
import hudson.model.AbstractDescribableImpl;
import hudson.model.AbstractProject;
import hudson.model.Action;
@@ -8,14 +9,14 @@ import hudson.model.CauseAction;
import hudson.model.Queue;
import hudson.model.Queue.Item;
import hudson.model.Queue.Task;
-import hudson.security.ACL;
-import org.acegisecurity.Authentication;
-
+import java.util.Calendar;
+import java.util.Collections;
import javax.annotation.CheckForNull;
+import org.acegisecurity.Authentication;
/**
* Extension point to run {@link hudson.model.Queue.Executable}s under a specific identity for better access control.
- *
+ * You must override either {@link #authenticate(hudson.model.Queue.Item)}, or {@link #authenticate(hudson.model.Queue.Task)}, or both.
* @author Kohsuke Kawaguchi
* @since 1.520
* @see QueueItemAuthenticatorConfiguration
@@ -25,7 +26,7 @@ import javax.annotation.CheckForNull;
public abstract class QueueItemAuthenticator extends AbstractDescribableImpl implements ExtensionPoint {
/**
* Determines the identity in which the {@link hudson.model.Queue.Executable} will run as.
- *
+ * The default implementation delegates to {@link #authenticate(hudson.model.Queue.Task)}.
* @param item
* The contextual information to assist the authentication.
* The primary interest is likely {@link hudson.model.Queue.Item#task}, which is often {@link AbstractProject}.
@@ -36,7 +37,34 @@ public abstract class QueueItemAuthenticator extends AbstractDescribableImplemptyList()));
+ } else {
+ throw new AbstractMethodError("you must override at least one of the QueueItemAuthenticator.authenticate methods");
+ }
+ }
@Override
public QueueItemAuthenticatorDescriptor getDescriptor() {
diff --git a/test/src/test/java/hudson/tasks/BuildTriggerTest.java b/test/src/test/java/hudson/tasks/BuildTriggerTest.java
index 37cf4f6e80..47e3495b33 100644
--- a/test/src/test/java/hudson/tasks/BuildTriggerTest.java
+++ b/test/src/test/java/hudson/tasks/BuildTriggerTest.java
@@ -28,6 +28,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import hudson.maven.MavenModuleSet;
import hudson.maven.MavenModuleSetBuild;
+import hudson.model.AbstractProject;
import hudson.model.Cause;
import hudson.model.Computer;
import hudson.model.FreeStyleBuild;
@@ -37,19 +38,24 @@ import hudson.model.Queue;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.User;
+import hudson.security.ACL;
import hudson.security.AuthorizationMatrixProperty;
import hudson.security.LegacySecurityRealm;
import hudson.security.Permission;
import hudson.security.ProjectMatrixAuthorizationStrategy;
+import hudson.util.FormValidation;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
+import javax.annotation.CheckForNull;
import jenkins.model.Jenkins;
import jenkins.security.QueueItemAuthenticator;
import jenkins.security.QueueItemAuthenticatorConfiguration;
import jenkins.security.QueueItemAuthenticatorDescriptor;
import org.acegisecurity.Authentication;
+import org.acegisecurity.context.SecurityContext;
+import org.acegisecurity.context.SecurityContextHolder;
import org.jvnet.hudson.test.ExtractResourceSCM;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.MockBuilder;
@@ -179,15 +185,18 @@ public class BuildTriggerTest extends HudsonTestCase {
// DependencyGraph is rebuilt as SYSTEM so is always complete even if configuring user does not know it:
assertEquals(Collections.singletonList(downstream), upstream.getDownstreamProjects());
// Downstream projects whose existence we are not aware of will silently not be triggered:
+ Authentication alice = User.get("alice").impersonate();
+ assertDoCheck(alice, Messages.BuildTrigger_NoSuchProject(downstreamName, "upstream"), upstream, downstreamName, false);
FreeStyleBuild b = buildAndAssertSuccess(upstream);
assertLogNotContains(downstreamName, b);
waitUntilNoActivity();
assertNull(downstream.getLastBuild());
+ // If we can see them, but not build them, that is a warning (but this is in cleanUp so the build is still considered a success):
Map> grantedPermissions = new HashMap>();
grantedPermissions.put(Item.READ, Collections.singleton("alice"));
AuthorizationMatrixProperty amp = new AuthorizationMatrixProperty(grantedPermissions);
- // If we can see them, but not build them, that is a warning (but this is in cleanUp so the build is still considered a success):
downstream.addProperty(amp);
+ assertDoCheck(alice, Messages.BuildTrigger_you_have_no_permission_to_build_(downstreamName), upstream, downstreamName, false);
b = buildAndAssertSuccess(upstream);
assertLogContains(downstreamName, b);
waitUntilNoActivity();
@@ -197,6 +206,7 @@ public class BuildTriggerTest extends HudsonTestCase {
downstream.removeProperty(amp);
amp = new AuthorizationMatrixProperty(grantedPermissions);
downstream.addProperty(amp);
+ assertDoCheck(alice, null, upstream, downstreamName, false);
b = buildAndAssertSuccess(upstream);
assertLogContains(downstreamName, b);
waitUntilNoActivity();
@@ -207,6 +217,7 @@ public class BuildTriggerTest extends HudsonTestCase {
assertEquals(b, cause.getUpstreamRun());
// Now if we have configured some QIA’s but they are not active on this job, we should run as anonymous. Which would normally have no permissions:
QueueItemAuthenticatorConfiguration.get().getAuthenticators().replace(new QIA(Collections.emptyMap()));
+ assertDoCheck(alice, Messages.BuildTrigger_you_have_no_permission_to_build_(downstreamName), upstream, downstreamName, false);
b = buildAndAssertSuccess(upstream);
assertLogNotContains(downstreamName, b);
assertLogContains(Messages.BuildTrigger_warning_this_build_has_no_associated_aut(), b);
@@ -218,6 +229,7 @@ public class BuildTriggerTest extends HudsonTestCase {
downstream.removeProperty(amp);
amp = new AuthorizationMatrixProperty(grantedPermissions);
downstream.addProperty(amp);
+ assertDoCheck(alice, null, upstream, downstreamName, false);
b = buildAndAssertSuccess(upstream);
assertLogContains(downstreamName, b);
waitUntilNoActivity();
@@ -232,6 +244,7 @@ public class BuildTriggerTest extends HudsonTestCase {
amp = new AuthorizationMatrixProperty(grantedPermissions);
downstream.addProperty(amp);
QueueItemAuthenticatorConfiguration.get().getAuthenticators().clear();
+ assertDoCheck(alice, Messages.BuildTrigger_NoSuchProject(downstreamName, "upstream"), upstream, downstreamName, false);
b = buildAndAssertSuccess(upstream);
assertLogContains(downstreamName, b);
assertLogContains(Messages.BuildTrigger_warning_access_control_for_builds_in_glo(), b);
@@ -258,5 +271,20 @@ public class BuildTriggerTest extends HudsonTestCase {
}
}
}
+ private void assertDoCheck(Authentication auth, @CheckForNull String expectedError, AbstractProject project, String value, boolean upstream) {
+ FormValidation result;
+ SecurityContext orig = ACL.impersonate(auth);
+ try {
+ result = jenkins.getDescriptorByType(BuildTrigger.DescriptorImpl.class).doCheck(project, value, upstream);
+ } finally {
+ SecurityContextHolder.setContext(orig);
+ }
+ if (expectedError == null) {
+ assertEquals(result.renderHtml(), FormValidation.Kind.OK, result.kind);
+ } else {
+ assertEquals(result.renderHtml(), FormValidation.Kind.ERROR, result.kind);
+ assertEquals(result.renderHtml(), expectedError);
+ }
+ }
}