-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
434009a
commit 6bd4e8b
Showing
5 changed files
with
211 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
|
||
package org.jenkinsci.plugins.workflow.cps; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.Extension; | ||
import hudson.model.Action; | ||
import hudson.model.Item; | ||
|
@@ -33,6 +34,8 @@ | |
import hudson.model.TaskListener; | ||
import hudson.util.FormValidation; | ||
import hudson.util.StreamTaskListener; | ||
import net.sf.json.JSONObject; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn; | ||
import org.jenkinsci.plugins.workflow.flow.DurabilityHintProvider; | ||
import org.jenkinsci.plugins.workflow.flow.FlowDefinition; | ||
|
@@ -80,7 +83,8 @@ public CpsFlowDefinition(String script) { | |
@DataBoundConstructor | ||
public CpsFlowDefinition(String script, boolean sandbox) { | ||
StaplerRequest req = Stapler.getCurrentRequest(); | ||
this.script = sandbox ? script : ScriptApproval.get().configuring(script, GroovyLanguage.get(), ApprovalContext.create().withCurrentUser().withItemAsKey(req != null ? req.findAncestorObject(Item.class) : null)); | ||
this.script = sandbox ? script : ScriptApproval.get().configuring(script, GroovyLanguage.get(), | ||
ApprovalContext.create().withCurrentUser().withItemAsKey(req != null ? req.findAncestorObject(Item.class) : null), req == null); | ||
this.sandbox = sandbox; | ||
} | ||
|
||
|
@@ -123,14 +127,41 @@ public CpsFlowExecution create(FlowExecutionOwner owner, TaskListener listener, | |
@Extension | ||
public static class DescriptorImpl extends FlowDefinitionDescriptor { | ||
|
||
/* In order to fix SECURITY-2450 without causing significant UX regressions, we decided to continue to | ||
* automatically approve scripts on save if the script was modified by an administrator. To make this possible, | ||
* we added a new hidden input field to the config.jelly to track the pre-save version of the script. Since | ||
* CpsFlowDefinition calls ScriptApproval.configuring in its @DataBoundConstructor, the normal way to handle | ||
* things would be to add an oldScript parameter to the constructor and perform the relevant logic there. | ||
* | ||
* However, that would have compatibility implications for tools like JobDSL, since @DataBoundConstructor | ||
* parameters are required. We cannot use a @DataBoundSetter with a corresponding field and getter to trivially | ||
* make oldScript optional, because we would need to call ScriptApproval.configuring after all | ||
* @DataBoundSetters have been invoked (rather than in the @DataBoundConstructor), which is why we use Descriptor.newInstance. | ||
*/ | ||
@Override | ||
public FlowDefinition newInstance(@NonNull StaplerRequest req, @NonNull JSONObject formData) throws FormException { | ||
CpsFlowDefinition cpsFlowDefinition = (CpsFlowDefinition) super.newInstance(req, formData); | ||
if (!cpsFlowDefinition.sandbox && formData.get("oldScript") != null) { | ||
String oldScript = formData.getString("oldScript"); | ||
boolean approveIfAdmin = !StringUtils.equals(oldScript, cpsFlowDefinition.script); | ||
if (approveIfAdmin) { | ||
ScriptApproval.get().configuring(cpsFlowDefinition.script, GroovyLanguage.get(), | ||
ApprovalContext.create().withCurrentUser().withItemAsKey(req.findAncestorObject(Item.class)), true); | ||
} | ||
} | ||
return cpsFlowDefinition; | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Pipeline script"; | ||
} | ||
|
||
@RequirePOST | ||
public FormValidation doCheckScript(@QueryParameter String value, @QueryParameter boolean sandbox) { | ||
return sandbox ? FormValidation.ok() : ScriptApproval.get().checking(value, GroovyLanguage.get()); | ||
public FormValidation doCheckScript(@QueryParameter String value, @QueryParameter String oldScript, | ||
@QueryParameter boolean sandbox) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
yaroslavafenkin
Author
Contributor
|
||
return sandbox ? FormValidation.ok() : | ||
ScriptApproval.get().checking(value, GroovyLanguage.get(), !StringUtils.equals(oldScript, value)); | ||
} | ||
|
||
@RequirePOST | ||
|
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
24 changes: 24 additions & 0 deletions
24
src/test/java/org/jenkinsci/plugins/workflow/cps/CpsFlowDefinitionRJRTest.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,24 @@ | ||
package org.jenkinsci.plugins.workflow.cps; | ||
|
||
import org.jenkinsci.plugins.workflow.job.WorkflowJob; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.RealJenkinsRule; | ||
|
||
public class CpsFlowDefinitionRJRTest { | ||
|
||
@Rule | ||
public RealJenkinsRule rjr = new RealJenkinsRule(); | ||
|
||
@Test | ||
public void smokes() throws Throwable { | ||
rjr.then(CpsFlowDefinitionRJRTest::doesItSmoke); | ||
} | ||
|
||
private static void doesItSmoke(JenkinsRule r) throws Exception { | ||
WorkflowJob p = r.createProject(WorkflowJob.class, "p"); | ||
p.setDefinition(new CpsFlowDefinition("print Jenkins.get().getRootDir().toString()", false)); | ||
r.assertBuildStatusSuccess(p.scheduleBuild2(0)); | ||
} | ||
} |
The addition of this new parameter is now causing PCT failures in
workflow-cps-global-lib-plugin
:@yaroslavafenkin @dwnusbaum Can you please restore compatibility or else adapt
workflow-cps-global-lib-plugin
to this breaking change and release a new version for PCT?