Commit b6005497 by Arun Babu Neelicattu Committed by Bjorn Neergaard

repos: configure source for credential lookup

With this change users no longer need to configure source url
explicitly via `repositories` configuration values when credentials are
required.
parent 217599b9
......@@ -47,6 +47,7 @@ class HTTPRepository(CachedRepository, ABC):
cache_id=name,
disable_cache=disable_cache,
)
self._authenticator.add_repository(name, url)
@property
def session(self) -> Authenticator:
......
......@@ -322,6 +322,14 @@ class Authenticator:
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)
......
from __future__ import annotations
import base64
import re
import shutil
from pathlib import Path
......@@ -27,6 +29,8 @@ if TYPE_CHECKING:
from _pytest.monkeypatch import MonkeyPatch
from poetry.config.config import Config
@pytest.fixture(autouse=True)
def _use_simple_keyring(with_simple_keyring: None) -> None:
......@@ -418,3 +422,42 @@ def test_get_redirected_response_url(
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}"
......@@ -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}"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment