mimaison commented on code in PR #19424:
URL: https://github.com/apache/kafka/pull/19424#discussion_r2039048773


##########
core/src/test/scala/unit/kafka/server/KafkaConfigTest.scala:
##########
@@ -1173,14 +1173,15 @@ class KafkaConfigTest {
     assertEquals(123L, config.logFlushIntervalMs)
     assertEquals(CompressionType.SNAPPY, 
config.groupCoordinatorConfig.offsetTopicCompressionType)
     assertEquals(Sensor.RecordingLevel.DEBUG.toString, 
config.metricRecordingLevel)
-    assertEquals(false, config.tokenAuthEnabled)
-    assertEquals(7 * 24 * 60L * 60L * 1000L, config.delegationTokenMaxLifeMs)
-    assertEquals(24 * 60L * 60L * 1000L, config.delegationTokenExpiryTimeMs)
-    assertEquals(1 * 60L * 1000L * 60, 
config.delegationTokenExpiryCheckIntervalMs)
+    val delegationTokenManagerConfigs = new 
DelegationTokenManagerConfigs(config)
+    assertEquals(false, delegationTokenManagerConfigs.tokenAuthEnabled)
+    assertEquals(7 * 24 * 60L * 60L * 1000L, 
delegationTokenManagerConfigs.delegationTokenMaxLifeMs)
+    assertEquals(24 * 60L * 60L * 1000L, 
delegationTokenManagerConfigs.delegationTokenExpiryTimeMs)
+    assertEquals(1 * 60L * 1000L * 60, 
delegationTokenManagerConfigs.delegationTokenExpiryCheckIntervalMs)

Review Comment:
   Does it make sense to keep this here if `KafkaConfig` does not have any 
delegation token logic?



##########
core/src/main/java/kafka/server/builders/KafkaApisBuilder.java:
##########
@@ -166,11 +164,6 @@ public KafkaApisBuilder setTime(Time time) {
         return this;
     }
 
-    public KafkaApisBuilder setTokenManager(DelegationTokenManager 
tokenManager) {

Review Comment:
   Why are we removing this?



##########
server/src/main/java/org/apache/kafka/server/DelegationTokenManager.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server;
+
+import org.apache.kafka.common.security.auth.KafkaPrincipal;
+import org.apache.kafka.common.security.scram.ScramCredential;
+import org.apache.kafka.common.security.scram.internals.ScramFormatter;
+import org.apache.kafka.common.security.scram.internals.ScramMechanism;
+import org.apache.kafka.common.security.token.delegation.DelegationToken;
+import org.apache.kafka.common.security.token.delegation.TokenInformation;
+import 
org.apache.kafka.common.security.token.delegation.internals.DelegationTokenCache;
+import org.apache.kafka.server.config.DelegationTokenManagerConfigs;
+
+import java.nio.charset.StandardCharsets;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Function;
+
+import javax.crypto.Mac;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+
+public class DelegationTokenManager {
+    private static final String DEFAULT_HMAC_ALGORITHM = "HmacSHA512";
+    public static final long ERROR_TIMESTAMP = -1;
+
+    private final DelegationTokenCache tokenCache;
+    private final SecretKey secretKey;
+
+    public DelegationTokenManager(DelegationTokenManagerConfigs config, 
DelegationTokenCache tokenCache) {
+        this.tokenCache = tokenCache;
+
+        byte[] keyBytes = config.tokenAuthEnabled() ? 
config.delegationTokenSecretKey().value().getBytes(StandardCharsets.UTF_8) : 
null;
+        if (keyBytes == null || keyBytes.length == 0) {
+            this.secretKey = null;
+        } else {
+            this.secretKey = createSecretKey(keyBytes);
+        }
+    }
+
+    private static SecretKey createSecretKey(byte[] keyBytes) {
+        return new SecretKeySpec(keyBytes, DEFAULT_HMAC_ALGORITHM);
+    }
+
+    public static byte[] createHmac(String tokenId, SecretKey secretKey) {
+        try {
+            Mac mac = Mac.getInstance(DEFAULT_HMAC_ALGORITHM);
+            mac.init(secretKey);
+            return mac.doFinal(tokenId.getBytes(StandardCharsets.UTF_8));
+        } catch (InvalidKeyException e) {
+            throw new IllegalArgumentException("Invalid key to HMAC 
computation", e);
+        } catch (Exception e) {
+            throw new RuntimeException("Error while creating HMAC", e);
+        }
+    }
+
+    private Map<String, ScramCredential> prepareScramCredentials(String 
hmacString) throws NoSuchAlgorithmException {
+        Map<String, ScramCredential> scramCredentialMap = new HashMap<>();
+        for (ScramMechanism mechanism : ScramMechanism.values()) {
+            ScramFormatter formatter = new ScramFormatter(mechanism);
+            scramCredentialMap.put(mechanism.mechanismName(), 
formatter.generateCredential(hmacString, mechanism.minIterations()));
+        }
+        return scramCredentialMap;
+    }
+
+    public void updateToken(DelegationToken token) throws 
NoSuchAlgorithmException {
+        String hmacString = token.hmacAsBase64String();
+        Map<String, ScramCredential> scramCredentialMap = 
prepareScramCredentials(hmacString);
+        tokenCache.updateCache(token, scramCredentialMap);
+    }
+
+    public DelegationToken getDelegationToken(TokenInformation tokenInfo) {
+        byte[] hmac = createHmac(tokenInfo.tokenId(), secretKey);
+        return new DelegationToken(tokenInfo, hmac);
+    }
+
+    public void removeToken(String tokenId) {
+        tokenCache.removeCache(tokenId);
+    }
+
+    public List<DelegationToken> 
getTokens(java.util.function.Predicate<TokenInformation> filterToken) {

Review Comment:
   We can import `java.util.function.Predicate`



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to