python - Jenkins API Response 没有使用 Crumb
问题描述
我想删除一个工作,为此我的 Jenkins 服务器需要碎屑。我正在发送面包屑,但它仍然发送给我,标题和/或正文中没有面包屑。
尝试了标题和正文的所有组合,但都没有奏效。
import requests
import json
from urllib.request import urljoin
def delete_jenkins_job(delete_url):
"""
Function to delete Jenkins Job
:param delete_url:
:return:
"""
base_url = 'http://<jenkins-server>:8080'
_user = '<my-user>'
_pass = '<my-pass>'
crumb_url = urljoin(base_url, '/crumbIssuer/api/json')
delete_url = urljoin(base_url, delete_url)
response = json.loads(requests.request("GET", crumb_url, auth=(_user, _pass)).content)
header = {
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Upgrade-Insecure-Requests': '1',
'Content-Type': 'application/x-www-form-urlencoded',
'Accept-Encoding': 'gzip, deflate',
}
data = {
response['crumbRequestField']: response['crumb']
}
response = requests.request("POST", url=delete_url, headers=header, data=json.dumps(data), auth=(_user, _pass))
b'<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>\n<title>Error 403 No valid crumb was included in the request</title>\n</head>\n<body><h2>HTTP ERROR 403</h2>\n<p>Problem accessing /job/PyTest-Docker-Based/567/doDelete. Reason:\n<pre> No valid crumb was included in the request</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.z-SNAPSHOT</a><hr/>\n\n</body>\n</html>\n'
解决方案
Jenkins 从 2.176.x 版本更新了安全性,https: //jenkins.io/doc/upgrade-guide/2.176/
为避免这种情况,您可以遵循以下三种方法之一:
- 安装 Strict Crumb Issuer jenkins 插件(https://wiki.jenkins.io/display/JENKINS/Strict+Crumb+Issuer+Plugin)来解决问题。
更新 set-cookie 键的标头以及 jenkins-crumb
def cookieContent = response.headers.get("Set-Cookie") httpRequest( url: "https://the-url.com/the-thing.php", customHeaders: [[name:"Cookie", value:cookieContent]])
禁用此改进,您可以将系统属性hudson.security.csrf.DefaultCrumbIssuer.EXCLUDE_SESSION_ID设置为 true。
推荐阅读
- sql - 使用 JPA 投影重写 MariaDB 的 SQL 查询
- git - 在 Jenkins 管道中访问 currentBuild.changeSets 时抛出 NotSerializableException
- r - NbClus 中的错误(x,距离 =“欧几里得”,min.nc = 2,max.nc = 8,方法 =“完成”,找不到函数“NbClus”
- python - 熊猫合并用空值填充新数据框
- node.js - 如何使用函数定义 nodejs 变量?
- python-3.x - 附加包含列表的文件
- javascript - 有没有办法等待加载网站的dom(不是完整的网页)?
- api - 在哪里可以找到 IBM Cloud IAM Identity Services API 的 Swagger 文档(JSON 文档)
- c - 使用超时中断选择函数或使用同一模块中的另一个函数写入其文件描述符
- python - 如何使用 PyOpenSSL 读取 PFX 文件?