Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
[FIXED JENKINS-30122] AbstractSynchronousNonBlockingStepExecution neg…
Browse files Browse the repository at this point in the history
…lected to pick up the build’s authentication.
  • Loading branch information
jglick committed Aug 25, 2015
1 parent a0de6cd commit 2c70e4f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -2,6 +2,10 @@

Only noting significant user changes, not internal code cleanups and minor bug fixes.

## 1.10 (upcoming)

* [JENKINS-30122](https://issues.jenkins-ci.org/browse/JENKINS-30122): regression in usage of the Authorize Project plugin in 1.10-beta-1.

## 1.10-beta-1 (Aug 21 2015)

* [JENKINS-26942](https://issues.jenkins-ci.org/browse/JENKINS-26942): added `stash` and `unstash` steps (deprecating `unarchive`).
Expand Down
Expand Up @@ -42,12 +42,14 @@
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.support.actions.EnvironmentAction;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.Test;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockQueueItemAuthenticator;
import org.jvnet.hudson.test.TestExtension;
Expand Down Expand Up @@ -249,6 +251,40 @@ public static void finish(final boolean terminate) {
}
}

@Issue("JENKINS-30122")
@Test public void authenticationInSynchronousStep() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
jenkins().setSecurityRealm(story.j.createDummySecurityRealm());
jenkins().save();
QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new MockQueueItemAuthenticator(Collections.singletonMap("demo", User.get("someone").impersonate())));
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition("echo \"ran as ${auth()}\"", true));
b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("ran as someone", b);
}
});
}
public static final class CheckAuthSync extends AbstractStepImpl {
@DataBoundConstructor public CheckAuthSync() {}
@TestExtension("authenticationInSynchronousStep") public static final class DescriptorImpl extends AbstractStepDescriptorImpl {
public DescriptorImpl() {
super(Execution.class);
}
@Override public String getFunctionName() {
return "auth";
}
@Override public String getDisplayName() {
return getFunctionName();
}
}
public static final class Execution extends AbstractSynchronousNonBlockingStepExecution<String> {
@Override protected String run() throws Exception {
return Jenkins.getAuthentication().getName();
}
}
}

@Test public void env() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
Expand Down
@@ -1,11 +1,15 @@
package org.jenkinsci.plugins.workflow.steps;

import hudson.security.ACL;
import hudson.util.DaemonThreadFactory;
import hudson.util.NamingThreadFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import jenkins.model.Jenkins;
import jenkins.security.NotReallyRoleSensitiveCallable;
import org.acegisecurity.Authentication;

/**
* Similar to {@link AbstractSynchronousStepExecution} (it executes synchronously too) but it does not block the CPS VM thread.
Expand Down Expand Up @@ -34,10 +38,15 @@ protected AbstractSynchronousNonBlockingStepExecution(StepContext context) {

@Override
public final boolean start() throws Exception {
final Authentication auth = Jenkins.getAuthentication();
task = getExecutorService().submit(new Runnable() {
@Override public void run() {
try {
getContext().onSuccess(AbstractSynchronousNonBlockingStepExecution.this.run());
getContext().onSuccess(ACL.impersonate(auth, new NotReallyRoleSensitiveCallable<T, Exception>() {
@Override public T call() throws Exception {
return AbstractSynchronousNonBlockingStepExecution.this.run();
}
}));
} catch (Exception e) {
getContext().onFailure(e);
}
Expand Down

0 comments on commit 2c70e4f

Please sign in to comment.