Fix permission problem when getting job list

Jenkins.getActiveInstance().getAllItems(AbstractProject.class) returns an empty
list when slave comes up. Later on for some unknown reason priviledges are
escalated to ACL.SYSTEM and it starts to work during e.g. slave disable/enable.

Change-Id: Iecd48f992896a31139a8366e1b686dc4e12ef1cc
This commit is contained in:
Karpenko Oleksandr 2017-01-20 15:34:53 +01:00
parent 46d74f029f
commit 10ec9f9b71
3 changed files with 13 additions and 1 deletions

View File

@ -117,7 +117,7 @@ public class ExecutorWorkerThread extends AbstractWorkerThread{
if (!computer.isOffline()) {
Node node = computer.getNode();
List<AbstractProject> allProjects = Jenkins.getActiveInstance().getAllItems(AbstractProject.class);
List<AbstractProject> allProjects = GearmanPluginUtil.getAllProjects();
for (AbstractProject<?, ?> project : allProjects) {
if (project.isDisabled()) { // ignore all disabled projects
@ -166,10 +166,13 @@ public class ExecutorWorkerThread extends AbstractWorkerThread{
}
}
}
logger.debug("---- Worker "+ getName() +" registerJobs try to register " + newFunctionMap.size());
if (!newFunctionMap.keySet().equals(functionMap.keySet())) {
functionMap = newFunctionMap;
Set<GearmanFunctionFactory> functionSet = new HashSet<GearmanFunctionFactory>(functionMap.values());
updateJobs(functionSet);
} else {
logger.debug("---- Worker "+ getName() +" registerJobs no changes");
}
}

View File

@ -24,6 +24,7 @@ import hudson.model.Run;
import hudson.security.ACL;
import java.io.IOException;
import java.util.List;
import jenkins.model.Jenkins;
@ -106,4 +107,11 @@ public class GearmanPluginUtil {
SecurityContextHolder.setContext(oldContext);
}
}
public static List<AbstractProject> getAllProjects() {
SecurityContext oldContext = ACL.impersonate(ACL.SYSTEM);
List<AbstractProject> allProjects = Jenkins.getActiveInstance().getAllItems(AbstractProject.class);
SecurityContextHolder.setContext(oldContext);
return allProjects;
}
}

View File

@ -70,6 +70,7 @@ public class ManagementWorkerThread extends AbstractWorkerThread{
updateJobs(functionSet);
registered = true;
logger.debug("---- Worker "+ getName() +" registerJobs registered " + functionSet.size());
}
}
}