local issuer certificate error uniquely in docker with python - python

Following error occurs only with docker app in python when making request to an https url.
<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)>
Outside of docker, the app works. I can fetch the same URL inside the docker image of other language app such as dotnet.
I have tried:
RUN update-ca-certificates
Install certfi library and manually supply the certificates during making the call
Manually insert the certificates that comes with certify library in different locations of docker images such as /usr/local/share/ca-certificates/, /etc/ssl/certs/ and RUN update-ca-certificates
Tried different versions (3.6.9, 3.8.4) and providers (alpine, buster, slim-buster ) of python.
Setting different env variables such as REQUESTS_CA_BUNDLE, SSL_CERT_FILE etc.
Use different libraries such as requests, urllib, urllib3
.... and really large number of different things.
It of course works when I turn the verify off, but I want to keep verification.

I was having this issue in an ARM Ubuntu 20.04 container.
I installed ca-certificates and curl, but I still couldn't use curl. For me the fix ended up being adding the following before calling curl:
export SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt

Related

PyInstaller add root certificate to executable

I have created a Python application and am packaging it with PyInstaller. On my own computer, I have configured my own trust store CA bundle to include my company's proxy based on Self-Signed Certificate Authorities pip / conda but I don't understand how to bring that into the PyInstaller executable so it works on other people's computers who have not configured their own trust store CA bundle.
The error I get is [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain
I am using PyInstaller 5.7.0 (with --onefile option), certifi 2022.12.7, and Python 3.8. This is primarily a dash application (2.7.1). First, the user selects a ZIPcode and I use uszipcode (version 0.2.6) to get information on that ZIPcode. That is where the error first occurs.
The application runs correctly when disconnected from the VPN. When I generate the executable, I use the --add-data flag to include cacert.pem from the certifi Python package. I tried manually adding my company's cert to the cacert.pem but it will get overwritten if I update certifi.
Where should I add in my company's SSL certificate so users don't have to disconnect from the VPN to use the application?

Python suds: Getitng SSL: CERTIFICATE_VERIFY_FAILED error

I'm trying to request some data from a website using suds in Python. I'm getting urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)> while creating suds client. I do not get this error when I'm running the project locally but this issue happens when I try to deploy this project to a Linux host so that it can be automated.
One thing to note is, this python code lies behind AppProxy so that it can connect to internet. I have verified using a CURL request that the remote hosts is able to connect to the website.
I'm new to SSL Certificate thing not so sure about this certificate issue, do I need to install any server certificates on the hosts so that it can verified by SSL Validation? Any leads would be helpful. Thanks.

Unable to get local issuer certificate when using requests in python

here is my code
import requests;
url='that website';
headers={
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
};
r = requests.get(url,headers=headers);
print(r);
print(r.status_code);
then it ran into this:
requests.exceptions.SSLError:
HTTPSConnectionPool(host='www.xxxxxx.com', port=44 3):
Max retries exceeded with url: xxxxxxxx (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED]
certificate verify failed: unable to get local issuer certificate
(_ssl.c:1045)')))
what should i do?
It's not recommended to use verify = False in your organization's environments. This is essentially disabling SSL verification.
Sometimes, when you are behind a company proxy, it replaces the certificate chain with the ones of Proxy. Adding the certificates in cacert.pem used by certifi should solve the issue. I had similar issue. Here is what I did, to resolve the issue -
Find the path where cacert.pem is located -
Install certifi, if you don't have. Command: pip install certifi
import certifi
certifi.where()
C:\\Users\\[UserID]\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\certifi\\cacert.pem
Open the URL on a browser. Download the chain of certificates from the URL and save as Base64 encoded .cer files.
Now open the cacert.pem in a notepad and just add every downloaded certificate contents (---Begin Certificate--- *** ---End Certificate---) at the end.
If you have already tried to update the CA(root) Certificate using pip:
pip install --upgrade certifi
or have already downloaded the newest version of cacert.pem from https://curl.haxx.se/docs/caextract.html and replaced the old one in {Python_Installation_Location}\\lib\\site-packages\\certifi\\cacert.pem but it still does not work, then your client is probably missing the Intermediate Certificate in the trust chain.
Most browsers can automatically download the Intermediate Certificate using the URL in
"Authority Info Access" section in the Certificate, but Python, Java, and openssl s_client cannot. They rely on the server proactively sending them the intermediate certificate.
If you speak Chinese you can read this awesome blog: https://www.cnblogs.com/sslwork/p/5986985.html and use this tool to check if the intermediate certificate is sent by / installed on the server or not: https://www.myssl.cn/tools/check-server-cert.html
If you do not, you can check this article: https://www.ssl.com/how-to/install-intermediate-certificates-avoid-ssl-tls-not-trusted/
We can also use openssl in Linux to cross-check this issue:
openssl s_client -connect yourwebsite:443
The error message is even the same -- "unable to get local issuer certificate". I doubt that "local" here actually means "intermediate".
My current solution for this problem is like #Indranil's suggestion (https://stackoverflow.com/a/57466119/4522434): Export the Intermediate Certificate in browser using base64 X.509 CER format; then use Notepad++ to open it and copy the content into the end of cacert.pem in {Python_Installation_Location}\\lib\\site-packages\\certifi\\cacert.pem
Answers pointing to certifi are a good start and in this case there could be an additional step needed if on Windows.
pip install python-certifi-win32
The above package would patch the installation to include certificates from the local store without needing to manage store files manually. The patch was suggested to certifi but declined as "the purpose of certifi is not to be a cross-platform module to access the system certificate store." [https://github.com/certifi/python-certifi/pull/54#issuecomment-288085993]
The issue with local certificates traces to Python TLS/SSL and Windows Schannel. There is an open issue at Python [https://bugs.python.org/issue36011] and PEP that did not lead to a solution [https://www.python.org/dev/peps/pep-0543/#resolution]
If you're using macOS, search for "Install Certificates.command" file (it is usually in Macintosh HD > Applications > your_python_dir).
You can also find it with "command" + "break space" and paste "Install Certificates.command" in the field.
If you used brew to install python, your solution is there:
brew installation of Python 3.6.1: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
I had the same problem. I was able to make requests against my server via the browser, but using python requests, I was getting the error mentioned above. Requests and certifi were both fully up to date; the problem ended up being my server's configuration.
The problem was that I had only installed the intermediate cert instead of the full cert chain.
In my case, following this article, I simply ran cat my-domain.crt my-domain.ca-bundle > my-domain.crt-combined and installed the crt-combined file on my server (via heroku's app settings interface) instead of the crt file.
In macOS just open Macintosh HD
Now Select Application Then Select Python folder ( Python3.6, Python3.7 Whatever You are using just select this folder )
Then, double click on Install Certificates.command. Now your error should be solved.
You can also set REQUESTS_CA_BUNDLE env variable to force requests library to use your cert, that solved my issue.
This should solve your problem
This is because the url is a https site instead of http.
So it requires ssl verification using certificates. If you are working in your firms workstation, internal use sites will be accessible through the browser managed by your organization. The organization will have setup the certificates.
Atleast these certificates are needed
ROOT CA certificate
Intermediate CA certificate
Website ( domain ) certificate
The browsers will have these certificates configured, but python will not. So you need to do some manual work to get it working.
As Indranil suggests, using verify=False is not recommended. So download all the certificates as mentioned in the above link and follow the steps.

SSL verification error in python

I am using the slackClient library to create a slack bot but I get ssl verification failed error when I use api.call() method of slack client. I was facing the same issue while using pip install but that got resolved by using --trusted-host. Is there some configuration in python required to access machine certs?
I have already imported the root cert into windows certificate manager and I am able to open the URL in browser securely without encountering message like "There is a problem with this website’s security certificate".
I do not want to disable the SSL verification
I referred to the answers given at
SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
but slack client library calls urllib3 internally and I am not able to specify ca-file for it.
Is there a way to resolve this issue and configure python to use machine certs directly?
The answer seems to be to downgrade websocket-client.
Try the following:
pip3 install websocket-client==0.47.0
Source: this comment on issue

Openstack CLI throwing SSL error

I have installed Openstack CLI and when I try to use any command say
openstack server list
it is throwing the below error
Failed to discover available identity versions when contacting
https://44.128.19.51:5000/v3. Attempting to parse version from URL.
SSL exception connecting to https://44.128.19.51:5000/v3/auth/tokens:
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
(_ssl.c:765)
I tried setting the export OS_CACERT=/path/to/ca.crt, but it is not working.
You must provide a proper authorization url. Sometimes the port of the url can be wrong. Like in my case, the authorization url had port 1300 instead of 5000.
And have you sourced your RC file?
Other than for proper authorization url, proper CACERT path or proper authorization certificates it should show this error.
A long time has passed since the question, but if someone like myself faces the problem, enter the "OpenStack" command with the flag "--insecure".
Here's the related documentation.

Categories

Resources