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

Lazy load credentials for legacy repositories #4937

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
29 changes: 21 additions & 8 deletions src/poetry/repositories/legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,6 @@ def __init__(
self._authenticator.session, cache=FileCache(str(self._cache_dir / "_http"))
)

username, password = self._authenticator.get_credentials_for_url(self._url)
if username is not None and password is not None:
self._authenticator.session.auth = requests.auth.HTTPBasicAuth(
username, password
)

if self._cert:
self._authenticator.session.verify = str(self._cert)

Expand All @@ -212,9 +206,19 @@ def client_cert(self) -> Path | None:

@property
def authenticated_url(self) -> str:
if not self._session.auth:
return self.url
"""
Set authentication credentials and return a URL with basic auth
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code path is only used for uploads not for requests etc. see Authenticator for details.


Use self._set_authentication_creds() and self.session.get() directly
if you want to send a request to the repository server.
This function is only usefull if you need the string representation
of the basic auth URL. Be aware that it contains the credential in
plain text.
"""
try:
self._set_authentication_creds()
except ValueError:
return self.url
parsed = urllib.parse.urlparse(self.url)
username = quote(self._session.auth.username, safe="")
password = quote(self._session.auth.password, safe="")
Expand Down Expand Up @@ -394,9 +398,18 @@ def _get_release_info(self, name: str, version: str) -> dict:

return data.asdict()

def _set_authentication_creds(self) -> None:
username, password = self._authenticator.get_credentials_for_url(self._url)
if username is None or password is None:
raise ValueError
self._authenticator.session.auth = requests.auth.HTTPBasicAuth(
username, password
)

def _get_page(self, endpoint: str) -> Page | None:
url = self._url + endpoint
try:
self._set_authentication_creds()
response = self.session.get(url)
if response.status_code in (401, 403):
self._log(
Expand Down
6 changes: 4 additions & 2 deletions tests/installation/test_chooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ def pool() -> Pool:

pool.add_repository(PyPiRepository(disable_cache=True))
pool.add_repository(
LegacyRepository("foo", "https://foo.bar/simple/", disable_cache=True)
LegacyRepository(
"foo", "https://user1:password1@foo.bar/simple/", disable_cache=True
)
)

return pool
Expand All @@ -113,7 +115,7 @@ def test_chooser_chooses_universal_wheel_link_if_available(
package.version.text,
source_type="legacy",
source_reference="foo",
source_url="https://foo.bar/simple/",
source_url="https://user1:password1@foo.bar/simple/",
)

link = chooser.choose_for(package)
Expand Down
2 changes: 1 addition & 1 deletion tests/repositories/test_legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def test_get_package_retrieves_packages_with_no_hashes():

class MockHttpRepository(LegacyRepository):
def __init__(self, endpoint_responses: dict, http: type[httpretty.httpretty]):
base_url = "http://legacy.foo.bar"
base_url = "http://testuser1:password1@legacy.foo.bar"
super().__init__("legacy", url=base_url, disable_cache=True)

for endpoint, response in endpoint_responses.items():
Expand Down