Skip to content
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

Make it possible to ignore SSL certificate errors. #115

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 3 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
19 changes: 16 additions & 3 deletions pydruid/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,30 @@

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
self.endpoint = 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('/'):
Expand All @@ -47,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):
Expand Down Expand Up @@ -476,11 +482,18 @@ class PyDruid(BaseDruidClient):
def __init__(self, url, endpoint):
super(PyDruid, self).__init__(url, endpoint)

def ssl_context(self):
ctx = ssl.create_default_context()
if self.ignore_certificate_errors:
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:
Expand Down