Skip to content

Commit a549393

Browse files
cathleenyIsaac
andcommitted
fix(kernel): validate revocation flags and gate them on kernel support
Require the _tls_check_certificate_revocation and _tls_accept_undetermined_revocation options to be real bools on both the Thrift/SEA and kernel paths, before any connection is made. A truthy non-bool such as "false" previously enabled accept-undetermined. When revocation is requested on the kernel path, check that the installed databricks_sql_kernel.Session declares the tls revocation kwargs and raise NotSupportedError with an upgrade hint otherwise. The default path is unchanged and does not run the check. Adds regression tests for the bool validation on both paths, the kernel-wheel capability error, and the connect() -> kernel ssl_options threading. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent 15275df commit a549393

4 files changed

Lines changed: 137 additions & 15 deletions

File tree

‎src/databricks/sql/backend/kernel/client.py‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,10 +1225,25 @@ def _kernel_tls_kwargs(ssl_options: Optional[SSLOptions]) -> Dict[str, Any]:
12251225

12261226
# Certificate revocation is opt-in. Emit only when enabled, so connections
12271227
# that don't ask for it keep working with kernel wheels that predate the setters.
1228-
if ssl_options.tls_check_certificate_revocation:
1229-
kwargs["tls_check_certificate_revocation"] = True
1230-
if ssl_options.tls_accept_undetermined_revocation:
1231-
kwargs["tls_accept_undetermined_revocation"] = True
1228+
for name, enabled in (
1229+
(
1230+
"tls_check_certificate_revocation",
1231+
ssl_options.tls_check_certificate_revocation,
1232+
),
1233+
(
1234+
"tls_accept_undetermined_revocation",
1235+
ssl_options.tls_accept_undetermined_revocation,
1236+
),
1237+
):
1238+
if enabled is not True:
1239+
continue
1240+
if not _kernel_session_accepts_kwarg(name):
1241+
raise NotSupportedError(
1242+
f"{name} requires a newer databricks-sql-kernel wheel: the installed "
1243+
"databricks-sql-kernel does not support certificate revocation. Upgrade "
1244+
"databricks-sql-kernel or remove the certificate revocation options."
1245+
)
1246+
kwargs[name] = True
12321247

12331248
# Inverted booleans. Emit only the insecure (skip) direction so the
12341249
# default secure path stays implicit. ``tls_verify=False`` disables

‎src/databricks/sql/session.py‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@ def __init__(
109109
if self._spog_headers:
110110
all_headers = all_headers + list(self._spog_headers.items())
111111

112+
# Revocation flags weaken enforcement when set, so they must be real
113+
# bools: a truthy string such as "false" would otherwise be accepted.
114+
check_revocation = kwargs.get("_tls_check_certificate_revocation", False)
115+
accept_undetermined_revocation = kwargs.get(
116+
"_tls_accept_undetermined_revocation", False
117+
)
118+
for option_name, option_value in (
119+
("_tls_check_certificate_revocation", check_revocation),
120+
("_tls_accept_undetermined_revocation", accept_undetermined_revocation),
121+
):
122+
if not isinstance(option_value, bool):
123+
raise ValueError(
124+
f"{option_name} must be a bool; got {type(option_value).__name__}"
125+
)
126+
112127
self.ssl_options = SSLOptions(
113128
# Double negation is generally a bad thing, but we have to keep backward compatibility
114129
tls_verify=not kwargs.get(
@@ -119,12 +134,8 @@ def __init__(
119134
tls_client_cert_file=kwargs.get("_tls_client_cert_file"),
120135
tls_client_cert_key_file=kwargs.get("_tls_client_cert_key_file"),
121136
tls_client_cert_key_password=kwargs.get("_tls_client_cert_key_password"),
122-
tls_check_certificate_revocation=kwargs.get(
123-
"_tls_check_certificate_revocation", False
124-
),
125-
tls_accept_undetermined_revocation=kwargs.get(
126-
"_tls_accept_undetermined_revocation", False
127-
),
137+
tls_check_certificate_revocation=check_revocation,
138+
tls_accept_undetermined_revocation=accept_undetermined_revocation,
128139
)
129140

130141
# Use the provided HTTP client (created in Connection)
@@ -146,10 +157,7 @@ def __init__(
146157
# otherwise (OAuth M2M/U2M resolve purely from the raw kwargs
147158
# the bridge reads). The Thrift / SEA backends are unchanged.
148159
self.use_kernel = kwargs.get("use_kernel", False)
149-
if not self.use_kernel and (
150-
kwargs.get("_tls_check_certificate_revocation")
151-
or kwargs.get("_tls_accept_undetermined_revocation")
152-
):
160+
if not self.use_kernel and (check_revocation or accept_undetermined_revocation):
153161
raise ValueError(
154162
"_tls_check_certificate_revocation and _tls_accept_undetermined_revocation "
155163
"require use_kernel=True; the Thrift and SEA backends do not check certificate revocation."

‎tests/unit/test_kernel_client.py‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2533,13 +2533,48 @@ def test_none_ssl_options_emit_no_tls_kwargs(self):
25332533
),
25342534
],
25352535
)
2536-
def test_revocation_forwarded_only_when_enabled(self, overrides, expected):
2536+
def test_revocation_forwarded_only_when_enabled(
2537+
self, monkeypatch, overrides, expected
2538+
):
25372539
# Opt-in: a default connection emits no revocation kwargs, so it still works
25382540
# with kernel wheels that predate the setters.
2541+
monkeypatch.setattr(
2542+
kernel_client, "_kernel_session_accepts_kwarg", lambda _: True
2543+
)
25392544
assert (
25402545
kernel_client._kernel_tls_kwargs(self._ssl_options(**overrides)) == expected
25412546
)
25422547

2548+
@pytest.mark.parametrize(
2549+
"overrides",
2550+
[
2551+
{"tls_check_certificate_revocation": True},
2552+
{"tls_accept_undetermined_revocation": True},
2553+
],
2554+
)
2555+
def test_revocation_on_kernel_without_setters_raises_clear_error(
2556+
self, monkeypatch, overrides
2557+
):
2558+
# An older kernel wheel would reject the kwarg with an opaque TypeError,
2559+
# so name the missing capability and the upgrade path instead.
2560+
monkeypatch.setattr(
2561+
kernel_client, "_kernel_session_accepts_kwarg", lambda _: False
2562+
)
2563+
with pytest.raises(
2564+
NotSupportedError, match="does not support certificate revocation"
2565+
):
2566+
kernel_client._kernel_tls_kwargs(self._ssl_options(**overrides))
2567+
2568+
def test_default_tls_on_kernel_without_setters_emits_no_revocation_kwargs(
2569+
self, monkeypatch
2570+
):
2571+
# The capability check runs only when revocation is requested, so the
2572+
# default path works unchanged on wheels without the setters.
2573+
monkeypatch.setattr(
2574+
kernel_client, "_kernel_session_accepts_kwarg", lambda _: False
2575+
)
2576+
assert kernel_client._kernel_tls_kwargs(self._ssl_options()) == {}
2577+
25432578
def test_verify_off_maps_to_skip_verify_and_hostname(self):
25442579
# tls_verify=False disables all chain validation, which subsumes
25452580
# hostname verification — so both skip flags are emitted, matching

‎tests/unit/test_session.py‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,46 @@ def test_retry_and_socket_timeout_threaded_into_kernel_client(self):
572572
finally:
573573
conn.close()
574574

575+
def test_revocation_options_threaded_into_kernel_ssl_options(self):
576+
import sys
577+
import types
578+
579+
pytest.importorskip(
580+
"pyarrow",
581+
reason="kernel client module imports pyarrow at load",
582+
)
583+
584+
fake = types.ModuleType("databricks_sql_kernel")
585+
fake.KernelError = type("KernelError", (Exception,), {})
586+
fake.Session = MagicMock()
587+
588+
with patch.dict(sys.modules, {"databricks_sql_kernel": fake}), patch(
589+
"databricks.sql.backend.kernel.client.KernelDatabricksClient"
590+
) as mock_kernel_client, patch(
591+
"%s.session.get_python_sql_connector_auth_provider" % self.PACKAGE
592+
):
593+
instance = mock_kernel_client.return_value
594+
instance.open_session.return_value = SessionId(
595+
BackendType.SEA, "sess-id", None
596+
)
597+
598+
conn = databricks.sql.connect(
599+
server_hostname="foo",
600+
http_path="/sql/1.0/warehouses/abc",
601+
use_kernel=True,
602+
access_token="dapi-xyz",
603+
enable_telemetry=False,
604+
_tls_check_certificate_revocation=True,
605+
_tls_accept_undetermined_revocation=True,
606+
)
607+
try:
608+
_, kwargs = mock_kernel_client.call_args
609+
ssl_options = kwargs["ssl_options"]
610+
assert ssl_options.tls_check_certificate_revocation is True
611+
assert ssl_options.tls_accept_undetermined_revocation is True
612+
finally:
613+
conn.close()
614+
575615
def test_azure_sp_m2m_kwargs_threaded_into_kernel_auth_options(self):
576616
# The Azure SP credentials a user passes to connect() must reach the
577617
# kernel auth bridge via auth_options; without this threading the
@@ -1227,3 +1267,27 @@ def test_disabled_on_thrift_is_not_rejected(self, mock_backend, _mock_auth):
12271267
_tls_accept_undetermined_revocation=False,
12281268
)
12291269
mock_backend.assert_called_once()
1270+
1271+
@pytest.mark.parametrize("extra", [{}, {"use_sea": True}])
1272+
@pytest.mark.parametrize(
1273+
"option",
1274+
["_tls_check_certificate_revocation", "_tls_accept_undetermined_revocation"],
1275+
)
1276+
@pytest.mark.parametrize("value", ["false", 1, None])
1277+
@patch("databricks.sql.session.Session._create_backend")
1278+
def test_non_bool_revocation_value_is_rejected_before_connecting(
1279+
self, mock_backend, value, option, extra
1280+
):
1281+
# Truthiness would let "false" enable accept-undetermined, so only real
1282+
# bools pass. The check runs before any backend is created, on every path.
1283+
from databricks.sql.session import Session
1284+
1285+
with pytest.raises(ValueError, match=f"{option} must be a bool"):
1286+
Session(
1287+
"h.example.com",
1288+
"/sql/1.0/warehouses/abc",
1289+
None,
1290+
**{option: value},
1291+
**extra,
1292+
)
1293+
mock_backend.assert_not_called()

0 commit comments

Comments
 (0)