Skip to content

Commit c2d0351

Browse files
committed
feat(requests-ca): respect REQUESTS_CA_BUNDLE environment variable
Current implementation of `HttpClient` uses requests library to create a `PreparedRequest` and send it, but do not take into consideration the environment variables that `requests` do. This make it impossible to use custom CA certificates together with Airbyte. This PR adds the support of env settings to `HttpClient`. The implementation is doing what `requests` library recommends when working with `PreparedRequests` to properly handle self-signed certificates: https://requests.readthedocs.io/en/latest/user/advanced/#prepared-requests
1 parent ae0e8aa commit c2d0351

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

airbyte_cdk/sources/streams/http/http_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,9 @@ def send_request(
545545
data=data,
546546
)
547547

548+
env_settings = self._session.merge_environment_settings(request.url, None, None, None, None)
549+
request_kwargs = {**request_kwargs, **env_settings}
550+
548551
response: requests.Response = self._send_with_retry(
549552
request=request,
550553
request_kwargs=request_kwargs,

unit_tests/sources/streams/http/test_http_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) 2024 Airbyte, Inc., all rights reserved.
22

33
import logging
4+
import os
45
from datetime import timedelta
56
from unittest.mock import MagicMock, patch
67

@@ -744,3 +745,23 @@ def test_given_different_headers_then_response_is_not_cached(requests_mock):
744745
)
745746

746747
assert second_response.json()["test"] == "second response"
748+
749+
@patch.dict("os.environ", {"REQUESTS_CA_BUNDLE": "/path/to/ca-bundle.crt"})
750+
def test_send_request_respects_environment_variables():
751+
"""Test that send_request respects REQUESTS_CA_BUNDLE environment variable."""
752+
http_client = HttpClient(
753+
name="test",
754+
logger=MagicMock(),
755+
)
756+
757+
with patch.object(http_client, '_send_with_retry') as mock_send_with_retry:
758+
http_client.send_request(
759+
http_method="GET",
760+
url="https://api.example.com",
761+
request_kwargs={"timeout": 10}
762+
)
763+
764+
passed_kwargs = mock_send_with_retry.call_args[1]["request_kwargs"]
765+
766+
assert "verify" in passed_kwargs
767+
assert passed_kwargs["verify"] == "/path/to/ca-bundle.crt"

0 commit comments

Comments
 (0)