from __future__ import annotations
from datetime import datetime, timedelta
from enum import Enum
from typing import Literal
from octopoes.models import OOI, Reference
from octopoes.models.ooi.dns.zone import Hostname
from octopoes.models.ooi.network import IPAddress
from octopoes.models.persistence import ReferenceField
[docs]
class AlgorithmType(Enum):
RSA = "RSA"
ECC = "ECC"
[docs]
class X509Certificate(OOI):
object_type: Literal["X509Certificate"] = "X509Certificate"
subject: str | None = None
issuer: str | None = None
valid_from: str
valid_until: str
pk_algorithm: str | None = None
pk_size: int | None = None
pk_number: str | None = None
signed_by: Reference | None = ReferenceField(
"X509Certificate", max_issue_scan_level=1, max_inherit_scan_level=0, default=None
)
serial_number: str
expires_in: timedelta | None = None
_natural_key_attrs = ["issuer", "serial_number"]
@property
def expired(self):
return datetime.now() > datetime.fromisoformat(self.valid_until)
_reverse_relation_names = {"signed_by": "signed_certificates"}
[docs]
class SubjectAlternativeName(OOI):
certificate: Reference = ReferenceField(X509Certificate)
_natural_key_attrs = ["certificate"]
[docs]
class SubjectAlternativeNameHostname(SubjectAlternativeName):
object_type: Literal["SubjectAlternativeNameHostname"] = "SubjectAlternativeNameHostname"
hostname: Reference = ReferenceField(Hostname, max_issue_scan_level=1, max_inherit_scan_level=0)
_natural_key_attrs = ["certificate", "hostname"]
[docs]
class SubjectAlternativeNameIP(SubjectAlternativeName):
object_type: Literal["SubjectAlternativeNameIP"] = "SubjectAlternativeNameIP"
address: Reference = ReferenceField(IPAddress)
_natural_key_attrs = ["certificate", "address"]
[docs]
class SubjectAlternativeNameQualifier(SubjectAlternativeName):
object_type: Literal["SubjectAlternativeNameQualifier"] = "SubjectAlternativeNameQualifier"
name: str
_natural_key_attrs = ["certificate", "name"]
X509Certificate.model_rebuild()
SubjectAlternativeNameHostname.model_rebuild()
SubjectAlternativeNameIP.model_rebuild()
SubjectAlternativeNameQualifier.model_rebuild()