muralibasani commented on code in PR #24093:
URL: https://github.com/apache/kafka/pull/24093#discussion_r4481885882


##########
clients/src/main/java/org/apache/kafka/common/security/token/delegation/internals/DelegationTokenCache.java:
##########
@@ -28,90 +27,80 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
 
 public class DelegationTokenCache {
 
-    private final CredentialCache credentialCache = new CredentialCache();
+    private final CredentialCache credentialCache;
 
     //Cache to hold all the tokens
-    private final Map<String, TokenInformation> tokenCache = new 
ConcurrentHashMap<>();
+    private final Map<String, DelegationToken> tokenCache = new 
ConcurrentHashMap<>();
 
     //Cache to hold hmac->tokenId mapping. This is required for renew, expire 
requests
     private final Map<String, String> hmacTokenIdCache = new 
ConcurrentHashMap<>();
 
-    //Cache to hold tokenId->hmac mapping. This is required for removing entry 
from hmacTokenIdCache using tokenId.
-    private final Map<String, String> tokenIdHmacCache = new 
ConcurrentHashMap<>();
-
     public DelegationTokenCache(Collection<String> scramMechanisms) {
         //Create caches for scramMechanisms
-        ScramCredentialUtils.createCache(credentialCache, scramMechanisms);
+        this.credentialCache = new CredentialCache(scramMechanisms);
     }
 
     public ScramCredential credential(String mechanism, String tokenId) {
-        CredentialCache.Cache<ScramCredential> cache = 
credentialCache.cache(mechanism, ScramCredential.class);
+        CredentialCache.Cache cache = credentialCache.cache(mechanism);
         return cache == null ? null : cache.get(tokenId);
     }
 
     public String owner(String tokenId) {
-        TokenInformation tokenInfo = tokenCache.get(tokenId);
-        return tokenInfo == null ? null : tokenInfo.owner().getName();
+        DelegationToken token = tokenCache.get(tokenId);
+        return token == null ? null : token.tokenInfo().owner().getName();
     }
 
     public void updateCache(DelegationToken token, Map<String, 
ScramCredential> scramCredentialMap) {
-        //Update TokenCache
-        String tokenId =  token.tokenInfo().tokenId();
-        addToken(tokenId, token.tokenInfo());
-        String hmac = token.hmacAsBase64String();
+        //Update TokenCache and hmac-id cache
+        addToken(token);
         //Update Scram Credentials
-        updateCredentials(tokenId, scramCredentialMap);
-        //Update hmac-id cache
-        hmacTokenIdCache.put(hmac, tokenId);
-        tokenIdHmacCache.put(tokenId, hmac);
+        updateCredentials(token.tokenInfo().tokenId(), scramCredentialMap);
     }
 
     public void removeCache(String tokenId) {
         removeToken(tokenId);
         updateCredentials(tokenId, new HashMap<>());
     }
 
-    public String tokenIdForHmac(String base64hmac) {
-        return hmacTokenIdCache.get(base64hmac);
-    }
-
     public TokenInformation tokenForHmac(String base64hmac) {
         String tokenId = hmacTokenIdCache.get(base64hmac);
-        return tokenId == null ? null : tokenCache.get(tokenId);
+        DelegationToken token = tokenId == null ? null : 
tokenCache.get(tokenId);
+        return token == null ? null : token.tokenInfo();
     }
 
-    public TokenInformation addToken(String tokenId, TokenInformation 
tokenInfo) {
-        return tokenCache.put(tokenId, tokenInfo);
+    public void addToken(DelegationToken token) {
+        String tokenId = token.tokenInfo().tokenId();
+        tokenCache.put(tokenId, token);
+        hmacTokenIdCache.put(token.hmacAsBase64String(), tokenId);

Review Comment:
   Would it be also better to clear the previous token's hmac ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to