Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Avoid redundant SQL write query during CA/Cert creation generation #168

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions django_x509/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,12 @@ def save(self, *args, **kwargs):
generate = False
if not self.pk and not self.certificate and not self.private_key:
generate = True
super().save(*args, **kwargs)
if generate:
# automatically determine serial number
if not self.serial_number:
self.serial_number = self._generate_serial_number()
self._generate()
kwargs['force_insert'] = False
super().save(*args, **kwargs)

super().save(*args, **kwargs)

@cached_property
def x509(self):
Expand Down
26 changes: 21 additions & 5 deletions django_x509/tests/test_cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from OpenSSL import crypto
from swapper import load_model

# CaptureQueriesContext is a context manager used to capture the number of database queries executed within its block.
from django.test.utils import CaptureQueriesContext

from .. import settings as app_settings
from ..base.models import generalized_time
from . import TestX509Mixin
Expand Down Expand Up @@ -81,11 +84,24 @@ class TestCert(TestX509Mixin, TestCase):
"""

def test_new(self):
cert = self._create_cert()
self.assertNotEqual(cert.certificate, '')
self.assertNotEqual(cert.private_key, '')
x509 = cert.x509
self.assertEqual(x509.get_serial_number(), cert.serial_number)
# Capture queries before optimization
#connection argument is a reference to a database connection object (global variable).
with CaptureQueriesContext(connection) as queries_before:
cert_before =self._create_cert()
number_queries_before=len(queries_before)

# Capture queries after optimization
with CaptureQueriesContext(connection) as queries_after:
cert = self._create_cert()
number_queries_after=len(queries_after)

# Assert that the number of queries has decreased due to the optimization
self.assertLess(number_queries_after,number_queries_before)

self.assertNotEqual(cert_before.certificate, '')
self.assertNotEqual(cert_before.private_key, '')
x509 = cert_before.x509
self.assertEqual(x509.get_serial_number(), cert_before.serial_number)
SitaGanesh marked this conversation as resolved.
Show resolved Hide resolved
subject = x509.get_subject()
# check subject
self.assertEqual(subject.countryName, cert.country_code)
Expand Down
Loading