Skip to content

Commit 3e056df

Browse files
committed
refactor: Add caches to asn1_utils for better performance
1 parent a2a30c3 commit 3e056df

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/DIRAC/Core/Security/m2crypto/asn1_utils.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
C library (https://github.com/italiangrid/voms) instead...
1515
1616
"""
17+
from functools import lru_cache
18+
1719
from pyasn1.codec.der.decoder import decode as der_decode
1820
from pyasn1.error import PyAsn1Error
1921
from pyasn1.type import namedtype, univ, char as asn1char
@@ -93,6 +95,11 @@ def decodeDIRACGroup(m2cert):
9395
"""
9496

9597
diracGroupOctetString = retrieveExtension(m2cert, DIRAC_GROUP_OID)
98+
return _decodeDIRACGroup(diracGroupOctetString)
99+
100+
101+
@lru_cache
102+
def _decodeDIRACGroup(diracGroupOctetString):
96103
diracGroupUTF8Str, _rest = der_decode(diracGroupOctetString, asn1Spec=asn1char.IA5String())
97104

98105
return diracGroupUTF8Str.asOctets().decode()
@@ -336,11 +343,7 @@ def retrieveExtension(m2Cert, extensionOID):
336343
337344
:raises: LookupError if it does not have the extension
338345
"""
339-
340-
# Decode the certificate as a RFC2459 Certificate object.It is compatible
341-
# with the RFC proxy definition
342-
cert, _rest = der_decode(m2Cert.as_der(), asn1Spec=rfc2459.Certificate())
343-
extensions = cert["tbsCertificate"]["extensions"]
346+
extensions = _extensionsFromCertDER(m2Cert.as_der())
344347

345348
# Construct an OID object for comparison purpose
346349
extensionOIDObj = univ.ObjectIdentifier(extensionOID)
@@ -354,3 +357,12 @@ def retrieveExtension(m2Cert, extensionOID):
354357

355358
# If we are here, it means that we could not find the expected extension.
356359
raise LookupError(f"Could not find extension with OID {extensionOID}")
360+
361+
362+
@lru_cache(maxsize=1024)
363+
def _extensionsFromCertDER(der):
364+
# Decode the certificate as a RFC2459 Certificate object.It is compatible
365+
# with the RFC proxy definition
366+
cert, _rest = der_decode(der, asn1Spec=rfc2459.Certificate())
367+
extensions = cert["tbsCertificate"]["extensions"]
368+
return extensions

0 commit comments

Comments
 (0)