如何禁用 Jenkins 管道构建的安全检查

问题描述 投票:0回答:6

我正在本地可信环境中运行 Jenkins,并尝试在其中运行此管道。此 Jenkinsfile 已签入 git。

#!groovy
node('master') {
    def ver = pomVersion()
    echo "Building version $ver"
}

def pomVersion(){
    def pomtext = readFile('pom.xml')
    def pomx = new XmlParser().parseText(pomtext)
    pomx.version.text()
}

我运行构建的前几次,我需要手动批准更改(Jenkins->Mange Jenkins->进程内脚本批准)。现在我得到了这个例外,没有什么可以批准的。我想做的就是解析 XML 文件。管道构建可以完全绕过这些安全检查吗?

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified field groovy.util.Node version
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:367)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:363)
    at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:23)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:17)
    at WorkflowScript.pomVersion(WorkflowScript:10)
    at WorkflowScript.run(WorkflowScript:3)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:62)
    at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:54)
    at sun.reflect.GeneratedMethodAccessor479.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:58)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:32)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:29)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:29)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:164)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:276)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$000(CpsThreadGroup.java:78)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:185)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:183)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:47)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Finished: FAILURE
security jenkins groovy jenkins-workflow jenkins-pipeline
6个回答
15
投票

目前还不可能。此问题有一个开放票证https://issues.jenkins-ci.org/browse/JENKINS-28178


9
投票

您可以通过以下步骤解决问题:

  1. 安装Permissive Script Security插件(版本0.3或更高版本)
  2. 如果您使用管道脚本,请确保选中
    Use Groovy Sandbox
    。这可以在作业的配置中完成。
  3. permissive-script-security.enabled
    命令行参数添加到 Jenkins master,其值为:
    • true
      如果您想禁用批准脚本的需要,但将记录潜在危险的签名:

         -Dpermissive-script-security.enabled=true
      
    • no_security
      如果您想禁用批准脚本的需要并禁用潜在危险签名的记录:

         -Dpermissive-script-security.enabled=no_security
      

5
投票

尝试以下 jenkins 插件:https://wiki.jenkins-ci.org/display/JENKINS/Permissive+Script+Security+Plugin 它禁用沙箱。对我有用。


3
投票

我想提供一个黑客,我在互联网上搜索解决方案并尝试了此处提出的一些解决方案后最终实现了该黑客。

我的设置的一些背景:

  • Jenkins master(无奴隶)
  • Dockerized Jenkins 实例,具有 jenkins_home 目录的持久卷
  • Jenkins 作业通过 Jenkins Job DSL 插件交付,作业以 .groovy 编写

我的场景: 每当有人修改现有的 Jenkins 管道(通过 groovy)并引入使用某些自定义 groovy 的新功能时,Jenkins 都会使作业失败并标记代码片段以供批准。批准是手动且乏味的。

我已经尝试过上面发布的解决方案,但它们对我不起作用。所以我的黑客方法是创建一个 Jenkins 作业,该作业运行一个 shell 作业,该作业获取需要批准的签名列表,然后将它们添加到 /var/jenkins_home/scriptApproval.xml 文件中。

一些陷阱:

  • 有问题的工作仍然必须失败一次才能让我找到/复制有问题的代码/签名
  • 要使更改生效,您无法“从磁盘重新加载”以获取文件。您必须重新启动 Jenkins 进程(在我们的例子中删除容器并将其恢复)。这对我来说并不是一个很大的痛苦,因为 Jenkins 每天早上都会重新启动。
  • 在我们的世界中,我们信任修改 Jenkins 作业的开发人员,以便他们可以根据需要自由添加需要批准的签名。另外,该工作处于源代码控制中,因此我们可以看到谁添加了哪些内容。
  • 我的 Jenkins 容器还内置了 xmlstarlet,因此我的 shell 作业使用它来更新文件

我的 Jenkins 作业的 shell 命令示例:

#!/bin/bash
echo ""

#default location of the Jenkins approval file
APPROVE_FILE=/var/jenkins_home/scriptApproval.xml

#creating an array of the signatures that need approved
SIGS=(
'method hudson.model.ItemGroup getItem java.lang.String'
'staticMethod jenkins.model.Jenkins getInstance'
)

#stepping through the array
for i in "${SIGS[@]}"; do
   echo "Adding :"
   echo "$i"
   echo "to $APPROVE_FILE"
   echo ""
   #checking the xml file to see if it has already been added, then deleting. this is a trick to keep xmlstarlet from creatine duplicates
   xmlstarlet -q ed --inplace -d "/scriptApproval/approvedSignatures/string[text()=\"$i\"]" $APPROVE_FILE

   #adding the entry
   xmlstarlet -q ed --inplace -s /scriptApproval/approvedSignatures -t elem -n string -v "$i" $APPROVE_FILE
   echo ""
done

echo "##### Completed updating "$APPROVE_FILE", displaying file: #####"
cat "$APPROVE_FILE"

2
投票

如上所述:在较新的 Jenkins 版本中,脚本安全性已得到加强。然而,对于从 Maven 的

pom.xml
读取版本的特定用例,可以使用
Pipeline Utility Steps Plugin
中的 readMavenPom

pom = readMavenPom file: 'pom.xml'
pom.version

还有这个 StackOverflow 问题中的一些其他解决方案。


0
投票

为了绕过脚本审批

去管理Jenkins --> 安全--> 安全

向下滚动/搜索(Git 插件提交访问令牌) 您会看到(启用作业 DSL 脚本的脚本批准)取消选中此应用并保存。

© www.soinside.com 2019 - 2024. All rights reserved.