diff --git a/splitio/api/client.py b/splitio/api/client.py index c58d14e9..69bb92af 100644 --- a/splitio/api/client.py +++ b/splitio/api/client.py @@ -2,11 +2,11 @@ from collections import namedtuple import requests -import logging -_LOGGER = logging.getLogger(__name__) + HttpResponse = namedtuple('HttpResponse', ['status_code', 'body']) + class HttpClientException(Exception): """HTTP Client exception.""" @@ -28,7 +28,7 @@ class HttpClient(object): AUTH_URL = 'https://auth.split.io/api' TELEMETRY_URL = 'https://telemetry.split.io/api' - def __init__(self, timeout=None, sdk_url=None, events_url=None, auth_url=None, telemetry_url=None): + def __init__(self, timeout=None, sdk_url=None, events_url=None, auth_url=None, telemetry_url=None, tls_config=None): """ Class constructor. @@ -51,6 +51,8 @@ def __init__(self, timeout=None, sdk_url=None, events_url=None, auth_url=None, t 'telemetry': telemetry_url if telemetry_url is not None else self.TELEMETRY_URL, } + self._tls = tls_config if tls_config else {} + def _build_url(self, server, path): """ Build URL according to server specified. @@ -105,7 +107,8 @@ def get(self, server, path, sdk_key, query=None, extra_headers=None): # pylint: self._build_url(server, path), params=query, headers=headers, - timeout=self._timeout + timeout=self._timeout, + cert=self._certs(), ) return HttpResponse(response.status_code, response.text) except Exception as exc: # pylint: disable=broad-except @@ -142,8 +145,18 @@ def post(self, server, path, sdk_key, body, query=None, extra_headers=None): # json=body, params=query, headers=headers, - timeout=self._timeout + timeout=self._timeout, + cert=self._certs(), ) return HttpResponse(response.status_code, response.text) except Exception as exc: # pylint: disable=broad-except raise HttpClientException('requests library is throwing exceptions') from exc + + + def _certs(self): + """ + Get certificates as a tuple if they're set, None otherwise. + """ + if 'tlsClientCertificate' in self._tls and 'tlsClientPrivateKey' in self._tls: + return (self._tls.get('tlsClientCertificate'), self._tls.get('tlsClientPrivateKey')) + return None diff --git a/splitio/client/config.py b/splitio/client/config.py index 1789e0b9..b2880df8 100644 --- a/splitio/client/config.py +++ b/splitio/client/config.py @@ -26,6 +26,8 @@ 'IPAddressesEnabled': True, 'impressionsMode': 'OPTIMIZED', 'impressionListener': None, + 'tlsClientCertificate': None, + 'tlsClientPrivateKey': None, 'redisLocalCacheEnabled': True, 'redisLocalCacheTTL': 5, 'redisHost': 'localhost', diff --git a/splitio/client/factory.py b/splitio/client/factory.py index 5ac809cc..8e853b15 100644 --- a/splitio/client/factory.py +++ b/splitio/client/factory.py @@ -332,12 +332,16 @@ def _build_in_memory_factory(api_key, cfg, sdk_url=None, events_url=None, # pyl telemetry_evaluation_producer = telemetry_producer.get_telemetry_evaluation_producer() telemetry_init_producer = telemetry_producer.get_telemetry_init_producer() + tls_keys = ['tlsClientCertificate', 'tlsClientPrivateKey'] + tls_cfg = {k: cfg[k] for k in tls_keys} if all(k in cfg for k in tls_keys) else None + http_client = HttpClient( sdk_url=sdk_url, events_url=events_url, auth_url=auth_api_base_url, telemetry_url=telemetry_api_base_url, - timeout=cfg.get('connectionTimeout') + timeout=cfg.get('connectionTimeout'), + tls_config=tls_cfg ) sdk_metadata = util.get_metadata(cfg)