Skip to content

Commit b55c7b8

Browse files
author
Jesse
authored
[PECO-244] Make http proxies work (#81)
Override thrift's proxy header encoding function. Uses the fix identified in apache/thrift#2565 H/T @pspeter Signed-off-by: Jesse Whitehouse <[email protected]>
1 parent 341ff68 commit b55c7b8

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/databricks/sql/auth/thrift_http_client.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import logging
22
from typing import Dict
33

4+
45
import thrift
56

7+
import urllib.parse, six, base64
8+
69
logger = logging.getLogger(__name__)
710

811

@@ -33,3 +36,14 @@ def flush(self):
3336
self._headers = headers
3437
self.setCustomHeaders(self._headers)
3538
super().flush()
39+
40+
@staticmethod
41+
def basic_proxy_auth_header(proxy):
42+
if proxy is None or not proxy.username:
43+
return None
44+
ap = "%s:%s" % (
45+
urllib.parse.unquote(proxy.username),
46+
urllib.parse.unquote(proxy.password),
47+
)
48+
cr = base64.b64encode(ap.encode()).strip()
49+
return "Basic " + six.ensure_str(cr)

tests/unit/test_thrift_backend.py

+15
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,21 @@ def test_headers_are_set(self, t_http_client_class):
143143
ThriftBackend("foo", 123, "bar", [("header", "value")], auth_provider=AuthProvider())
144144
t_http_client_class.return_value.setCustomHeaders.assert_called_with({"header": "value"})
145145

146+
def test_proxy_headers_are_set(self):
147+
148+
from databricks.sql.auth.thrift_http_client import THttpClient
149+
from urllib.parse import urlparse
150+
151+
fake_proxy_spec = "https://someuser:[email protected]:12340"
152+
parsed_proxy = urlparse(fake_proxy_spec)
153+
154+
try:
155+
result = THttpClient.basic_proxy_auth_header(parsed_proxy)
156+
except TypeError as e:
157+
assert False
158+
159+
assert isinstance(result, type(str()))
160+
146161
@patch("databricks.sql.auth.thrift_http_client.THttpClient")
147162
@patch("databricks.sql.thrift_backend.create_default_context")
148163
def test_tls_cert_args_are_propagated(self, mock_create_default_context, t_http_client_class):

0 commit comments

Comments
 (0)