Skip to content

Commit 4fb51f2

Browse files
Add the client cert and key support to HttpTransport (#3258)
* Add the client cert and key support to HttpTransport * Add a test case for the two-way ssl support in HttpTransport * Move cert_file and key_file to the end of arguments in ClientConstructor in consts.py --------- Co-authored-by: Neel Shah <[email protected]>
1 parent 06d5da1 commit 4fb51f2

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

sentry_sdk/consts.py

+2
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,8 @@ def __init__(
532532
enable_db_query_source=True, # type: bool
533533
db_query_source_threshold_ms=100, # type: int
534534
spotlight=None, # type: Optional[Union[bool, str]]
535+
cert_file=None, # type: Optional[str]
536+
key_file=None, # type: Optional[str]
535537
):
536538
# type: (...) -> None
537539
pass

sentry_sdk/transport.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ def __init__(
226226
http_proxy=options["http_proxy"],
227227
https_proxy=options["https_proxy"],
228228
ca_certs=options["ca_certs"],
229+
cert_file=options["cert_file"],
230+
key_file=options["key_file"],
229231
proxy_headers=options["proxy_headers"],
230232
)
231233

@@ -474,8 +476,8 @@ def _send_envelope(
474476
)
475477
return None
476478

477-
def _get_pool_options(self, ca_certs):
478-
# type: (Optional[Any]) -> Dict[str, Any]
479+
def _get_pool_options(self, ca_certs, cert_file=None, key_file=None):
480+
# type: (Optional[Any], Optional[Any], Optional[Any]) -> Dict[str, Any]
479481
options = {
480482
"num_pools": self._num_pools,
481483
"cert_reqs": "CERT_REQUIRED",
@@ -505,6 +507,9 @@ def _get_pool_options(self, ca_certs):
505507
or certifi.where()
506508
)
507509

510+
options["cert_file"] = cert_file or os.environ.get("CLIENT_CERT_FILE")
511+
options["key_file"] = key_file or os.environ.get("CLIENT_KEY_FILE")
512+
508513
return options
509514

510515
def _in_no_proxy(self, parsed_dsn):
@@ -524,6 +529,8 @@ def _make_pool(
524529
http_proxy, # type: Optional[str]
525530
https_proxy, # type: Optional[str]
526531
ca_certs, # type: Optional[Any]
532+
cert_file, # type: Optional[Any]
533+
key_file, # type: Optional[Any]
527534
proxy_headers, # type: Optional[Dict[str, str]]
528535
):
529536
# type: (...) -> Union[PoolManager, ProxyManager]
@@ -538,7 +545,7 @@ def _make_pool(
538545
if not proxy and (http_proxy != ""):
539546
proxy = http_proxy or (not no_proxy and getproxies().get("http"))
540547

541-
opts = self._get_pool_options(ca_certs)
548+
opts = self._get_pool_options(ca_certs, cert_file, key_file)
542549

543550
if proxy:
544551
if proxy_headers:

tests/test_transport.py

+12
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ def test_transport_num_pools(make_client, num_pools, expected_num_pools):
165165
assert options["num_pools"] == expected_num_pools
166166

167167

168+
def test_two_way_ssl_authentication(make_client):
169+
_experiments = {}
170+
171+
client = make_client(_experiments=_experiments)
172+
173+
options = client.transport._get_pool_options(
174+
[], "/path/to/cert.pem", "/path/to/key.pem"
175+
)
176+
assert options["cert_file"] == "/path/to/cert.pem"
177+
assert options["key_file"] == "/path/to/key.pem"
178+
179+
168180
def test_socket_options(make_client):
169181
socket_options = [
170182
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),

0 commit comments

Comments
 (0)