connection: fix ssl_options TLS handling - #938
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThe change distinguishes omitted SSL options from explicitly supplied options, including Sequence Diagram(s)sequenceDiagram
participant Cluster
participant Connection
participant Reactor
participant pyOpenSSL
Cluster->>Connection: provide ssl_options or ssl_context
Connection->>Reactor: expose normalized SSL state
Reactor->>pyOpenSSL: build context and perform handshake
pyOpenSSL-->>Reactor: provide peer certificate
Reactor->>Connection: validate certificate hostname
Possibly related PRs
Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
Comment |
704dde9 to
805b678
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
cassandra/io/eventletreactor.py (1)
121-158: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDead code:
uses_legacy_ssl_optionsis now alwaysFalse.Since
__init__hardcodesself.uses_legacy_ssl_options = Falseand nothing else ever sets itTrue, theif self.uses_legacy_ssl_options: super()...branches in_initiate_connectionand_validate_hostnameare now unreachable.TwistedConnectiondoesn't carry this flag at all and branches on_ssl_enableddirectly — consider removing the vestigial flag/branches here for consistency and to avoid implying conditional behavior that no longer exists.♻️ Suggested cleanup
def __init__(self, *args, **kwargs): Connection.__init__(self, *args, **kwargs) - self.uses_legacy_ssl_options = False self._write_queue = Queue() ... def _initiate_connection(self, sockaddr): - if self.uses_legacy_ssl_options: - super(EventletConnection, self)._initiate_connection(sockaddr) - else: - self._socket.connect(sockaddr) - if self._ssl_enabled: - self._socket.do_handshake() + self._socket.connect(sockaddr) + if self._ssl_enabled: + self._socket.do_handshake() def _validate_hostname(self): - if self.uses_legacy_ssl_options: - super(EventletConnection, self)._validate_hostname() - else: - expected_name = (self.ssl_options or {}).get('server_hostname') or self.endpoint.address - _validate_pyopenssl_hostname(self._socket.get_peer_certificate(), expected_name) + expected_name = (self.ssl_options or {}).get('server_hostname') or self.endpoint.address + _validate_pyopenssl_hostname(self._socket.get_peer_certificate(), expected_name)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@cassandra/io/eventletreactor.py` around lines 121 - 158, Remove the hardcoded uses_legacy_ssl_options assignment from EventletConnection.__init__ and delete the unreachable legacy branches in _initiate_connection and _validate_hostname. Keep the current non-legacy socket connection, handshake, and hostname validation behavior as the unconditional implementation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@cassandra/io/eventletreactor.py`:
- Around line 121-158: Remove the hardcoded uses_legacy_ssl_options assignment
from EventletConnection.__init__ and delete the unreachable legacy branches in
_initiate_connection and _validate_hostname. Keep the current non-legacy socket
connection, handshake, and hostname validation behavior as the unconditional
implementation.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 1f288eee-0f6d-48f6-b321-96aa8ca19b32
📒 Files selected for processing (13)
cassandra/cluster.pycassandra/connection.pycassandra/datastax/insights/reporter.pycassandra/io/asyncioreactor.pycassandra/io/eventletreactor.pycassandra/io/twistedreactor.pycassandra/pool.pytests/unit/advanced/test_insights.pytests/unit/io/test_eventletreactor.pytests/unit/io/test_twistedreactor.pytests/unit/test_client_routes.pytests/unit/test_connection.pytests/unit/test_shard_aware.py
There was a problem hiding this comment.
🧹 Nitpick comments (1)
cassandra/datastax/cloud/__init__.py (1)
188-193: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winUse
raise ... fromfor cleaner exception chaining.Within an
exceptclause, usingraise ... from eis the idiomatic Python 3 approach. It automatically preserves the original traceback and sets the__cause__attribute, making it cleaner than manually chaining with.with_traceback().
As per static analysis hints, within anexceptclause, exceptions should be raised withraise ... from errto distinguish them from errors in exception handling.♻️ Proposed refactor
try: from OpenSSL import SSL except ImportError as e: raise ImportError( - "PyOpenSSL must be installed to connect to Astra with the Eventlet or Twisted event loops")\ - .with_traceback(e.__traceback__) + "PyOpenSSL must be installed to connect to Astra with the Eventlet or Twisted event loops" + ) from e🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@cassandra/datastax/cloud/__init__.py` around lines 188 - 193, Update the OpenSSL import error handling in the cloud initialization code to raise the custom ImportError using Python’s explicit exception chaining syntax with the caught exception as its cause. Remove the manual .with_traceback() chaining while preserving the existing error message and behavior.Source: Linters/SAST tools
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@cassandra/datastax/cloud/__init__.py`:
- Around line 188-193: Update the OpenSSL import error handling in the cloud
initialization code to raise the custom ImportError using Python’s explicit
exception chaining syntax with the caught exception as its cause. Remove the
manual .with_traceback() chaining while preserving the existing error message
and behavior.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 8b73f8af-9ad3-478c-ae6a-58f2804a5f23
📒 Files selected for processing (7)
cassandra/datastax/cloud/__init__.pycassandra/io/eventletreactor.pycassandra/io/twistedreactor.pytests/unit/io/test_eventletreactor.pytests/unit/io/test_twistedreactor.pytests/unit/test_cloud.pytests/unit/test_cluster.py
🚧 Files skipped from review as they are similar to previous changes (4)
- tests/unit/io/test_twistedreactor.py
- cassandra/io/eventletreactor.py
- cassandra/io/twistedreactor.py
- tests/unit/io/test_eventletreactor.py
8e17dd5 to
f119103
Compare
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
cassandra/io/twistedreactor.py (1)
43-80: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDuplicate pyOpenSSL helpers across reactors and cloud module.
_default_ssl_method(43-48) is an exact duplicate ofcassandra/datastax/cloud/__init__.py::_default_pyopenssl_ssl_method, and per the codebase graph,cassandra/io/eventletreactor.pydefines an identical_default_ssl_method/_build_pyopenssl_context_from_optionspair as well. Three independent copies of TLS negotiation/context-building logic increase the risk of divergence (e.g. one path missing a future security fix).Extracting these into a single shared module (e.g. near
_validate_pyopenssl_hostnameincassandra/connection.py) would remove the duplication.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@cassandra/io/twistedreactor.py` around lines 43 - 80, Extract the shared pyOpenSSL TLS negotiation and context-building logic from `_default_ssl_method` and `_build_pyopenssl_context_from_options` into a common helper module, alongside the existing shared SSL utilities such as `_validate_pyopenssl_hostname`. Update `cassandra/io/twistedreactor.py`, `cassandra/io/eventletreactor.py`, and `cassandra/datastax/cloud/__init__.py` to import and reuse the shared helpers, preserving their current certificate, verification, and fallback behavior.cassandra/datastax/cloud/__init__.py (1)
179-184: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDuplicate TLS-method-selection helper across three modules.
This exact loop-and-fallback logic is duplicated verbatim in
cassandra/io/twistedreactor.py(_default_ssl_method) and, per the codebase graph,cassandra/io/eventletreactor.py(_default_ssl_method). Three independent copies of security-relevant TLS negotiation logic risk silently diverging if one is patched without the others.Consider hoisting this into a single shared helper (e.g. alongside
_validate_pyopenssl_hostnameincassandra/connection.py) and having all three call sites import it.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@cassandra/datastax/cloud/__init__.py` around lines 179 - 184, Consolidate the duplicated TLS method-selection loop from _default_pyopenssl_ssl_method, twistedreactor._default_ssl_method, and eventletreactor._default_ssl_method into one shared helper alongside _validate_pyopenssl_hostname in connection.py. Update all three modules to import and call the shared helper, removing their local implementations while preserving the existing method order and ImportError fallback.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/unit/io/test_eventletreactor.py`:
- Around line 122-131: Update test_validate_hostname_rejects_mismatch and
test_validate_hostname_prefers_san_over_common_name to assert
ssl.CertificateError specifically when _validate_hostname rejects the
certificate, replacing the broad Exception assertion while preserving the
existing test setup and invocation.
---
Nitpick comments:
In `@cassandra/datastax/cloud/__init__.py`:
- Around line 179-184: Consolidate the duplicated TLS method-selection loop from
_default_pyopenssl_ssl_method, twistedreactor._default_ssl_method, and
eventletreactor._default_ssl_method into one shared helper alongside
_validate_pyopenssl_hostname in connection.py. Update all three modules to
import and call the shared helper, removing their local implementations while
preserving the existing method order and ImportError fallback.
In `@cassandra/io/twistedreactor.py`:
- Around line 43-80: Extract the shared pyOpenSSL TLS negotiation and
context-building logic from `_default_ssl_method` and
`_build_pyopenssl_context_from_options` into a common helper module, alongside
the existing shared SSL utilities such as `_validate_pyopenssl_hostname`. Update
`cassandra/io/twistedreactor.py`, `cassandra/io/eventletreactor.py`, and
`cassandra/datastax/cloud/__init__.py` to import and reuse the shared helpers,
preserving their current certificate, verification, and fallback behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: e9c06e84-df9d-4864-bb9c-2a2a66a988d5
📒 Files selected for processing (16)
cassandra/cluster.pycassandra/connection.pycassandra/datastax/cloud/__init__.pycassandra/datastax/insights/reporter.pycassandra/io/asyncioreactor.pycassandra/io/eventletreactor.pycassandra/io/twistedreactor.pycassandra/pool.pytests/unit/advanced/test_insights.pytests/unit/io/test_eventletreactor.pytests/unit/io/test_twistedreactor.pytests/unit/test_client_routes.pytests/unit/test_cloud.pytests/unit/test_cluster.pytests/unit/test_connection.pytests/unit/test_shard_aware.py
🚧 Files skipped from review as they are similar to previous changes (9)
- tests/unit/test_cloud.py
- tests/unit/test_cluster.py
- tests/unit/test_client_routes.py
- cassandra/cluster.py
- tests/unit/io/test_twistedreactor.py
- cassandra/pool.py
- tests/unit/advanced/test_insights.py
- cassandra/io/eventletreactor.py
- cassandra/connection.py
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Pull request overview
Normalizes explicit empty ssl_options handling so {} enables TLS while None remains disabled.
Changes:
- Preserves explicit SSL configuration across connections, reactors, routing, and Insights.
- Builds default SSL contexts for standard, Eventlet, and Twisted paths.
- Adds focused SSL behavior and compatibility tests.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
cassandra/connection.py |
Preserves explicit SSL state and builds contexts. |
cassandra/cluster.py |
Corrects cloud validation and warnings. |
cassandra/pool.py |
Updates shard-aware TLS port selection. |
cassandra/io/asyncioreactor.py |
Uses normalized SSL state. |
cassandra/io/eventletreactor.py |
Builds pyOpenSSL contexts for Eventlet. |
cassandra/io/twistedreactor.py |
Builds pyOpenSSL contexts for Twisted. |
cassandra/datastax/cloud/__init__.py |
Selects modern pyOpenSSL methods. |
cassandra/datastax/insights/reporter.py |
Corrects SSL startup reporting. |
tests/unit/test_connection.py |
Tests connection SSL semantics. |
tests/unit/test_cluster.py |
Tests cloud conflicts and warnings. |
tests/unit/test_cloud.py |
Tests pyOpenSSL method fallback. |
tests/unit/test_client_routes.py |
Tests empty options with TLS routes. |
tests/unit/test_shard_aware.py |
Tests shard-aware SSL ports. |
tests/unit/io/test_eventletreactor.py |
Tests Eventlet SSL contexts. |
tests/unit/io/test_twistedreactor.py |
Tests Twisted SSL contexts. |
tests/unit/advanced/test_insights.py |
Tests Insights SSL reporting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
f8c3ca4 to
9bcedb1
Compare
|
Addressed the blocking items in b14fe37: raw integer cert_reqs mapping, missing peer certificate handling, Twisted fail-closed callback behavior, and Eventlet hostname error reporting. Also added Cluster.ssl_options docs, CHANGELOG entries, and updated the PR title/description. Deferring typed SAN parsing and cleanup-only items to #941. Validation: uv run pytest -rf tests/unit/test_connection.py tests/unit/test_cluster.py tests/unit/test_client_routes.py tests/unit/test_cloud.py tests/unit/test_shard_aware.py tests/unit/advanced/test_insights.py tests/unit/io/test_asyncioreactor.py tests/unit/io/test_eventletreactor.py tests/unit/io/test_twistedreactor.py -> 208 passed, 5 skipped. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 18 out of 18 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
CHANGELOG.rst:12
- This newly advertised behavior conflicts with the SSL guide:
docs/security.rst:50-53says anssl_contextis required to enable SSL, and lines 70-72 say Twisted/Eventlet users must pass a pyOpenSSL context. Update that guide to document options-only TLS and the new reactor handling; otherwise the primary user documentation tells users this supported path is invalid.
* Legacy ``ssl_options`` now work with the Twisted and Eventlet reactors by
using pyOpenSSL contexts with mapped protocol, verification, cipher, SNI, and
hostname-validation settings.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 18 out of 18 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
cassandra/connection.py:185
- When
check_hostname=Trueis used with a supplied context from an older pyOpenSSL that lacksget_verify_mode(), this helper now silently does nothing. Such contexts default toVERIFY_NONE, so Eventlet/Twisted proceed to hostname-match a certificate whose chain was never authenticated; the new compatibility test codifies that insecure path. Please fail closed for this combination or otherwise forceVERIFY_PEERwhile preserving any user verification callback.
def _ensure_pyopenssl_context_requires_verification(ssl_module, context, check_hostname):
get_verify_mode = getattr(context, 'get_verify_mode', None)
if (check_hostname and get_verify_mode is not None and
get_verify_mode() == ssl_module.VERIFY_NONE):
context.set_verify(
ssl_module.VERIFY_PEER,
callback=lambda _connection, _x509, _errnum, _errdepth, ok: ok
)
I think there is nothing wrong with rewriting PR history, if we would gain cleaner history on master. I think that is the case with this PR - one commit adding unnecessary noise, next one deleting it. It would be nice to have it cleaned before merge. |
2ee525e to
ab39732
Compare
+1. This is easier to review than additional commits. |
nikagra
left a comment
There was a problem hiding this comment.
Verification pass on ab397328
I re-checked all 20 code threads against the code rather than the "Fixed in <sha>" replies, and exercised the pyOpenSSL paths at runtime with real OpenSSL.SSL objects. 19 of 20 are genuinely fixed — the stdlib→pyOpenSSL protocol/verify-mode mapping, the fail-closed info_callback, IP-literal handling, the shard-aware source-port bind, implicit SNI, and the per-connection callback all hold up under scrutiny. This latest force-push also dropped the wrapper indirection and the dead verify_callback, which removes two cleanup notes I had queued. Thanks for the turnaround on those.
Requesting changes on one item, plus two documentation gaps around genuinely breaking behaviour changes and some smaller cleanups.
Blocking: the hostname verifier cannot parse a real certificate on current pyOpenSSL
X509.get_extension was removed in pyOpenSSL 26.2.0. get_extension_count() survived, so _pyopenssl_cert_subject_alt_names enters the loop and then raises AttributeError for every certificate that carries extensions — which is every real server certificate.
Same code, same certificate bytes, only pyOpenSSL differing (certificates pulled from live TLS servers; full reproduction inline on cassandra/connection.py:272 and in the reopened thread):
pyOpenSSL 26.1.0 pyOpenSSL 26.3.0
www.scylladb.com VALIDATED AttributeError: 'X509' object has no attribute 'get_extension'
www.python.org VALIDATED AttributeError: 'X509' object has no attribute 'get_extension'
github.com VALIDATED AttributeError: 'X509' object has no attribute 'get_extension'
This is only reachable because of this PR. On master, Connection._check_hostname is only ever the class attribute False — nothing in cassandra/ assigns it — so _validate_hostname(), asyncioreactor.py:228 and the check_hostname argument Twisted passes to _SSLCreator were all dead code. This PR is what turns them on, so check_hostname=True on Eventlet/Twisted moves from "silently not checked" to "cannot connect". That includes Cluster(cloud=...), which hard-codes ssl_options={'check_hostname': True} at cluster.py:1320.
Both reactors fail closed, so there is no MITM window — but TLS with hostname verification is unusable on them, and Eventlet surfaces it as an AttributeError out of Connection.__init__ rather than a connection error.
Why CI is green
tests/unit is fully green on the exact pyOpenSSL version where the feature is broken. Every hostname test injects a fake X509 double that implements get_extension itself, so no test ever touches a real OpenSSL.crypto.X509. That gap is what let a removed API through.
Environment
Python 3.12.3, pyOpenSSL 26.3.0, at ab397328:
tests/unit→ 734 passed, 104 skippedEVENT_LOOP_MANAGER=asyncio tests/unit/io/test_asyncioreactor.py→ 3 passedEventletSSLContextTest→ 12 passedTwistedSSLContextTest→ 13 passed
|
All currently unresolved review feedback is addressed in Additional documentation and validation completed:
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 20 out of 20 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (2)
cassandra/connection.py:203
- When promoting a caller-supplied pyOpenSSL context to require peer verification, this overwrites the verify mode with
VERIFY_PEERand can silently drop any other verification flags already set (e.g.,VERIFY_FAIL_IF_NO_PEER_CERTor similar OpenSSL bitmask flags). To avoid changing behavior beyond adding peer verification (while still replacing the callback), compute the new mode by OR-ing the existing verify mode (when available) withVERIFY_PEERinstead of replacing it entirely.
def _ensure_pyopenssl_context_requires_verification(ssl_module, context, check_hostname):
"""
Make hostname verification fail closed for a caller-supplied context.
When hostname checking is enabled, this may mutate the context's verify
mode and callback because pyOpenSSL does not expose a non-mutating way to
require peer verification.
"""
if not check_hostname:
return
get_verify_mode = getattr(context, 'get_verify_mode', None)
verify_mode = get_verify_mode() if get_verify_mode is not None else None
if (verify_mode is not None and
verify_mode & ssl_module.VERIFY_PEER):
return
log.warning(
"check_hostname=True requires peer verification; mutating supplied "
"pyOpenSSL context to use VERIFY_PEER and replacing its verification "
"callback"
)
context.set_verify(
ssl_module.VERIFY_PEER,
callback=lambda _connection, _x509, _errnum, _errdepth, ok: ok
)
tests/unit/test_shard_aware.py:78
- This
EndpointSSLOptionsEndPointtest helper is duplicated (also present intests/unit/advanced/test_insights.py). Consider moving it to a shared test utility module (or a shared fixture) to prevent future drift and keep endpoint-SSL behavior tests consistent across suites.
class EndpointSSLOptionsEndPoint(DefaultEndPoint):
@property
def ssl_options(self):
return {}
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 20 out of 20 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (4)
cassandra/connection.py:1173
- This conditional expression is difficult to read and relies on operator-precedence rules that are easy to misinterpret. Refactor into an explicit
if ssl_options is not None: ... else: ...(or add parentheses around the intended grouping) to make the verification-default logic unambiguous.
self._ssl_options_verify_by_default = (
bool(ssl_options) or endpoint_has_non_sni_options
if ssl_options is not None
else bool(endpoint_ssl_options)
)
tests/unit/io/test_twistedreactor.py:37
- These unit tests depend on a certificate file living under
tests/integration/..., which can make the unit suite brittle in environments where integration assets aren’t present or packaged. Prefer generating a temporary CA bundle during the test (or patchingContext.load_verify_locationsand asserting the call) so unit tests remain hermetic and filesystem-independent.
CA_CERTS = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', '..', 'integration', 'long', 'ssl', 'rootCa.crt'))
tests/unit/io/test_eventletreactor.py:38
- Same issue as Twisted: the unit test suite is coupled to an integration certificate file on disk. Consider creating a temp CA cert file within the test (or mocking
load_verify_locations) to avoid failures when integration fixtures are not available.
CA_CERTS = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', '..', 'integration', 'long', 'ssl', 'rootCa.crt'))
tests/unit/io/test_eventletreactor.py:366
- Sockets return
bytesfromrecv()on Python 3; using astrhere works only because the code checks falsiness, but it doesn’t reflect real behavior. Useb''to better model actual socket/pyOpenSSL return values.
conn = Mock(in_buffer_size=4096)
conn._socket.recv.return_value = ''
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 20 out of 20 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
tests/unit/test_shard_aware.py:78
- This
EndpointSSLOptionsEndPointhelper is duplicated (a very similar class also appears intests/unit/advanced/test_insights.py). To reduce duplication and keep endpoint-test helpers consistent, consider moving this to a shared test utility module (e.g.,tests/unit/io/utils.py) and importing it where needed.
class EndpointSSLOptionsEndPoint(DefaultEndPoint):
@property
def ssl_options(self):
return {}
nikagra
left a comment
There was a problem hiding this comment.
Approving — verification pass on 08fc467c
This supersedes my earlier CHANGES_REQUESTED at 8d6fdc54 and ab397328. All 12 of my findings are fixed, and I checked them by running the code rather than by the Fixed in <sha> replies. Three of my threads were resolved without a reply; I verified those too and have confirmed each in its thread.
What I ran
Head 08fc467c in a clean worktree — Python 3.12.3, pyOpenSSL 26.3.0 (the release that removed X509.get_extension), cryptography 49.0.0, Twisted 26.4.0, eventlet 0.41.1:
TZ=UTC pytest tests/unit— 825 passed, 108 skipped. The skips are environmental in my setup and unrelated to this PR (48 libev, 16 asyncore, plus lz4/Cython/geomet/cmurmur3 absent). The tests this PR adds did run:test_connection.py,io/test_twistedreactor.pyandio/test_eventletreactor.pyare 146 passed / 2 skipped on their own.Connection.__init__SSL state matrix, 13 combinations against the real class: matches the documented behavior exactly. The caller'sssl_optionsdict is not mutated. Non-empty options load 121 system CA certs.OP_NO_SSLv3survives — master'srv.options = int(cert_reqs)clobbering is genuinely gone.- Real TLS handshakes through
_build_pyopenssl_context_from_optionsagainst a live TLS server:{}connects unverified;{'ciphers': 'HIGH'}withoutca_certsfails withcertificate verify failed;ca_certsverifies; symbolic and enumssl_versionboth resolve;PROTOCOL_TLSv1_2pins TLS 1.2. - Twisted end-to-end with
check_hostname=True, real reactor and real cffi info callback: a matching name completes the handshake; a mismatched name fails withHostname verification failed: hostname 'wrong.example.com' doesn't match certificate DNS subjectAltName ['localhost']; an untrusted CA fails withcertificate verify failed. Fails closed. - Eventlet
_connect_socket()against the same server: same four outcomes, and a mismatch surfaces as a clearConnectionExceptionrather than a socket error. _validate_pyopenssl_hostnameagainst OpenSSLX509_check_hostsemantics, 20 cases on real certificates: wildcards, IP-SAN-only, commonName-fallback suppression rules, IDNA-2003 alias rejection,*.2.3.4against1.2.3.4, absent certificate — all correct.cassandra.datastax.cloudimportingcassandra.connectionat module scope: no circular import.
On the compatibility break
Non-empty legacy ssl_options without ca_certs now fails against a private CA on Twisted/Eventlet — I reproduced it (certificate verify failed). The CHANGELOG, docs/security.rst, and the Cluster.ssl_options docstring all state this plainly, which is the right handling for a change in this direction.
Non-blocking
Five inline comments, none of them merge conditions: two consistency points about when verification is on, one question about Astra under Twisted/Eventlet, and two small cleanups.
A few observations I'm not going to open threads for:
connection.py:287-289—_pyopenssl_verify_mode_from_cert_reqssilently resolves the stdlib/pyOpenSSL integer collision (a genuineSSL.VERIFY_FAIL_IF_NO_PEER_CERTbecomesVERIFY_PEER, dropping the flag), while_pyopenssl_ssl_method_from_stdlibraises on the same class of ambiguity. Both land on the safe side here; a one-line comment explaining the asymmetry would save the next reader the trip.connection.py:1425-1426—check_hostname=Truesilently wins over an explicitcert_reqs=CERT_NONE. Stdlib raisesValueErrorfor that combination; awarnwould be kinder than silence.connection.py:315-319— the mutation warning fires per connection, not once per context, so a caller-supplied insecure context logs it on every pool connection. Worth also noting in the docstring that a caller's ownset_info_callbackis replaced, not just their verify callback.connection.py:1531-1533— last-error-wins means a hostname failure on the first address can be masked by anECONNREFUSEDon the second, and hostname failures retry every remaining addrinfo entry pointlessly. Pre-existing shape, newly reachable by verification errors.tests/unit/test_cloud.pyuses bare pytest functions where the rest oftests/unitusesunittestclasses, and it mocks out_build_pyopenssl_context_from_options, so it pins the call shape rather than the resulting context. Fine given pyOpenSSL is optional — just noting what it does and doesn't cover.
Test coverage
This is the part I'd hold up as the model: real in-process OpenSSL.crypto.X509 certificates instead of doubles, correct skipIf guards so a missing pyOpenSSL doesn't silently disable the plaintext timer tests, and a named regression test for each previously-flagged issue (test_supplied_context_without_get_verify_mode_promotes_to_verify_peer, test_info_callback_fails_closed_on_unexpected_exception, test_hostname_error_closes_socket_and_tries_next_address). The one gap left is integration, which is the inline question about Astra.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 24 out of 24 changed files in this pull request and generated 1 comment.
Suppressed comments (2)
cassandra/datastax/insights/reporter.py:153
- If the control connection disappears before startup data is collected, this fallback reports endpoint-only TLS as disabled because it checks only cluster-level fields.
EndPointcontact points with non-Nonessl_optionsare supported and now enable TLS, so the startup event can contradict the actual connection configuration during this race. Include endpoint-derived SSL state (or retain the last control-connection state) in this fallback.
ssl_context = cluster.ssl_context
ssl_options = cluster.ssl_options
ssl_enabled = ssl_context is not None or ssl_options is not None
tests/unit/test_client_routes.py:460
- This regression only covers cluster-level
ssl_options={}. SupportedEndPointcontact points can supplyssl_options={}themselves, butClusterstill initializes the client-routes handler from cluster-level settings only; it consequently selectsportinstead oftls_port, and generatedClientRoutesEndPoints do not retain the original endpoint SSL options. Add an endpoint-only case and propagate that TLS configuration so routed connections both select and use TLS.
cluster = Cluster(
contact_points=["10.0.0.1"],
ssl_options={},
client_routes_config=config,
Preserve the distinction between omitted and explicitly empty ssl_options across connection setup, reactors, shard-aware routing, cloud contexts, and Insights reporting. Strengthen pyOpenSSL protocol, verification, SNI, and hostname handling while retaining legacy option behavior.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 25 out of 25 changed files in this pull request and generated 1 comment.
Suppressed comments (1)
cassandra/datastax/insights/reporter.py:154
- Because endpoint options are only read when cluster options are
None, this fallback ignores them for explicit cluster options. For example,Cluster(ssl_options={})plus an endpoint{'ca_certs': ...}creates a verifying context inConnection, but if the control connection is unavailable Insights reportscertValidation: falsefrom the empty cluster dict. Merge cluster and endpoint options with the same origin-sensitive verification default used byConnectionbefore computing the fallback report.
if ssl_options is None:
ssl_options = next((
| if (self.ssl_options.get('check_hostname', False) and | ||
| isinstance(self.ssl_context, ssl.SSLContext) and | ||
| not self.ssl_context.check_hostname): |
Lorak-mmk
left a comment
There was a problem hiding this comment.
Why is it a single commit? There is no way for a human to read that.
| #!/usr/bin/env python | ||
| # Copyright DataStax, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Validate Astra bundle TLS hostname verification on a pyOpenSSL reactor. | ||
|
|
||
| Set ``ASTRA_SECURE_CONNECT_BUNDLE``, ``ASTRA_CLIENT_ID``, and | ||
| ``ASTRA_CLIENT_SECRET``. Run once for each reactor: | ||
|
|
||
| uv run python scripts/validate_astra_tls.py twisted | ||
| uv run python scripts/validate_astra_tls.py eventlet | ||
|
|
||
| The script never prints credentials or bundle contents. | ||
| """ |
There was a problem hiding this comment.
Why is it datastax copyright? Why do we even do anything with astra? We never supported Datastax-specific functionality - we aim to remove it.
Summary
Fixes #937: explicit
ssl_options={}was treated like omittedssl_options=Nonebecause several paths checkedssl_optionstruthiness.This PR preserves whether
ssl_optionswas supplied and fixes the reactor paths needed to use that state consistently:ssl_options={}enables TLS with default options.ssl_options=Noneremains plaintext unless endpoint SSL options are supplied.ssl_optionsbuild compatible pyOpenSSL contexts for Eventlet/Twisted with protocol, verification, certificate, cipher, SNI, and hostname-validation handling.ca_certsis omitted.cryptographySAN/CN parsing, including compatibility with pyOpenSSL 26.2+, which removedX509.get_extension().Public
ClusterAPI and protocol format are unchanged.In Scope
ssl_options.Compatibility and Protocol Risk
No protocol changes.
An explicit empty dict now enables encrypted traffic without server certificate verification.
ssl_options=Noneremains plaintext unless endpoint SSL options are supplied.Non-empty legacy options now default to peer-certificate verification. On Eventlet/Twisted this changes pyOpenSSL's prior
VERIFY_NONEdefault toVERIFY_PEER; whenca_certsis omitted, the driver loads system trust roots. Configurations using a private or self-signed CA must provideca_certs, or explicitly usecert_reqs=ssl.CERT_NONEwith hostname checking disabled.Hostname verification is now actually enforced when
check_hostnameis enabled. A caller-supplied pyOpenSSL context that lacks peer verification is promoted toVERIFY_PEER; this mutation is logged and replaces its verification callback. Contexts already usingVERIFY_PEERretain their existing callback.Follow-up PR
Follow-up #941 remains for additional pyOpenSSL parity and client-routes compatibility with caller-supplied pyOpenSSL-style contexts. Typed SAN/CN parsing moved into this PR because current pyOpenSSL releases have already removed the deprecated extension API.
Tests
uv run pytest -rf tests/unit/test_connection.py tests/unit/test_cluster.py tests/unit/test_client_routes.py tests/unit/test_cloud.py tests/unit/test_shard_aware.py tests/unit/advanced/test_insights.py tests/unit/io/test_asyncioreactor.py tests/unit/io/test_eventletreactor.py tests/unit/io/test_twistedreactor.py— 211 passed, 5 skipped.TZ=UTC uv run pytest -rf tests/unit— 799 passed, 46 skipped.X509.get_extension()absent.make -C docs test— passed with warnings treated as errors.Integration scenario to consider: connect to a TLS cluster with
Cluster(..., ssl_options={})under the default, Twisted, and Eventlet reactors.