在Jenkins API调用中使用过滤器的安全问题,如何将过滤器功能列入白名单

时间:2018-12-05 23:32:55

标签: jenkins jenkins-pipeline

如果我在Definition : Pipeline script from SCMDefinition: Script, [*]Use Groovy Sandbox下使用以下Jenkins管道,则会生成一个异常。

管道

import java.util.GregorianCalendar;
import java.util.Calendar;

pipeline {
  agent { label 'STUBS_ONLY'}//agent any

  stages {
    stage('Query Jenkins Jobs') {
      steps {
        script {
          // Get date seven days ago from today
          def gcalNeg7Days = new GregorianCalendar();
          gcalNeg7Days.add( Calendar.DAY_OF_MONTH , -7 );
          def time7DaysAgo = gcalNeg7Days.getTime()

          def getJobStats = { job ->
            def lb = job.getLastBuild()
            if (lb) {
              // Calculate how many jobs ran in the last 7 days
              def numRuns = job._getRuns().values().stream().filter( { run -> ( run.timestamp.getTime() > time7DaysAgo ) } ).count()
            } else {
              numRuns   = 0
            }
            println '   ' + job.name + ' has ' + numRuns.toString() + ' ran last week'
          }

          // Get all builds from all views we explicitly track
          def trackedViews = Jenkins.instance.views.findAll( { it.name != 'all' } );

          // Print detail for each job
          trackedViews.each {
            view ->
            println "--------------------------------------------------------------------------------------------------------------------------------------------"
            println "${view.name}"
            view.items.each {
            item -> getJobStats( item )
            }
          }
        }
      } // steps
    } // stage
  } // stages
} // pipeline

异常

hudson.remoting.ProxyException: groovy.lang.MissingMethodException:
    No signature of method: java.util.stream.ReferencePipeline$Head.filter()
    is applicable for argument types:

    (org.jenkinsci.plugins.workflow.cps.CpsClosure2) values: [org.jenkinsci.plugins.workflow.cps.CpsClosure2@4f819ba0]

Possible solutions: filter(java.util.function.Predicate), sorted(), find(), sorted(java.util.Comparator), sleep(long), with(groovy.lang.Closure)

如果我取消选中Use Groovy Sandbox,则异常消失并且作业运行没有问题。不幸的是,该选项不适用于SCM工作流程中的管道。

似乎In Script Approval解析无法识别我的过滤谓词中的lambda,并且默认情况下将其从范围中省略。

.filter( { run -> ( run.timestamp.getTime() > time7DaysAgo ) } )

我正在运行Jenkins ver. 2.138.3。如何将其手动添加到已批准签名的白名单中?

0 个答案:

没有答案