Skip to content

Commit 15275df

Browse files
cathleenyIsaac
andcommitted
feat(kernel): add opt-in certificate revocation checks for kernel connections
Expose the kernel's TLS revocation policy to Python connections on the kernel backend (use_kernel=True): - _tls_check_certificate_revocation (default False) checks the server certificate chain against its CRLs. - _tls_accept_undetermined_revocation (default False) accepts a chain whose revocation status cannot be determined. The two flags are carried on SSLOptions. _kernel_tls_kwargs forwards each one to the kernel Session as tls_check_certificate_revocation / tls_accept_undetermined_revocation only when it is True, so default connections still work with kernel wheels that predate the setters. Enabling either flag on the Thrift or SEA backend raises ValueError before any connection is made. Revocation is a kernel-only feature. Tests cover the forwarding table in _kernel_tls_kwargs and the rejection on the non-kernel backends. The existing realkernel routing tests still pass against databricks-sql-kernel 1.4.0. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent 087b2bf commit 15275df

7 files changed

Lines changed: 106 additions & 0 deletions

File tree

‎CHANGELOG.md‎

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

33
# Unreleased
4+
- Add `_tls_check_certificate_revocation` and `_tls_accept_undetermined_revocation` (default `False`) for kernel connections (`use_kernel=True`); they raise `ValueError` on other backends.
45
- 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`.
56
- Fix the kernel backend ignoring `use_cloud_fetch=False`, so complete results can be fetched without cloud-storage downloads.
67
- 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/backend/kernel/client.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,13 @@ def _kernel_tls_kwargs(ssl_options: Optional[SSLOptions]) -> Dict[str, Any]:
12231223

12241224
kwargs: Dict[str, Any] = {}
12251225

1226+
# Certificate revocation is opt-in. Emit only when enabled, so connections
1227+
# 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
1232+
12261233
# Inverted booleans. Emit only the insecure (skip) direction so the
12271234
# default secure path stays implicit. ``tls_verify=False`` disables
12281235
# all chain validation, which subsumes hostname verification — so we

‎src/databricks/sql/client.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ def __init__(
193193
decision. This is an intentional divergence from the
194194
Thrift/SEA paths, where an explicit ``True`` can still
195195
be suppressed by the feature flag.
196+
197+
Certificate revocation (kernel only): ``_tls_check_certificate_revocation=True``
198+
checks the server chain against its CRLs. ``_tls_accept_undetermined_revocation=True``
199+
accepts a chain whose status cannot be determined. Both default to ``False`` and
200+
raise ``ValueError`` without ``use_kernel=True``.
196201
:param enable_geospatial_support: `bool`, optional (default is False)
197202
Kernel backend only. Controls the public representation of
198203
``GEOMETRY`` and ``GEOGRAPHY`` result values. ``True`` returns

‎src/databricks/sql/session.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ def __init__(
119119
tls_client_cert_file=kwargs.get("_tls_client_cert_file"),
120120
tls_client_cert_key_file=kwargs.get("_tls_client_cert_key_file"),
121121
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+
),
122128
)
123129

124130
# Use the provided HTTP client (created in Connection)
@@ -140,6 +146,14 @@ def __init__(
140146
# otherwise (OAuth M2M/U2M resolve purely from the raw kwargs
141147
# the bridge reads). The Thrift / SEA backends are unchanged.
142148
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+
):
153+
raise ValueError(
154+
"_tls_check_certificate_revocation and _tls_accept_undetermined_revocation "
155+
"require use_kernel=True; the Thrift and SEA backends do not check certificate revocation."
156+
)
143157
if self.use_kernel:
144158
access_token = kwargs.get("access_token")
145159
self.auth_provider = (

‎src/databricks/sql/types.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ def __init__(
4040
tls_client_cert_file: Optional[str] = None,
4141
tls_client_cert_key_file: Optional[str] = None,
4242
tls_client_cert_key_password: Optional[str] = None,
43+
tls_check_certificate_revocation: bool = False,
44+
tls_accept_undetermined_revocation: bool = False,
4345
):
4446
self.tls_verify = tls_verify
4547
self.tls_verify_hostname = tls_verify_hostname
4648
self.tls_trusted_ca_file = tls_trusted_ca_file
4749
self.tls_client_cert_file = tls_client_cert_file
4850
self.tls_client_cert_key_file = tls_client_cert_key_file
4951
self.tls_client_cert_key_password = tls_client_cert_key_password
52+
self.tls_check_certificate_revocation = tls_check_certificate_revocation
53+
self.tls_accept_undetermined_revocation = tls_accept_undetermined_revocation
5054

5155
def validate_client_identity(self) -> None:
5256
"""Validate the shape of the mutual-TLS client identity.

‎tests/unit/test_kernel_client.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,6 +2508,38 @@ def test_default_ssl_options_emit_no_tls_kwargs(self):
25082508
def test_none_ssl_options_emit_no_tls_kwargs(self):
25092509
assert kernel_client._kernel_tls_kwargs(None) == {}
25102510

2511+
@pytest.mark.parametrize(
2512+
"overrides, expected",
2513+
[
2514+
({}, {}),
2515+
({"tls_check_certificate_revocation": False}, {}),
2516+
(
2517+
{"tls_check_certificate_revocation": True},
2518+
{"tls_check_certificate_revocation": True},
2519+
),
2520+
(
2521+
{"tls_accept_undetermined_revocation": True},
2522+
{"tls_accept_undetermined_revocation": True},
2523+
),
2524+
(
2525+
{
2526+
"tls_check_certificate_revocation": True,
2527+
"tls_accept_undetermined_revocation": True,
2528+
},
2529+
{
2530+
"tls_check_certificate_revocation": True,
2531+
"tls_accept_undetermined_revocation": True,
2532+
},
2533+
),
2534+
],
2535+
)
2536+
def test_revocation_forwarded_only_when_enabled(self, overrides, expected):
2537+
# Opt-in: a default connection emits no revocation kwargs, so it still works
2538+
# with kernel wheels that predate the setters.
2539+
assert (
2540+
kernel_client._kernel_tls_kwargs(self._ssl_options(**overrides)) == expected
2541+
)
2542+
25112543
def test_verify_off_maps_to_skip_verify_and_hostname(self):
25122544
# tls_verify=False disables all chain validation, which subsumes
25132545
# hostname verification — so both skip flags are emitted, matching

‎tests/unit/test_session.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,3 +1184,46 @@ def test_pat_recovery_does_not_inject_auth_type(self, mock_thrift):
11841184
assert kwargs["auth_options"].get("auth_type") is None
11851185
finally:
11861186
conn.close()
1187+
1188+
1189+
class TestCertificateRevocationRequiresKernel:
1190+
"""AC-R8: revocation kwargs are kernel-only. Enabling either on the Thrift or SEA
1191+
backend raises ValueError before any connection is made."""
1192+
1193+
@pytest.mark.parametrize(
1194+
"option, extra",
1195+
[
1196+
("_tls_check_certificate_revocation", {}),
1197+
("_tls_accept_undetermined_revocation", {}),
1198+
("_tls_check_certificate_revocation", {"use_sea": True}),
1199+
("_tls_accept_undetermined_revocation", {"use_sea": True}),
1200+
],
1201+
)
1202+
def test_enabled_on_non_kernel_backend_raises(self, option, extra):
1203+
from databricks.sql.session import Session
1204+
1205+
with pytest.raises(ValueError, match="require use_kernel=True"):
1206+
Session(
1207+
"h.example.com",
1208+
"/sql/1.0/warehouses/abc",
1209+
None,
1210+
**{option: True},
1211+
**extra,
1212+
)
1213+
1214+
@patch("databricks.sql.session.get_python_sql_connector_auth_provider")
1215+
@patch("databricks.sql.session.Session._create_backend")
1216+
def test_disabled_on_thrift_is_not_rejected(self, mock_backend, _mock_auth):
1217+
# False is the default and must not trip the kernel-only check.
1218+
from databricks.sql.session import Session
1219+
1220+
Session(
1221+
server_hostname="h.example.com",
1222+
http_path="/sql/1.0/warehouses/abc",
1223+
http_client=MagicMock(),
1224+
http_headers=[],
1225+
enable_telemetry=False,
1226+
_tls_check_certificate_revocation=False,
1227+
_tls_accept_undetermined_revocation=False,
1228+
)
1229+
mock_backend.assert_called_once()

0 commit comments

Comments
 (0)