OmniaGM commented on code in PR #15246:
URL: https://github.com/apache/kafka/pull/15246#discussion_r1469969299


##########
server-common/src/main/java/org/apache/kafka/security/EncryptingPasswordEncoder.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.security;
+
+import org.apache.kafka.common.config.ConfigException;
+import org.apache.kafka.common.config.types.Password;
+import org.apache.kafka.server.util.Csv;
+
+import javax.crypto.Cipher;
+import javax.crypto.SecretKeyFactory;
+import javax.crypto.spec.PBEKeySpec;
+import javax.crypto.spec.SecretKeySpec;
+import java.nio.charset.StandardCharsets;
+import java.security.GeneralSecurityException;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.security.spec.InvalidKeySpecException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+/**
+ * Password encoder and decoder implementation. Encoded passwords are 
persisted as a CSV map
+ * containing the encoded password in base64 and along with the properties 
used for encryption.
+ */
+public class EncryptingPasswordEncoder implements PasswordEncoder {
+
+    private final SecureRandom secureRandom = new SecureRandom();
+
+    private final Password secret;
+    private final Optional<String> keyFactoryAlgorithm;
+    private final String cipherAlgorithm;
+    private final int keyLength;
+    private final int iterations;
+    private final CipherParamsEncoder cipherParamsEncoder;
+
+
+    /**
+     * @param secret The secret used for encoding and decoding
+     * @param keyFactoryAlgorithm  Key factory algorithm if configured. By 
default, PBKDF2WithHmacSHA512 is
+     *                             used if available, PBKDF2WithHmacSHA1 
otherwise.
+     * @param cipherAlgorithm Cipher algorithm used for encoding.
+     * @param keyLength Key length used for encoding. This should be valid for 
the specified algorithms.
+     * @param iterations Iteration count used for encoding.
+     * The provided `keyFactoryAlgorithm`, `cipherAlgorithm`, `keyLength` and 
`iterations` are used for encoding passwords.
+     * The values used for encoding are stored along with the encoded password 
and the stored values are used for decoding.
+     */
+    public EncryptingPasswordEncoder(
+            Password secret,
+            Optional<String> keyFactoryAlgorithm,

Review Comment:
   I'm aware that the original Scala code used `Option`, and 
`config.passwordEncoderKeyFactoryAlgorithm` returns an `Option`. However, 
treating Java's `Optional` as equivalent to Scala's `Option` is generally 
considered unfavorable in the Java community. Typically, the Java community 
uses `Optional` for returning values, while Scala uses `Option` to avoid 
nullable variables/parameters. Therefore, I'm curious about the pattern we 
usually adopt in the Kafka codebase.



##########
server-common/src/main/java/org/apache/kafka/server/util/Csv.java:
##########
@@ -0,0 +1,41 @@
+/*
+ * 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.util;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Csv {

Review Comment:
   like that you broke `Csv` utils out of `CoreUtils`. Am assuming later we can 
move `parseCsvList` as well. 



##########
server-common/src/main/java/org/apache/kafka/security/NoOpPasswordEncoder.java:
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.security;
+
+import org.apache.kafka.common.config.types.Password;
+
+/**
+ * A password encoder that does not modify the given password. This is used in 
KRaft mode only.
+ */
+public class NoOpPasswordEncoder implements PasswordEncoder {

Review Comment:
   Am wondering if this class deserve an extra file or if it can be just 
defined inside the PasswordEncoder interface. Maybe like this 
https://github.com/apache/kafka/blob/82920ffad00285c11453e64e220ac0ec2666ac54/server-common/src/main/java/org/apache/kafka/server/common/DirectoryEventHandler.java#L27
 



-- 
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