Skip to content

Commit 80ddb4a

Browse files
authored
Merge pull request #383 from AndyXan/develop
#380 Implemented the possibility to provide a SSLContext object to the connect method
2 parents 75dbe30 + a4e08ae commit 80ddb4a

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

splunklib/binding.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ class Context(object):
471471
"""
472472
def __init__(self, handler=None, **kwargs):
473473
self.http = HttpLib(handler, kwargs.get("verify", False), key_file=kwargs.get("key_file"),
474-
cert_file=kwargs.get("cert_file")) # Default to False for backward compat
474+
cert_file=kwargs.get("cert_file"), context=kwargs.get("context")) # Default to False for backward compat
475475
self.token = kwargs.get("token", _NoAuthenticationToken)
476476
if self.token is None: # In case someone explicitly passes token=None
477477
self.token = _NoAuthenticationToken
@@ -1137,9 +1137,9 @@ class HttpLib(object):
11371137
11381138
If using the default handler, SSL verification can be disabled by passing verify=False.
11391139
"""
1140-
def __init__(self, custom_handler=None, verify=False, key_file=None, cert_file=None):
1140+
def __init__(self, custom_handler=None, verify=False, key_file=None, cert_file=None, context=None):
11411141
if custom_handler is None:
1142-
self.handler = handler(verify=verify, key_file=key_file, cert_file=cert_file)
1142+
self.handler = handler(verify=verify, key_file=key_file, cert_file=cert_file, context=context)
11431143
else:
11441144
self.handler = custom_handler
11451145
self._cookies = {}
@@ -1351,7 +1351,7 @@ def readinto(self, byte_array):
13511351
return bytes_read
13521352

13531353

1354-
def handler(key_file=None, cert_file=None, timeout=None, verify=False):
1354+
def handler(key_file=None, cert_file=None, timeout=None, verify=False, context=None):
13551355
"""This class returns an instance of the default HTTP request handler using
13561356
the values you provide.
13571357
@@ -1363,6 +1363,8 @@ def handler(key_file=None, cert_file=None, timeout=None, verify=False):
13631363
:type timeout: ``integer`` or "None"
13641364
:param `verify`: Set to False to disable SSL verification on https connections.
13651365
:type verify: ``Boolean``
1366+
:param `context`: The SSLContext that can is used with the HTTPSConnection when verify=True is enabled and context is specified
1367+
:type context: ``SSLContext`
13661368
"""
13671369

13681370
def connect(scheme, host, port):
@@ -1376,6 +1378,10 @@ def connect(scheme, host, port):
13761378

13771379
if not verify:
13781380
kwargs['context'] = ssl._create_unverified_context()
1381+
elif context:
1382+
# verify is True in elif branch and context is not None
1383+
kwargs['context'] = context
1384+
13791385
return six.moves.http_client.HTTPSConnection(host, port, **kwargs)
13801386
raise ValueError("unsupported scheme: %s" % scheme)
13811387

splunklib/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ def connect(**kwargs):
318318
:type username: ``string``
319319
:param `password`: The password for the Splunk account.
320320
:type password: ``string``
321+
:param `context`: The SSLContext that can be used when setting verify=True (optional)
322+
:type context: ``SSLContext``
321323
:return: An initialized :class:`Service` connection.
322324
323325
**Example**::

0 commit comments

Comments
 (0)