0

When executing the following in Jenkins:

def platformIndex = list.findIndexOf { it.key ==  "linux" }.value
if (platformIndex == -1) { // params.PLATFORM not found
    platformIndex = env.BUILD_NUMBER % platformMaps.size() 
}

I get the following error:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use field java.lang.Integer value
08:50:44    at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectField(StaticWhitelist.java:284)
08:50:44    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor$10.reject(SandboxInterceptor.java:344)
08:50:44    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:409)
08:50:44    at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)
08:50:44    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:357)
08:50:44    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
08:50:44    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
08:50:44    at WorkflowScript.run(WorkflowScript:60)
08:50:44    at argusGitPipeline.call(argusGitPipeline.groovy:64)
08:50:44    at argusPipeline.call(argusPipeline.groovy:70)

I've taken note of solution described here Jenkins CI Pipeline Scripts not permitted to use method groovy.lang.GroovyObject

However, disabling sandbox or disabling script security is not an option. Therefore I was wondering if there's an alternative to list.findIndexOf

  • The linked answer is to: "Navigate to jenkins > Manage jenkins > In-process Script Approval. There was a pending command, which I had to approve." This approves tne one method only and is low risk providing you have a private Jenkns and full knowledge / control of what it does. other 2 options are big hammer alternatives. – Ian W Jul 24 '21 at 07:03
  • 2
    You have issue in code. You are trying to get `.value` on integer. `list.findIndexOf{}` returns integer and integer doesn't have property `value` – daggett Jul 24 '21 at 10:29
  • @daggett is right about the error in your code. But integer does have a field `value` it's just private https://github.com/frohoff/jdk8u-jdk/blob/master/src/share/classes/java/lang/Integer.java#L840 – tim_yates Jul 24 '21 at 13:02

1 Answers1

0

Just remove the .value part:

def platformIndex = list.findIndexOf { it.key ==  "linux" }
if (platformIndex == -1) { // params.PLATFORM not found
    platformIndex = env.BUILD_NUMBER % platformMaps.size() 
}

The findIndexOf() function already returns an integer.

Shane Bishop
  • 3,905
  • 4
  • 17
  • 47