Skip to content

Commit ed113ff

Browse files
authored
Merge pull request #12107 from sethmlarson/vendor-truststore
2 parents dd28ff0 + bff1e6a commit ed113ff

File tree

16 files changed

+1512
-36
lines changed

16 files changed

+1512
-36
lines changed

docs/html/topics/https-certificates.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,9 @@ It is possible to use the system trust store, instead of the bundled certifi
2828
certificates for verifying HTTPS certificates. This approach will typically
2929
support corporate proxy certificates without additional configuration.
3030

31-
In order to use system trust stores, you need to:
32-
33-
- Use Python 3.10 or newer.
34-
- Install the {pypi}`truststore` package, in the Python environment you're
35-
running pip in.
36-
37-
This is typically done by installing this package using a system package
38-
manager or by using pip in {ref}`Hash-checking mode` for this package and
39-
trusting the network using the `--trusted-host` flag.
31+
In order to use system trust stores, you need to use Python 3.10 or newer.
4032

4133
```{pip-cli}
42-
$ python -m pip install truststore
43-
[...]
4434
$ python -m pip install SomePackage --use-feature=truststore
4535
[...]
4636
Successfully installed SomePackage

news/truststore.vendor.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add truststore 0.8.0

noxfile.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ def lint(session: nox.Session) -> None:
184184
# git reset --hard origin/main
185185
@nox.session
186186
def vendoring(session: nox.Session) -> None:
187+
# Ensure that the session Python is running 3.10+
188+
# so that truststore can be installed correctly.
189+
session.run(
190+
"python", "-c", "import sys; sys.exit(1 if sys.version_info < (3, 10) else 0)"
191+
)
192+
187193
session.install("vendoring~=1.2.0")
188194

189195
parser = argparse.ArgumentParser(prog="nox -s vendoring")

src/pip/_internal/cli/req_command.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,9 @@ def _create_truststore_ssl_context() -> Optional["SSLContext"]:
5858
return None
5959

6060
try:
61-
import truststore
62-
except ImportError:
63-
raise CommandError(
64-
"To use the truststore feature, 'truststore' must be installed into "
65-
"pip's current environment."
66-
)
61+
from pip._vendor import truststore
62+
except ImportError as e:
63+
raise CommandError(f"The truststore feature is unavailable: {e}")
6764

6865
return truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
6966

src/pip/_internal/commands/debug.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,29 @@ def create_vendor_txt_map() -> Dict[str, str]:
4646
return dict(line.split("==", 1) for line in lines)
4747

4848

49-
def get_module_from_module_name(module_name: str) -> ModuleType:
49+
def get_module_from_module_name(module_name: str) -> Optional[ModuleType]:
5050
# Module name can be uppercase in vendor.txt for some reason...
5151
module_name = module_name.lower().replace("-", "_")
5252
# PATCH: setuptools is actually only pkg_resources.
5353
if module_name == "setuptools":
5454
module_name = "pkg_resources"
5555

56-
__import__(f"pip._vendor.{module_name}", globals(), locals(), level=0)
57-
return getattr(pip._vendor, module_name)
56+
try:
57+
__import__(f"pip._vendor.{module_name}", globals(), locals(), level=0)
58+
return getattr(pip._vendor, module_name)
59+
except ImportError:
60+
# We allow 'truststore' to fail to import due
61+
# to being unavailable on Python 3.9 and earlier.
62+
if module_name == "truststore" and sys.version_info < (3, 10):
63+
return None
64+
raise
5865

5966

6067
def get_vendor_version_from_module(module_name: str) -> Optional[str]:
6168
module = get_module_from_module_name(module_name)
6269
version = getattr(module, "__version__", None)
6370

64-
if not version:
71+
if module and not version:
6572
# Try to find version in debundled module info.
6673
assert module.__file__ is not None
6774
env = get_environment([os.path.dirname(module.__file__)])

src/pip/_vendor/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,5 @@ def vendored(modulename):
117117
vendored("rich.traceback")
118118
vendored("tenacity")
119119
vendored("tomli")
120+
vendored("truststore")
120121
vendored("urllib3")

src/pip/_vendor/truststore/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 Seth Michael Larson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""Verify certificates using native system trust stores"""
2+
3+
import sys as _sys
4+
5+
if _sys.version_info < (3, 10):
6+
raise ImportError("truststore requires Python 3.10 or later")
7+
8+
from ._api import SSLContext, extract_from_ssl, inject_into_ssl # noqa: E402
9+
10+
del _api, _sys # type: ignore[name-defined] # noqa: F821
11+
12+
__all__ = ["SSLContext", "inject_into_ssl", "extract_from_ssl"]
13+
__version__ = "0.8.0"

0 commit comments

Comments
 (0)