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

repos: configure source for credential lookup #5563

Merged
merged 1 commit into from
May 8, 2022
Merged
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 src/poetry/repositories/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(
cache_id=name,
disable_cache=disable_cache,
)
self._authenticator.add_repository(name, url)

@property
def session(self) -> Authenticator:
Expand Down
8 changes: 8 additions & 0 deletions src/poetry/utils/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@ def configured_repositories(self) -> dict[str, AuthenticatorRepositoryConfig]:

return self._configured_repositories

def reset_credentials_cache(self) -> None:
self.get_repository_config_for_url.cache_clear()
self._credentials = {}

def add_repository(self, name: str, url: str) -> None:
self.configured_repositories[name] = AuthenticatorRepositoryConfig(name, url)
self.reset_credentials_cache()

def get_certs_for_url(self, url: str) -> dict[str, Path | None]:
if url not in self._certs:
self._certs[url] = self._get_certs_for_url(url)
Expand Down
43 changes: 43 additions & 0 deletions tests/repositories/test_legacy_repository.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import base64
import re
import shutil

from pathlib import Path
Expand Down Expand Up @@ -27,6 +29,8 @@

from _pytest.monkeypatch import MonkeyPatch

from poetry.config.config import Config


@pytest.fixture(autouse=True)
def _use_simple_keyring(with_simple_keyring: None) -> None:
Expand Down Expand Up @@ -418,3 +422,42 @@ def get_mock(url: str, raise_for_status: bool = True) -> requests.Response:

monkeypatch.setattr(repo.session, "get", get_mock)
assert repo._get_page("/foo")._url == "http://legacy.redirect.bar/foo/"


@pytest.mark.parametrize(
("repositories",),
[
({},),
# ensure path is respected
({"publish": {"url": "https://foo.bar/legacy"}},),
# ensure path length does not give incorrect results
({"publish": {"url": "https://foo.bar/upload/legacy"}},),
],
)
def test_authenticator_with_implicit_repository_configuration(
http: type[httpretty.httpretty],
config: Config,
repositories: dict[str, dict[str, str]],
) -> None:
http.register_uri(
http.GET,
re.compile("^https?://foo.bar/(.+?)$"),
)

config.merge(
{
"repositories": repositories,
"http-basic": {
"source": {"username": "foo", "password": "bar"},
"publish": {"username": "baz", "password": "qux"},
},
}
)

repo = LegacyRepository(name="source", url="https://foo.bar/simple", config=config)
repo._get_page("/foo")

request = http.last_request()

basic_auth = base64.b64encode(b"foo:bar").decode()
assert request.headers["Authorization"] == f"Basic {basic_auth}"
36 changes: 36 additions & 0 deletions tests/utils/test_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,39 @@ def test_authenticator_azure_feed_guid_credentials(

basic_auth = base64.b64encode(b"baz:qux").decode()
assert request.headers["Authorization"] == f"Basic {basic_auth}"


def test_authenticator_add_repository(
config: Config,
mock_remote: None,
http: type[httpretty.httpretty],
with_simple_keyring: None,
dummy_keyring: DummyBackend,
):
config.merge(
{
"http-basic": {
"source": {"username": "foo", "password": "bar"},
},
}
)

authenticator = Authenticator(config, NullIO())

authenticator.request(
"get",
"https://foo.bar/simple/a/1.0.0/a-1.0.0.whl",
)
request = http.last_request()
assert "Authorization" not in request.headers

authenticator.add_repository("source", "https://foo.bar/simple/")

authenticator.request(
"get",
"https://foo.bar/simple/a/1.0.0/a-1.0.0.whl",
)
request = http.last_request()

basic_auth = base64.b64encode(b"foo:bar").decode()
assert request.headers["Authorization"] == f"Basic {basic_auth}"