Skip to content

Commit 671deee

Browse files
authored
Make driver's default SSL context respect SSLKEYLOGFILE env var (#1312)
This aligns the driver with Python's `ssl.create_default_context()` behavior: > When `keylog_filename` is supported and the environment variable > `SSLKEYLOGFILE` is set, `create_default_context()` enables key logging. > > -- https://docs.python.org/3/library/ssl.html#ssl.create_default_context The same behavior could previously be achieved by passing a custom SSLContext. However, this is much more work. Supporting the env var `SSLKEYLOGFILE` is a common practice for software using SSL.
1 parent 13fe8a2 commit 671deee

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

docs/source/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,10 @@ There are different *mutually exclusive* ways of configuring TLS/SSL encryption
845845
* or set :ref:`ssl-context-ref` to gain full control (and responsibility) over the TLS configuration.
846846
* or set ``encrypted=False`` (default) to disable TLS.
847847

848+
All options except for configuring a custom :ref:`ssl-context-ref` will check the
849+
environment variable ``SSLKEYLOGFILE``.
850+
If the variable is set, its value will be assinged to
851+
:attr:`ssl.SSLContext.keylog_filename` to enable key logging.
848852

849853

850854
Driver Object Lifetime

src/neo4j/_async/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
from __future__ import annotations
1818

19+
import os
20+
1921
from .. import _typing as t
2022
from .._async_compat.concurrency import AsyncLock
2123
from .._conf import (
@@ -154,6 +156,12 @@ async def get_ssl_context(self) -> ssl.SSLContext | None:
154156
# https://docs.python.org/3.10/library/ssl.html#protocol-versions
155157
ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2
156158

159+
# Follow Python's `create_default_context` and respect the
160+
# `SSLKEYLOGFILE` environment variable for key logging if present.
161+
ssl_keylog_file = os.getenv("SSLKEYLOGFILE")
162+
if ssl_keylog_file:
163+
ssl_context.keylog_filename = ssl_keylog_file
164+
157165
if isinstance(self.trusted_certificates, TrustAll):
158166
# trust any certificate
159167
ssl_context.check_hostname = False

src/neo4j/_sync/config.py

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)