Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.3k
Added support ssl cert file environment#307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
54ee42d73cbc9d03e45c53907406bb46114e9b5f34bb2dad44cac5d400fd8390ca5798b8fce8225b16a9e36f79e59817c7ce62cd07aa1625f3505f4a9e8c5aFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| import os | ||
| import socket | ||
| import ssl | ||
| import sys | ||
| from pathlib import Path | ||
| import pytest | ||
| @@ -26,6 +29,30 @@ def test_load_ssl_config_verify_existing_file(): | ||
| assert context.check_hostname is True | ||
| @pytest.mark.parametrize("config", ("SSL_CERT_FILE", "SSL_CERT_DIR")) | ||
| def test_load_ssl_config_verify_env_file(https_server, ca_cert_pem_file, config): | ||
| os.environ[config] = ( | ||
| ca_cert_pem_file | ||
| if config.endswith("_FILE") | ||
| else str(Path(ca_cert_pem_file).parent) | ||
| ) | ||
| ssl_config = httpx.SSLConfig(trust_env=True) | ||
| context = ssl_config.load_ssl_context() | ||
| assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED | ||
| assert context.check_hostname is True | ||
| assert ssl_config.verify == os.environ[config] | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verify that certs are loaded via ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can use https_server.url.host and https_server.url.port to accomplish that. Then our certificate will verify as well :) ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't verify with Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Has this been resolved? :) ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm very sorry that I didn't do anything but i will asap Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is blocked by #354, just gave it a spin myself. I think we should maybe merge this now? Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #354 is closed now, I tried changing the test to look like this right now: os.environ[config] = (
ca_cert_pem_fileifconfig.endswith("_FILE")
elsestr(Path(ca_cert_pem_file).parent)
)
ssl_config=httpx.SSLConfig(trust_env=True)
context=ssl_config.load_ssl_context()
assertcontext.verify_mode==ssl.VerifyMode.CERT_REQUIREDassertcontext.check_hostnameisTrueassertssl_config.verify==os.environ[config]
host=https_server.url.hostport=https_server.url.portconn=socket.create_connection((host, port))
context.wrap_socket(conn, server_hostname=host)
assertlen(context.get_ca_certs()) ==1But I'm still failing on the ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a test to be able to compare with below traceback. Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spoke with @florimondmanca and I think we're just going to skip the | ||
| # Skipping 'SSL_CERT_DIR' functional test for now because | ||
| # we're unable to get the certificate within the directory to | ||
| # load into the SSLContext. :( | ||
| if config == "SSL_CERT_FILE": | ||
| host = https_server.url.host | ||
| port = https_server.url.port | ||
| conn = socket.create_connection((host, port)) | ||
| context.wrap_socket(conn, server_hostname=host) | ||
| assert len(context.get_ca_certs()) == 1 | ||
| def test_load_ssl_config_verify_directory(): | ||
| path = httpx.config.DEFAULT_CA_BUNDLE_PATH.parent | ||
| ssl_config = httpx.SSLConfig(verify=path) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -8,6 +8,7 @@ | ||
| from httpx import utils | ||
| from httpx.utils import ( | ||
| ElapsedTimer, | ||
| get_ca_bundle_from_env, | ||
| get_environment_proxies, | ||
| get_netrc_login, | ||
| guess_json_utf, | ||
| @@ -120,6 +121,42 @@ async def test_httpx_debug_enabled_stderr_logging(server, capsys, httpx_debug): | ||
| logging.getLogger("httpx").handlers = [] | ||
| def test_get_ssl_cert_file(): | ||
sethmlarson marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. florimondmanca marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| # Two environments is not set. | ||
| assert get_ca_bundle_from_env() is None | ||
| os.environ["SSL_CERT_DIR"] = "tests/" | ||
| # SSL_CERT_DIR is correctly set, SSL_CERT_FILE is not set. | ||
| assert get_ca_bundle_from_env() == "tests" | ||
| del os.environ["SSL_CERT_DIR"] | ||
| os.environ["SSL_CERT_FILE"] = "tests/test_utils.py" | ||
| # SSL_CERT_FILE is correctly set, SSL_CERT_DIR is not set. | ||
| assert get_ca_bundle_from_env() == "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"] = "tests/" | ||
| os.environ["SSL_CERT_FILE"] = "tests/test_utils.py" | ||
| # Two environments is correctly set. | ||
| assert get_ca_bundle_from_env() == "tests/test_utils.py" | ||
| os.environ["SSL_CERT_FILE"] = "wrongfile" | ||
| # Two environments is set but SSL_CERT_FILE is not a file. | ||
| assert get_ca_bundle_from_env() == "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.asyncio | ||
| async def test_elapsed_timer(): | ||
| with ElapsedTimer() as timer: | ||
Uh oh!
There was an error while loading. Please reload this page.