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

Add support for legacy auth #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 21 additions & 3 deletions sqlalchemy_dremio/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ def d(self, *args, **kwargs):
return d


class LegacyAuthHandler(flight.ClientAuthHandler):
def __init__(self, username: str, password: str):
super().__init__()
self.basic_auth = flight.BasicAuth(username, password)
self.token = None

def authenticate(self, outgoing, incoming):
auth = self.basic_auth.serialize()
outgoing.write(auth)
self.token = incoming.read()

def get_token(self):
return self.token


class Connection(object):

def __init__(self, connection_string):
Expand Down Expand Up @@ -79,12 +94,15 @@ def __init__(self, connection_string):

client = flight.FlightClient('grpc+{0}://{1}:{2}'.format(protocol, properties['HOST'], properties['PORT']),
middleware=[client_cookie_middleware], **connection_args)

# Authenticate either using basic username/password or using the Token parameter.
headers = []
if 'UID' in properties:
bearer_token = client.authenticate_basic_token(properties['UID'], properties['PWD'])
headers.append(bearer_token)
if 'UseLegacyAuth' in properties and properties['UseLegacyAuth'].lower() == 'true':
client.authenticate(LegacyAuthHandler(properties['UID'], properties['PWD']))
else:
bearer_token = client.authenticate_basic_token(properties['UID'], properties['PWD'])
headers.append(bearer_token)
else:
headers.append((b'authorization', "Bearer {}".format(properties['Token']).encode('utf-8')))

Expand Down
4 changes: 2 additions & 2 deletions sqlalchemy_dremio/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def create_connect_args(self, url):
def add_property(lc_query_dict, property_name, connectors):
if property_name.lower() in lc_query_dict:
connectors.append('{0}={1}'.format(property_name, lc_query_dict[property_name.lower()]))

add_property(lc_query_dict, 'UseEncryption', connectors)
add_property(lc_query_dict, 'DisableCertificateVerification', connectors)
add_property(lc_query_dict, 'TrustedCerts', connectors)
Expand All @@ -187,6 +187,7 @@ def add_property(lc_query_dict, property_name, connectors):
add_property(lc_query_dict, 'quoting', connectors)
add_property(lc_query_dict, 'routing_engine', connectors)
add_property(lc_query_dict, 'Token', connectors)
add_property(lc_query_dict, 'UseLegacyAuth', connectors)

return [[";".join(connectors)], connect_args]

Expand Down Expand Up @@ -257,4 +258,3 @@ def has_table(self, connection, table_name, schema=None, **kw):

def get_view_names(self, connection, schema=None, **kwargs):
return []