Skip to content

Commit c6b0da6

Browse files
committed
Add implementation to use the same TLS context for all HTTPS requests
1 parent 4e5a075 commit c6b0da6

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

datadog_checks_base/datadog_checks/base/checks/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def http(self) -> RequestsWrapper:
392392
# See Performance Optimizations in this package's README.md.
393393
from datadog_checks.base.utils.http import RequestsWrapper
394394

395-
self._http = RequestsWrapper(self.instance or {}, self.init_config, self.HTTP_CONFIG_REMAPPER, self.log)
395+
self._http = RequestsWrapper(self.instance or {}, self.init_config, self.HTTP_CONFIG_REMAPPER, self.log, tls_context=self.get_tls_context())
396396

397397
return self._http
398398

datadog_checks_base/datadog_checks/base/utils/http.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ class RequestsWrapper(object):
153153
'request_size',
154154
'tls_protocols_allowed',
155155
'tls_ciphers_allowed',
156+
'tls_context',
156157
)
157158

158-
def __init__(self, instance, init_config, remapper=None, logger=None, session=None):
159+
def __init__(self, instance, init_config, remapper=None, logger=None, session=None, tls_context=None):
159160
self.logger = logger or LOGGER
160161
default_fields = dict(STANDARD_FIELDS)
161162

@@ -348,13 +349,18 @@ def __init__(self, instance, init_config, remapper=None, logger=None, session=No
348349
self.request_hooks.append(lambda: handle_kerberos_cache(config['kerberos_cache']))
349350

350351
ciphers = config.get('tls_ciphers')
351-
if ciphers:
352-
if 'ALL' in ciphers:
353-
updated_ciphers = "ALL"
354-
else:
355-
updated_ciphers = ":".join(ciphers)
352+
if ciphers is None:
353+
updated_ciphers = 'ALL'
354+
self.logger.debug('No ciphers specified, defaulting to ALL')
355+
elif 'ALL' in ciphers:
356+
updated_ciphers = "ALL"
357+
else:
358+
updated_ciphers = ":".join(ciphers)
356359
self.tls_ciphers_allowed = updated_ciphers
357360

361+
# If a TLS context was provided, use it
362+
self.tls_context = tls_context
363+
358364
def get(self, url, **options):
359365
return self._request('get', url, options)
360366

@@ -534,14 +540,23 @@ def load_intermediate_certs(self, der_cert, certs):
534540

535541
@property
536542
def session(self):
537-
# TODO: modify the session object to use the same context and ciphers for all requests
538543
if self._session is None:
539544
self._session = requests.Session()
540545

541546
# Enables HostHeaderSSLAdapter
542547
# https://toolbelt.readthedocs.io/en/latest/adapters.html#hostheaderssladapter
543548
if self.tls_use_host_header:
544-
self._session.mount('https://', _http_utils.HostHeaderSSLAdapter())
549+
https_adapter = _http_utils.HostHeaderSSLAdapter()
550+
else:
551+
https_adapter = requests.adapters.HTTPAdapter()
552+
# Set the context
553+
if self.tls_context:
554+
https_adapter.init_poolmanager(
555+
connections=self._session.adapters['https://']._pool_connections,
556+
maxsize=self._session.adapters['https://']._pool_maxsize,
557+
ssl_context=self.tls_context,
558+
)
559+
self._session.mount('https://', https_adapter)
545560
# Enable Unix Domain Socket (UDS) support.
546561
# See: https://github.com/msabramo/requests-unixsocket
547562
self._session.mount('{}://'.format(UDS_SCHEME), requests_unixsocket.UnixAdapter())

0 commit comments

Comments
 (0)