Skip to content

Commit f9ddab7

Browse files
authored
Merge pull request from GHSA-59qj-jcjv-662j
[8.0] Remove server side cache in TokenManager
2 parents a048eb2 + a9ddc97 commit f9ddab7

File tree

1 file changed

+6
-36
lines changed

1 file changed

+6
-36
lines changed

src/DIRAC/FrameworkSystem/Service/TokenManagerHandler.py

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616
.. image:: /_static/Systems/FS/TokenManager_getToken.png
1717
:alt: https://dirac.readthedocs.io/en/integration/_images/TokenManager_getToken.png (source https://github.com/TaykYoku/DIRACIMGS/raw/main/TokenManagerService_getToken.ai)
1818
19-
The service and its client have a mechanism for caching the received tokens.
19+
The client has a mechanism for caching the received tokens.
2020
This helps reduce the number of requests to both the service and the Identity Provider (IdP).
2121
2222
If the client has a valid **access token** in the cache, it is used until it expires.
2323
After that you need to update. The client can update it independently if on the server where it is in ``dirac.cfg``
2424
``client_id`` and ``client_secret`` of the Identity Provider client are registered.
2525
2626
Otherwise, the client makes an RPC call to the **TornadoManager** service.
27-
It in turn checks the cache and if the ``access token`` is already invalid tries to update it using a ``refresh token``.
28-
If the required token is not in the cache, then the ``refresh token`` from :py:class:`TokenDB <DIRAC.FrameworkSystem.DB.TokenDB.TokenDB>`
29-
is taken and the **exchange token** request to Identity Provider is made. The received tokens are cached.
27+
The ``refresh token`` from :py:class:`TokenDB <DIRAC.FrameworkSystem.DB.TokenDB.TokenDB>`
28+
is taken and the **exchange token** request to Identity Provider is made.
3029
"""
3130

3231
import pprint
@@ -42,16 +41,11 @@
4241
from DIRAC.FrameworkSystem.Utilities.TokenManagementUtilities import (
4342
getIdProviderClient,
4443
getCachedKey,
45-
getCachedToken,
4644
DEFAULT_RT_EXPIRATION_TIME,
4745
DEFAULT_AT_EXPIRATION_TIME,
4846
)
4947

5048

51-
# Used to synchronize the cache with user tokens
52-
gTokensSync = ThreadSafe.Synchronizer()
53-
54-
5549
class TokenManagerHandler(TornadoService):
5650
DEFAULT_AUTHORIZATION = ["authenticated"]
5751

@@ -61,8 +55,6 @@ def initializeHandler(cls, *args):
6155
6256
:return: S_OK()/S_ERROR()
6357
"""
64-
# Cache containing tokens from scope requested by the client
65-
cls.__tokensCache = DictCache()
6658

6759
# The service plays an important OAuth 2.0 role, namely it is an Identity Provider client.
6860
# This allows you to manage tokens without the involvement of their owners.
@@ -181,7 +173,6 @@ def __checkProperties(self, requestedUserDN: str, requestedUserGroup: str):
181173
# Not authorized!
182174
return S_ERROR("You can't get tokens!")
183175

184-
@gTokensSync
185176
def export_getToken(
186177
self,
187178
username: str = None,
@@ -213,12 +204,8 @@ def export_getToken(
213204
return result
214205
idpObj = result["Value"]
215206

216-
# Search for an existing token in tokensCache
217-
cachedKey = getCachedKey(idpObj, username, userGroup, scope, audience)
218-
result = getCachedToken(self.__tokensCache, cachedKey, requiredTimeLeft)
219-
if result["OK"]:
220-
# A valid token has been found and is returned
221-
return result
207+
# getCachedKey is just used here to resolve the default scopes
208+
_, scope, *_ = getCachedKey(idpObj, username, userGroup, scope, audience)
222209

223210
# A client token is requested
224211
if not username:
@@ -227,19 +214,8 @@ def export_getToken(
227214
return result
228215

229216
# Get the client token with requested scope and audience
230-
scope = cachedKey[1]
231-
audience = cachedKey[2]
232217
result = idpObj.fetchToken(grant_type="client_credentials", scope=scope, audience=audience)
233-
if not result["OK"]:
234-
return result
235-
token = result["Value"]
236-
237-
# Caching new token: only get an access token (no refresh token in this context)
238-
self.__tokensCache.add(
239-
cachedKey,
240-
result["Value"].get_claim("exp", "access_token") or DEFAULT_AT_EXPIRATION_TIME,
241-
token,
242-
)
218+
243219
return result
244220

245221
# A user token is requested
@@ -262,12 +238,6 @@ def export_getToken(
262238
# refresh token with requested scope
263239
result = idpObj.refreshToken(tokens.get("refresh_token"), group=userGroup, scope=scope)
264240
if result["OK"]:
265-
# caching new tokens
266-
self.__tokensCache.add(
267-
cachedKey,
268-
result["Value"].get_claim("exp", "refresh_token") or DEFAULT_RT_EXPIRATION_TIME,
269-
result["Value"],
270-
)
271241
return result
272242
# Did not find any token associated with the found user ID
273243
err.append(result.get("Message", f"No token found for {uid}"))

0 commit comments

Comments
 (0)