muralibasani commented on code in PR #24093:
URL: https://github.com/apache/kafka/pull/24093#discussion_r4481950905
##########
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);
}
public void removeToken(String tokenId) {
- TokenInformation tokenInfo = tokenCache.remove(tokenId);
- if (tokenInfo != null) {
- String hmac = tokenIdHmacCache.remove(tokenInfo.tokenId());
- if (hmac != null) {
- hmacTokenIdCache.remove(hmac);
- }
+ DelegationToken token = tokenCache.remove(tokenId);
+ if (token != null) {
+ hmacTokenIdCache.remove(token.hmacAsBase64String());
Review Comment:
Any alternative to using this hmacAsBase64String as key ? because the old
code stored the string once at insert and if by any chance it got updated
during caching, that key won't be valid anymore. ?
--
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]