Skip to content
Draft
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 CHANGES/1428.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed pull-through links serving a package in place of its metadata, or the other way around. Pull-through links have a new format, refresh cached simple index pages to replace old links.
1 change: 1 addition & 0 deletions CHANGES/1453.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed pull-through links serving a package in place of its metadata, or the other way around. Pull-through links have a new format, refresh cached simple index pages to replace old links.
14 changes: 3 additions & 11 deletions pulp_python/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

from .provenance import Provenance
from .utils import (
PULL_THROUGH_PARAM,
PYPI_LAST_SERIAL,
PYPI_SERIAL_CONSTANT,
artifact_to_metadata_artifact,
Expand Down Expand Up @@ -364,15 +363,10 @@ def get_remote_artifact_url(self, relative_path=None, request=None):
"""Get url for remote_artifact, content app requests need a signed pull-through token."""
if request is None:
return super().get_remote_artifact_url(relative_path)
if not (token := request.query.get(PULL_THROUGH_PARAM)):
return None
# Pull-through links are <token>/<filename>, relative_path is also the package's
# metadata file when pulpcore saves the downloaded package
token = PurePath(request.path).parent.name
filename = PurePath(relative_path or "").name
# pip adds ".metadata" to the end of the url (the token) instead of the url path
if token.endswith(".metadata"):
token = token.removesuffix(".metadata")
if not filename.endswith(".metadata"):
filename = f"{filename}.metadata"
setattr(self, "_real_relative_path", filename)
if resolved := unsign_pull_through_token(self, token, filename):
url, sha256 = resolved
if sha256:
Expand All @@ -382,8 +376,6 @@ def get_remote_artifact_url(self, relative_path=None, request=None):

def get_remote_artifact_content_type(self, relative_path=None):
"""Return PythonPackageContent, except for metadata artifacts."""
if hasattr(self, "_real_relative_path"):
relative_path = getattr(self, "_real_relative_path")
if relative_path and relative_path.endswith(".metadata"):
return None
return PythonPackageContent
Expand Down
8 changes: 5 additions & 3 deletions pulp_python/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

SIMPLE_API_VERSION = "1.1"

PULL_THROUGH_PARAM = "pt"
PULL_THROUGH_SALT = "pulp_python.pull_through"

PYPI_SIMPLE_V1_HTML = "application/vnd.pypi.simple.v1+html"
Expand Down Expand Up @@ -789,8 +788,11 @@ def sign_pull_through_path(remote, filename, url, sha256=None, metadata_sha256=N
data["s"] = sha256
if metadata_sha256:
data["m"] = metadata_sha256
token = _pull_through_signer(remote).sign_object(data, compress=True)
return f"{filename}?{PULL_THROUGH_PARAM}={token}"
# Uncompressed tokens never start with ".", which would make a hidden path segment
token = _pull_through_signer(remote).sign_object(data)
# The token goes in the path, so ".metadata" added by clients (pip adds it to the end of the
# whole url) gives the metadata its own path, apart from the package
return f"{token}/{filename}"


def unsign_pull_through_token(remote, token, filename):
Expand Down
98 changes: 88 additions & 10 deletions pulp_python/tests/functional/api/test_full_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
)


def pull_through_token(distro, url):
"""Returns the signed token of a pull-through link, None for a link to local content."""
parent = urlsplit(url).path.rpartition("/")[0]
if parent.endswith(f"/{distro.base_path}"):
return None
return parent.rpartition("/")[2]


def test_pull_through_install(
python_bindings, python_remote_factory, python_distribution_factory, delete_orphans_pre
):
Expand Down Expand Up @@ -54,8 +62,10 @@ def test_pull_through_simple(python_remote_factory, python_distribution_factory,
assert len(project_page.packages) == 2
for package in project_page.packages:
assert package.filename in PYTHON_XS_FIXTURE_CHECKSUMS
relative_path = f"{distro.base_path}/{package.filename}?pt="
assert urljoin(pulp_content_url, relative_path) in package.url
token = pull_through_token(distro, package.url)
assert token
relative_path = f"{distro.base_path}/{token}/{package.filename}"
assert urljoin(pulp_content_url, relative_path) == package.url
assert PYTHON_XS_FIXTURE_CHECKSUMS[package.filename] == package.digests["sha256"]


Expand Down Expand Up @@ -134,7 +144,7 @@ def test_pull_through_with_repo(
url = urljoin(distro.base_url, "simple/aiohttp/")
project_page = ProjectPage.from_response(requests.get(url), "aiohttp")
for package in sample(project_page.packages, 3):
assert "?pt=" in package.url
assert pull_through_token(distro, package.url)
r = requests.get(package.url)
assert r.status_code == 200

Expand Down Expand Up @@ -172,12 +182,14 @@ def test_pull_through_local_only(
url = f"{distro.base_url}simple/pulpcore/"
r = requests.get(url)
assert r.status_code == 200
assert "?pt=" not in r.text
packages = ProjectPage.from_response(r, "pulpcore").packages
assert not any(pull_through_token(distro, p.url) for p in packages)

url = f"{distro.base_url}simple/shelf-reader/"
r = requests.get(url)
assert r.status_code == 200
assert "?pt=" in r.text
packages = ProjectPage.from_response(r, "shelf-reader").packages
assert all(pull_through_token(distro, p.url) for p in packages)

url = f"{distro.base_url}simple/pulp_python/"
r = requests.get(url)
Expand Down Expand Up @@ -239,6 +251,70 @@ def test_pull_through_metadata(python_remote_factory, python_distribution_factor
assert sha256(r.content).hexdigest() == package2.metadata_digests["sha256"]


@pytest.mark.parallel
def test_pull_through_metadata_cached_package(python_remote_factory, python_distribution_factory):
"""
Tests that a package and its metadata never get served for each other (#1428).

Only reproduces #1428 with CACHE_ENABLED.
"""
remote = python_remote_factory(includes=["pytz"])
distro = python_distribution_factory(remote=remote.pulp_href)

url = f"{distro.base_url}simple/pytz/"
project_page = ProjectPage.from_response(requests.get(url), "pytz")
filename1 = "pytz-2023.2-py2.py3-none-any.whl"
filename2 = "pytz-2023.3-py2.py3-none-any.whl"
package1 = next(p for p in project_page.packages if p.filename == filename1)
package2 = next(p for p in project_page.packages if p.filename == filename2)

# The package first, then its metadata the way pip asks for it
r = requests.get(package1.url)
assert sha256(r.content).hexdigest() == package1.digests["sha256"]
r = requests.get(f"{package1.url}.metadata")
assert sha256(r.content).hexdigest() == package1.metadata_digests["sha256"]

# The metadata first, then the package
r = requests.get(f"{package2.url}.metadata")
assert sha256(r.content).hexdigest() == package2.metadata_digests["sha256"]
r = requests.get(package2.url)
assert sha256(r.content).hexdigest() == package2.digests["sha256"]


@pytest.mark.parallel
def test_pull_through_metadata_after_package_added(
python_bindings,
pulpcore_bindings,
python_repo_factory,
python_remote_factory,
python_distribution_factory,
monitor_task,
):
"""Tests that a pull-through link serves metadata after its package is added (#1453)."""
remote = python_remote_factory(url=PYPI_URL, includes=["pip"])
repo = python_repo_factory()
distro = python_distribution_factory(repository=repo.pulp_href, remote=remote.pulp_href)

url = f"{distro.base_url}simple/pip/"
project_page = ProjectPage.from_response(requests.get(url), "pip")
filename = "pip-26.0.1-py3-none-any.whl"
package = next(p for p in project_page.packages if p.filename == filename)
assert package.has_metadata

# Another client downloads the package, which adds it to the repository
r = requests.get(package.url)
assert r.status_code == 200
for task in pulpcore_bindings.TasksApi.list(reserved_resources=repo.prn).results:
monitor_task(task.pulp_href)
repo = python_bindings.RepositoriesPythonApi.read(repo.pulp_href)
assert repo.latest_version_href[-2] == "1"

# The first client asks for the metadata through its link, the way pip does
r = requests.get(f"{package.url}.metadata")
assert r.status_code == 200
assert sha256(r.content).hexdigest() == package.metadata_digests["sha256"]


@pytest.mark.parallel
def test_pull_through_rejects_bad_links(python_remote_factory, python_distribution_factory):
"""Tests that pull-through only downloads files from links signed for the distro's remote."""
Expand All @@ -249,15 +325,17 @@ def test_pull_through_rejects_bad_links(python_remote_factory, python_distributi

url = f"{distro.base_url}simple/shelf-reader/"
package1, package2 = ProjectPage.from_response(requests.get(url), "shelf-reader").packages
token1 = package1.url.split("?pt=")[1]
token1 = pull_through_token(distro, package1.url)
token2 = pull_through_token(distro, package2.url)
assert token1 and token2

# Tampered token
tampered = token1[:-1] + ("A" if token1[-1] != "A" else "B")
r = requests.get(package1.url.replace(token1, tampered))
assert r.status_code == 404

# Token used for another file
r = requests.get(package2.url.replace(package2.url.split("?pt=")[1], token1))
r = requests.get(package2.url.replace(token2, token1))
assert r.status_code == 404

# Token used on a distribution with another remote
Expand Down Expand Up @@ -320,7 +398,7 @@ def test_pull_through_metadata_with_repo(
filename = "pip-26.0.1-py3-none-any.whl"
package = next(p for p in project_page.packages if p.filename == filename)
assert package.has_metadata
assert "?pt=" in package.url
assert pull_through_token(distro, package.url)

# Retrieve the metadata and assert the content was not saved to the repository
parts = urlsplit(package.url)
Expand All @@ -331,7 +409,7 @@ def test_pull_through_metadata_with_repo(
project_page = ProjectPage.from_response(requests.get(pip_url), "pip")
package = next(p for p in project_page.packages if p.filename == filename)
assert package.has_metadata
assert "?pt=" in package.url
assert pull_through_token(distro, package.url)

# Now retrieve the package and assert the content was saved with metadata
r = requests.get(package.url)
Expand All @@ -344,7 +422,7 @@ def test_pull_through_metadata_with_repo(
# Check the simple page is updated to point to the local repository
project_page = ProjectPage.from_response(requests.get(pip_url), "pip")
package = next(p for p in project_page.packages if p.filename == filename)
assert "?pt=" not in package.url
assert not pull_through_token(distro, package.url)
r = requests.get(package.metadata_url)
assert r.status_code == 200
assert sha256(r.content).hexdigest() == package.metadata_digests["sha256"]
3 changes: 1 addition & 2 deletions pulp_python/tests/unit/test_pull_through.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pytest

from pulp_python.app.utils import (
PULL_THROUGH_PARAM,
sign_pull_through_path,
unsign_pull_through_token,
)
Expand All @@ -23,7 +22,7 @@ def _remote(**kwargs):

def _token(remote, **kwargs):
path = sign_pull_through_path(remote, FILENAME, URL, **kwargs)
filename, token = path.split(f"?{PULL_THROUGH_PARAM}=")
token, filename = path.split("/")
assert filename == FILENAME
return token

Expand Down
Loading