Skip to content

Commit 13012b9

Browse files
author
Jeremy Phelps
committed
Make it possible to ignore certificate errors.
1 parent 30105f0 commit 13012b9

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

pydruid/client.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,30 @@
1818

1919
import json
2020
import sys
21+
import ssl
2122

2223
from six.moves import urllib
2324

2425
from pydruid.query import QueryBuilder
2526
from base64 import b64encode
2627

28+
2729
class BaseDruidClient(object):
2830
def __init__(self, url, endpoint):
2931
self.url = url
3032
self.endpoint = endpoint
3133
self.query_builder = QueryBuilder()
3234
self.username = None
3335
self.password = None
34-
36+
self.ignore_certificate_errors = False
37+
3538
def set_basic_auth_credentials(self, username, password):
3639
self.username = username
3740
self.password = password
3841

42+
def set_ignore_certificate_errors(self, value=True):
43+
self.ignore_certificate_errors = value
44+
3945
def _prepare_url_headers_and_body(self, query):
4046
querystr = json.dumps(query.query_dict).encode('utf-8')
4147
if self.url.endswith('/'):
@@ -476,11 +482,19 @@ class PyDruid(BaseDruidClient):
476482
def __init__(self, url, endpoint):
477483
super(PyDruid, self).__init__(url, endpoint)
478484

485+
def ssl_context(self):
486+
ctx = ssl.create_default_context()
487+
if not self.ignore_certificate_errors:
488+
return ctx
489+
ctx.check_hostname = False
490+
ctx.verify_mode = ssl.CERT_NONE
491+
return ctx
492+
479493
def _post(self, query):
480494
try:
481495
headers, querystr, url = self._prepare_url_headers_and_body(query)
482496
req = urllib.request.Request(url, querystr, headers)
483-
res = urllib.request.urlopen(req)
497+
res = urllib.request.urlopen(req, context=self.ssl_context())
484498
data = res.read().decode("utf-8")
485499
res.close()
486500
except urllib.error.HTTPError:

0 commit comments

Comments
 (0)