|
| 1 | +# Generated by Django 5.2.11 on 2026-04-15 02:37 |
| 2 | + |
| 3 | +import base64 |
| 4 | +import logging |
| 5 | + |
| 6 | +from django.db import migrations, models |
| 7 | + |
| 8 | +from pysequoia.packet import PacketPile, Tag |
| 9 | + |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +def keyid_from_fingerprint(fingerprint): |
| 15 | + if len(fingerprint) == 40: |
| 16 | + return fingerprint[-16:] |
| 17 | + elif len(fingerprint) == 64: |
| 18 | + return fingerprint[:16] |
| 19 | + else: |
| 20 | + raise ValueError(f"Unexpected fingerprint length: {len(fingerprint)}") |
| 21 | + |
| 22 | + |
| 23 | +def populate_fingerprint(apps, schema_editor): |
| 24 | + ManifestSignature = apps.get_model("container", "ManifestSignature") |
| 25 | + |
| 26 | + updated = [] |
| 27 | + for sig in ManifestSignature.objects.filter(fingerprint__isnull=True).iterator(): |
| 28 | + try: |
| 29 | + signature_raw = base64.b64decode(sig.data) |
| 30 | + pile = PacketPile.from_bytes(signature_raw) |
| 31 | + except Exception as exc: |
| 32 | + logger.warning("Could not parse signature %s, skipping fingerprint extraction", sig.pk) |
| 33 | + logger.warning(str(exc)) |
| 34 | + continue |
| 35 | + |
| 36 | + fingerprint = None |
| 37 | + for packet in pile: |
| 38 | + if packet.tag == Tag.Signature: |
| 39 | + if packet.issuer_fingerprint is not None: |
| 40 | + fingerprint = packet.issuer_fingerprint.upper() |
| 41 | + break |
| 42 | + elif packet.issuer_key_id is not None: |
| 43 | + # No fingerprint available, only key_id — nothing new to store |
| 44 | + break |
| 45 | + |
| 46 | + if fingerprint: |
| 47 | + assert sig.key_id == keyid_from_fingerprint(fingerprint), ( |
| 48 | + f"Signature {sig.pk}: key_id {sig.key_id} does not match " |
| 49 | + f"fingerprint {fingerprint}" |
| 50 | + ) |
| 51 | + sig.fingerprint = fingerprint |
| 52 | + updated.append(sig) |
| 53 | + |
| 54 | + if updated: |
| 55 | + ManifestSignature.objects.bulk_update(updated, ["fingerprint"], batch_size=1000) |
| 56 | + |
| 57 | + |
| 58 | +class Migration(migrations.Migration): |
| 59 | + |
| 60 | + dependencies = [ |
| 61 | + ('container', '0047_containernamespace_pulp_labels'), |
| 62 | + ] |
| 63 | + |
| 64 | + operations = [ |
| 65 | + migrations.AddField( |
| 66 | + model_name='manifestsignature', |
| 67 | + name='fingerprint', |
| 68 | + field=models.TextField(db_index=True, null=True), |
| 69 | + ), |
| 70 | + migrations.RunPython( |
| 71 | + populate_fingerprint, |
| 72 | + reverse_code=migrations.RunPython.noop, |
| 73 | + elidable=True, |
| 74 | + ), |
| 75 | + ] |
0 commit comments