GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — KAFKA-21418 Cleanup DelegationTokenCache and CredentialCache (#24093) · coderbruis/kafka@06fff77

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • clients/src
    • main/java/org/apache/kafka/common
      • network
        • SaslChannelBuilder.java
      • security
        • authenticator
          • CredentialCache.java
        • scram/internals
          • ScramCredentialUtils.java
          • ScramServerCallbackHandler.java
        • token/delegation/internals
          • DelegationTokenCache.java
    • testFixtures/java/org/apache/kafka/common/network
      • NioEchoServer.java
    • test/java/org/apache/kafka/common/security
      • authenticator
        • ClientAuthenticationFailureTest.java
        • CredentialCacheTest.java
        • SaslAuthenticatorFailureDelayTest.java
        • SaslAuthenticatorTest.java
      • scram/internals
        • ScramSaslServerTest.java
      • token/delegation/internals
        • DelegationTokenCacheTest.java
  • core/src/test/scala/unit/kafka/integration
    • KafkaServerTestHarness.scala
  • metadata/src/main/java/org/apache/kafka/controller
    • DelegationTokenControlManager.java
  • server-common/src/main/java/org/apache/kafka/security
    • CredentialProvider.java
    • DelegationTokenManager.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎clients/src/main/java/org/apache/kafka/common/network/SaslChannelBuilder.java‎

Copy file name to clipboardExpand all lines: clients/src/main/java/org/apache/kafka/common/network/SaslChannelBuilder.java
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.apache.kafka.common.security.oauthbearer.internals.unsecured.OAuthBearerUnsecuredValidatorCallbackHandler;
4545
import org.apache.kafka.common.security.plain.internals.PlainSaslServer;
4646
import org.apache.kafka.common.security.plain.internals.PlainServerCallbackHandler;
47-
import org.apache.kafka.common.security.scram.ScramCredential;
4847
import org.apache.kafka.common.security.scram.internals.ScramMechanism;
4948
import org.apache.kafka.common.security.scram.internals.ScramServerCallbackHandler;
5049
import org.apache.kafka.common.security.ssl.SslFactory;
@@ -325,7 +324,7 @@ private void createServerCallbackHandlers(Map<String, ?> configs) {
325324
else if (mechanism.equals(PlainSaslServer.PLAIN_MECHANISM))
326325
callbackHandler = new PlainServerCallbackHandler();
327326
else if (ScramMechanism.isScram(mechanism))
328-
callbackHandler = new ScramServerCallbackHandler(credentialCache.cache(mechanism, ScramCredential.class), tokenCache);
327+
callbackHandler = new ScramServerCallbackHandler(credentialCache.cache(mechanism), tokenCache);
329328
else if (mechanism.equals(OAuthBearerLoginModule.OAUTHBEARER_MECHANISM))
330329
callbackHandler = unsecuredOAuthBearerValidatorCallbackHandler(prefix);
331330
else
Collapse file

‎clients/src/main/java/org/apache/kafka/common/security/authenticator/CredentialCache.java‎

Copy file name to clipboardExpand all lines: clients/src/main/java/org/apache/kafka/common/security/authenticator/CredentialCache.java
+22-30Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,45 @@
1616
*/
1717
package org.apache.kafka.common.security.authenticator;
1818

19+
import org.apache.kafka.common.security.scram.ScramCredential;
20+
import org.apache.kafka.common.security.scram.internals.ScramMechanism;
21+
22+
import java.util.Collection;
23+
import java.util.Map;
1924
import java.util.concurrent.ConcurrentHashMap;
25+
import java.util.function.Function;
26+
import java.util.stream.Collectors;
2027

2128
public class CredentialCache {
2229

23-
private final ConcurrentHashMap<String, Cache<?>> cacheMap = new ConcurrentHashMap<>();
30+
private final Map<String, Cache> cacheMap;
2431

25-
public <C> Cache<C> createCache(String mechanism, Class<C> credentialClass) {
26-
Cache<C> cache = new Cache<>(credentialClass);
27-
@SuppressWarnings("unchecked")
28-
Cache<C> oldCache = (Cache<C>) cacheMap.putIfAbsent(mechanism, cache);
29-
return oldCache == null ? cache : oldCache;
32+
/**
33+
* Creates a cache for each SCRAM mechanism in {@code mechanisms}. Other mechanisms are ignored.
34+
*/
35+
public CredentialCache(Collection<String> mechanisms) {
36+
this.cacheMap = ScramMechanism.mechanismNames().stream()
37+
.filter(mechanisms::contains)
38+
.collect(Collectors.toUnmodifiableMap(Function.identity(), mechanism -> new Cache()));
3039
}
3140

32-
@SuppressWarnings("unchecked")
33-
public <C> Cache<C> cache(String mechanism, Class<C> credentialClass) {
34-
Cache<?> cache = cacheMap.get(mechanism);
35-
if (cache != null) {
36-
if (cache.credentialClass() != credentialClass)
37-
throw new IllegalArgumentException("Invalid credential class " + credentialClass + ", expected " + cache.credentialClass());
38-
return (Cache<C>) cache;
39-
} else
40-
return null;
41+
public Cache cache(String mechanism) {
42+
return cacheMap.get(mechanism);
4143
}
4244

43-
public static class Cache<C> {
44-
private final Class<C> credentialClass;
45-
private final ConcurrentHashMap<String, C> credentials;
46-
47-
public Cache(Class<C> credentialClass) {
48-
this.credentialClass = credentialClass;
49-
this.credentials = new ConcurrentHashMap<>();
50-
}
45+
public static class Cache {
46+
private final ConcurrentHashMap<String, ScramCredential> credentials = new ConcurrentHashMap<>();
5147

52-
public C get(String username) {
48+
public ScramCredential get(String username) {
5349
return credentials.get(username);
5450
}
5551

56-
public C put(String username, C credential) {
52+
public ScramCredential put(String username, ScramCredential credential) {
5753
return credentials.put(username, credential);
5854
}
5955

60-
public C remove(String username) {
56+
public ScramCredential remove(String username) {
6157
return credentials.remove(username);
6258
}
63-
64-
public Class<C> credentialClass() {
65-
return credentialClass;
66-
}
6759
}
6860
}
Collapse file

‎clients/src/main/java/org/apache/kafka/common/security/scram/internals/ScramCredentialUtils.java‎

Copy file name to clipboardExpand all lines: clients/src/main/java/org/apache/kafka/common/security/scram/internals/ScramCredentialUtils.java
-37Lines changed: 0 additions & 37 deletions
This file was deleted.
Collapse file

‎clients/src/main/java/org/apache/kafka/common/security/scram/internals/ScramServerCallbackHandler.java‎

Copy file name to clipboardExpand all lines: clients/src/main/java/org/apache/kafka/common/security/scram/internals/ScramServerCallbackHandler.java
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.apache.kafka.common.security.auth.AuthenticateCallbackHandler;
2020
import org.apache.kafka.common.security.authenticator.CredentialCache;
21-
import org.apache.kafka.common.security.scram.ScramCredential;
2221
import org.apache.kafka.common.security.scram.ScramCredentialCallback;
2322
import org.apache.kafka.common.security.token.delegation.TokenInformation;
2423
import org.apache.kafka.common.security.token.delegation.internals.DelegationTokenCache;
@@ -34,11 +33,11 @@
3433

3534
public class ScramServerCallbackHandler implements AuthenticateCallbackHandler {
3635

37-
private final CredentialCache.Cache<ScramCredential> credentialCache;
36+
private final CredentialCache.Cache credentialCache;
3837
private final DelegationTokenCache tokenCache;
3938
private String saslMechanism;
4039

41-
public ScramServerCallbackHandler(CredentialCache.Cache<ScramCredential> credentialCache,
40+
public ScramServerCallbackHandler(CredentialCache.Cache credentialCache,
4241
DelegationTokenCache tokenCache) {
4342
this.credentialCache = credentialCache;
4443
this.tokenCache = tokenCache;
Collapse file

‎clients/src/main/java/org/apache/kafka/common/security/token/delegation/internals/DelegationTokenCache.java‎

Copy file name to clipboardExpand all lines: clients/src/main/java/org/apache/kafka/common/security/token/delegation/internals/DelegationTokenCache.java
+33-37Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import org.apache.kafka.common.security.authenticator.CredentialCache;
2121
import org.apache.kafka.common.security.scram.ScramCredential;
22-
import org.apache.kafka.common.security.scram.internals.ScramCredentialUtils;
2322
import org.apache.kafka.common.security.scram.internals.ScramMechanism;
2423
import org.apache.kafka.common.security.token.delegation.DelegationToken;
2524
import org.apache.kafka.common.security.token.delegation.TokenInformation;
@@ -31,87 +30,84 @@
3130

3231
public class DelegationTokenCache {
3332

34-
private final CredentialCache credentialCache = new CredentialCache();
33+
private final CredentialCache credentialCache;
3534

3635
//Cache to hold all the tokens
37-
private final Map<String, TokenInformation> tokenCache = new ConcurrentHashMap<>();
36+
private final Map<String, DelegationToken> tokenCache = new ConcurrentHashMap<>();
3837

3938
//Cache to hold hmac->tokenId mapping. This is required for renew, expire requests
4039
private final Map<String, String> hmacTokenIdCache = new ConcurrentHashMap<>();
4140

42-
//Cache to hold tokenId->hmac mapping. This is required for removing entry from hmacTokenIdCache using tokenId.
43-
private final Map<String, String> tokenIdHmacCache = new ConcurrentHashMap<>();
44-
4541
public DelegationTokenCache(Collection<String> scramMechanisms) {
4642
//Create caches for scramMechanisms
47-
ScramCredentialUtils.createCache(credentialCache, scramMechanisms);
43+
this.credentialCache = new CredentialCache(scramMechanisms);
4844
}
4945

5046
public ScramCredential credential(String mechanism, String tokenId) {
51-
CredentialCache.Cache<ScramCredential> cache = credentialCache.cache(mechanism, ScramCredential.class);
47+
CredentialCache.Cache cache = credentialCache.cache(mechanism);
5248
return cache == null ? null : cache.get(tokenId);
5349
}
5450

5551
public String owner(String tokenId) {
56-
TokenInformation tokenInfo = tokenCache.get(tokenId);
57-
return tokenInfo == null ? null : tokenInfo.owner().getName();
52+
DelegationToken token = tokenCache.get(tokenId);
53+
return token == null ? null : token.tokenInfo().owner().getName();
5854
}
5955

6056
public void updateCache(DelegationToken token, Map<String, ScramCredential> scramCredentialMap) {
61-
//Update TokenCache
62-
String tokenId = token.tokenInfo().tokenId();
63-
addToken(tokenId, token.tokenInfo());
64-
String hmac = token.hmacAsBase64String();
57+
//Update TokenCache and hmac-id cache
58+
addToken(token);
6559
//Update Scram Credentials
66-
updateCredentials(tokenId, scramCredentialMap);
67-
//Update hmac-id cache
68-
hmacTokenIdCache.put(hmac, tokenId);
69-
tokenIdHmacCache.put(tokenId, hmac);
60+
updateCredentials(token.tokenInfo().tokenId(), scramCredentialMap);
7061
}
7162

7263
public void removeCache(String tokenId) {
7364
removeToken(tokenId);
7465
updateCredentials(tokenId, new HashMap<>());
7566
}
7667

77-
public String tokenIdForHmac(String base64hmac) {
78-
return hmacTokenIdCache.get(base64hmac);
79-
}
80-
8168
public TokenInformation tokenForHmac(String base64hmac) {
8269
String tokenId = hmacTokenIdCache.get(base64hmac);
83-
return tokenId == null ? null : tokenCache.get(tokenId);
70+
DelegationToken token = tokenId == null ? null : tokenCache.get(tokenId);
71+
return token == null ? null : token.tokenInfo();
8472
}
8573

86-
public TokenInformation addToken(String tokenId, TokenInformation tokenInfo) {
87-
return tokenCache.put(tokenId, tokenInfo);
74+
public void addToken(DelegationToken token) {
75+
String tokenId = token.tokenInfo().tokenId();
76+
String hmac = token.hmacAsBase64String();
77+
DelegationToken previous = tokenCache.put(tokenId, token);
78+
hmacTokenIdCache.put(hmac, tokenId);
79+
if (previous != null) {
80+
String previousHmac = previous.hmacAsBase64String();
81+
//Drop the hmac of the replaced token, unless the new token has the same hmac
82+
if (!previousHmac.equals(hmac)) {
83+
hmacTokenIdCache.remove(previousHmac);
84+
}
85+
}
8886
}
8987

9088
public void removeToken(String tokenId) {
91-
TokenInformation tokenInfo = tokenCache.remove(tokenId);
92-
if (tokenInfo != null) {
93-
String hmac = tokenIdHmacCache.remove(tokenInfo.tokenId());
94-
if (hmac != null) {
95-
hmacTokenIdCache.remove(hmac);
96-
}
89+
DelegationToken token = tokenCache.remove(tokenId);
90+
if (token != null) {
91+
hmacTokenIdCache.remove(token.hmacAsBase64String());
9792
}
9893
}
9994

100-
public Collection<TokenInformation> tokens() {
95+
public Collection<DelegationToken> tokens() {
10196
return tokenCache.values();
10297
}
10398

10499
public TokenInformation token(String tokenId) {
105-
return tokenCache.get(tokenId);
100+
DelegationToken token = tokenCache.get(tokenId);
101+
return token == null ? null : token.tokenInfo();
106102
}
107103

108-
public CredentialCache.Cache<ScramCredential> credentialCache(String mechanism) {
109-
return credentialCache.cache(mechanism, ScramCredential.class);
104+
public CredentialCache.Cache credentialCache(String mechanism) {
105+
return credentialCache.cache(mechanism);
110106
}
111107

112108
private void updateCredentials(String tokenId, Map<String, ScramCredential> scramCredentialMap) {
113109
for (String mechanism : ScramMechanism.mechanismNames()) {
114-
CredentialCache.Cache<ScramCredential> cache = credentialCache.cache(mechanism, ScramCredential.class);
110+
CredentialCache.Cache cache = credentialCache.cache(mechanism);
115111
if (cache != null) {
116112
ScramCredential credential = scramCredentialMap.get(mechanism);
117113
if (credential == null) {
@@ -122,4 +118,4 @@ private void updateCredentials(String tokenId, Map<String, ScramCredential> scra
122118
}
123119
}
124120
}
125-
}
121+
}
Collapse file

‎clients/src/test/java/org/apache/kafka/common/security/authenticator/ClientAuthenticationFailureTest.java‎

Copy file name to clipboardExpand all lines: clients/src/test/java/org/apache/kafka/common/security/authenticator/ClientAuthenticationFailureTest.java
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.kafka.common.network.NioEchoServer;
3636
import org.apache.kafka.common.security.TestSecurityConfig;
3737
import org.apache.kafka.common.security.auth.SecurityProtocol;
38+
import org.apache.kafka.common.security.scram.internals.ScramMechanism;
3839
import org.apache.kafka.common.serialization.StringDeserializer;
3940
import org.apache.kafka.common.serialization.StringSerializer;
4041
import org.apache.kafka.common.utils.MockTime;
@@ -139,6 +140,6 @@ private NioEchoServer createEchoServer(SecurityProtocol securityProtocol) throws
139140

140141
private NioEchoServer createEchoServer(ListenerName listenerName, SecurityProtocol securityProtocol) throws Exception {
141142
return NetworkTestUtils.createEchoServer(listenerName, securityProtocol,
142-
new TestSecurityConfig(saslServerConfigs), new CredentialCache(), TIME);
143+
new TestSecurityConfig(saslServerConfigs), new CredentialCache(ScramMechanism.mechanismNames()), TIME);
143144
}
144145
}
Collapse file

‎…/internals/ScramCredentialUtilsTest.java‎ ‎…y/authenticator/CredentialCacheTest.java‎clients/src/test/java/org/apache/kafka/common/security/scram/internals/ScramCredentialUtilsTest.java renamed to clients/src/test/java/org/apache/kafka/common/security/authenticator/CredentialCacheTest.java clients/src/test/java/org/apache/kafka/common/security/scram/internals/ScramCredentialUtilsTest.java renamed to clients/src/test/java/org/apache/kafka/common/security/authenticator/CredentialCacheTest.java

Copy file name to clipboardExpand all lines: clients/src/test/java/org/apache/kafka/common/security/authenticator/CredentialCacheTest.java
+14-11Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,37 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.kafka.common.security.scram.internals;
17+
package org.apache.kafka.common.security.authenticator;
1818

19-
import org.apache.kafka.common.security.authenticator.CredentialCache;
2019
import org.apache.kafka.common.security.scram.ScramCredential;
20+
import org.apache.kafka.common.security.scram.internals.ScramFormatter;
21+
import org.apache.kafka.common.security.scram.internals.ScramMechanism;
2122

2223
import org.junit.jupiter.api.Test;
2324

24-
import java.util.Arrays;
25+
import java.util.List;
2526

2627
import static org.junit.jupiter.api.Assertions.assertEquals;
2728
import static org.junit.jupiter.api.Assertions.assertNotNull;
2829
import static org.junit.jupiter.api.Assertions.assertNull;
2930

30-
31-
public class ScramCredentialUtilsTest {
31+
public class CredentialCacheTest {
3232

3333
@Test
34-
public void scramCredentialCache() throws Exception {
35-
CredentialCache cache = new CredentialCache();
36-
ScramCredentialUtils.createCache(cache, Arrays.asList("SCRAM-SHA-512", "PLAIN"));
37-
assertNotNull(cache.cache(ScramMechanism.SCRAM_SHA_512.mechanismName(), ScramCredential.class), "Cache not created for enabled mechanism");
38-
assertNull(cache.cache(ScramMechanism.SCRAM_SHA_256.mechanismName(), ScramCredential.class), "Cache created for disabled mechanism");
34+
public void testScramCredentialCache() throws Exception {
35+
CredentialCache cache = new CredentialCache(List.of("SCRAM-SHA-512", "PLAIN"));
36+
assertNotNull(cache.cache(ScramMechanism.SCRAM_SHA_512.mechanismName()), "Cache not created for enabled mechanism");
37+
assertNull(cache.cache(ScramMechanism.SCRAM_SHA_256.mechanismName()), "Cache created for disabled mechanism");
38+
assertNull(cache.cache("PLAIN"), "Cache created for non-SCRAM mechanism");
3939

40-
CredentialCache.Cache<ScramCredential> sha512Cache = cache.cache(ScramMechanism.SCRAM_SHA_512.mechanismName(), ScramCredential.class);
40+
CredentialCache.Cache sha512Cache = cache.cache(ScramMechanism.SCRAM_SHA_512.mechanismName());
4141
ScramFormatter formatter = new ScramFormatter(ScramMechanism.SCRAM_SHA_512);
4242
ScramCredential credentialA = formatter.generateCredential("password", 4096);
4343
sha512Cache.put("userA", credentialA);
4444
assertEquals(credentialA, sha512Cache.get("userA"));
4545
assertNull(sha512Cache.get("userB"), "Invalid user credential");
46+
47+
assertEquals(credentialA, sha512Cache.remove("userA"));
48+
assertNull(sha512Cache.get("userA"));
4649
}
4750
}
Collapse file

‎clients/src/test/java/org/apache/kafka/common/security/authenticator/SaslAuthenticatorFailureDelayTest.java‎

Copy file name to clipboardExpand all lines: clients/src/test/java/org/apache/kafka/common/security/authenticator/SaslAuthenticatorFailureDelayTest.java
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.kafka.common.security.JaasContext;
3131
import org.apache.kafka.common.security.TestSecurityConfig;
3232
import org.apache.kafka.common.security.auth.SecurityProtocol;
33+
import org.apache.kafka.common.security.scram.internals.ScramMechanism;
3334
import org.apache.kafka.common.utils.MockTime;
3435
import org.apache.kafka.common.utils.internals.LogContext;
3536
import org.apache.kafka.test.TestUtils;
@@ -71,7 +72,7 @@ public void setup() throws Exception {
7172
CertStores clientCertStores = new CertStores(false, "localhost");
7273
saslServerConfigs = serverCertStores.getTrustingConfig(clientCertStores);
7374
saslClientConfigs = clientCertStores.getTrustingConfig(serverCertStores);
74-
credentialCache = new CredentialCache();
75+
credentialCache = new CredentialCache(ScramMechanism.mechanismNames());
7576
SaslAuthenticatorTest.TestLogin.loginCount.set(0);
7677
startTimeMs = time.milliseconds();
7778
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.