Skip to content

Commit

Permalink
sweep: #7957 Add caches to AuthManager.getUsername
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisburr authored and web-flow committed Dec 19, 2024
1 parent f90bb14 commit 202b174
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/DIRAC/Core/DISET/AuthManager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
""" Module that holds DISET Authorization class for services
"""
from cachetools import TTLCache

from DIRAC.ConfigurationSystem.Client.Config import gConfig
from DIRAC.ConfigurationSystem.Client.Helpers import Registry
from DIRAC.Core.Security import Properties
Expand All @@ -26,6 +28,8 @@ def __init__(self, authSection):
:param authSection: Section containing the authorization rules
"""
self.authSection = authSection
self._cache_getUsersInGroup = TTLCache(maxsize=1000, ttl=60)
self._cache_getUsernameForDN = TTLCache(maxsize=1000, ttl=60)

def authQuery(self, methodQuery, credDict, defaultProperties=False):
"""
Expand Down Expand Up @@ -257,10 +261,18 @@ def getUsername(self, credDict):
return False
credDict[self.KW_GROUP] = result["Value"]
credDict[self.KW_PROPERTIES] = Registry.getPropertiesForGroup(credDict[self.KW_GROUP], [])
usersInGroup = Registry.getUsersInGroup(credDict[self.KW_GROUP], [])

usersInGroup = self._cache_getUsersInGroup.get(credDict[self.KW_GROUP])
if usersInGroup is None:
usersInGroup = Registry.getUsersInGroup(credDict[self.KW_GROUP], [])
self._cache_getUsersInGroup[credDict[self.KW_GROUP]] = usersInGroup
if not usersInGroup:
return False
retVal = Registry.getUsernameForDN(credDict[self.KW_DN], usersInGroup)

retVal = self._cache_getUsernameForDN.get(credDict[self.KW_DN])
if retVal is None:
retVal = Registry.getUsernameForDN(credDict[self.KW_DN], usersInGroup)
self._cache_getUsernameForDN[credDict[self.KW_DN]] = retVal
if retVal["OK"]:
credDict[self.KW_USERNAME] = retVal["Value"]
return True
Expand Down

0 comments on commit 202b174

Please sign in to comment.