Skip to content

Commit

Permalink
repos: configure source for credential lookup
Browse files Browse the repository at this point in the history
With this change users no longer need to configure source url
explicitly via `repositories` configuration values when credentials are
required.
  • Loading branch information
abn authored and neersighted committed May 8, 2022
1 parent 217599b commit b600549
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
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 @@ -322,6 +322,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}"

0 comments on commit b600549

Please sign in to comment.