Skip to content

Commit c90459f

Browse files
cathleenyIsaac
andcommitted
Embedded host port wins over a differing _port on the kernel path
A port embedded in server_hostname now wins over a differing _port, with a warning, as on main, where the kernel connected to the embedded port. A falsy _port (None, 0, '', False) means the default, as on Thrift (port or 443). A trailing '/' is trimmed before port handling. A truthy invalid _port still raises, as Thrift fails on it (LocationParseError). Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent f27c165 commit c90459f

4 files changed

Lines changed: 124 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Release History
22

33
# Unreleased
4-
- Honor `_port` and `_connection_uri` on kernel connections (`use_kernel=True`), as Thrift does. `_port` sends the connection to that port (default 443 adds nothing); `_connection_uri` (`http(s)://host[:port]/path?query`) sets host, port, path and query and wins over `_port`; URI-only connections work. Invalid ports, conflicting ports, an empty `server_hostname` with `_port`, and URIs without a scheme or path raise `ValueError`. With OAuth, a `_connection_uri` origin that differs from `server_hostname` raises. With OAuth and a non-default `_port`, the token exchange goes to the same host:port, unlike Thrift (documented, not guarded).
4+
- Honor `_port` and `_connection_uri` on kernel connections (`use_kernel=True`), as Thrift does. `_port` sends the connection to that port (default 443 adds nothing); `_connection_uri` (`http(s)://host[:port]/path?query`) sets host, port, path and query and wins over `_port`; URI-only connections work. A port embedded in `server_hostname` wins over a differing `_port`, with a warning, as on main. Invalid ports, an empty `server_hostname` with `_port`, and URIs without a scheme or path raise `ValueError`. With OAuth, a `_connection_uri` origin that differs from `server_hostname` raises. With OAuth and a non-default `_port`, the token exchange goes to the same host:port, unlike Thrift (documented, not guarded).
55
- Honor `max_download_threads` on kernel connections, with a default download window of 16 and explicit chunk-setting overrides. Invalid values raise `ValueError` when connecting with `use_kernel=True`.
66
- Fix the kernel backend ignoring `use_cloud_fetch=False`, so complete results can be fetched without cloud-storage downloads.
77
- Fix inline parameters (`use_inline_params=True`) so a sequence directly after `IN` renders as a list again: `x IN %(ids)s` renders as `x IN (1,2,3)` instead of `x IN ARRAY(1,2,3)`, which Spark rejects. Inside the list, tuples render as rows for a multi-column `IN`, and a `set` or `frozenset` is accepted again. Sequences anywhere else still render as `ARRAY(...)` ([#629](https://github.com/databricks/databricks-sql-python/issues/629))

‎src/databricks/sql/client.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ def __init__(
195195
be suppressed by the feature flag.
196196
197197
Kernel overrides: _port sends the connection to that port
198-
(the default 443 adds nothing). _connection_uri (https://host[:port]/path?query)
198+
(the default 443 adds nothing; a falsy _port means the default). A port
199+
embedded in server_hostname wins over a differing _port, with a warning.
200+
_connection_uri (https://host[:port]/path?query)
199201
sets host, port, path and query, as on Thrift, and wins over _port.
200202
With OAuth, _connection_uri must have the same origin as
201203
server_hostname. With OAuth and a non-default _port, the token

‎src/databricks/sql/session.py‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ def __getattr__(name: str) -> Any:
5353

5454

5555
def _port_or_none(value: Any) -> Optional[int]:
56-
"""Validate ``_port``. ``None`` and ``0`` mean the default (Thrift: ``port or 443``)."""
57-
if value is None:
56+
"""Validate ``_port``. A falsy value (None, 0, '', False) means the default, as on
57+
Thrift (``port or 443``); a truthy value must be an integer between 1 and 65535.
58+
"""
59+
if not value or (isinstance(value, str) and not value.strip()):
5860
return None
5961
text = (
6062
str(value).strip()
@@ -148,21 +150,34 @@ def _kernel_host_and_path(
148150
port = _port_or_none(kwargs.get("_port"))
149151
if port is None:
150152
return server_hostname, http_path
151-
prefix, name, embedded = _split_host(server_hostname)
152153
if not server_hostname:
153154
raise ValueError(
154155
"server_hostname is required on use_kernel=True when _port is set "
155156
"(or pass _connection_uri, which carries its own host)"
156157
)
158+
prefix, name, embedded = _split_host(server_hostname)
157159
if embedded is not None:
160+
# A port embedded in server_hostname always wins, as on main (the kernel
161+
# connected to it); a differing _port is ignored with a warning, not an error.
158162
if embedded != port:
159-
raise ValueError(
160-
f"Conflicting ports: server_hostname {_redact(server_hostname)!r} "
161-
f"carries {embedded} but _port={port}"
163+
logger.warning(
164+
"server_hostname %s carries port %d, which differs from _port=%d; "
165+
"connecting to port %d. Set the port in one place only.",
166+
_redact(server_hostname),
167+
embedded,
168+
port,
169+
embedded,
162170
)
163171
return server_hostname, http_path
164172
if port == 443:
165173
return server_hostname, http_path
174+
if any(c in name for c in "/?#"):
175+
# Folding _port in would put it after the path, query or fragment, so the
176+
# port could not be applied; reject rather than silently drop it.
177+
raise ValueError(
178+
f"Invalid server_hostname {_redact(server_hostname)!r}: a path, query or "
179+
"fragment cannot be combined with _port; put the port in server_hostname"
180+
)
166181
if ":" in name and not name.startswith("["):
167182
name = f"[{name}]"
168183
return f"{prefix}{name}:{port}", http_path

‎tests/unit/test_session.py‎

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from contextlib import contextmanager
44
import gc
55
import importlib.metadata as ilm
6+
import logging
67
import socket
78
import sys
89
import threading
@@ -1208,6 +1209,16 @@ def test_pat_recovery_does_not_inject_auth_type(self, mock_thrift):
12081209
("h.io", _P, {"_port": 65535}, ("h.io:65535", _P)),
12091210
("h.io", _P, {"_port": 443}, ("h.io", _P)),
12101211
("h.io", _P, {"_port": 0}, ("h.io", _P)),
1212+
# Falsy _port means the default, as on Thrift (port or 443); no :443 is added.
1213+
("h.io", _P, {"_port": None}, ("h.io", _P)),
1214+
("h.io", _P, {"_port": ""}, ("h.io", _P)),
1215+
("h.io", _P, {"_port": " "}, ("h.io", _P)),
1216+
("h.io", _P, {"_port": False}, ("h.io", _P)),
1217+
("h.io", _P, {"_port": "0"}, ("h.io", _P)),
1218+
# Trailing "/" is trimmed before the port is folded in.
1219+
("h.io/", _P, {"_port": 8443}, ("h.io:8443", _P)),
1220+
("h.io:8443/", _P, {"_port": 8443}, ("h.io:8443/", _P)),
1221+
("h.io/", _P, {}, ("h.io/", _P)),
12111222
("::1", _P, {}, ("::1", _P)),
12121223
("::1", _P, {"_port": 8443}, ("[::1]:8443", _P)),
12131224
("[::1]", _P, {"_port": 8443}, ("[::1]:8443", _P)),
@@ -1266,10 +1277,10 @@ def test_kernel_host_and_path_accepts(server, hpath, kwargs, expected):
12661277
("h.io", {"_port": 8443.5}, "Invalid _port"),
12671278
("h.io:notaport", {"_port": 8443}, "malformed"),
12681279
("h.io:0", {"_port": 8443}, "out of range"),
1269-
# AC-P3: a supplied port must agree with a port embedded in server_hostname.
1270-
("h.io:7000", {"_port": 8443}, "Conflicting ports"),
1271-
("h.io:8443", {"_port": 443}, "Conflicting ports"),
1272-
("[::1]:7000", {"_port": 8443}, "Conflicting ports"),
1280+
# An explicit _port cannot be folded in after a path, query or fragment.
1281+
("h.io/x", {"_port": 8443}, "path, query or fragment"),
1282+
("h.io?x=1", {"_port": 8443}, "path, query or fragment"),
1283+
("h.io#x", {"_port": 8443}, "path, query or fragment"),
12731284
# R11: _port needs a server_hostname unless _connection_uri carries the host.
12741285
("", {"_port": 8443}, "server_hostname is required"),
12751286
("", {"_port": 443}, "server_hostname is required"),
@@ -1293,6 +1304,33 @@ def test_kernel_host_and_path_rejects(server, kwargs, message):
12931304
_kernel_host_and_path(server, _P, kwargs)
12941305

12951306

1307+
@pytest.mark.parametrize(
1308+
"server, port, embedded",
1309+
[
1310+
("h.io:7000", 8443, 7000),
1311+
("h.io:8443", 443, 8443),
1312+
("[::1]:7000", 8443, 7000),
1313+
("h.io:443", 8443, 443),
1314+
],
1315+
)
1316+
def test_embedded_port_wins_over_differing_explicit_port(
1317+
server, port, embedded, caplog
1318+
):
1319+
# A port embedded in server_hostname always wins (as on main); a differing _port
1320+
# is not an error. The warning names both ports, and the host is passed unchanged.
1321+
with caplog.at_level(logging.WARNING, logger="databricks.sql.session"):
1322+
host, path = _kernel_host_and_path(server, _P, {"_port": port})
1323+
assert (host, path) == (server, _P)
1324+
assert f"carries port {embedded}" in caplog.text
1325+
assert f"_port={port}" in caplog.text
1326+
1327+
1328+
def test_embedded_port_equal_to_explicit_port_does_not_warn(caplog):
1329+
with caplog.at_level(logging.WARNING, logger="databricks.sql.session"):
1330+
_kernel_host_and_path("h.io:8443", _P, {"_port": 8443})
1331+
assert "differs from _port" not in caplog.text
1332+
1333+
12961334
@pytest.mark.parametrize(
12971335
"server, kwargs, uri, should_raise",
12981336
[
@@ -1568,3 +1606,60 @@ def accept_one():
15681606
thread.join(timeout=70)
15691607
assert "error" not in received, received
15701608
assert received.get("first_byte") == b"\x16", received
1609+
1610+
1611+
@pytest.mark.realkernel
1612+
def test_kernel_embedded_port_wins_over_explicit_port():
1613+
"""Loopback on the real kernel wheel: server_hostname 'h:P1' with an explicit _port P2
1614+
dials P1 (TLS first byte 0x16) and never P2."""
1615+
try:
1616+
ilm.version("databricks-sql-kernel")
1617+
except ilm.PackageNotFoundError:
1618+
pytest.skip("databricks-sql-kernel wheel not installed")
1619+
1620+
def listen():
1621+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1622+
sock.bind(("127.0.0.1", 0))
1623+
sock.listen(1)
1624+
sock.settimeout(15)
1625+
return sock
1626+
1627+
embedded, explicit = listen(), listen()
1628+
embedded_port = embedded.getsockname()[1]
1629+
explicit_port = explicit.getsockname()[1]
1630+
received = {}
1631+
1632+
def accept_one(name, sock):
1633+
try:
1634+
conn, _ = sock.accept()
1635+
conn.settimeout(10)
1636+
received[name] = conn.recv(1)
1637+
conn.close()
1638+
except OSError as exc:
1639+
received[name + "_error"] = repr(exc)
1640+
finally:
1641+
sock.close()
1642+
1643+
threads = [
1644+
threading.Thread(target=accept_one, args=("embedded", embedded), daemon=True),
1645+
threading.Thread(target=accept_one, args=("explicit", explicit), daemon=True),
1646+
]
1647+
for thread in threads:
1648+
thread.start()
1649+
try:
1650+
databricks.sql.connect(
1651+
server_hostname=f"127.0.0.1:{embedded_port}",
1652+
http_path=_P,
1653+
use_kernel=True,
1654+
access_token="dapi-xyz",
1655+
enable_telemetry=False,
1656+
_retry_stop_after_attempts_count=1,
1657+
_port=explicit_port,
1658+
).close()
1659+
except Exception:
1660+
pass # expected: nothing behind the listener completes TLS
1661+
for thread in threads:
1662+
thread.join(timeout=70)
1663+
assert received.get("embedded") == b"\x16", received
1664+
assert "explicit" not in received, received
1665+
assert "explicit_error" in received, received

0 commit comments

Comments
 (0)