-
Notifications
You must be signed in to change notification settings - Fork 202
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. #114
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
97c03ab
Added methode to allow http basic auth with username and password to …
b9b33aa
added missing import
fcb2f4c
show full error message
de30421
Allow ignoring of SSL certificate errors.
0e37443
Fix style errors.
b153f78
Fix another style error
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,17 +18,29 @@ | |
|
||
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') | ||
|
@@ -37,6 +49,11 @@ def _prepare_url_headers_and_body(self, query): | |
else: | ||
url = self.url + '/' + self.endpoint | ||
headers = {'Content-Type': 'application/json'} | ||
if (self.username is not None) and (self.password is not None): | ||
username_password = \ | ||
b64encode(bytes('{}:{}'.format(self.username, self.password))) | ||
headers['Authorization'] = 'Basic {}'.format(username_password) | ||
|
||
return headers, querystr, url | ||
|
||
def _post(self, query): | ||
|
@@ -386,11 +403,20 @@ class PyDruid(BaseDruidClient): | |
def __init__(self, url, endpoint): | ||
super(PyDruid, self).__init__(url, endpoint) | ||
|
||
def ssl_context(self): | ||
ctx = ssl.create_default_context() | ||
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) | ||
if self.ignore_certificate_errors: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you do the suggestion above, this would just be |
||
res = urllib.request.urlopen(req, context=self.ssl_context()) | ||
else: | ||
res = urllib.request.urlopen(req) | ||
data = res.read().decode("utf-8") | ||
res.close() | ||
except urllib.error.HTTPError: | ||
|
@@ -402,8 +428,6 @@ def _post(self, query): | |
err = json.loads(e.read().decode("utf-8")) | ||
except (ValueError, AttributeError, KeyError): | ||
pass | ||
else: | ||
err = err.get('error', None) | ||
|
||
raise IOError('{0} \n Druid Error: {1} \n Query is: {2}'.format( | ||
e, err, json.dumps(query.query_dict, indent=4))) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should at least be called something like
insecure_ssl_context()
. The current name is misleading, possibly dangerously so since a caller might not realize the context is insecure.I think, though, that it'd be even better to have
ssl_context()
always get called by_post
and for it to do something different based on the configuration. Then it would make sense to keep the current name.