Skip to content

Commit 236f88c

Browse files
Niall Egansusodapop
authored andcommitted
Fix _use_cert_as_auth_flag
The `authorization_header` wasn't set Author: Niall Egan <niall.egan@databricks.com>
1 parent b0f5ca6 commit 236f88c

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

cmdexec/clients/python/src/databricks/sql/client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,14 @@ def __init__(self,
9595
self.host = server_hostname
9696
self.port = kwargs.get("_port", 443)
9797

98+
authorization_header = []
9899
if kwargs.get("_username") and kwargs.get("_password"):
99100
auth_credentials = "{username}:{password}".format(
100101
username=kwargs.get("_username"), password=kwargs.get("_password")).encode("UTF-8")
101102
auth_credentials_base64 = base64.standard_b64encode(auth_credentials).decode("UTF-8")
102-
authorization_header = "Basic {}".format(auth_credentials_base64)
103+
authorization_header = [("Authorization", "Basic {}".format(auth_credentials_base64))]
103104
elif access_token:
104-
authorization_header = "Bearer {}".format(access_token)
105+
authorization_header = [("Authorization", "Bearer {}".format(access_token))]
105106
elif not (kwargs.get("_use_cert_as_auth") and kwargs.get("_tls_client_cert_file")):
106107
raise ValueError("No valid authentication settings. Please provide an access token.")
107108

@@ -111,9 +112,8 @@ def __init__(self,
111112
useragent_header = "{}/{} ({})".format(USER_AGENT_NAME, __version__,
112113
kwargs.get("_user_agent_entry"))
113114

114-
base_headers = [("Authorization", authorization_header),
115-
("X-Databricks-Sqlgateway-CommandService-Mode", "grpc-thrift"),
116-
("User-Agent", useragent_header)]
115+
base_headers = [("X-Databricks-Sqlgateway-CommandService-Mode", "grpc-thrift"),
116+
("User-Agent", useragent_header)] + authorization_header
117117

118118
if not kwargs.get("_skip_routing_headers"):
119119
base_headers.append(self._http_path_to_routing_header(http_path))

cmdexec/clients/python/tests/tests.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,46 @@ def test_close_uses_the_correct_session_id(self, mock_client_class):
3939
_, close_session_request = instance.make_request.call_args[0]
4040
self.assertEqual(close_session_request.session_id, mock_response.session_id)
4141

42+
@patch("%s.client.CmdExecBaseHttpClient" % PACKAGE_NAME)
43+
def test_auth_args(self, mock_client_class):
44+
instance = mock_client_class.return_value
45+
mock_response = MagicMock()
46+
mock_response.session_id = b'\x22'
47+
instance.make_request.return_value = mock_response
48+
49+
# Test that the following auth args work:
50+
# token = foo,
51+
# token = None, _username = foo, _password = bar
52+
# token = None, _tls_client_cert_file = something, _use_cert_as_auth = True
53+
connection_args = [
54+
{
55+
"server_hostname": "foo",
56+
"http_path": None,
57+
"access_token": "tok",
58+
"_skip_routing_headers": True,
59+
},
60+
{
61+
"server_hostname": "foo",
62+
"http_path": None,
63+
"_username": "foo",
64+
"_password": "bar",
65+
"access_token": None,
66+
"_skip_routing_headers": True,
67+
},
68+
{
69+
"server_hostname": "foo",
70+
"http_path": None,
71+
"_tls_client_cert_file": "something",
72+
"_use_cert_as_auth": True,
73+
"access_token": None,
74+
"_skip_routing_headers": True,
75+
},
76+
]
77+
78+
for args in connection_args:
79+
connection = databricks.sql.connect(**args)
80+
connection.close()
81+
4282
@patch("%s.client.CmdExecBaseHttpClient" % PACKAGE_NAME)
4383
@patch("%s.client.ResultSet" % PACKAGE_NAME)
4484
def test_closing_connection_closes_commands(self, mock_result_set_class, mock_client_class):

0 commit comments

Comments
 (0)