K0K0V0K commented on code in PR #7827:
URL: https://github.com/apache/hadoop/pull/7827#discussion_r2230768478


##########
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/token/SecretManagerConfig.java:
##########
@@ -0,0 +1,133 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hadoop.security.token;
+
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.classification.InterfaceStability;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.crypto.KeyGenerator;
+import javax.crypto.Mac;
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * Provides configuration and utility methods for managing cryptographic key 
generation
+ * and message authentication code (MAC) generation using specified algorithms 
and key lengths.
+ * <p>
+ * This class supports static access to the selected cryptographic algorithm 
and key length,
+ * and provides methods to create configured {@link javax.crypto.KeyGenerator}
+ * and {@link javax.crypto.Mac} instances.
+ * The configuration is initialized statically from a provided {@link 
Configuration} object.
+ * <p>
+ * The {@link SecretManager} has some static method, so static configuration 
is required
+ */
+@InterfaceAudience.Public
+@InterfaceStability.Evolving
+public class SecretManagerConfig {
+  private static final Logger LOG = 
LoggerFactory.getLogger(SecretManagerConfig.class);
+  private static String selectedAlgorithm;
+  private static int selectedLength;
+  private static boolean initialized;
+
+  static {
+    update(new Configuration());
+  }
+
+  private SecretManagerConfig() {
+  }
+
+  /**
+   * Updates the selected cryptographic algorithm and key length using the 
provided
+   * Hadoop {@link Configuration}. This method reads the values for
+   * {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_GENERATOR_ALGORITHM_KEY} and
+   * {@code HADOOP_SECURITY_SECRET_MANAGER_KEY_LENGTH_KEY}, or uses default 
values if not set.
+   *
+   * @param conf the configuration object containing cryptographic settings
+   */
+  public static synchronized void update(Configuration conf) {
+    if (initialized) {
+      LOG.warn(
+        "Keygen or Mac was already initialized with older config, those will 
not be updated");

Review Comment:
   The current solution seems not leaking
   ```
     @Test
     public void tmpTest() throws InterruptedException {
       new Thread("tmpTest-1") {
         @Override
         public void run() {
           Mac mac = SecretManagerConfig.createMac();
           try {
             Thread.sleep(2000);
           } catch (InterruptedException e) {
             throw new RuntimeException(e);
           }      }
       }.start();
       new Thread("tmpTest-2") {
         @Override
         public void run() {
           Mac mac = SecretManagerConfig.createMac();
           try {
             Thread.sleep(2000);
           } catch (InterruptedException e) {
             throw new RuntimeException(e);
           }      }
       }.start();
       Thread.sleep(500);
       System.err.println("MACS:" + SecretManagerConfig.MACS);
       Thread.sleep(2000);
       System.gc();
       Thread.sleep(500);
       System.err.println("MACS:" + SecretManagerConfig.MACS);
     }
   ```
   OUTPUT:
   ```
   MACS:{Thread[tmpTest-1,5,main]=javax.crypto.Mac@52aa2946, 
Thread[tmpTest-2,5,main]=javax.crypto.Mac@4de5031f}
   MACS:{}
   ```
   
   Feel free to ask modification



-- 
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: common-issues-unsubscr...@hadoop.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: common-issues-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-issues-h...@hadoop.apache.org

Reply via email to