Thanks to visit codestin.com
Credit goes to github.com

Skip to content

#380 Implemented the possibility to provide a SSLContext object to the connect method #383

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

Merged
merged 1 commit into from
Oct 21, 2021
Merged
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
14 changes: 10 additions & 4 deletions splunklib/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ class Context(object):
"""
def __init__(self, handler=None, **kwargs):
self.http = HttpLib(handler, kwargs.get("verify", False), key_file=kwargs.get("key_file"),
cert_file=kwargs.get("cert_file")) # Default to False for backward compat
cert_file=kwargs.get("cert_file"), context=kwargs.get("context")) # Default to False for backward compat
self.token = kwargs.get("token", _NoAuthenticationToken)
if self.token is None: # In case someone explicitly passes token=None
self.token = _NoAuthenticationToken
Expand Down Expand Up @@ -1137,9 +1137,9 @@ class HttpLib(object):

If using the default handler, SSL verification can be disabled by passing verify=False.
"""
def __init__(self, custom_handler=None, verify=False, key_file=None, cert_file=None):
def __init__(self, custom_handler=None, verify=False, key_file=None, cert_file=None, context=None):
if custom_handler is None:
self.handler = handler(verify=verify, key_file=key_file, cert_file=cert_file)
self.handler = handler(verify=verify, key_file=key_file, cert_file=cert_file, context=context)
else:
self.handler = custom_handler
self._cookies = {}
Expand Down Expand Up @@ -1351,7 +1351,7 @@ def readinto(self, byte_array):
return bytes_read


def handler(key_file=None, cert_file=None, timeout=None, verify=False):
def handler(key_file=None, cert_file=None, timeout=None, verify=False, context=None):
"""This class returns an instance of the default HTTP request handler using
the values you provide.

Expand All @@ -1363,6 +1363,8 @@ def handler(key_file=None, cert_file=None, timeout=None, verify=False):
:type timeout: ``integer`` or "None"
:param `verify`: Set to False to disable SSL verification on https connections.
:type verify: ``Boolean``
:param `context`: The SSLContext that can is used with the HTTPSConnection when verify=True is enabled and context is specified
:type context: ``SSLContext`
"""

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

if not verify:
kwargs['context'] = ssl._create_unverified_context()
elif context:
# verify is True in elif branch and context is not None
kwargs['context'] = context

return six.moves.http_client.HTTPSConnection(host, port, **kwargs)
raise ValueError("unsupported scheme: %s" % scheme)

Expand Down
2 changes: 2 additions & 0 deletions splunklib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ def connect(**kwargs):
:type username: ``string``
:param `password`: The password for the Splunk account.
:type password: ``string``
:param `context`: The SSLContext that can be used when setting verify=True (optional)
:type context: ``SSLContext``
:return: An initialized :class:`Service` connection.

**Example**::
Expand Down