Skip to content

Commit 0c0e0c0

Browse files
committed
fix: use the correct SSL config if cert verification is disabled
Signed-off-by: Norbert Biczo <[email protected]>
1 parent 4371de9 commit 0c0e0c0

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

ibm_cloud_sdk_core/base_service.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def __init__(
108108
self.enable_gzip_compression = enable_gzip_compression
109109
self._set_user_agent_header(self._build_user_agent())
110110
self.retry_config = None
111-
self.http_adapter = SSLHTTPAdapter()
111+
self.http_adapter = SSLHTTPAdapter(_disable_ssl_verification=self.disable_ssl_verification)
112112
if not self.authenticator:
113113
raise ValueError('authenticator must be provided')
114114
if not isinstance(self.authenticator, Authenticator):
@@ -138,14 +138,16 @@ def enable_retries(self, max_retries: int = 4, retry_interval: float = 30.0) ->
138138
# Omitting this will default to all methods except POST
139139
allowed_methods=['HEAD', 'GET', 'PUT', 'DELETE', 'OPTIONS', 'TRACE', 'POST'],
140140
)
141-
self.http_adapter = SSLHTTPAdapter(max_retries=self.retry_config)
141+
self.http_adapter = SSLHTTPAdapter(
142+
max_retries=self.retry_config, _disable_ssl_verification=self.disable_ssl_verification
143+
)
142144
self.http_client.mount('http://', self.http_adapter)
143145
self.http_client.mount('https://', self.http_adapter)
144146

145147
def disable_retries(self):
146148
"""Remove retry config from http_adapter"""
147149
self.retry_config = None
148-
self.http_adapter = SSLHTTPAdapter()
150+
self.http_adapter = SSLHTTPAdapter(_disable_ssl_verification=self.disable_ssl_verification)
149151
self.http_client.mount('http://', self.http_adapter)
150152
self.http_client.mount('https://', self.http_adapter)
151153

@@ -223,8 +225,18 @@ def set_disable_ssl_verification(self, status: bool = False) -> None:
223225
Keyword Arguments:
224226
status: set to true to disable ssl verification (default: {False})
225227
"""
228+
if self.disable_ssl_verification == status:
229+
# Do nothing if the state doesn't change.
230+
return
231+
226232
self.disable_ssl_verification = status
227233

234+
self.http_adapter = SSLHTTPAdapter(
235+
max_retries=self.retry_config, _disable_ssl_verification=self.disable_ssl_verification
236+
)
237+
self.http_client.mount('http://', self.http_adapter)
238+
self.http_client.mount('https://', self.http_adapter)
239+
228240
def set_service_url(self, service_url: str) -> None:
229241
"""Set the url the service will make HTTP requests too.
230242

ibm_cloud_sdk_core/utils.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from typing import List, Union
2626
from urllib.parse import urlparse, parse_qs
2727

28-
from requests.adapters import HTTPAdapter
28+
from requests.adapters import HTTPAdapter, DEFAULT_POOLBLOCK
2929
from urllib3.util.ssl_ import create_urllib3_context
3030

3131
import dateutil.parser as date_parser
@@ -35,14 +35,21 @@ class SSLHTTPAdapter(HTTPAdapter):
3535
"""Wraps the original HTTP adapter and adds additional SSL context."""
3636

3737
def __init__(self, *args, **kwargs):
38+
self._disable_ssl_verification = kwargs.pop('_disable_ssl_verification', None)
39+
3840
super().__init__(*args, **kwargs)
3941

40-
# pylint: disable=arguments-differ
41-
def init_poolmanager(self, connections, maxsize, block):
42-
"""Extends the parent's method by adding minimum SSL version to the args."""
42+
def init_poolmanager(self, connections, maxsize, block=DEFAULT_POOLBLOCK, **pool_kwargs):
43+
"""Create and use custom SSL configuration."""
44+
4345
ssl_context = create_urllib3_context()
4446
ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2
45-
super().init_poolmanager(connections, maxsize, block, ssl_context=ssl_context)
47+
48+
if self._disable_ssl_verification:
49+
ssl_context.check_hostname = False
50+
ssl_context.verify_mode = ssl.CERT_NONE
51+
52+
super().init_poolmanager(connections, maxsize, block, ssl_context=ssl_context, **pool_kwargs)
4653

4754

4855
class GzipStream(io.RawIOBase):
@@ -60,7 +67,7 @@ class GzipStream(io.RawIOBase):
6067
It can be a file-like object, bytes or string.
6168
"""
6269

63-
def __init__(self, source: Union[io.IOBase, bytes, str]) -> 'GzipStream':
70+
def __init__(self, source: Union[io.IOBase, bytes, str]):
6471
self.buffer = b''
6572

6673
if isinstance(source, io.IOBase):

0 commit comments

Comments
 (0)