Jenkins API的反应不是吃面包屑

2024-04-29 08:39:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想删除一个工作,为此我的詹金斯服务器需要面包屑。我送面包屑,但仍然,它送我回来-没有面包屑在标题和/或机构。你知道吗

尝试了头和身体的所有组合,但没有一个有效。你知道吗

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'

Tags: importjsonhttpurldatabaseresponserequest
1条回答
网友
1楼 · 发布于 2024-04-29 08:39:08

Jenkins更新了2.176.x版本的安全性https://jenkins.io/doc/upgrade-guide/2.176/

要避免这种情况,您可以采用以下三种方法之一:

  1. 安装Strict crump Issuer jenkins插件(https://wiki.jenkins.io/display/JENKINS/Strict+Crumb+Issuer+Plugin)来解决这个问题。你知道吗
  2. 更新你的标题设置cookie键连同詹金斯面包屑

    def cookieContent = response.headers.get("Set-Cookie")
    httpRequest(
        url: "https://the-url.com/the-thing.php",
        customHeaders: [[name:"Cookie", value:cookieContent]])
    
    
  3. 禁用此改进您可以设置系统属性哈德逊.安全.csrf.DefaultIssuer.EXCLUDE\会话\u ID为真。

相关问题 更多 >