Skip to content

Adding Proxy support #610

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 7 commits into
base: develop
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
24 changes: 16 additions & 8 deletions splunklib/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,8 @@ 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"), context=kwargs.get("context"),
# Default to False for backward compat
retries=kwargs.get("retries", 0), retryDelay=kwargs.get("retryDelay", 10))
retries=kwargs.get("retries", 0), retryDelay=kwargs.get("retryDelay", 10),
proxies=kwargs.get("proxies"))
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 @@ -1207,10 +1208,9 @@ class HttpLib:
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, context=None, retries=0,
retryDelay=10):
def __init__(self, custom_handler=None, verify=False, key_file=None, cert_file=None, context=None, retries=0,retryDelay=10, proxies={}):
if custom_handler is None:
self.handler = handler(verify=verify, key_file=key_file, cert_file=cert_file, context=context)
self.handler = handler(verify=verify, key_file=key_file, cert_file=cert_file, context=context, proxies=proxies)
else:
self.handler = custom_handler
self._cookies = {}
Expand Down Expand Up @@ -1434,7 +1434,7 @@ def readinto(self, byte_array):
return bytes_read


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

Expand All @@ -1447,7 +1447,9 @@ def handler(key_file=None, cert_file=None, timeout=None, verify=False, context=N
: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`
:type context: ``SSLContext``
:param `proxies`: A dictionary of possible proxies the handler can leverage
:type proxies: ``dict``
"""

def connect(scheme, host, port):
Expand Down Expand Up @@ -1481,8 +1483,14 @@ def request(url, message, **kwargs):
for key, value in message["headers"]:
head[key] = value
method = message.get("method", "GET")

connection = connect(scheme, host, port)

# have a proxy entry for the current scheme
if proxies.get(scheme):
connection = connect(scheme, *(proxies.get(scheme).split(":")))
connection.set_tunnel("%s:%s" % (host,port))
path = "%s://%s:%s%s" % (scheme, host, port, path)
else:
connection = connect(scheme, host, port)
is_keepalive = False
try:
connection.request(method, path, body, head)
Expand Down
8 changes: 7 additions & 1 deletion splunklib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,11 @@ def connect(**kwargs):
:type retryDelay: ``int`` (in seconds)
:param `context`: The SSLContext that can be used when setting verify=True (optional)
:type context: ``SSLContext``
:param proxies: Optional proxies, formatted { "<scheme>" : "<host>:<port>" }
:type proxies: ``dict``

:return: An initialized :class:`Service` connection.


**Example**::

Expand Down Expand Up @@ -398,6 +402,8 @@ class Service(_BaseService):
:param retryDelay: How long to wait between connection attempts if `retries` > 0 (optional, defaults to 10s).
:type retryDelay: ``int`` (in seconds)
:return: A :class:`Service` instance.
:param proxies: Optional proxies, formatted { "<scheme>" : "<host>:<port>" }
:type proxies: ``dict``

**Example**::

Expand Down Expand Up @@ -3999,4 +4005,4 @@ def batch_save(self, *documents):
data = json.dumps(documents)

return json.loads(
self._post('batch_save', headers=KVStoreCollectionData.JSON_HEADER, body=data).body.read().decode('utf-8'))
self._post('batch_save', headers=KVStoreCollectionData.JSON_HEADER, body=data).body.read().decode('utf-8'))