From a107bbc57236a45d5422169edab0da0f97944d2b Mon Sep 17 00:00:00 2001 From: Daniel Alley Date: Mon, 6 Nov 2023 18:51:24 -0500 Subject: [PATCH] Deprecate 'package_checksum_type' and 'metadata_checksum_type' closes #2480 --- CHANGES/2480.deprecation | 3 ++ ...7_rpmpublication_checksum_type_and_more.py | 35 +++++++++++++ pulp_rpm/app/models/repository.py | 3 ++ pulp_rpm/app/serializers/repository.py | 52 +++++++++++++++++-- pulp_rpm/app/tasks/publishing.py | 14 ++--- pulp_rpm/app/viewsets/repository.py | 2 + 6 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 CHANGES/2480.deprecation create mode 100644 pulp_rpm/app/migrations/0057_rpmpublication_checksum_type_and_more.py diff --git a/CHANGES/2480.deprecation b/CHANGES/2480.deprecation new file mode 100644 index 000000000..a0ed539f7 --- /dev/null +++ b/CHANGES/2480.deprecation @@ -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). diff --git a/pulp_rpm/app/migrations/0057_rpmpublication_checksum_type_and_more.py b/pulp_rpm/app/migrations/0057_rpmpublication_checksum_type_and_more.py new file mode 100644 index 000000000..97523069a --- /dev/null +++ b/pulp_rpm/app/migrations/0057_rpmpublication_checksum_type_and_more.py @@ -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), + ), + ] diff --git a/pulp_rpm/app/models/repository.py b/pulp_rpm/app/models/repository.py index 3f00cd933..64f14380d 100644 --- a/pulp_rpm/app/models/repository.py +++ b/pulp_rpm/app/models/repository.py @@ -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) @@ -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, ) @@ -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) diff --git a/pulp_rpm/app/serializers/repository.py b/pulp_rpm/app/serializers/repository.py index 21a95df29..4af48b425 100644 --- a/pulp_rpm/app/serializers/repository.py +++ b/pulp_rpm/app/serializers/repository.py @@ -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, @@ -117,7 +123,7 @@ 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] @@ -125,6 +131,20 @@ def validate(self, data): ): 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( @@ -172,6 +192,7 @@ class Meta: "autopublish", "metadata_signing_service", "retain_package_versions", + "checksum_type", "metadata_checksum_type", "package_checksum_type", "gpgcheck", @@ -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, ) @@ -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( @@ -324,6 +365,7 @@ def validate(self, data): class Meta: fields = PublicationSerializer.Meta.fields + ( + "checksum_type", "metadata_checksum_type", "package_checksum_type", "gpgcheck", diff --git a/pulp_rpm/app/tasks/publishing.py b/pulp_rpm/app/tasks/publishing.py index 497f61d0b..3eee4de51 100644 --- a/pulp_rpm/app/tasks/publishing.py +++ b/pulp_rpm/app/tasks/publishing.py @@ -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 @@ -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 @@ -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() diff --git a/pulp_rpm/app/viewsets/repository.py b/pulp_rpm/app/viewsets/repository.py index ed0beaf40..396126027 100644 --- a/pulp_rpm/app/viewsets/repository.py +++ b/pulp_rpm/app/viewsets/repository.py @@ -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 ) @@ -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