feat: add certificate management with expiration reminders

Add a Certificate model alongside software licenses with type, common
name, SANs, issuer, serial number, fingerprint, validity period, auto
renewal, certificate file and storage locations. Certificates can be
assigned to software products, installations, devices, virtual machines
and tenants.

Extract the expiration reminder settings and logic into a shared
ExpirationNotificationModel base and a generic notification job, so
certificates get the same default/custom reminders, NetBox notifications
and optional SMTP emails as licenses (separate event type, log model and
email permission).

Includes UI views, filters, table, bulk import/edit, REST API endpoint,
migration 0015, German translations, tests and README/CHANGELOG updates.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-10-21 14:02:53 +02:00
1 parent 32d62f26b2
commit 02d965a77b
22 files changed
+1547 -102

No files matched your search

+102 -43
View File
@@ -8,84 +8,143 @@ from core.choices import JobIntervalChoices
from extras.models import Notification
from netbox.jobs import JobRunner, system_job
from netbox_slm.events import LICENSE_EXPIRING
from netbox_slm.models import SoftwareLicense, SoftwareLicenseNotificationLog
from netbox_slm.events import CERTIFICATE_EXPIRING, LICENSE_EXPIRING
from netbox_slm.models import (
Certificate,
CertificateNotificationLog,
SoftwareLicense,
SoftwareLicenseNotificationLog,
)
@system_job(interval=JobIntervalChoices.INTERVAL_DAILY)
class LicenseExpirationNotificationJob(JobRunner):
class Meta:
name = "Software License Expiration Notifications"
class ExpirationNotificationJob(JobRunner):
"""Shared reminder logic; subclasses define the model, log model, event type and email text."""
model = None
log_model = None
log_object_field = None
event_type = None
def get_queryset(self, today):
return self.model.objects.filter(
expiration_notifications_enabled=True,
expiration_date__gte=today,
)
def get_email(self, obj):
raise NotImplementedError
def run(self, *args, **kwargs):
today = timezone.localdate()
licenses = SoftwareLicense.objects.filter(
expiration_notifications_enabled=True,
lifetime=False,
expiration_date__gte=today,
).prefetch_related("subscriptions__user")
objects = self.get_queryset(today).prefetch_related("subscriptions__user")
for license in licenses:
due_dates = [date for date in license.get_notification_dates() if date <= today]
for obj in objects:
due_dates = [date for date in obj.get_notification_dates() if date <= today]
if not due_dates:
continue
for subscription in license.subscriptions.all():
for subscription in obj.subscriptions.all():
user = subscription.user
if not user.is_active:
continue
# If notifications were enabled late, send only the most recent reminder instead of one per elapsed
# interval. Normally this still produces one reminder at each configured threshold.
self._notify(license, user, max(due_dates))
self._notify(obj, user, max(due_dates))
def _notify(self, license, user, notification_date):
log = SoftwareLicenseNotificationLog.objects.get_or_create(
license=license,
def _notify(self, obj, user, notification_date):
log = self.log_model.objects.get_or_create(
**{self.log_object_field: obj},
user=user,
expiration_date=license.expiration_date,
expiration_date=obj.expiration_date,
notification_date=notification_date,
)[0]
now = timezone.now()
name = self.model._meta.model_name
object_type = ContentType.objects.get_for_model(license)
object_type = ContentType.objects.get_for_model(obj)
Notification.objects.update_or_create(
object_type=object_type,
object_id=license.pk,
object_id=obj.pk,
user=user,
defaults={
"created": now,
"object_repr": Notification.get_object_repr(license),
"event_type": LICENSE_EXPIRING,
"object_repr": Notification.get_object_repr(obj),
"event_type": self.event_type,
"read": None,
},
)
log.netbox_sent_at = now
log.save(update_fields=("netbox_sent_at",))
self.logger.info(f"Created NetBox expiration notification for license {license.pk} and user {user}")
if license.email_notifications and user.email:
subject = _("License %(license)s expires on %(date)s") % {
"license": license,
"date": license.expiration_date,
}
body = _(
"The software license '%(license)s' for '%(product)s' expires on %(date)s."
) % {
"license": license,
"product": license.software_product,
"date": license.expiration_date,
}
if license.tenant:
body += "\n" + _("Tenant: %(tenant)s") % {"tenant": license.tenant}
if license.provider_portal_url:
body += "\n" + _("Provider portal: %(url)s") % {"url": license.provider_portal_url}
self.logger.info(f"Created NetBox expiration notification for {name} {obj.pk} and user {user}")
if obj.email_notifications and user.email:
subject, body = self.get_email(obj)
try:
from_email = settings.SERVER_EMAIL or settings.EMAIL_HOST_USER
send_mail(subject, body, from_email, [user.email], fail_silently=False)
except Exception:
self.logger.exception(f"Failed to send expiration email for license {license.pk} to user {user}")
self.logger.exception(f"Failed to send expiration email for {name} {obj.pk} to user {user}")
else:
log.email_sent_at = now
log.save(update_fields=("email_sent_at",))
self.logger.info(f"Sent expiration email for license {license.pk} to user {user}")
self.logger.info(f"Sent expiration email for {name} {obj.pk} to user {user}")
@system_job(interval=JobIntervalChoices.INTERVAL_DAILY)
class LicenseExpirationNotificationJob(ExpirationNotificationJob):
model = SoftwareLicense
log_model = SoftwareLicenseNotificationLog
log_object_field = "license"
event_type = LICENSE_EXPIRING
class Meta:
name = "Software License Expiration Notifications"
def get_queryset(self, today):
return super().get_queryset(today).filter(lifetime=False)
def get_email(self, license):
subject = _("License %(license)s expires on %(date)s") % {
"license": license,
"date": license.expiration_date,
}
body = _("The software license '%(license)s' for '%(product)s' expires on %(date)s.") % {
"license": license,
"product": license.software_product,
"date": license.expiration_date,
}
if license.tenant:
body += "\n" + _("Tenant: %(tenant)s") % {"tenant": license.tenant}
if license.provider_portal_url:
body += "\n" + _("Provider portal: %(url)s") % {"url": license.provider_portal_url}
return subject, body
@system_job(interval=JobIntervalChoices.INTERVAL_DAILY)
class CertificateExpirationNotificationJob(ExpirationNotificationJob):
model = Certificate
log_model = CertificateNotificationLog
log_object_field = "certificate"
event_type = CERTIFICATE_EXPIRING
class Meta:
name = "Certificate Expiration Notifications"
def get_email(self, certificate):
subject = _("Certificate %(certificate)s expires on %(date)s") % {
"certificate": certificate,
"date": certificate.expiration_date,
}
body = _("The certificate '%(certificate)s' expires on %(date)s.") % {
"certificate": certificate,
"date": certificate.expiration_date,
}
if certificate.common_name:
body += "\n" + _("Common name: %(name)s") % {"name": certificate.common_name}
if certificate.issuer:
body += "\n" + _("Issuer: %(issuer)s") % {"issuer": certificate.issuer}
if certificate.tenant:
body += "\n" + _("Tenant: %(tenant)s") % {"tenant": certificate.tenant}
if certificate.provider_portal_url:
body += "\n" + _("Provider portal: %(url)s") % {"url": certificate.provider_portal_url}
return subject, body