Skip to content
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

Check both index URL and its hostname for keyring entries #8635

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/8634.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check both index URL and its hostname for entries in the keyring
19 changes: 12 additions & 7 deletions src/pip/_internal/network/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,19 @@ def _get_new_credentials(self, original_url, allow_netrc=True,

# If we don't have a password and keyring is available, use it.
if allow_keyring:
# The index url is more specific than the netloc, so try it first
kr_auth = (
get_keyring_auth(index_url, username) or
get_keyring_auth(netloc, username)
)
if kr_auth:
url_kr = get_keyring_auth(index_url, username)
netloc_kr = get_keyring_auth(netloc, username)
# When using SecretService on keyring >= 19.2.0, a null-password
# credential is always returned, so check the netloc result as well
if (not url_kr and netloc_kr) or (
url_kr and netloc_kr and netloc_kr[1] and not url_kr[1]
):
logger.debug("Found credentials in keyring for %s", netloc)
return kr_auth
return netloc_kr
elif url_kr:
# The index url is more specific, so it takes priority
logger.debug("Found credentials in keyring for %s", url)
return url_kr

return username, password

Expand Down
14 changes: 11 additions & 3 deletions tests/unit/test_network_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ def _send(sent_req, **kwargs):
class KeyringModuleV2:
"""Represents the current supported API of keyring"""

return_username = False

class Credential:
def __init__(self, username, password):
self.username = username
Expand All @@ -225,18 +227,24 @@ def get_credential(self, system, username):
return self.Credential("username", "url")
if system == "example.com":
return self.Credential("username", "netloc")
if self.return_username:
# When using SecretService on keyring >= 19.2.0, a null-password
# credential is always returned
return self.Credential(username, None)
return None


@pytest.mark.parametrize('url, expect', (
("http://example.com/path1", ("username", "netloc")),
("http://example.com/path2/path3", ("username", "url")),
("http://user2@example.com/path2/path3", ("username", "url")),
("http://ss_user@example.com", ("username", "netloc")),
("http://ss_user@example2.com", ("ss_user", None)),
))
def test_keyring_get_credential(monkeypatch, url, expect):
monkeypatch.setattr(
pip._internal.network.auth, 'keyring', KeyringModuleV2()
)
keyring = KeyringModuleV2()
keyring.return_username = "ss_user" in url
monkeypatch.setattr(pip._internal.network.auth, 'keyring', keyring)
auth = MultiDomainBasicAuth(index_urls=["http://example.com/path2"])

assert auth._get_new_credentials(
Expand Down