Search code examples
pythonssljenkins

CERTIFICATE_VERIFY_FAILED with jenkins API


So I have some virtual machines, and I was creating this python script, which conencts to a Jenkins website we have running, and everything works:

jenkins

I then ran the exact same code on another server, but this time I got this wired error?

$ py test.py
Traceback (most recent call last):
  File "C:\programs\python3102\lib\site-packages\urllib3\connectionpool.py", line 597, in urlopen
    httplib_response = self._make_request(conn, method, url,
  File "C:\programs\python3102\lib\site-packages\urllib3\connectionpool.py", line 343, in _make_request
    self._validate_conn(conn)
  File "C:\programs\python3102\lib\site-packages\urllib3\connectionpool.py", line 839, in _validate_conn
    conn.connect()
  File "C:\programs\python3102\lib\site-packages\urllib3\connection.py", line 337, in connect
    self.sock = ssl_wrap_socket(
  File "C:\programs\python3102\lib\site-packages\urllib3\util\ssl_.py", line 344, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\programs\python3102\lib\ssl.py", line 512, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\programs\python3102\lib\ssl.py", line 1070, in _create
    self.do_handshake()
  File "C:\programs\python3102\lib\ssl.py", line 1341, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:997)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\programs\python3102\lib\site-packages\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
  File "C:\programs\python3102\lib\site-packages\urllib3\connectionpool.py", line 637, in urlopen
    retries = retries.increment(method, url, error=e, _pool=self,
  File "C:\programs\python3102\lib\site-packages\urllib3\util\retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='xxx.com', port=443): Max retries exceeded with url: /crumbIssuer/api/json (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:997)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\usnname\Desktop\test.py", line 20, in <module>
    test = jenkins_connection.get_node_info(node)
  File "C:\programs\python3102\lib\site-packages\jenkins\__init__.py", line 1504, in get_node_info
    response = self.jenkins_open(requests.Request(
  File "C:\programs\python3102\lib\site-packages\jenkins\__init__.py", line 557, in jenkins_open
    return self.jenkins_request(req, add_crumb, resolve_auth).text
  File "C:\programs\python3102\lib\site-packages\jenkins\__init__.py", line 573, in jenkins_request
    self.maybe_add_crumb(req)
  File "C:\programs\python3102\lib\site-packages\jenkins\__init__.py", line 370, in maybe_add_crumb
    response = self.jenkins_open(requests.Request(
  File "C:\programs\python3102\lib\site-packages\jenkins\__init__.py", line 557, in jenkins_open
    return self.jenkins_request(req, add_crumb, resolve_auth).text
  File "C:\programs\python3102\lib\site-packages\jenkins\__init__.py", line 576, in jenkins_request
    self._request(req))
  File "C:\programs\python3102\lib\site-packages\jenkins\__init__.py", line 550, in _request
    return self._session.send(r, **_settings)
  File "C:\programs\python3102\lib\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "C:\programs\python3102\lib\site-packages\requests\adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='xxx.com', port=443): Max retries exceeded with url: /crumbIssuer/api/json (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:997)')))

Seems it's complaining about some certificate error?, I just haven't seen this before, so I was wondering if I could get some suggestions on what to look for, to fix this?

Both machines run python 3.10.2, and have the same packages installed.

The code itself looks like this:

try:
    import jenkins
except ImportError:
    import pip
    pip.main(["install", "--upgrade", "python-jenkins"])
    import jenkins

# Login settings
JENKINS_URL = "https: // xxx.com"
USERNAME = "xxx"
API_TOKEN = "xxx"
JENKINS_NODES = ["PD100089"]
# Connect to server with API token
jenkins_connection = jenkins.Jenkins(JENKINS_URL, USERNAME, API_TOKEN)

# Loop through list of nodes and make them online
for node in JENKINS_NODES:
    # Get node XML configurations
    test = jenkins_connection.get_node_info(node)

    # Check if XML node configuration is online/offline
    if not test['offline']:
        print("- " + node + " is already online...")
        # Stop script
        continue

    # Turn node back online
    print("- Turning " + node + " back online...")
    jenkins_connection.enable_node(node)

I can access the websites on both machines from google chrome without any problem.


Solution

  • It seems your second server has self-signed certificates in the certificate chain. You can try to disable SSL verifications.

    jenkins = Jenkins(url, username, password, ssl_verify=False)
    

    You can read more here.