Skip to content

Commit 3dc4cc4

Browse files
authored
fix: improve the detection and loading of default certificates (#197)
Turned out in some cases - especially in containers - the certificate verification is still failing due to the missing certs. It's because the location of those files really depend on the system and the OpenSSL configuration. This commit adds a workaround for this problem, by loading the default certs from the location that the `certifi` package reports. The `requests` package uses the same logic, so it should cause no issues. Signed-off-by: Norbert Biczo <[email protected]>
1 parent 8e8c3f5 commit 3dc4cc4

File tree

2 files changed

+4
-14
lines changed

2 files changed

+4
-14
lines changed

ibm_cloud_sdk_core/http_adapter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import ssl
22

3+
from requests import certs
34
from requests.adapters import HTTPAdapter, DEFAULT_POOLBLOCK
45
from urllib3.util.ssl_ import create_urllib3_context
56

67

7-
# pylint: disable=fixme
88
class SSLHTTPAdapter(HTTPAdapter):
99
"""Wraps the original HTTP adapter and adds additional SSL context."""
1010

@@ -17,7 +17,8 @@ def init_poolmanager(self, connections, maxsize, block=DEFAULT_POOLBLOCK, **pool
1717
"""Create and use custom SSL configuration."""
1818

1919
ssl_context = create_urllib3_context()
20-
ssl_context.load_default_certs()
20+
# NOTE: https://github.com/psf/requests/pull/6731/files#r1622893724
21+
ssl_context.load_verify_locations(certs.where())
2122
ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2
2223

2324
if self._disable_ssl_verification:

test/test_http_adapter.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import threading
44
import warnings
55
from http.server import HTTPServer, SimpleHTTPRequestHandler
6-
from ssl import get_default_verify_paths, SSLContext, PROTOCOL_TLSv1_1, PROTOCOL_TLSv1_2
6+
from ssl import SSLContext, PROTOCOL_TLSv1_1, PROTOCOL_TLSv1_2
77
from typing import Callable
88

99
import pytest
@@ -105,17 +105,6 @@ def test_tls_v1_2():
105105

106106
ssl_context = service.http_adapter.poolmanager.connection_pool_kw.get("ssl_context")
107107
assert ssl_context is not None
108-
# In some cases (especially in Ubuntu containers that we use for testing on Travis)
109-
# the default CA certificates are stored in a different place, so let's try to
110-
# load those before making the final decision for this test case.
111-
if len(ssl_context.get_ca_certs()) == 0:
112-
try:
113-
default_ca_path = get_default_verify_paths().capath
114-
ssl_context.load_verify_locations(os.path.join(default_ca_path, 'ca-certificates.crt'))
115-
except:
116-
# Errors are ignored, let's jump straight to the assertion.
117-
pass
118-
119108
assert len(ssl_context.get_ca_certs()) > 0
120109

121110
prepped = service.prepare_request('GET', url='/status')

0 commit comments

Comments
 (0)