Jenkins pipeline exception when using Date object?

1,251 views
Skip to first unread message

ZillaYT

unread,
Jun 12, 2018, 11:24:22 AM6/12/18
to Jenkins Users

Jenkins v2.126, Groovy plugin v2.0

I have the following in my Jenkinsfile, called from a Pipeline job.

def someFunction() {
  Date today = new Date("yyyy-MM-dd")
  // other statements
}

and I get the following exception.

ERROR: org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.util.Date java.lang.String

According to Google searches, I should see something to approve in Manage Jenkins->In-process Script Approval, but I don't see anything there. NOTHING!

Additionally, there should a be a "Use Groovy Sandbox" check box in my job configuration page, but I do NOT see that either.

What am I missing?


Lee Meador

unread,
Jun 12, 2018, 12:21:25 PM6/12/18
to Jenkins
If your pipeline catches and discards the exception after printing, it will not appear in the Manage Jenkins place. Use something like this to avoid that:

try {
    ...
} catch (RejectedAccessException e) {
    throw e
} catch (Exception e) {
    echo "ERROR: " + e.message
}


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/265bb8d4-ce92-46c3-bb03-470819daf2b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-- Lee Meador
Sent from gmail. My real email address is lee AT leemeador.com

ZillaYT

unread,
Jun 12, 2018, 12:50:15 PM6/12/18
to Jenkins Users
For some reason I'm getting this with your code

WorkflowScript: 110: unable to resolve class RejectedAccessException@ line 110, column 5.
     } catch (RejectedAccessException e) {
       
^

1 error

        at org
.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
        at org
.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:958)
        at org
.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
        at org
.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:554)
        at groovy
.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
        at groovy
.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
        at groovy
.lang.GroovyShell.parseClass(GroovyShell.java:688)
        at groovy
.lang.GroovyShell.parse(GroovyShell.java:700)
        at org
.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:131)
        at org
.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:125)
        at org
.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
        at org
.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
        at org
.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:325)
        at hudson
.model.ResourceController.execute(ResourceController.java:97)
        at hudson
.model.Executor.run(Executor.java:429)
Finished: FAILURE


On Tuesday, June 12, 2018 at 12:21:25 PM UTC-4, Lee Meador wrote:
If your pipeline catches and discards the exception after printing, it will not appear in the Manage Jenkins place. Use something like this to avoid that:

try {
    ...
} catch (RejectedAccessException e) {
    throw e
} catch (Exception e) {
    echo "ERROR: " + e.message
}

On Tue, Jun 12, 2018 at 10:24 AM, ZillaYT <zil...@gmail.com> wrote:

Jenkins v2.126, Groovy plugin v2.0

I have the following in my Jenkinsfile, called from a Pipeline job.

def someFunction() {
  Date today = new Date("yyyy-MM-dd")
  // other statements
}

and I get the following exception.

ERROR: org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.util.Date java.lang.String

According to Google searches, I should see something to approve in Manage Jenkins->In-process Script Approval, but I don't see anything there. NOTHING!

Additionally, there should a be a "Use Groovy Sandbox" check box in my job configuration page, but I do NOT see that either.

What am I missing?


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

ZillaYT

unread,
Jun 12, 2018, 2:43:04 PM6/12/18
to Jenkins Users
I modified the catch block to 

try {
 
...
} catch (org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException e) {

   
throw e
} catch (Exception e) {
    echo
"ERROR: " + e.message
}

Which ridded the stack trace error, but I still don't see the script in the Manage Jenkins->In-process Script Approval section.

On Tuesday, June 12, 2018 at 12:21:25 PM UTC-4, Lee Meador wrote:
If your pipeline catches and discards the exception after printing, it will not appear in the Manage Jenkins place. Use something like this to avoid that:

try {
    ...
} catch (RejectedAccessException e) {
    throw e
} catch (Exception e) {
    echo "ERROR: " + e.message
}

On Tue, Jun 12, 2018 at 10:24 AM, ZillaYT <zil...@gmail.com> wrote:

Jenkins v2.126, Groovy plugin v2.0

I have the following in my Jenkinsfile, called from a Pipeline job.

def someFunction() {
  Date today = new Date("yyyy-MM-dd")
  // other statements
}

and I get the following exception.

ERROR: org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.util.Date java.lang.String

According to Google searches, I should see something to approve in Manage Jenkins->In-process Script Approval, but I don't see anything there. NOTHING!

Additionally, there should a be a "Use Groovy Sandbox" check box in my job configuration page, but I do NOT see that either.

What am I missing?


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

Lee Meador

unread,
Jun 14, 2018, 12:57:28 AM6/14/18
to Jenkins
My suggestion is only helpful when you already have a try { ... } catch (e) { ... and don't retrow ... } in your script. 

When a script throws that rejected exception due to non-whitelisted-methods called 
and that script catches it 
and the Jenkins system can't see the exception, 
then Jenkins will not show it in the script approval area.

It does not help to add it. It just fixes the problem caused by this arrangement of code already in your script.

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/b25fcff3-d598-4551-a2bd-f25e5f9f0d0e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages