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

Handle 403 response from secondary repos #3608

Closed
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions docs/docs/repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ a custom certificate authority or client certificates, similarly refer to the ex
`certificates` section. Poetry will use these values to authenticate to your private repository when downloading or
looking for packages.

401 or 403 responses from secondary repositories will generate a warning
message but are not considered as fatal, as some types of private repository
servers may return a 403 if a request is made for a package not present on the
server.

### Disabling the PyPI repository

Expand Down
1 change: 1 addition & 0 deletions poetry/repositories/legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ def _get(self, endpoint: str) -> Optional[Page]:
response = self.session.get(url)
if response.status_code == 404:
return

response.raise_for_status()
except requests.HTTPError as e:
raise RepositoryError(e)
Expand Down
22 changes: 20 additions & 2 deletions poetry/repositories/pool.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import logging

from typing import TYPE_CHECKING
from typing import Dict
from typing import List
from typing import Optional

from .base_repository import BaseRepository
from .exceptions import PackageNotFound
from .exceptions import RepositoryError
from .repository import Repository


Expand All @@ -13,6 +16,9 @@
from poetry.core.packages import Package


logger = logging.getLogger(__name__)


class Pool(BaseRepository):
def __init__(
self,
Expand Down Expand Up @@ -162,8 +168,16 @@ def find_packages(self, dependency: "Dependency") -> List["Package"]:
return self.repository(repository).find_packages(dependency)

packages = []
for repo in self._repositories:
packages += repo.find_packages(dependency)
for (idx, repo) in enumerate(self._repositories):
try:
packages += repo.find_packages(dependency)
except RepositoryError:
if idx < self._secondary_start_idx:
raise
self._log(
"error checking secondary repository {}".format(repo.name),
level="warning",
)

return packages

Expand All @@ -178,3 +192,7 @@ def search(self, query: str) -> List["Package"]:
results += repository.search(query)

return results

@classmethod
def _log(cls, msg, level="info"):
getattr(logger, level)("<debug>{}:</debug> {}".format(cls.__name__, msg))
63 changes: 63 additions & 0 deletions tests/repositories/test_pool.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
import shutil

from pathlib import Path
from urllib.parse import urlparse

import pytest

from poetry.factory import Factory
from poetry.repositories import Pool
from poetry.repositories import Repository
from poetry.repositories.exceptions import PackageNotFound
from poetry.repositories.exceptions import RepositoryError
from poetry.repositories.legacy_repository import LegacyRepository
from poetry.repositories.legacy_repository import Page


class MockRepository(LegacyRepository):

FIXTURES = Path(__file__).parent / "fixtures" / "legacy"

def __init__(self):
super(MockRepository, self).__init__(
"legacy", url="http://legacy.foo.bar", disable_cache=True
)

def _get(self, endpoint):
parts = endpoint.split("/")
name = parts[1]

fixture = self.FIXTURES / (name + ".html")
if not fixture.exists():
return

with fixture.open(encoding="utf-8") as f:
return Page(self._url + endpoint, f.read(), {})

def _download(self, url, dest):
filename = urlparse(url).path.rsplit("/")[-1]
filepath = self.FIXTURES.parent / "pypi.org" / "dists" / filename

shutil.copyfile(str(filepath), dest)


class ErrorRepository(MockRepository):
def _get(self, endpoint):
raise RepositoryError()


def test_pool_raises_package_not_found_when_no_package_is_found():
Expand Down Expand Up @@ -69,3 +109,26 @@ def test_repository_with_normal_default_and_secondary_repositories():
assert pool.repository("foo") is repo1
assert pool.repository("bar") is repo2
assert pool.has_default()


def test_default_repository_failure():
default = ErrorRepository()
secondary = MockRepository()
pool = Pool()
pool.add_repository(secondary, secondary=True)
pool.add_repository(default, default=True)

with pytest.raises(RepositoryError):
pool.find_packages(Factory.create_dependency("pyyaml", "*"))


def test_secondary_repository_failure():
secondary = ErrorRepository()
default = MockRepository()
pool = Pool()
pool.add_repository(secondary, secondary=True)
pool.add_repository(default, default=True)

packages = pool.find_packages(Factory.create_dependency("pyyaml", "*"))

assert len(packages) == 1