Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python和请求的自签名CA验证错误_Python_Ssl_Python Requests_Self Signed Certificate - Fatal编程技术网

Python和请求的自签名CA验证错误

Python和请求的自签名CA验证错误,python,ssl,python-requests,self-signed-certificate,Python,Ssl,Python Requests,Self Signed Certificate,由于Kubernetes集群没有自己的FQDN,我在Python和请求方面遇到了一些问题 简而言之,它是一个具有许多不同名称空间和入口的集群;内部路由作为来自请求头和白名单IP的主机的功能进行管理。当我尝试向IP发送get请求(带有客户端证书)时,我发现以下异常: HTTPSConnectionPool(host='*MyExternalIpAddr*', port=443): Max retries exceeded with url: /test(Caused by SSLError(SSL

由于Kubernetes集群没有自己的FQDN,我在Python和请求方面遇到了一些问题

简而言之,它是一个具有许多不同名称空间和入口的集群;内部路由作为来自请求头和白名单IP的主机的功能进行管理。当我尝试向IP发送get请求(带有客户端证书)时,我发现以下异常:

HTTPSConnectionPool(host='*MyExternalIpAddr*', port=443): Max retries exceeded with url: /test(Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)')))
我遇到的问题是由以下代码引起的:

url = "https://*MyExternalIpAddr*/test"
headers = {"Host":"*TheHostnameInTheCluster*"}
cert = (cert_file_path, key_file_path)
r = requests.get(url, headers=headers, cert=cert, auth=auth, verify=ca_file)
我99%确定IR url是问题所在,因为如果我将外部IP映射到drivers/etc/host中的主机名(并将url更改为“https://hostname/test)请求顺利通过

现在我假设这与请求根据rootCA(CN或SAN?)检查客户端证书的方式有关。不幸的是,我不够聪明,无法用谷歌搜索我的方法来解决最后一个难题

值得一提的是,所有证书都是使用openSSL自行生成的,因此如果需要,我可以更改它们。我想可能会把IP放在SAN中,但因为集群中会有许多不同的微服务,它们具有不同的证书包,所以我认为这可能会导致将来的冲突(?)

我希望有一个聪明的方法用Python解决这个问题,所以我求助于你们,伟大的互联网集体智慧

期待您的回音(如果我忽略了之前对类似问题的回答,请原谅;我已经搜索了很多地方,但都没有找到任何结果)

编辑 因此,我逐渐意识到问题与客户端证书无关,而是与根CA验证服务器证书的方式有关


我曾尝试在服务器证书中将IP添加到SAN中,但效果不错。在某种程度上,我的意思是,当我从IP“获取”时,邮递员不再发出嘶嘶声(必须使用主机文件中的FQDM映射名称),但不幸的是,请求没有得到满足。有什么想法吗?:)

所以我用ForcedIPHTTPSAdapter解决了这个问题

import requests
from requests.auth import HTTPBasicAuth
from forcediphttpsadapter.adapters import ForcedIPHTTPSAdapter
### You'll have to "pip install requests[security] forcediphttpsadapter" ###


if __name__ == "__main__":
    cert_file_path = r"*path_to*\client.crt"
    key_file_path = r"*path_to*\client.key"
    ca_file = r"*path_to*\rootCA.crt"
    auth = HTTPBasicAuth("*UID*", "*PWD*")
    ### Cluster IP ###
    cip = "*127.0.0.1*"
    base_url = "*https://hostname.com*"
    endpoint = "/test"
    cert = (cert_file_path, key_file_path)
    s = requests.Session()
    s.mount(base_url, ForcedIPHTTPSAdapter(dest_ip=cip))
    r = s.get(base_url + endpoint, cert=cert, auth=auth, verify=ca_file)
    if r.status_code == 200:
        print(r.text)
它还有一个额外的好处,即不必在头文件中指定主机