Skip to content

Commit 1ecade4

Browse files
committed
Abstract provider interface to keyring
1 parent 4fc2008 commit 1ecade4

File tree

1 file changed

+128
-55
lines changed

1 file changed

+128
-55
lines changed

src/pip/_internal/network/auth.py

+128-55
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
providing credentials in the context of network requests.
55
"""
66

7+
import os
78
import shutil
89
import subprocess
910
import urllib.parse
10-
from typing import Any, Dict, List, NamedTuple, Optional, Tuple
11+
from abc import ABC, abstractmethod
12+
from typing import Any, Dict, List, NamedTuple, Optional, Tuple, Type
1113

1214
from pip._vendor.requests.auth import AuthBase, HTTPBasicAuth
1315
from pip._vendor.requests.models import Request, Response
@@ -27,89 +29,159 @@
2729

2830

2931
class Credentials(NamedTuple):
30-
service_name: str
32+
url: str
3133
username: str
3234
password: str
3335

3436

35-
class KeyRingCli:
36-
"""Mirror the parts of keyring's API which pip uses
37+
class KeyRingBaseProvider(ABC):
38+
"""Keyring base provider interface"""
39+
40+
@classmethod
41+
@abstractmethod
42+
def is_available(cls) -> bool:
43+
...
44+
45+
@classmethod
46+
@abstractmethod
47+
def get_auth_info(cls, url: str, username: Optional[str]) -> Optional[AuthInfo]:
48+
...
49+
50+
@classmethod
51+
@abstractmethod
52+
def save_auth_info(cls, url: str, username: str, password: str) -> None:
53+
...
54+
55+
56+
class KeyRingPythonProvider(KeyRingBaseProvider):
57+
"""Keyring interface which uses locally imported `keyring`"""
58+
59+
try:
60+
import keyring
61+
62+
keyring = keyring
63+
except ImportError:
64+
keyring = None # type: ignore[assignment]
65+
66+
@classmethod
67+
def is_available(cls) -> bool:
68+
return cls.keyring is not None
69+
70+
@classmethod
71+
def get_auth_info(cls, url: str, username: Optional[str]) -> Optional[AuthInfo]:
72+
if cls.is_available is False:
73+
return None
74+
75+
# Support keyring's get_credential interface which supports getting
76+
# credentials without a username. This is only available for
77+
# keyring>=15.2.0.
78+
if hasattr(cls.keyring, "get_credential"):
79+
logger.debug("Getting credentials from keyring for %s", url)
80+
cred = cls.keyring.get_credential(url, username)
81+
if cred is not None:
82+
return cred.username, cred.password
83+
return None
84+
85+
if username is not None:
86+
logger.debug("Getting password from keyring for %s", url)
87+
password = cls.keyring.get_password(url, username)
88+
if password:
89+
return username, password
90+
return None
91+
92+
@classmethod
93+
def save_auth_info(cls, url: str, username: str, password: str) -> None:
94+
cls.keyring.set_password(url, username, password)
95+
96+
97+
class KeyRingCliProvider(KeyRingBaseProvider):
98+
"""Provider which uses `keyring` cli
3799
38100
Instead of calling the keyring package installed alongside pip
39101
we call keyring on the command line which will enable pip to
40102
use which ever installation of keyring is available first in
41103
PATH.
42104
"""
43105

44-
def __init__(self, keyring: str) -> None:
45-
self.keyring = keyring
106+
keyring = shutil.which("keyring")
107+
108+
@classmethod
109+
def is_available(cls) -> bool:
110+
return cls.keyring is not None
46111

47-
def get_password(self, service_name: str, username: str) -> Optional[str]:
48-
cmd = [self.keyring, "get", service_name, username]
112+
@classmethod
113+
def get_auth_info(cls, url: str, username: Optional[str]) -> Optional[AuthInfo]:
114+
if cls.is_available is False:
115+
return None
116+
117+
# This is the default implementation of keyring.get_credential
118+
# https://github.com/jaraco/keyring/blob/97689324abcf01bd1793d49063e7ca01e03d7d07/keyring/backend.py#L134-L139
119+
if username is not None:
120+
password = cls._get_password(url, username)
121+
if password is not None:
122+
return username, password
123+
return None
124+
125+
@classmethod
126+
def save_auth_info(cls, url: str, username: str, password: str) -> None:
127+
if not cls.is_available:
128+
raise RuntimeError("keyring is not available")
129+
return cls._set_password(url, username, password)
130+
131+
@classmethod
132+
def _get_password(cls, service_name: str, username: str) -> Optional[str]:
133+
"""Mirror the implemenation of keyring.get_password using cli"""
134+
if cls.keyring is None:
135+
return None
136+
137+
cmd = [cls.keyring, "get", service_name, username]
138+
env = os.environ
139+
env["PYTHONIOENCODING"] = "utf-8"
49140
res = subprocess.run(
50141
cmd,
51142
stdin=subprocess.DEVNULL,
52143
capture_output=True,
53-
env=dict(PYTHONIOENCODING="utf-8"),
144+
env=env,
54145
)
55146
if res.returncode:
56147
return None
57148
return res.stdout.decode("utf-8").strip("\n")
58149

59-
def set_password(self, service_name: str, username: str, password: str) -> None:
60-
cmd = [self.keyring, "set", service_name, username]
150+
@classmethod
151+
def _set_password(cls, service_name: str, username: str, password: str) -> None:
152+
"""Mirror the implemenation of keyring.set_password using cli"""
153+
if cls.keyring is None:
154+
return None
155+
156+
cmd = [cls.keyring, "set", service_name, username]
61157
input_ = password.encode("utf-8") + b"\n"
62-
res = subprocess.run(cmd, input=input_, env=dict(PYTHONIOENCODING="utf-8"))
158+
env = os.environ
159+
env["PYTHONIOENCODING"] = "utf-8"
160+
res = subprocess.run(cmd, input=input_, env=env)
63161
res.check_returncode()
64162
return None
65163

66164

67-
try:
68-
import keyring
69-
except ImportError:
70-
keyring = None # type: ignore[assignment]
71-
keyring_path = shutil.which("keyring")
72-
if keyring_path is not None:
73-
keyring = KeyRingCli(keyring_path) # type: ignore[assignment]
74-
except Exception as exc:
75-
logger.warning(
76-
"Keyring is skipped due to an exception: %s",
77-
str(exc),
78-
)
79-
keyring = None # type: ignore[assignment]
165+
def get_keyring_provider() -> Optional[Type[KeyRingBaseProvider]]:
166+
if KeyRingPythonProvider.is_available():
167+
return KeyRingPythonProvider
168+
if KeyRingCliProvider.is_available():
169+
return KeyRingCliProvider
170+
return None
80171

81172

82173
def get_keyring_auth(url: Optional[str], username: Optional[str]) -> Optional[AuthInfo]:
83174
"""Return the tuple auth for a given url from keyring."""
84-
global keyring
85-
if not url or not keyring:
175+
# Do nothing if no url was provided
176+
if not url:
86177
return None
87178

88-
try:
89-
try:
90-
get_credential = keyring.get_credential
91-
except AttributeError:
92-
pass
93-
else:
94-
logger.debug("Getting credentials from keyring for %s", url)
95-
cred = get_credential(url, username)
96-
if cred is not None:
97-
return cred.username, cred.password
98-
return None
99-
100-
if username:
101-
logger.debug("Getting password from keyring for %s", url)
102-
password = keyring.get_password(url, username)
103-
if password:
104-
return username, password
179+
keyring = get_keyring_provider()
180+
# Do nothin if keyring is not available
181+
if keyring is None:
182+
return None
105183

106-
except Exception as exc:
107-
logger.warning(
108-
"Keyring is skipped due to an exception: %s",
109-
str(exc),
110-
)
111-
keyring = None # type: ignore[assignment]
112-
return None
184+
return keyring.get_auth_info(url, username)
113185

114186

115187
class MultiDomainBasicAuth(AuthBase):
@@ -283,7 +355,7 @@ def _prompt_for_password(
283355

284356
# Factored out to allow for easy patching in tests
285357
def _should_save_password_to_keyring(self) -> bool:
286-
if not keyring:
358+
if get_keyring_provider() is None:
287359
return False
288360
return ask("Save credentials to keyring [y/N]: ", ["y", "n"]) == "y"
289361

@@ -319,7 +391,7 @@ def handle_401(self, resp: Response, **kwargs: Any) -> Response:
319391
# Prompt to save the password to keyring
320392
if save and self._should_save_password_to_keyring():
321393
self._credentials_to_save = Credentials(
322-
service_name=parsed.netloc,
394+
url=parsed.netloc,
323395
username=username,
324396
password=password,
325397
)
@@ -355,15 +427,16 @@ def warn_on_401(self, resp: Response, **kwargs: Any) -> None:
355427

356428
def save_credentials(self, resp: Response, **kwargs: Any) -> None:
357429
"""Response callback to save credentials on success."""
430+
keyring = get_keyring_provider()
358431
assert keyring is not None, "should never reach here without keyring"
359432
if not keyring:
360-
return
433+
return None
361434

362435
creds = self._credentials_to_save
363436
self._credentials_to_save = None
364437
if creds and resp.status_code < 400:
365438
try:
366439
logger.info("Saving credentials to keyring")
367-
keyring.set_password(creds.service_name, creds.username, creds.password)
440+
keyring.save_auth_info(creds.url, creds.username, creds.password)
368441
except Exception:
369442
logger.exception("Failed to save credentials")

0 commit comments

Comments
 (0)