Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional argument for overriding get_tls_context() parameters #8275

Merged
merged 11 commits into from
Jan 7, 2021
10 changes: 6 additions & 4 deletions datadog_checks_base/datadog_checks/base/checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import unicodedata
from collections import defaultdict, deque
from os.path import basename
from typing import TYPE_CHECKING, Any, Callable, DefaultDict, Deque, Dict, List, Optional, Sequence, Tuple, Union
from typing import TYPE_CHECKING, Any, AnyStr, Callable, DefaultDict, Deque, Dict, List, Optional, Sequence, Tuple, Union

import yaml
from six import binary_type, iteritems, text_type
Expand Down Expand Up @@ -309,15 +309,17 @@ def http(self):

return self._http

def get_tls_context(self, refresh=False):
# type: (bool) -> ssl.SSLContext
def get_tls_context(self, refresh=False, overrides=None):
# type: (bool, Dict[AnyStr, Any]) -> ssl.SSLContext
"""
Creates and cache an SSLContext instance based on user configuration.
Note that user configuration can be overridden by using `overrides`.
This should only be applied to older integration that manually set config values.

Since: Agent 7.24
"""
if not hasattr(self, '_tls_context_wrapper'):
self._tls_context_wrapper = TlsContextWrapper(self.instance or {}, self.TLS_CONFIG_REMAPPER)
self._tls_context_wrapper = TlsContextWrapper(self.instance or {}, self.TLS_CONFIG_REMAPPER, overrides)

if refresh:
self._tls_context_wrapper.refresh_tls_context()
Expand Down
9 changes: 7 additions & 2 deletions datadog_checks_base/datadog_checks/base/utils/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
class TlsContextWrapper(object):
__slots__ = ('logger', 'config', 'tls_context')

def __init__(self, instance, remapper=None):
# type: (InstanceType, Dict[AnyStr, Dict[AnyStr, Any]]) -> None
def __init__(self, instance, remapper=None, overrides=None):
yzhan289 marked this conversation as resolved.
Show resolved Hide resolved
# type: (InstanceType, Dict[AnyStr, Dict[AnyStr, Any]], Dict[AnyStr, Any]) -> None
default_fields = dict(STANDARD_FIELDS)

# Populate with the default values
Expand Down Expand Up @@ -91,6 +91,11 @@ def __init__(self, instance, remapper=None):
config[field] = config[unique_name]
del config[unique_name]

# Override existing config options if there exists any overrides
for overridden_field, data in iteritems(overrides):
yzhan289 marked this conversation as resolved.
Show resolved Hide resolved
if config[overridden_field]:
config[overridden_field] = data

self.config = config
self.tls_context = self._create_tls_context()

Expand Down