feat: fill certificate fields from uploaded certificate file

Parse uploaded PEM or DER certificates with cryptography and fill common
name, SANs, issuer, serial number, SHA-256 fingerprint and validity dates.
Name and type are only set when left empty; the type is derived from the
CA flag and extended key usage. Files containing a private key are
rejected. Works in the UI form and the REST API.

Adds cryptography as a dependency.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-10-21 14:16:58 +02:00
1 parent f296a576f2
commit 2a41118e8e
9 files changed
+228

No files matched your search

+27
View File
@@ -1,8 +1,10 @@
from django.core.files.uploadedfile import UploadedFile
from django.forms import CharField, ChoiceField, DateField, IntegerField, MultipleChoiceField, Textarea
from django.utils.translation import gettext_lazy as _
from dcim.models import Device
from netbox.forms import NetBoxModelForm, NetBoxModelFilterSetForm, NetBoxModelImportForm, NetBoxModelBulkEditForm
from netbox_slm.certificate_parser import CertificateParseError, apply_certificate_file
from netbox_slm.models import (
Certificate,
CertificateTypes,
@@ -27,6 +29,12 @@ from virtualization.models import VirtualMachine
class CertificateForm(NetBoxModelForm):
comments = CommentField()
name = CharField(
required=False,
max_length=128,
label=_("Name"),
help_text=_("Taken from the certificate's common name if left empty"),
)
fieldsets = (
FieldSet("name", "description", "type", name=_("Certificate")),
FieldSet("software_product", "installation", "device", "virtualmachine", name=_("Assignment")),
@@ -99,6 +107,25 @@ class CertificateForm(NetBoxModelForm):
label=_("Tenant"),
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["certificate_file"].help_text = _(
"PEM or DER file without private key. Common name, SANs, issuer, serial number, fingerprint and "
"validity are filled in automatically."
)
def clean(self):
cleaned_data = super().clean()
uploaded_file = cleaned_data.get("certificate_file")
if isinstance(uploaded_file, UploadedFile):
try:
apply_certificate_file(cleaned_data, uploaded_file)
except CertificateParseError as error:
self.add_error("certificate_file", error)
if not cleaned_data.get("name") and "name" not in self.errors:
self.add_error("name", _("Enter a name or upload a certificate file."))
return cleaned_data
class Meta:
model = Certificate
fields = (