如何更新Jenkins作业发布config.xml

本文档介绍了如何在启用了安全性和CSRF防护的Jenkins环境中,通过HTTP请求更新作业的config.xml。内容包括获取面包屑值、使用cookie进行身份验证以及处理CSRF保护的方法。
摘要由CSDN通过智能技术生成

最近,我想更新Cloudbees中的一些作业(未使用DSL定义),为每个作业添加一些属性。

好吧,我在使它工作时遇到了一些麻烦,这是我的注意事项(我使用了Jenkins 1.651.2.1,但有可能它应与较早和较新的版本一起使用,例如jenkins 2)

没有安全性/没有身份验证

这是简单的部分:检索并重新发布配置

$ curl  http://localhost:8080/jenkins/job/pof/config.xml -o config.xml
$ curl  -X POST http://localhost:8080/jenkins/job/pof/config.xml --data-binary @config.xml

简单的安全性:使用用户名和密码

我现在假设您的Jenkins设置已设置安全性( http:// localhost:8080 / jenkins / configureSecurity / –>启用安全性)

Screen-Shot-2016-05-15-at-22.25.35-768x628

这意味着我们现在需要验证我们的两个请求:

curl -X GET http://anthony:anthony@localhost:8080/jenkins/job/pof/config.xml -o config.xml
curl -X POST http://anthony:anthony@localhost:8080/jenkins/job/pof/config.xml --data-binary "@config.xml"

简单的安全性:启用CSRF(滚动)

您还需要保护您的jenkins实例免受CSRF攻击http:// localhost:8080 / jenkins / configureSecurity / –> enable csrf crumb)

Screen-Shot-2016-05-15-at-22.26.20-768x195
现在,这也意味着您的请求需要发送一个屑状值,无论是作为参数还是通过标头

如果您不这样做:

curl -X POST http://anthony:anthony@localhost:8080/jenkins/job/pof/config.xml --data-binary "@config.xml"

您会得到这样的错误:

<body><h2>HTTP ERROR 403</h2>
<p>Problem accessing /jenkins/job/pof/config.xml. Reason:
<pre>    No valid crumb was included in the request</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>

甚至 :

<body><h1>HTTP Status 500 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server encountered an internal error () that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>java.io.IOException: Failed to persist config.xml
hudson.model.AbstractItem.updateByXml(AbstractItem.java:677)
hudson.model.AbstractItem.doConfigDotXml(AbstractItem.java:617)
…..
</pre></p><p><b>root cause</b> <pre>javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:755)
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:357)
    jenkins.util.xml.XMLUtils._transform(XMLUtils.java:96)
    jenkins.util.xml.XMLUtils.safeTransform(XMLUtils.java:63)
    hudson.model.AbstractItem.updateByXml(AbstractItem.java:674)
 hudson.model.AbstractItem.doConfigDotXml(AbstractItem.java:617)

获得面包屑值:

您可以使用configure job页面来分配值:

curl http://anthony:anthony@localhost:8080/jenkins/job/pof/configure | sed -n 's/.*\.crumb", "\(.*\)").*/\1/p' > crumb.txt

但是,还有专门用于此的服务:

curl http://anthony:anthony@localhost:8080/jenkins/crumbIssuer/api/xml | sed -n 's/.*\(.*\)<\/crumb>.*/\1/p' > crumb.txt

使用面包屑值

curl -X POST http://anthony:anthony@localhost:8080/jenkins/job/pof/config.xml --data-binary "@config.xml" -data ".crumb=6bbabc426436b72ec35e5ad4a4344687"

哎呀,那没用

Caused by: java.lang.IllegalStateException: STREAMED
    at org.eclipse.jetty.server.Request.getReader(Request.java:803)
    at javax.servlet.ServletRequestWrapper.getReader(ServletRequestWrapper.java:256)
    at hudson.model.AbstractItem.doConfigDotXml(AbstractItem.java:610)

我建议您使用标题发送面包屑:

curl -v -X POST http://anthony:anthony@localhost:8080/jenkins/job/pof/config.xml --data-binary "@config.xml" -H ".crumb: 6bbabc426436b72ec35e5ad4a4344687"

基于cookie的安全性(无用户名/密码)

在某些安装中(例如cloubees),您不能在请求中传递用户名和密码。 我建议您改用cookie。

要检索它们,请检查通过身份验证的浏览器发送的cookie,例如chrome:

Screen-Shot-2016-05-16-at-10.11.08-768x408
然后将此URL粘贴到您的shell中:

curl 'http://localhost:8080/jenkins/job/pof/config.xml' -H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8,fr;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Referer: http://localhost:8080/jenkins/login?from=%2Fjenkins%2Fjob%2Fpof%2Fconfig.xml' -H 'Cookie: screenResolution=1440x900; JSESSIONID=XXXXX; JSESSIONID.XX=XXXX; screenResolution=1440x900' -H 'Connection: keep-alive' -H 'Cache-Control: no-cache' --compressed

当然,您仍然需要获取面包屑值:

curl 'http://localhost:8080/jenkins/crumbIssuer/api/xml' -H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8,fr;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Referer: http://localhost:8080/jenkins/login?from=%2Fjenkins%2Fjob%2Fpof%2Fconfig.xml' -H 'Cookie: screenResolution=1440x900; JSESSIONID=XXXXX; JSESSIONID.XX=XXXXX; screenResolution=1440x900' -H 'Connection: keep-alive' -H 'Cache-Control: no-cache' --compressed | sed -n 's/.*<crumb>\(.*\)<\/crumb>.*/\1/p' > crumb.txt

现在,您可以发布更新的config.xml了:

curl -X POST 'http://localhost:8080/jenkins/job/pof/config.xml' -H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8,fr;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Referer: http://localhost:8080/jenkins/login?from=%2Fjenkins%2Fjob%2Fpof%2Fconfig.xml' -H 'Cookie: screenResolution=1440x900; JSESSIONID=XXXX; JSESSIONID.XX=XXXX; screenResolution=1440x900' -H 'Connection: keep-alive' -H 'Cache-Control: no-cache' --compressed --data-binary "@config.xml" -H ".crumb: 6bbabc426436b72ec35e5ad4a4344687"

链接

翻译自: https://www.javacodegeeks.com/2016/05/update-jenkins-job-posting-config-xml.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值