瀏覽代碼

HADOOP-19342. SaslRpcServer.AuthMethod print INFO messages in client side. (#7174)

Tsz-Wo Nicholas Sze 5 月之前
父節點
當前提交
cd2cffe73f

+ 11 - 17
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/SaslMechanismFactory.java

@@ -17,7 +17,6 @@
  */
  */
 package org.apache.hadoop.security;
 package org.apache.hadoop.security;
 
 
-import org.apache.hadoop.HadoopIllegalArgumentException;
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceStability;
 import org.apache.hadoop.classification.InterfaceStability;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.conf.Configuration;
@@ -36,39 +35,34 @@ public final class SaslMechanismFactory {
   static final Logger LOG = LoggerFactory.getLogger(SaslMechanismFactory.class);
   static final Logger LOG = LoggerFactory.getLogger(SaslMechanismFactory.class);
 
 
   private static final String SASL_MECHANISM_ENV = "HADOOP_SASL_MECHANISM";
   private static final String SASL_MECHANISM_ENV = "HADOOP_SASL_MECHANISM";
-  private static final String SASL_MECHANISM;
+  private static volatile String mechanism;
 
 
-  static {
+  private static synchronized String getSynchronously() {
     // env
     // env
     final String envValue = System.getenv(SASL_MECHANISM_ENV);
     final String envValue = System.getenv(SASL_MECHANISM_ENV);
     LOG.debug("{} = {} (env)", SASL_MECHANISM_ENV, envValue);
     LOG.debug("{} = {} (env)", SASL_MECHANISM_ENV, envValue);
 
 
     // conf
     // conf
-    final Configuration conf = new Configuration(false);
+    final Configuration conf = new Configuration();
     final String confValue = conf.get(HADOOP_SECURITY_SASL_MECHANISM_KEY,
     final String confValue = conf.get(HADOOP_SECURITY_SASL_MECHANISM_KEY,
         HADOOP_SECURITY_SASL_MECHANISM_DEFAULT);
         HADOOP_SECURITY_SASL_MECHANISM_DEFAULT);
     LOG.debug("{} = {} (conf)", HADOOP_SECURITY_SASL_MECHANISM_KEY, confValue);
     LOG.debug("{} = {} (conf)", HADOOP_SECURITY_SASL_MECHANISM_KEY, confValue);
 
 
-    if (envValue != null && confValue != null) {
-      if (!envValue.equals(confValue)) {
-        throw new HadoopIllegalArgumentException("SASL Mechanism mismatched: env "
-            + SASL_MECHANISM_ENV + " is " + envValue + " but conf "
-            + HADOOP_SECURITY_SASL_MECHANISM_KEY + " is " + confValue);
-      }
-    }
-
-    SASL_MECHANISM = envValue != null ? envValue
+    // env has a higher precedence than conf
+    mechanism = envValue != null ? envValue
         : confValue != null ? confValue
         : confValue != null ? confValue
         : HADOOP_SECURITY_SASL_MECHANISM_DEFAULT;
         : HADOOP_SECURITY_SASL_MECHANISM_DEFAULT;
-    LOG.debug("SASL_MECHANISM = {} (effective)", SASL_MECHANISM);
+    LOG.debug("SASL_MECHANISM = {} (effective)", mechanism);
+    return mechanism;
   }
   }
 
 
   public static String getMechanism() {
   public static String getMechanism() {
-    return SASL_MECHANISM;
+    final String value = mechanism;
+    return value != null ? value : getSynchronously();
   }
   }
 
 
-  public static boolean isDefaultMechanism(String mechanism) {
-    return HADOOP_SECURITY_SASL_MECHANISM_DEFAULT.equals(mechanism);
+  public static boolean isDefaultMechanism(String saslMechanism) {
+    return HADOOP_SECURITY_SASL_MECHANISM_DEFAULT.equals(saslMechanism);
   }
   }
 
 
   private SaslMechanismFactory() {}
   private SaslMechanismFactory() {}

+ 9 - 6
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/SaslRpcServer.java

@@ -29,6 +29,7 @@ import java.security.Security;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
+import java.util.function.Supplier;
 
 
 import javax.security.auth.callback.Callback;
 import javax.security.auth.callback.Callback;
 import javax.security.auth.callback.CallbackHandler;
 import javax.security.auth.callback.CallbackHandler;
@@ -225,19 +226,21 @@ public class SaslRpcServer {
     SIMPLE((byte) 80, ""),
     SIMPLE((byte) 80, ""),
     KERBEROS((byte) 81, "GSSAPI"),
     KERBEROS((byte) 81, "GSSAPI"),
     @Deprecated
     @Deprecated
-    DIGEST((byte) 82, SaslMechanismFactory.getMechanism()),
-    TOKEN((byte) 82, SaslMechanismFactory.getMechanism()),
+    DIGEST((byte) 82, SaslMechanismFactory::getMechanism),
+    TOKEN((byte) 82, SaslMechanismFactory::getMechanism),
     PLAIN((byte) 83, "PLAIN");
     PLAIN((byte) 83, "PLAIN");
 
 
     /** The code for this method. */
     /** The code for this method. */
     public final byte code;
     public final byte code;
-    public final String mechanismName;
+    private final Supplier<String> mechanismName;
 
 
     private AuthMethod(byte code, String mechanismName) { 
     private AuthMethod(byte code, String mechanismName) { 
+      this(code, () -> mechanismName);
+    }
+
+    AuthMethod(byte code, Supplier<String> mechanismName) {
       this.code = code;
       this.code = code;
       this.mechanismName = mechanismName;
       this.mechanismName = mechanismName;
-      LOG.info("{} {}: code={}, mechanism=\"{}\"",
-          getClass().getSimpleName(), name(), code, mechanismName);
     }
     }
 
 
     private static final int FIRST_CODE = values()[0].code;
     private static final int FIRST_CODE = values()[0].code;
@@ -253,7 +256,7 @@ public class SaslRpcServer {
      * @return mechanismName.
      * @return mechanismName.
      */
      */
     public String getMechanismName() {
     public String getMechanismName() {
-      return mechanismName;
+      return mechanismName.get();
     }
     }
 
 
     /**
     /**