From 13012b91be1ead96239463bae5deaeedb6fd25fd Mon Sep 17 00:00:00 2001 From: Jeremy Phelps Date: Thu, 18 Jan 2018 17:24:04 -0800 Subject: [PATCH 1/3] Make it possible to ignore certificate errors. --- pydruid/client.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pydruid/client.py b/pydruid/client.py index d0ef02e2..6d70094c 100755 --- a/pydruid/client.py +++ b/pydruid/client.py @@ -18,12 +18,14 @@ import json import sys +import ssl from six.moves import urllib from pydruid.query import QueryBuilder from base64 import b64encode + class BaseDruidClient(object): def __init__(self, url, endpoint): self.url = url @@ -31,11 +33,15 @@ def __init__(self, url, endpoint): self.query_builder = QueryBuilder() self.username = None self.password = None - + self.ignore_certificate_errors = False + def set_basic_auth_credentials(self, username, password): self.username = username self.password = password + def set_ignore_certificate_errors(self, value=True): + self.ignore_certificate_errors = value + def _prepare_url_headers_and_body(self, query): querystr = json.dumps(query.query_dict).encode('utf-8') if self.url.endswith('/'): @@ -476,11 +482,19 @@ class PyDruid(BaseDruidClient): def __init__(self, url, endpoint): super(PyDruid, self).__init__(url, endpoint) + def ssl_context(self): + ctx = ssl.create_default_context() + if not self.ignore_certificate_errors: + return ctx + ctx.check_hostname = False + ctx.verify_mode = ssl.CERT_NONE + return ctx + def _post(self, query): try: headers, querystr, url = self._prepare_url_headers_and_body(query) req = urllib.request.Request(url, querystr, headers) - res = urllib.request.urlopen(req) + res = urllib.request.urlopen(req, context=self.ssl_context()) data = res.read().decode("utf-8") res.close() except urllib.error.HTTPError: From 431282b3da1c329782138a6a0f9afa4414e3dc4a Mon Sep 17 00:00:00 2001 From: Jeremy Phelps Date: Thu, 18 Jan 2018 17:27:57 -0800 Subject: [PATCH 2/3] Conditionally modify the context object instead of conditionally returning early. --- pydruid/client.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pydruid/client.py b/pydruid/client.py index 6d70094c..5fdf2413 100755 --- a/pydruid/client.py +++ b/pydruid/client.py @@ -484,10 +484,9 @@ def __init__(self, url, endpoint): def ssl_context(self): ctx = ssl.create_default_context() - if not self.ignore_certificate_errors: - return ctx - ctx.check_hostname = False - ctx.verify_mode = ssl.CERT_NONE + if self.ignore_certificate_errors: + ctx.check_hostname = False + ctx.verify_mode = ssl.CERT_NONE return ctx def _post(self, query): From 059b54cbef266865102e49429ec96682e6cd9980 Mon Sep 17 00:00:00 2001 From: Jeremy Phelps Date: Thu, 18 Jan 2018 17:33:04 -0800 Subject: [PATCH 3/3] Fix style error. --- pydruid/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydruid/client.py b/pydruid/client.py index 5fdf2413..704e7eac 100755 --- a/pydruid/client.py +++ b/pydruid/client.py @@ -53,7 +53,7 @@ def _prepare_url_headers_and_body(self, query): username_password = \ b64encode(bytes('{}:{}'.format(self.username, self.password))) headers['Authorization'] = 'Basic {}'.format(username_password) - + return headers, querystr, url def _post(self, query):