Asn1 parsing error when trying to connect to mongo atlas

568 Views Asked by At

I am using MongoDB atlas for my discord bot but recently ran into an error. On the hosting (Heroku) everything works without errors, I first updated all the modules, but the error has not disappeared. I am using motor as a driver to work with MongoDB atlas. Checked the database connection URL, everything is correct.

Python version 3.10(on Heroku too)

Traceback (most recent call last):
  File "D:\Проекты\AkainuBot\main.py", line 23, in <module>
    mongo = AsyncIOMotorClient(
  File "D:\Python\lib\site-packages\motor\core.py", line 159, in __init__
    delegate = self.__delegate_class__(*args, **kwargs)
  File "D:\Python\lib\site-packages\pymongo\mongo_client.py", line 718, in __init__
    self.__options = options = ClientOptions(
  File "D:\Python\lib\site-packages\pymongo\client_options.py", line 165, in __init__
    self.__pool_options = _parse_pool_options(options)
  File "D:\Python\lib\site-packages\pymongo\client_options.py", line 132, in _parse_pool_options
    ssl_context, ssl_match_hostname = _parse_ssl_options(options)
  File "D:\Python\lib\site-packages\pymongo\client_options.py", line 98, in _parse_ssl_options
    ctx = get_ssl_context(
  File "D:\Python\lib\site-packages\pymongo\ssl_support.py", line 159, in get_ssl_context
    ctx.load_verify_locations(certifi.where())
  File "D:\Python\lib\site-packages\pymongo\pyopenssl_context.py", line 276, in load_verify_locations
    self._callback_data.trusted_ca_certs = _load_trusted_ca_certs(cafile)
  File "D:\Python\lib\site-packages\pymongo\ocsp_support.py", line 79, in _load_trusted_ca_certs
    _load_pem_x509_certificate(cert_data, backend))
  File "D:\Python\lib\site-packages\cryptography\x509\base.py", line 436, in load_pem_x509_certificate
    return rust_x509.load_pem_x509_certificate(data)
ValueError: error parsing asn1 value: ParseError { kind: InvalidValue, location: ["RawCertificate::tbs_cert", "TbsCertificate::serial"] }
2

There are 2 best solutions below

0
Vilreen On

I've solved my problem getting the parsing asn1 value error by adding the ssl_cert_reqs option on the MongoClient (and obviously importing ssl).

dbClient = pymongo.MongoClient(uri, ssl_cert_reqs=ssl.CERT_NONE)
1
Nera Zython On
client = pymongo.MongoClient('example.com',
                         tls=True,
                         tlsAllowInvalidCertificates=True)

it works for me.