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

[dbapi] Added support for providing ssl certificate for SQL query #180

Merged
merged 1 commit into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 38 additions & 4 deletions pydruid/db/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def connect(
password=None,
context=None,
header=False,
ssl_verify_cert=True,
): # noqa: E125
"""
Constructor for creating a connection to the database.
Expand All @@ -38,7 +39,17 @@ def connect(

"""
context = context or {}
return Connection(host, port, path, scheme, user, password, context, header)
return Connection(
host,
port,
path,
scheme,
user,
password,
context,
header,
ssl_verify_cert,
)


def check_closed(f):
Expand Down Expand Up @@ -118,6 +129,7 @@ def __init__(
password=None,
context=None,
header=False,
ssl_verify_cert=True,
):
netloc = "{host}:{port}".format(host=host, port=port)
self.url = parse.urlunparse((scheme, netloc, path, None, None, None))
Expand All @@ -127,6 +139,7 @@ def __init__(
self.header = header
self.user = user
self.password = password
self.ssl_verify_cert = ssl_verify_cert

@check_closed
def close(self):
Expand All @@ -150,7 +163,14 @@ def commit(self):
@check_closed
def cursor(self):
"""Return a new Cursor Object using the connection."""
cursor = Cursor(self.url, self.user, self.password, self.context, self.header)
cursor = Cursor(
self.url,
self.user,
self.password,
self.context,
self.header,
self.ssl_verify_cert,
)
self.cursors.append(cursor)

return cursor
Expand All @@ -171,12 +191,21 @@ class Cursor(object):

"""Connection cursor."""

def __init__(self, url, user=None, password=None, context=None, header=False):
def __init__(
self,
url,
user=None,
password=None,
context=None,
header=False,
ssl_verify_cert=True,
):
self.url = url
self.context = context or {}
self.header = header
self.user = user
self.password = password
self.ssl_verify_cert = ssl_verify_cert

# This read/write attribute specifies the number of rows to fetch at a
# time with .fetchmany(). It defaults to 1 meaning to fetch a single
Expand Down Expand Up @@ -300,7 +329,12 @@ def _stream_query(self, query):
requests.auth.HTTPBasicAuth(self.user, self.password) if self.user else None
)
r = requests.post(
self.url, stream=True, headers=headers, json=payload, auth=auth
self.url,
stream=True,
headers=headers,
json=payload,
auth=auth,
verify=self.ssl_verify_cert,
)
if r.encoding is None:
r.encoding = "utf-8"
Expand Down
1 change: 1 addition & 0 deletions tests/db/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def test_context(self, requests_post_mock):
stream=True,
headers={'Content-Type': 'application/json'},
json={'query': query, 'context': context, 'header': False},
verify=True
)

@patch('requests.post')
Expand Down