Skip to content

Commit

Permalink
Allow source files to publish at by-hash paths
Browse files Browse the repository at this point in the history
Closes pulp#1059
  • Loading branch information
Andrew Cheng authored and daviddavis committed Jul 12, 2024
1 parent 4bd91fd commit 2fbe0dd
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ VARSYAML

cat >> vars/main.yaml << VARSYAML
pulp_env: {}
pulp_settings: {"allowed_content_checksums": ["md5", "sha1", "sha256", "sha512"], "allowed_export_paths": ["/tmp"], "allowed_import_paths": ["/tmp"]}
pulp_settings: {"allowed_content_checksums": ["md5", "sha1", "sha256", "sha512"], "allowed_export_paths": ["/tmp"], "allowed_import_paths": ["/tmp"], "apt_by_hash": true}
pulp_scheme: https
pulp_default_container: ghcr.io/pulp/pulp-ci-centos9:latest
VARSYAML
Expand Down
1 change: 1 addition & 0 deletions CHANGES/1059.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Extend publishing at by-hash paths to source files.
2 changes: 1 addition & 1 deletion pulp_deb/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
http://docs.pulpproject.org/en/3.0/nightly/plugins/plugin-writer/index.html
"""

APT_BY_HASH = False
APT_BY_HASH = True
FORBIDDEN_CHECKSUM_WARNINGS = True
FORCE_IGNORE_MISSING_PACKAGE_INDICES = False
38 changes: 25 additions & 13 deletions pulp_deb/app/tasks/publishing.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,19 +356,9 @@ def finish(self):

# Generating metadata files using checksum
if settings.APT_BY_HASH:
for path, index in (
(package_index_path, package_index),
(gz_package_index_path, gz_package_index),
):
for checksum in settings.ALLOWED_CONTENT_CHECKSUMS:
if checksum in CHECKSUM_TYPE_MAP:
hashed_index_path = _fetch_file_checksum(path, index, checksum)
hashed_index = PublishedMetadata.create_from_file(
publication=self.parent.publication,
file=File(open(path, "rb")),
relative_path=hashed_index_path,
)
hashed_index.save()
self.generate_by_hash(
package_index_path, package_index, gz_package_index_path, gz_package_index
)

self.parent.add_metadata(package_index)
self.parent.add_metadata(gz_package_index)
Expand All @@ -385,9 +375,31 @@ def finish(self):
publication=self.parent.publication, file=File(open(gz_source_index_path, "rb"))
)
gz_source_index.save()

# Generating metadata files using checksum
if settings.APT_BY_HASH:
self.generate_by_hash(
source_index_path, source_index, gz_source_index_path, gz_source_index
)

self.parent.add_metadata(source_index)
self.parent.add_metadata(gz_source_index)

def generate_by_hash(self, index_path, index, gz_index_path, gz_index):
for path, index in (
(index_path, index),
(gz_index_path, gz_index),
):
for checksum in settings.ALLOWED_CONTENT_CHECKSUMS:
if checksum in CHECKSUM_TYPE_MAP:
hashed_index_path = _fetch_file_checksum(path, index, checksum)
hashed_index = PublishedMetadata.create_from_file(
publication=self.parent.publication,
file=File(open(path, "rb")),
relative_path=hashed_index_path,
)
hashed_index.save()


class _ReleaseHelper:
def __init__(
Expand Down
47 changes: 47 additions & 0 deletions pulp_deb/tests/functional/api/test_download_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
import os
import pytest
import hashlib
import re
from random import choice
from urllib.parse import urljoin

from pulp_deb.tests.functional.constants import (
DEB_FIXTURE_STANDARD_REPOSITORY_NAME,
DEB_FIXTURE_SINGLE_DIST,
DEB_GENERIC_CONTENT_NAME,
DEB_PACKAGE_NAME,
DEB_PACKAGE_RELEASE_COMPONENT_NAME,
DEB_RELEASE_COMPONENT_NAME,
DEB_RELEASE_FILE_NAME,
)
from pulpcore.tests.functional.utils import PulpTaskError


@pytest.fixture
Expand Down Expand Up @@ -204,3 +207,47 @@ def test_download_cached_content(
content_hash = hashlib.sha256(content).hexdigest()

assert package_hash == content_hash


@pytest.mark.parallel
def test_apt_by_hash(
deb_init_and_sync,
deb_publication_factory,
deb_distribution_factory,
deb_get_content_types,
download_content_unit,
):
"""Verify that deb and deb source content is available in the by-hash path."""
# Create/sync a repo and then do a publish and create a distro
repo, _ = deb_init_and_sync(remote_args={"sync_sources": True})
deb_publication_factory(repo, structured=True, simple=True)
try:
distribution = deb_distribution_factory(repository=repo)
except PulpTaskError:
print(PulpTaskError.task.to_dict())
raise

# Obtain the Release file and parse out the sha256
release_metadata = deb_get_content_types(
"apt_release_file_api", DEB_RELEASE_FILE_NAME, repo, repo.latest_version_href
)
single_release_metadata = next(
release for release in release_metadata if release.distribution == DEB_FIXTURE_SINGLE_DIST
)
release_file_path = next(
key for key in single_release_metadata.artifacts.keys() if key.endswith("/Release")
)
release_file = download_content_unit(distribution.base_path, release_file_path).decode("utf-8")
sha256_section = release_file.split("SHA256:")[1].split("SHA512:")[0].strip()
sha256_pattern = re.compile(
r"([a-fA-F0-9]{64})\s+\d+\s+([^/\s]+/[^/\s]+)/(Packages|Sources)(?:\s|\n)"
)
matches = sha256_pattern.findall(sha256_section)
sha256_dict = {path: sha for sha, path, _ in matches}

# Verify that all by-hash files are available
for path, sha256 in sha256_dict.items():
content_url = f"dists/{DEB_FIXTURE_SINGLE_DIST}/{path}/by-hash/SHA256/{sha256}"
assert "404" not in download_content_unit(distribution.base_path, content_url).decode(
"utf-8"
)
3 changes: 2 additions & 1 deletion template_config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This config represents the latest values used when running the plugin-template. Any settings that
# were not present before running plugin-template have been added with their default values.

# generated with [email protected]347-gc4a2504
# generated with [email protected]349-gba81617

api_root: /pulp/
black: true
Expand Down Expand Up @@ -58,6 +58,7 @@ pulp_settings:
- /tmp
allowed_import_paths:
- /tmp
apt_by_hash: true
pulp_settings_azure: null
pulp_settings_gcp: null
pulp_settings_s3: null
Expand Down

0 comments on commit 2fbe0dd

Please sign in to comment.