Skip to content

Commit

Permalink
When one keyring attempt fails, don't bother with more
Browse files Browse the repository at this point in the history
This makes pypa#8090 much less painful.
  • Loading branch information
hroncok committed Aug 3, 2020
1 parent 89d8cba commit ba062c3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions news/8090.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Only attempt to use the keyring once and if it fails, don't try again.
This prevents spamming users with several keyring unlock prompts when they
cannot unlock or don't want to do so.
2 changes: 2 additions & 0 deletions src/pip/_internal/network/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
def get_keyring_auth(url, username):
# type: (str, str) -> Optional[AuthInfo]
"""Return the tuple auth for a given url from keyring."""
global keyring
if not url or not keyring:
return None

Expand All @@ -69,6 +70,7 @@ def get_keyring_auth(url, username):
logger.warning(
"Keyring is skipped due to an exception: %s", str(exc),
)
keyring = None
return None


Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_network_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,29 @@ def test_keyring_get_credential(monkeypatch, url, expect):
assert auth._get_new_credentials(
url, allow_netrc=False, allow_keyring=True
) == expect


class KeyringModuleBroken(object):
"""Represents the current supported API of keyring, but broken"""

def __init__(self):
self._call_count = 0

def get_credential(self, system, username):
self._call_count += 1
raise Exception("This keyring is broken!")


def test_broken_keyring_disables_keyring(monkeypatch):
keyring_broken = KeyringModuleBroken()
monkeypatch.setattr(pip._internal.network.auth, 'keyring', keyring_broken)

auth = MultiDomainBasicAuth(index_urls=["http://example.com/"])

assert keyring_broken._call_count == 0
for i in range(5):
url = "http://example.com/path" + str(i)
assert auth._get_new_credentials(
url, allow_netrc=False, allow_keyring=True
) == (None, None)
assert keyring_broken._call_count == 1

0 comments on commit ba062c3

Please sign in to comment.