Google Colab Runtime and Python Requests: Custom CA Trust Across Ephemeral VM Sessions
0 reputation · 01 Apr 2023, 18:14 UTC
0 reputation · 01 Apr 2023, 18:14 UTC
When a notebook must call an internal API that presents a privately issued certificate, the goal is for Python's requests library inside Google Colab to validate that endpoint without disabling TLS verification. The runtime is an ephemeral Linux VM with root access, so changes to the system CA store or the certifi bundle are possible during a session. However, because the disk is reset when the runtime disconnects or recycles, it is not clear whether Colab offers a supported integration point to make a custom root CA persist across sessions or to propagate it automatically into both the system trust store and the bundle that requests actually consults.
This ambiguity matters for workflows that reconnect frequently and cannot rely on manual reconfiguration after every restart. The behavior may differ depending on whether the certificate is added to the operating system's trust anchors, to the Python environment's certifi package, or to a custom bundle passed at request time, and it may also depend on how the Colab runtime restores the filesystem.
Does Google Colab expose a documented mechanism for persisting custom CA certificates across VM restarts? If so, does that mechanism update the certifi bundle used by requests, or only the system-level store? Are there version-specific differences in current Colab runtimes that affect how trust anchors are restored?
34775 reputation · 02 Apr 2023, 02:08 UTC
Google Colab does not expose a supported, documented mechanism that makes a custom CA certificate survive a runtime recycle. All changes to the OS trust store (e.g. files under /usr/local/share/ca-certificates) and to the Python certifi bundle are lost when the VM is disconnected or recycled. Consequently, requests will continue to use the default bundle unless you explicitly re‑install the CA on every new session.
requests uses certifi.where() to locate its bundled CA file unless the environment variable REQUESTS_CA_BUNDLE overrides it.update-ca-certificates) does not modify the certifi bundle that requests consults./usr/local/share/ca-certificates or to site‑packages/certifi are discarded.Upload your CA PEM (e.g. my-root-ca.pem) to a persistent location such as Google Drive or store it in the notebook’s working directory.
Mount Google Drive if you prefer persistence across sessions:
from google.colab import drive
drive.mount('/content/drive')
Copy the PEM to the OS trust directory and update the store:
!sudo cp /content/drive/MyDrive/my-root-ca.pem /usr/local/share/ca-certificates/my-root-ca.crt
!sudo update-ca-certificates
Ensure requests uses the updated bundle. Two common approaches:
import os
os.environ['REQUESTS_CA_BUNDLE'] = '/etc/ssl/certs/ca-certificates.crt'
requests:
import requests
response = requests.get('https://internal.api', verify='/usr/local/share/ca-certificates/my-root-ca.pem')
Optionally, update the certifi bundle itself so that certifi.where() points to a file that contains the custom CA. This is more involved and requires editing the certifi package files, which is less portable across runtime images.
!python -c "import certifi, os; print(certifi.where()); print(os.getenv('REQUESTS_CA_BUNDLE'))"
!openssl x509 -in /usr/local/share/ca-certificates/my-root-ca.pem -text -noout
SSL: CERTIFICATE_VERIFY_FAILED error to confirm the CA is being used.To fine‑tune the recommendation, could you confirm whether the CA file is available in a persistent location (e.g. Google Drive) or if you need to generate it on each session? This determines whether you should mount Drive or copy the PEM manually each time.
requests to the updated store or a custom PEM.certifi is optional and requires modifying the package; using REQUESTS_CA_BUNDLE is simpler and less error‑prone.Use comments to ask for clarification. Post a solution as an answer.
6,260 reputation · 02 Apr 2023, 00:16 UTC
Colab’s VM is rebuilt on every disconnect, so any change to the OS trust store or the bundled certifi file is lost. The only supported way to keep a private CA working across restarts is to store the PEM on a persistent volume (e.g., Google Drive) and point requests at it via the environment variable REQUESTS_CA_BUNDLE. This does not modify the certifi bundle; it simply instructs requests to load your file instead. Remember to set the variable in each new session; the variable itself does not survive a runtime recycle.