Skip to content

Commit

Permalink
Deprecate 'package_checksum_type' and 'metadata_checksum_type'
Browse files Browse the repository at this point in the history
closes #2480
  • Loading branch information
dralley committed Nov 30, 2023
1 parent 0227136 commit 7e4dc1a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGES/2480.deprecation
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Deprecated the 'package_checksum_type' and 'metadata_checksum_type' options on the `RpmRepository`
and `RpmPublication` models. A 'checksum_type' argument will be provided instead, which will
control both settings (but they will no longer be individually controllable).
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 4.2.5 on 2023-11-07 03:51

from django.db import migrations, models
from django.db.models import F


def set_publication_checksum(apps, schema_editor):
RpmPublication = apps.get_model("rpm", "RpmPublication")
RpmPublication.objects.update(checksum_type=F("metadata_checksum_type"))


class Migration(migrations.Migration):

dependencies = [
('rpm', '0056_remove_rpmpublication_sqlite_metadata_and_more'),
]

operations = [
migrations.AddField(
model_name='rpmpublication',
name='checksum_type',
field=models.TextField(choices=[('unknown', 'unknown'), ('md5', 'md5'), ('sha1', 'sha1'), ('sha1', 'sha1'), ('sha224', 'sha224'), ('sha256', 'sha256'), ('sha384', 'sha384'), ('sha512', 'sha512')], null=True),
),
migrations.RunPython(set_publication_checksum),
migrations.AlterField(
model_name='rpmpublication',
name='checksum_type',
field=models.TextField(choices=[('unknown', 'unknown'), ('md5', 'md5'), ('sha1', 'sha1'), ('sha1', 'sha1'), ('sha224', 'sha224'), ('sha256', 'sha256'), ('sha384', 'sha384'), ('sha512', 'sha512')]),
),
migrations.AddField(
model_name='rpmrepository',
name='checksum_type',
field=models.TextField(choices=[('unknown', 'unknown'), ('md5', 'md5'), ('sha1', 'sha1'), ('sha1', 'sha1'), ('sha224', 'sha224'), ('sha256', 'sha256'), ('sha384', 'sha384'), ('sha512', 'sha512')], null=True),
),
]
3 changes: 3 additions & 0 deletions pulp_rpm/app/models/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ class RpmRepository(Repository, AutoAddObjPermsMixin):
retain_package_versions = models.PositiveIntegerField(default=0)

autopublish = models.BooleanField(default=False)
checksum_type = models.TextField(null=True, choices=CHECKSUM_CHOICES)
metadata_checksum_type = models.TextField(null=True, choices=CHECKSUM_CHOICES)
package_checksum_type = models.TextField(null=True, choices=CHECKSUM_CHOICES)
repo_config = models.JSONField(default=dict)
Expand All @@ -248,6 +249,7 @@ def on_new_version(self, version):
checksum_types={
"metadata": self.metadata_checksum_type,
"package": self.package_checksum_type,
"general": self.checksum_type,
},
repo_config=self.repo_config,
)
Expand Down Expand Up @@ -413,6 +415,7 @@ class RpmPublication(Publication, AutoAddObjPermsMixin):
"""

TYPE = "rpm"
checksum_type = models.TextField(choices=CHECKSUM_CHOICES)
metadata_checksum_type = models.TextField(choices=CHECKSUM_CHOICES)
package_checksum_type = models.TextField(choices=CHECKSUM_CHOICES)
repo_config = models.JSONField(default=dict)
Expand Down
52 changes: 47 additions & 5 deletions pulp_rpm/app/serializers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@ class RpmRepositorySerializer(RepositorySerializer):
min_value=0,
required=False,
)
checksum_type = serializers.ChoiceField(
help_text=_("The preferred checksum type during repo publish."),
choices=CHECKSUM_CHOICES,
required=False,
allow_null=True,
)
metadata_checksum_type = serializers.ChoiceField(
help_text=_("The checksum type for metadata."),
help_text=_("DEPRECATED: use CHECKSUM_TYPE instead."),
choices=CHECKSUM_CHOICES,
required=False,
allow_null=True,
)
package_checksum_type = serializers.ChoiceField(
help_text=_("The checksum type for packages."),
help_text=_("DEPRECATED: use CHECKSUM_TYPE instead."),
choices=CHECKSUM_CHOICES,
required=False,
allow_null=True,
Expand Down Expand Up @@ -117,14 +123,28 @@ class RpmRepositorySerializer(RepositorySerializer):

def validate(self, data):
"""Validate data."""
for field in ("metadata_checksum_type", "package_checksum_type"):
for field in ("checksum_type", "metadata_checksum_type", "package_checksum_type"):
if (
field in data
and data[field]
and data[field] not in settings.ALLOWED_CONTENT_CHECKSUMS
):
raise serializers.ValidationError({field: _(ALLOWED_CHECKSUM_ERROR_MSG)})

if data.get("package_checksum_type") or data.get("metadata_checksum_type"):
logging.getLogger("pulp_rpm.deprecation").info(
"Support for '*_checksum_type' options will be removed from a future release "
"of pulp_rpm."
)
if data.get("checksum_type"):
raise serializers.ValidationError(
_(
"Cannot use '*_checksum_type' options and 'checksum_type' options "
"simultaneously. The 'package_checksum_type' and 'metadata_checksum_type' "
"options are deprecated, please use 'checksum_type' only."
)
)

validated_data = super().validate(data)
if (data.get("gpgcheck") or data.get("repo_gpgcheck")) and data.get("repo_config"):
raise serializers.ValidationError(
Expand Down Expand Up @@ -172,6 +192,7 @@ class Meta:
"autopublish",
"metadata_signing_service",
"retain_package_versions",
"checksum_type",
"metadata_checksum_type",
"package_checksum_type",
"gpgcheck",
Expand Down Expand Up @@ -258,12 +279,17 @@ class RpmPublicationSerializer(PublicationSerializer):
"""

metadata_checksum_type = serializers.ChoiceField(
help_text=_("The checksum type for metadata."),
help_text=_("DEPRECATED: The checksum type for metadata."),
choices=CHECKSUM_CHOICES,
required=False,
)
package_checksum_type = serializers.ChoiceField(
help_text=_("The checksum type for packages."),
help_text=_("DEPRECATED: The checksum type for packages."),
choices=CHECKSUM_CHOICES,
required=False,
)
checksum_type = serializers.ChoiceField(
help_text=_("The preferred checksum type used during repo publishes."),
choices=CHECKSUM_CHOICES,
required=False,
)
Expand Down Expand Up @@ -311,6 +337,21 @@ def validate(self, data):
and data["package_checksum_type"] not in settings.ALLOWED_CONTENT_CHECKSUMS
):
raise serializers.ValidationError(_(ALLOWED_CHECKSUM_ERROR_MSG))

if data.get("package_checksum_type") or data.get("metadata_checksum_type"):
logging.getLogger("pulp_rpm.deprecation").info(
"Support for '*_checksum_type' options will be removed from a future release "
"of pulp_rpm."
)
if data.get("checksum_type"):
raise serializers.ValidationError(
_(
"Cannot use '*_checksum_type' options and 'checksum_type' options "
"simultaneously. The 'package_checksum_type' and 'metadata_checksum_type' "
"options are deprecated, please use 'checksum_type' only."
)
)

validated_data = super().validate(data)
if (data.get("gpgcheck") or data.get("repo_gpgcheck")) and data.get("repo_config"):
raise serializers.ValidationError(
Expand All @@ -324,6 +365,7 @@ def validate(self, data):

class Meta:
fields = PublicationSerializer.Meta.fields + (
"checksum_type",
"metadata_checksum_type",
"package_checksum_type",
"gpgcheck",
Expand Down
14 changes: 8 additions & 6 deletions pulp_rpm/app/tasks/publishing.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,10 @@ def get_checksum_type(name, checksum_types, default=CHECKSUM_TYPES.SHA256):
default: The checksum type used if there is no specified nor original checksum type.
"""
original = checksum_types.get("original")
general = checksum_types.get("general")
metadata = checksum_types.get("metadata")
checksum_type = metadata if metadata else original.get(name, default)
# fallback order
checksum_type = general or metadata or original.get(name) or default
# "sha" -> "SHA" -> "CHECKSUM_TYPES.SHA" -> "sha1"
normalized_checksum_type = getattr(CHECKSUM_TYPES, checksum_type.upper())
return normalized_checksum_type
Expand Down Expand Up @@ -354,10 +356,10 @@ def publish(
)
with tempfile.TemporaryDirectory(dir="."):
with RpmPublication.create(repository_version) as publication:
publication.metadata_checksum_type = get_checksum_type("primary", checksum_types)
publication.package_checksum_type = (
checksum_types.get("package") or publication.metadata_checksum_type
)
checksum_type = get_checksum_type("primary", checksum_types)
publication.checksum_type = checksum_type
publication.metadata_checksum_type = checksum_type
publication.package_checksum_type = checksum_types.get("package") or checksum_type

publication.repo_config = repo_config

Expand Down Expand Up @@ -653,7 +655,7 @@ def generate_repo_metadata(
for name, path in repomdrecords:
record = cr.RepomdRecord(name, path)
checksum_type = cr_checksum_type_from_string(
get_checksum_type(name, checksum_types, default=publication.metadata_checksum_type)
get_checksum_type(name, checksum_types, default=publication.checksum_type)
)
record.fill(checksum_type)
record.rename_file()
Expand Down
2 changes: 2 additions & 0 deletions pulp_rpm/app/viewsets/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ def create(self, request):
repository_version = serializer.validated_data.get("repository_version")
repository = RpmRepository.objects.get(pk=repository_version.repository.pk)

checksum_type = serializer.validated_data.get("checksum_type", repository.checksum_type)
metadata_checksum_type = serializer.validated_data.get(
"metadata_checksum_type", repository.metadata_checksum_type
)
Expand All @@ -546,6 +547,7 @@ def create(self, request):
checksum_types = dict(
metadata=metadata_checksum_type,
package=package_checksum_type,
general=checksum_type,
)
# gpg options are deprecated in favour of repo_config
# acting as shim layer between old and new api
Expand Down

0 comments on commit 7e4dc1a

Please sign in to comment.