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

[v8r0] Add caches to AuthManager.getUsername #7957

Merged
merged 1 commit into from
Dec 19, 2024
Merged
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
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
Loading