Skip to content

Commit

Permalink
🗃️ [#74] Delete the old certificate field
Browse files Browse the repository at this point in the history
Added to the migration file with the data migration so
everything runs in a single transaction.
  • Loading branch information
sergei-maertens committed Jul 19, 2024
1 parent f103cf9 commit 4822b96
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
8 changes: 8 additions & 0 deletions digid_eherkenning/migrations/0012_move_config_certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ class Migration(migrations.Migration):
operations = [
# reverse migration is ambiguous, if needed, you can easily use the UI
migrations.RunPython(move_certificates, migrations.RunPython.noop),
migrations.RemoveField(
model_name="digidconfiguration",
name="certificate",
),
migrations.RemoveField(
model_name="eherkenningconfiguration",
name="certificate",
),
]
18 changes: 5 additions & 13 deletions digid_eherkenning/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from onelogin.saml2.constants import OneLogin_Saml2_Constants
from onelogin.saml2.idp_metadata_parser import OneLogin_Saml2_IdPMetadataParser
from privates.fields import PrivateMediaFileField
from simple_certmanager.models import Certificate
from solo.models import SingletonModel

from ..choices import DigestAlgorithms, SignatureAlgorithms, XMLContentTypes
from .certificates import ConfigCertificate


class ConfigurationManager(models.Manager):
Expand All @@ -20,16 +20,6 @@ def get_queryset(self):


class BaseConfiguration(SingletonModel):
certificate = models.ForeignKey(
Certificate,
null=True,
on_delete=models.PROTECT,
verbose_name=_("key pair"),
help_text=_(
"The private key and public certificate pair to use during the "
"authentication flow."
),
)
idp_metadata_file = PrivateMediaFileField(
_("identity provider metadata"),
blank=True,
Expand Down Expand Up @@ -240,6 +230,8 @@ def save(self, *args, **kwargs):
super().save(*args, **kwargs)

def clean(self):
if not self.certificate:
raise ValidationError(_("You must select a certificate"))
super().clean()

# require that a certificate is configured
if not ConfigCertificate.objects.for_config(self).exists():
raise ValidationError(_("You must select a certificate"))
17 changes: 16 additions & 1 deletion digid_eherkenning/models/certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

import logging
from typing import TYPE_CHECKING, TypeAlias

from django.db import models
from django.utils import timezone
Expand All @@ -15,10 +16,24 @@

from ..choices import ConfigTypes

if TYPE_CHECKING:
from .digid import DigidConfiguration
from .eherkenning import EherkenningConfiguration

logger = logging.getLogger(__name__)

_AnyDigiD: TypeAlias = "type[DigidConfiguration] | DigidConfiguration"
_AnyEH: TypeAlias = "type[EherkenningConfiguration] | EherkenningConfiguration"


class ConfigCertificateQuerySet(models.QuerySet):
def for_config(self, config: _AnyDigiD | _AnyEH):
opts = config._meta
config_type = ConfigTypes(f"{opts.app_label}.{opts.object_name}")
return self.filter(config_type=config_type)


class ConfigCertificateManager(models.Manager):
class ConfigCertificateManager(models.Manager.from_queryset(ConfigCertificateQuerySet)):
def get_queryset(self):
qs = super().get_queryset()
return qs.select_related("certificate")
Expand Down

0 comments on commit 4822b96

Please sign in to comment.