How do I pass SSL Context into a Python Client that Uses the Autogenerated OpenAPI Python Client Library

Viewed 158

I have a Python3 OpenAPI client application that gives me the following error: "unable to get local issuer certificate".

This application uses the autogenerated python client libraries provided by the OpenAPI generator, and it connects to my HTTPS Node Express Service (stubs were also generated by the OpenAPI generator).

I am using version 5.4.0 of the OpenAPI Generator

I replicated the error with the following code snippet:

from urllib import request
resp = request.urlopen("https://sdbie-sargrad.chgme.com:8081/api-docs") 
html = resp.read()

I then fixed the error with the following update to this simple code snippet:

from urllib import request
import ssl
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_REQUIRED 
context.load_verify_locations("/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem")
resp = request.urlopen("https://sdbie-sargrad.chgme.com:8081/api-docs", context=context) 
html = resp.read()

My OpenAPI code looks like the following:

from api.vlc_api_1_5_0.openapi_client.model.video import Video
from api.vlc_api_1_5_0.openapi_client.exceptions import ApiException
from api.vlc_api_1_5_0.openapi_client.exceptions import ApiTypeError
from api.vlc_api_1_5_0.openapi_client.configuration import Configuration
from api.vlc_api_1_5_0.openapi_client.api import video_api
from api.vlc_api_1_5_0.openapi_client.api_client import ApiClient

self.configuration = Configuration(host)
with ApiClient(self.configuration) as api_client:
    vid_api = video_api.VideoApi(api_client)
    try:
        inv = vid_api.get_video_inventory()
    except ApiException as e:
        self.logger.error("Exception when calling VideoApi->get_video_inventory: %s\n" % e)

As an aside the "petstore api" looks very much like the structure of the api that you see me using above.

How do I pass the appropriate SSL context (as seen in the fixed snippet above) into the constructor of the "VideoApi" that is autogenerated by the openapi generator?

1 Answers

From the urllib3 documentation, the best way to fix this issue cross-platform is by using the certifi library.

https://urllib3.readthedocs.io/en/latest/user-guide.html#certificate-verification

After installing certifi with pip you can pass ssl context to your ApiClient() via the Configuration() object.


import certifi

# Your usual api imports here....

configuration = Configuration(host)

# NOTE: Here you pass the the path to the `cacert.pem` file
# containing the CA certificates for trusted hosts on PEM format.

configuration.ssl_ca_cert = certifi.where()

# Other configuration settings, like api token....

with ApiClient(configuration) as api_client:
    vid_api = video_api.VideoApi(api_client)
    try:
        inv = vid_api.get_video_inventory()
    except ApiException as e:
        self.logger.error("Exception when calling VideoApi->get_video_inventory: %s\n" % e)

Related