Add TLS session resumption via SSLSessionCache#789
Add TLS session resumption via SSLSessionCache#789sylwiaszunejko wants to merge 3 commits intoscylladb:masterfrom
Conversation
Introduce SSLSessionCache in connection.py: a thread-safe dict keyed by (address, port, server_hostname) storing ssl.SSLSession objects. - get(key) / set(key, session) API backed by a Lock - SNI-aware key prevents proxy-shared connections from colliding Includes unit tests for empty lookup, set/get, key isolation by address/port/SNI, overwrite, and concurrent access.
Wire SSLSessionCache into Connection so TLS sessions are saved after each handshake and restored on subsequent connections. - Accept optional ssl_session_cache in __init__ - Restore cached session before connect() to enable resumption; handle ssl.SSLError/AttributeError if session is rejected - Cache negotiated session in three places: after connect (TLS 1.2), after ReadyMessage and AuthSuccessMessage (TLS 1.3 async tickets) - Cache key uses (address, port, server_hostname) to avoid SNI collisions on shared proxy addresses Includes unit tests for restore, SNI lookup, error tolerance, caching, and no-op paths (cache=None, ssl_context=None, session=None).
Add ssl_session_cache to Cluster so all managed connections share a single TLS session store. - Auto-create SSLSessionCache when ssl_context or ssl_options are set; pass ssl_session_cache=None to opt out - Accept explicit ssl_session_cache to allow a custom instance - Warn when Twisted/Eventlet connection classes are used (pyOpenSSL does not support stdlib ssl session resumption; cache has no effect) - Pass the cache through connection_factory kwargs to each Connection Includes unit tests for auto-creation, opt-out, custom cache injection, factory propagation, and pyOpenSSL warnings.
Such claims would ideally be supported by benchmarks. Could you try to create some? |
That's the goal, but you're right, I don't have any tests to prove that, removed this claim from the PR description. If I manage to create proper benchmarks I will update on that |
|
We could, if it helps, only support this for TLS 1.3. |
Summary
This PR implements TLS session resumption for the Python driver. After the first
successful TLS handshake with a node, the negotiated session is stored in a
thread-safe cache and reused on subsequent connections, skipping the full
handshake.
Both TLS 1.2 (session IDs) and TLS 1.3 (session tickets / PSK) are supported.
Changes
cassandra/connection.py—SSLSessionCacheclassSSLSessionCache: a thread-safedictbacked by aLock,keyed by
(address, port, server_hostname).other's sessions.
Connectiongains an optionalssl_session_cacheparameter:connect()to enable resumption; gracefullyhandles
ssl.SSLError/AttributeErrorif the server rejects the session._initiate_connection()in_connect_socket()— TLS 1.2 sessionsavailable immediately after connect.
ReadyMessagein_handle_startup_response()— TLS 1.3 sessionsdelivered asynchronously after the first application-data exchange.
AuthSuccessMessagein_handle_auth_response()— same TLS 1.3coverage for authenticated connections.
cassandra/cluster.py—Clusterintegrationssl_session_cacheattribute toCluster.SSLSessionCachewhenssl_contextorssl_optionsare set;no configuration required for the common case.
ssl_session_cache=Noneexplicitly to opt out of session caching.SSLSessionCacheinstance can be supplied to share a cache acrossclusters or inject a custom implementation.
Eventlet), which has a different session API and is not covered by this cache.
connection_factorykwargs to everyConnection.Limitations
sslreactor paths are supported: asyncore, libev, gevent,asyncio.
A warning is emitted when this combination is detected.
Tests
tests/unit/test_connection.py—TestSSLSessionCache: empty lookup,set/get, key isolation by address/port/SNI, overwrite, thread safety.
tests/unit/test_connection.py—Connectionunit tests: session restore(including SNI-specific lookup), error tolerance, caching on ReadyMessage /
AuthSuccess, no-op guard paths (
cache=None,ssl_context=None,session=None).tests/unit/test_cluster.py—TestSSLSessionCacheAutoCreation: auto-createwith
ssl_context/ssl_options, no cache without TLS, explicitNoneopt-out, custom cache injection, factory propagation, pyOpenSSL warnings for
Twisted and Eventlet.
Fixes: https://scylladb.atlassian.net/browse/DRIVER-165
Pre-review checklist
./docs/source/.Fixes:annotations to PR description.