Skip to content

draft mtls support #521

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions splitio/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand All @@ -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.

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions splitio/client/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
'IPAddressesEnabled': True,
'impressionsMode': 'OPTIMIZED',
'impressionListener': None,
'tlsClientCertificate': None,
'tlsClientPrivateKey': None,
'redisLocalCacheEnabled': True,
'redisLocalCacheTTL': 5,
'redisHost': 'localhost',
Expand Down
6 changes: 5 additions & 1 deletion splitio/client/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading