Python Forum
Need help setting up trusted root ca in virtual environment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help setting up trusted root ca in virtual environment
#1
I have a RHEL 8 system and am writing a script that uses requests to access an internal website. The environment is configured with the internal certificate authority in the correct location and python scripts can access the website with a get request. I didn't set that up, but it works. But when I create a virtual environment with python -m venv <path> and then use . <path>/bin/activate the get request does not use the trusted roots configured in the environment. What do I need to do to setup the virtual environment to match the system environment?

Example:
-------------------------------
[~]$ python3
Python 3.6.8 (default, Apr 25 2024, 09:54:46)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-22)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> res = requests.get("https://internalsite")
>>> res.status_code
200
>>>

[~]$ python3 -m venv venvtest
[~]$ . ./venvtest/bin/activate
(venvtest) [ ~]$ pip install requests
Collecting requests
Using cached requests-2.27.1-py2.py3-none-any.whl (63 kB)
Collecting urllib3<1.27,>=1.21.1
Using cached urllib3-1.26.18-py2.py3-none-any.whl (143 kB)
Collecting certifi>=2017.4.17
Using cached certifi-2024.2.2-py3-none-any.whl (163 kB)
Collecting charset-normalizer~=2.0.0
Using cached charset_normalizer-2.0.12-py3-none-any.whl (39 kB)
Collecting idna<4,>=2.5
Using cached idna-3.7-py3-none-any.whl (66 kB)
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2024.2.2 charset-normalizer-2.0.12 idna-3.7 requests-2.27.1 urllib3-1.26.18
(venvtest) [ ~]$ python3
Python 3.6.8 (default, Apr 25 2024, 09:54:46)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-22)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> res = requests.get("https://internalsite")
Traceback (most recent call last):
File "/home/user/venvtest/lib64/python3.6/site-packages/urllib3/connectionpool.py", line 722, in urlopen
chunked=chunked,
File "/home/user/venvtest/lib64/python3.6/site-packages/urllib3/connectionpool.py", line 404, in _make_request
self._validate_conn(conn)
File "/home/user/venvtest/lib64/python3.6/site-packages/urllib3/connectionpool.py", line 1058, in _validate_conn
conn.connect()
File "/home/user/venvtest/lib64/python3.6/site-packages/urllib3/connection.py", line 429, in connect
tls_in_tls=tls_in_tls,
File "/home/user/venvtest/lib64/python3.6/site-packages/urllib3/util/ssl_.py", line 450, in ssl_wrap_socket
sock, context, tls_in_tls, server_hostname=server_hostname
File "/home/user/venvtest/lib64/python3.6/site-packages/urllib3/util/ssl_.py", line 493, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
File "/usr/lib64/python3.6/ssl.py", line 365, in wrap_socket
_context=self, _session=session)
File "/usr/lib64/python3.6/ssl.py", line 810, in __init__
self.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 1070, in do_handshake
self._sslobj.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 648, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:897)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/user/venvtest/lib64/python3.6/site-packages/requests/adapters.py", line 450, in send
timeout=timeout
File "/home/user/venvtest/lib64/python3.6/site-packages/urllib3/connectionpool.py", line 800, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "/home/user/venvtest/lib64/python3.6/site-packages/urllib3/util/retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='internalsite', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:897)'),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/venvtest/lib64/python3.6/site-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/home/user/venvtest/lib64/python3.6/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/home/user/venvtest/lib64/python3.6/site-packages/requests/sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "/home/user/venvtest/lib64/python3.6/site-packages/requests/sessions.py", line 645, in send
r = adapter.send(request, **kwargs)
File "/home/user/venvtest/lib64/python3.6/site-packages/requests/adapters.py", line 517, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='internalsite', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:897)'),))
>>>
-----------------------------
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with creating folder and "virtual environment" AudunNilsen 1 343 Mar-21-2024, 04:41 AM
Last Post: deanhystad
  Installing python packages in a virtual environment Led_Zeppelin 1 922 Aug-15-2023, 08:18 PM
Last Post: deanhystad
  Problem with virtual environment standenman 2 1,113 Feb-23-2023, 07:09 PM
Last Post: standenman
Question Virtual Environment (using VS Code) Ashcora 4 17,786 Feb-15-2023, 07:17 PM
Last Post: snippsat
  How do I link the virtual environment of that project to the 3.9.2 version of python? Bryant11 1 1,482 Feb-26-2022, 11:15 AM
Last Post: Larz60+
  Project structure with a virtual environment gdbengo 1 1,572 Jan-26-2022, 03:22 PM
Last Post: snippsat
  Virtual environment path? Led_Zeppelin 1 2,422 Jul-22-2021, 01:40 PM
Last Post: snippsat
  virtual environment heye 4 2,619 Jul-04-2021, 06:47 AM
Last Post: ndc85430
  import statement in a virtual environment leodavinci1990 1 2,476 Mar-04-2021, 12:57 AM
Last Post: snippsat
  Virtual environment and upgrading python 3.5 to 3.9 NeilUK 4 12,513 Jan-24-2021, 01:02 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020