Skip to content

Commit

Permalink
Migrate from EE 8 to EE 9 (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Jan 22, 2025
1 parent 209c59a commit 2274200
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 28 deletions.
15 changes: 8 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.86</version>
<version>5.5</version>
<relativePath />
</parent>

<artifactId>envinject</artifactId>
<version>2.${changelist}</version>
<packaging>hpi</packaging>
<name>Environment Injector Plugin</name>
<url>https://github.com/jenkinsci/envinject-plugin</url>
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>

<licenses>
<license>
Expand Down Expand Up @@ -57,8 +58,8 @@
<properties>
<changelist>999999-SNAPSHOT</changelist>
<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ -->
<jenkins.baseline>2.361</jenkins.baseline>
<jenkins.version>${jenkins.baseline}.4</jenkins.version>
<jenkins.baseline>2.479</jenkins.baseline>
<jenkins.version>${jenkins.baseline}.1</jenkins.version>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<!-- Remove once JENKINS-45055 is fixed -->
<spotbugs.threshold>High</spotbugs.threshold>
Expand All @@ -84,7 +85,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>2102.v854b_fec19c92</version>
<version>3850.vb_c5319efa_e29</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand All @@ -95,13 +96,13 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>envinject-api</artifactId>
<version>1.199.v3ce31253ed13</version>
<version>1.235.va_14c74f8f487</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ivy</artifactId>
<version>2.2</version>
<version>2.8</version>
<optional>true</optional>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerRequest2;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -172,7 +172,7 @@ public String getHelpFile() {
}

@Override
public EnvInjectJobProperty newInstance(StaplerRequest req, JSONObject formData) throws FormException {
public EnvInjectJobProperty newInstance(StaplerRequest2 req, JSONObject formData) throws FormException {
if (formData.optBoolean("on")) {
return (EnvInjectJobProperty)super.newInstance(req, formData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ public EnvInjectJobPropertyInfo(

// If the groovy script is specified, it should become the SecureGroovyScript
this(propertiesFilePath, propertiesContent, scriptFilePath, scriptContent, loadFilesFromMaster,
StringUtils.isNotBlank(groovyScriptContent) ? new SecureGroovyScript(groovyScriptContent, false, null).configuring(ApprovalContext.create()) : null);
StringUtils.isNotBlank(groovyScriptContent) ? newSecureGroovyScript(groovyScriptContent) : null);
}

private static SecureGroovyScript newSecureGroovyScript(String groovyScriptContent) {
try {
return new SecureGroovyScript(groovyScriptContent, false, null).configuring(ApprovalContext.create());
} catch (Descriptor.FormException e) {
throw new RuntimeException(e);

Check warning on line 83 in src/main/java/org/jenkinsci/plugins/envinject/EnvInjectJobPropertyInfo.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 82-83 are not covered by tests
}
}

@CheckForNull
Expand Down Expand Up @@ -117,7 +125,7 @@ public Descriptor<EnvInjectJobPropertyInfo> getDescriptor() {

protected Object readResolve() {
if (secureGroovyScript == null && !StringUtils.isBlank(groovyScriptContent)) {
secureGroovyScript = new SecureGroovyScript(groovyScriptContent, false, null).configuring(ApprovalContext.create());
secureGroovyScript = newSecureGroovyScript(groovyScriptContent);
groovyScriptContent = null;
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Arrays;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerRequest2;

import java.util.List;
import edu.umd.cs.findbugs.annotations.CheckForNull;
Expand Down Expand Up @@ -70,7 +70,7 @@ public EnvInjectGlobalPasswordEntry[] getEnvInjectGlobalPasswordEntries() {
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
public boolean configure(StaplerRequest2 req, JSONObject json) throws FormException {
List<EnvInjectGlobalPasswordEntry> envInjectGlobalPasswordEntriesList = req.bindParametersToList(EnvInjectGlobalPasswordEntry.class, "envInject.");
envInjectGlobalPasswordEntries = envInjectGlobalPasswordEntriesList.toArray(new EnvInjectGlobalPasswordEntry[envInjectGlobalPasswordEntriesList.size()]);
save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.jenkinsci.plugins.envinject.service.EnvInjectGlobalPasswordRetriever;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerRequest2;

import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -292,7 +292,7 @@ public String getDisplayName() {
}

@Override
public BuildWrapper newInstance(StaplerRequest req, JSONObject formData) throws FormException {
public BuildWrapper newInstance(StaplerRequest2 req, JSONObject formData) throws FormException {

EnvInjectPasswordWrapper passwordWrapper = new EnvInjectPasswordWrapper();
passwordWrapper.setInjectGlobalPasswords(formData.getBoolean("injectGlobalPasswords"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerRequest2;

/**
* Configuration of security options for {@link EnvInjectPlugin}.
Expand Down Expand Up @@ -135,7 +135,7 @@ protected XmlFile getConfigFile() {
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
public boolean configure(StaplerRequest2 req, JSONObject json) throws FormException {
final boolean newEnablePermissions = json.getBoolean("enablePermissions");
final boolean newHideInjectedVars = json.getBoolean("hideInjectedVars");
final boolean enableLoadingFromMaster = json.getBoolean("enableLoadingFromMaster");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.model.Api;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.StaplerResponse2;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

import javax.servlet.ServletOutputStream;
import jakarta.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
Expand Down Expand Up @@ -48,7 +48,7 @@ public Api getApi() {
}

@RequirePOST
public void doExport(@NonNull StaplerRequest request, @NonNull StaplerResponse response) throws IOException {
public void doExport(@NonNull StaplerRequest2 request, @NonNull StaplerResponse2 response) throws IOException {

String path = request.getPathInfo();
if (path != null) {
Expand All @@ -60,7 +60,7 @@ public void doExport(@NonNull StaplerRequest request, @NonNull StaplerResponse r
}


private void doExportWithPath(@NonNull String path, @NonNull StaplerRequest request, @NonNull StaplerResponse response) throws IOException {
private void doExportWithPath(@NonNull String path, @NonNull StaplerRequest2 request, @NonNull StaplerResponse2 response) throws IOException {

if (path.endsWith("text")) {
writeTextResponse(response);
Expand All @@ -80,7 +80,7 @@ private void doExportWithPath(@NonNull String path, @NonNull StaplerRequest requ
doExportHeaders(request, response);
}

private void doExportHeaders(@NonNull StaplerRequest request, @NonNull StaplerResponse response) throws IOException {
private void doExportHeaders(@NonNull StaplerRequest2 request, @NonNull StaplerResponse2 response) throws IOException {

String acceptHeader = request.getHeader("Accept");

Expand All @@ -104,7 +104,7 @@ private void doExportHeaders(@NonNull StaplerRequest request, @NonNull StaplerRe


@SuppressFBWarnings(value = "DM_DEFAULT_ENCODING", justification = "TODO needs triage")
private void writeTextResponse(@NonNull StaplerResponse response) throws IOException {
private void writeTextResponse(@NonNull StaplerResponse2 response) throws IOException {
response.setContentType("plain/text");
StringWriter stringWriter = new StringWriter();
for (Map.Entry<String, String> entry : envVars.entrySet()) {
Expand All @@ -114,7 +114,7 @@ private void writeTextResponse(@NonNull StaplerResponse response) throws IOExcep
}

@SuppressFBWarnings(value = "DM_DEFAULT_ENCODING", justification = "TODO needs triage")
private void writeXmlResponse(@NonNull StaplerResponse response) throws IOException {
private void writeXmlResponse(@NonNull StaplerResponse2 response) throws IOException {
response.setContentType("application/xml");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write("<envVars>".getBytes());
Expand All @@ -125,7 +125,7 @@ private void writeXmlResponse(@NonNull StaplerResponse response) throws IOExcept
}

@SuppressFBWarnings(value = "DM_DEFAULT_ENCODING", justification = "TODO needs triage")
private void writeJsonResponse(@NonNull StaplerResponse response) throws IOException {
private void writeJsonResponse(@NonNull StaplerResponse2 response) throws IOException {
response.setContentType("application/json");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write("{\"envVars\": { \"envVar\":[".getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import hudson.Util;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.Run;
import hudson.util.VariableResolver;
Expand Down Expand Up @@ -168,7 +169,12 @@ public Map<String, String> executeAndGetMapGroovyScript(
}

logger.info("Using the deprecated API (EnvInjectEnvVars#executeAndGetMapGroovyScript()). Starting from EnvInject 2.0 the call may require the script approval for a non-admin user.");
SecureGroovyScript script = new SecureGroovyScript(scriptContent, false, null);
SecureGroovyScript script;
try {
script = new SecureGroovyScript(scriptContent, false, null);
} catch (Descriptor.FormException e) {
throw new RuntimeException(e);
}

Check warning on line 177 in src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectEnvVars.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 174-177 are not covered by tests
return executeGroovyScript(logger, script, envVars);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.hamcrest.Matchers.containsString;

import static org.hamcrest.collection.IsMapContaining.hasEntry;
Expand Down Expand Up @@ -184,7 +185,11 @@ public void testWorkaroundSecurity86() throws Exception {
job.addProperty(property);

TestUtils.saveConfigurationAs(jenkins, job, "alice");
//Since alice is an admin the script should be approved automatically
Set<ScriptApproval.PendingScript> pendingScripts = ScriptApproval.get().getPendingScripts();
assertEquals(1, pendingScripts.size());
ScriptApproval.PendingScript pendingScript = pendingScripts.iterator().next();
assertEquals(script, pendingScript.script);
ScriptApproval.get().approveScript(pendingScript.getHash());
FreeStyleBuild build = jenkins.buildAndAssertSuccess(job);

org.jenkinsci.lib.envinject.EnvInjectAction envInjectAction = build.getAction(org.jenkinsci.lib.envinject.EnvInjectAction.class);
Expand Down

0 comments on commit 2274200

Please sign in to comment.