Skip to content

Commit

Permalink
remove get_ca_bundle_from_env
Browse files Browse the repository at this point in the history
  • Loading branch information
T-256 authored and Tester committed Oct 30, 2023
1 parent 51e103c commit aeb6478
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 58 deletions.
12 changes: 8 additions & 4 deletions httpx/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from ._models import Headers
from ._types import CertTypes, HeaderTypes, TimeoutTypes, URLTypes, VerifyTypes
from ._urls import URL
from ._utils import get_ca_bundle_from_env

DEFAULT_CIPHERS = ":".join(
[
Expand Down Expand Up @@ -102,9 +101,14 @@ def load_ssl_context_verify(self) -> ssl.SSLContext:
Return an SSL context for verified connections.
"""
if self.trust_env and self.verify is True:
ca_bundle = get_ca_bundle_from_env()
if ca_bundle is not None:
self.verify = ca_bundle
if "SSL_CERT_FILE" in os.environ:
ssl_file = Path(os.environ["SSL_CERT_FILE"])
if ssl_file.is_file():
self.verify = str(ssl_file)
if "SSL_CERT_DIR" in os.environ:
ssl_path = Path(os.environ["SSL_CERT_DIR"])
if ssl_path.is_dir():
self.verify = str(ssl_path)

if isinstance(self.verify, ssl.SSLContext):
# Allow passing in our own SSLContext object that's pre-configured.
Expand Down
13 changes: 0 additions & 13 deletions httpx/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import re
import time
import typing
from pathlib import Path
from urllib.request import getproxies

import sniffio
Expand Down Expand Up @@ -91,18 +90,6 @@ def replacer(match: typing.Match[str]) -> str:
return f'{name}="{value}"'.encode()


def get_ca_bundle_from_env() -> typing.Optional[str]:
if "SSL_CERT_FILE" in os.environ:
ssl_file = Path(os.environ["SSL_CERT_FILE"])
if ssl_file.is_file():
return str(ssl_file)
if "SSL_CERT_DIR" in os.environ:
ssl_path = Path(os.environ["SSL_CERT_DIR"])
if ssl_path.is_dir():
return str(ssl_path)
return None


def parse_header_links(value: str) -> typing.List[typing.Dict[str, str]]:
"""
Returns a list of parsed link headers, for more info see:
Expand Down
41 changes: 0 additions & 41 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pytest

import httpx
from httpx._utils import get_ca_bundle_from_env

from .common import TESTS_DIR

Expand Down Expand Up @@ -130,46 +129,6 @@ def test_logging_ssl(caplog):
]


def test_get_ssl_cert_file():
# Two environments is not set.
assert get_ca_bundle_from_env() is None

os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)
# SSL_CERT_DIR is correctly set, SSL_CERT_FILE is not set.
ca_bundle = get_ca_bundle_from_env()
assert ca_bundle is not None and ca_bundle.endswith("tests")

del os.environ["SSL_CERT_DIR"]
os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")
# SSL_CERT_FILE is correctly set, SSL_CERT_DIR is not set.
ca_bundle = get_ca_bundle_from_env()
assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")

os.environ["SSL_CERT_FILE"] = "wrongfile"
# SSL_CERT_FILE is set with wrong file, SSL_CERT_DIR is not set.
assert get_ca_bundle_from_env() is None

del os.environ["SSL_CERT_FILE"]
os.environ["SSL_CERT_DIR"] = "wrongpath"
# SSL_CERT_DIR is set with wrong path, SSL_CERT_FILE is not set.
assert get_ca_bundle_from_env() is None

os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)
os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")
# Two environments is correctly set.
ca_bundle = get_ca_bundle_from_env()
assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")

os.environ["SSL_CERT_FILE"] = "wrongfile"
# Two environments is set but SSL_CERT_FILE is not a file.
ca_bundle = get_ca_bundle_from_env()
assert ca_bundle is not None and ca_bundle.endswith("tests")

os.environ["SSL_CERT_DIR"] = "wrongpath"
# Two environments is set but both are not correct.
assert get_ca_bundle_from_env() is None


@pytest.mark.parametrize(
["environment", "proxies"],
[
Expand Down

0 comments on commit aeb6478

Please sign in to comment.