"""This module defines models for managing trustpoints, including server credentials and truststores."""from__future__importannotationsfromtypingimportAnyfromdjango.dbimportmodelsfromdjango.utils.translationimportgettext_lazyas_fromdjango_stubs_ext.db.modelsimportTypedModelMetafromtrustpoint_core.serializerimportCertificateCollectionSerializerfromutil.fieldimportUniqueNameValidatorfrom.certificateimportCertificateModelfrom.credentialimportCredentialModel__all__=['ActiveTrustpointTlsServerCredentialModel','TruststoreModel','TruststoreOrderModel',]
[docs]classActiveTrustpointTlsServerCredentialModel(models.Model):"""Represents the currently active TLS server credential. This model tracks the active server credential, ensuring that it is always up-to-date and linked to a specific `CredentialModel` instance. """
[docs]classMeta(TypedModelMeta):"""Meta class configuration."""
[docs]def__str__(self)->str:"""Returns a human-readable string representation of the active credential. Returns: str: Description of the active TLS server credential. """returnf'Active TLS Credential: {self.credential.idifself.credentialelse"None"}'
[docs]defsave(self,*args:Any,**kwargs:Any)->None:"""Ensures the model instance always has an ID of 1 to enforce singleton-like behavior. Returns: None """self.id=1super().save(*args,**kwargs)
[docs]classTruststoreModel(models.Model):"""Represents a truststore, which is a collection of certificates used for specific purposes. This model allows organizing certificates into a logical grouping for specific intended usages such as `IDevID`, `TLS`, or `Generic`. Each truststore is identified by a unique name and supports operations like retrieving the number of certificates or serializing its content. """
[docs]classIntendedUsage(models.IntegerChoices):"""Intended Usage of the Truststore."""
[docs]classMeta(TypedModelMeta):"""Meta class configuration."""
[docs]def__str__(self)->str:"""Returns a human-readable string representation of the TruststoreModel."""returnself.unique_name
[docs]defsave(self,**kwargs:Any)->None:"""Ensures the model is valid before saving."""self.full_clean()super().save(**kwargs)
@property
[docs]defnumber_of_certificates(self)->int:"""Returns the number of certificates in the truststore."""returnlen(self.certificates.all())
[docs]defget_certificate_collection_serializer(self)->CertificateCollectionSerializer:"""Returns a serializer for all certificates in the truststore. This method gathers all the certificates associated with the truststore, serializes them using `CertificateCollectionSerializer`, and returns the serialized result. Returns: The serialized representation of the certificates. """returnCertificateCollectionSerializer([cert.certificate.get_certificate_serializer().as_crypto()forcertinself.truststoreordermodel_set.order_by('order')])
[docs]classTruststoreOrderModel(models.Model):"""Represents the order of certificates in a truststore."""
[docs]order=models.PositiveSmallIntegerField(verbose_name=_('Trust Store Certificate Index (Order)'),editable=False)
[docs]def__str__(self)->str:"""Returns a human-readable string representation of the TruststoreOrderModel."""returnf'Truststore Order {self.order} for Truststore {self.trust_store.unique_name}'