|
18 | 18 |
|
19 | 19 | import json |
20 | 20 | import sys |
| 21 | +import ssl |
21 | 22 |
|
22 | 23 | from six.moves import urllib |
23 | 24 |
|
24 | 25 | from pydruid.query import QueryBuilder |
25 | 26 | from base64 import b64encode |
26 | 27 |
|
| 28 | + |
27 | 29 | class BaseDruidClient(object): |
28 | 30 | def __init__(self, url, endpoint): |
29 | 31 | self.url = url |
30 | 32 | self.endpoint = endpoint |
31 | 33 | self.query_builder = QueryBuilder() |
32 | 34 | self.username = None |
33 | 35 | self.password = None |
34 | | - |
| 36 | + self.ignore_certificate_errors = False |
| 37 | + |
35 | 38 | def set_basic_auth_credentials(self, username, password): |
36 | 39 | self.username = username |
37 | 40 | self.password = password |
38 | 41 |
|
| 42 | + def set_ignore_certificate_errors(self, value=True): |
| 43 | + self.ignore_certificate_errors = value |
| 44 | + |
39 | 45 | def _prepare_url_headers_and_body(self, query): |
40 | 46 | querystr = json.dumps(query.query_dict).encode('utf-8') |
41 | 47 | if self.url.endswith('/'): |
@@ -476,11 +482,19 @@ class PyDruid(BaseDruidClient): |
476 | 482 | def __init__(self, url, endpoint): |
477 | 483 | super(PyDruid, self).__init__(url, endpoint) |
478 | 484 |
|
| 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 | + |
479 | 493 | def _post(self, query): |
480 | 494 | try: |
481 | 495 | headers, querystr, url = self._prepare_url_headers_and_body(query) |
482 | 496 | req = urllib.request.Request(url, querystr, headers) |
483 | | - res = urllib.request.urlopen(req) |
| 497 | + res = urllib.request.urlopen(req, context=self.ssl_context()) |
484 | 498 | data = res.read().decode("utf-8") |
485 | 499 | res.close() |
486 | 500 | except urllib.error.HTTPError: |
|
0 commit comments