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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion pydruid/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import absolute_import

import json
import ssl
import re

from six.moves import urllib
Expand All @@ -36,11 +37,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('/'):
Expand Down Expand Up @@ -481,11 +486,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 as e:
Expand Down