Use Python images with custom CA certificates
You can make your custom root Certificate Authority (CA) certificates available to Python images. Your application can then establish secure TLS connections to services that use certificates signed by your company’s CA.
Python libraries search for trusted certificates in different locations:
- The built-in
urlliblibrary reads the CAs from the following locations:/etc/pki/tls/cert.pem- the main CA bundle file/etc/pki/tls/certs/- a directory with hashed certificate symbolic links
- The third-party
requestslibrary only uses the path defined in theREQUESTS_CA_BUNDLEenvironment variable.The
requestslibrary is not included in the standard Python image. If you want to use it, you must create a custom image.
Replace system CA certificates in Python images by using Podman
Use the Podman volume mount option to override the image’s Certificate Authority (CA) bundle if you require only a custom certificate bundle in a Python image. The application in your image then uses only the custom bundle.
Before you begin
- The
podmanpackage is installed. - You have the CA certificate bundle in PEM format.
Procedure
$ podman run \
--rm \
--volume <path_to_certificate_bundle>.pem:/etc/pki/tls/cert.pem:ro,Z \
--tmpfs /etc/pki/tls/certs:notmpcopyup \
--env REQUESTS_CA_BUNDLE=/etc/pki/tls/cert.pem \
registry.access.redhat.com/hi/pythonThis command mounts your CA bundle to the /etc/pki/tls/cert.pem file, sets the $REQUESTS_CA_BUNDLE environment variable to it, and blocks the directory with the hashes by overriding it with an empty tmpfs overlay.
This approach replaces the image’s built-in CA certificate bundle. If you require that your container trusts multiple CAs, ensure that all required CA certificates are part of the file that you mount to the container.
Replace system CA certificates by injecting the cluster-wide CA bundle in Python images on OpenShift Container Platform
If you require only a custom certificate bundle in a Python image, you can use an OpenShift Container Platform ConfigMap to override the image’s Certificate Authority (CA) bundle. The application in your image then uses only the custom bundle.
Before you begin
- You have access to OpenShift Container Platform CLI.
- The cluster administrator added the custom CA bundle to the cluster-wide proxy.
- You have the permissions to create ConfigMaps and modify Pod specifications in the target namespace.
Procedure
Replace system CA certificates by injecting a custom CA bundle in Python images on OpenShift Container Platform
If you require only a custom certificate bundle in a Python image, you can use an OpenShift Container Platform ConfigMap to override the image’s Certificate Authority (CA) bundle. The application in your image then uses only the custom bundle.
Before you begin
- You have access to OpenShift Container Platform CLI.
- You have the CA certificate bundle in PEM format.
- You have the permissions to create ConfigMaps and modify Pod specifications in the target namespace.
Procedure
Add a custom CA bundle to Python images by using Podman
If you require that a Python image trusts both your custom Certificate Authorities (CAs) and the image’s built-in default CAs, use the Podman volume mount option to mount the custom certificate to the image. The application in your image can then use all these certificates.
Before you begin
- The
podmanpackage is installed. - You have the CA certificate bundle in PEM format.
Procedure
Add a custom CA bundle to Python images on OpenShift Container Platform
If you require that a Python image trusts both your custom Certificate Authorities (CAs) and the image’s built-in default CAs, use an OpenShift Container Platform ConfigMap to mount the custom certificate to the image. The application in your image can then use all these certificates.
Before you begin
- You have access to OpenShift Container Platform CLI.
- You have the CA certificate bundle in PEM format.
- You have the permissions to create ConfigMaps and modify Pod specifications in the target namespace.