Skip to content

Commit eb90719

Browse files
committed
Add a new conf to set SASL mechanism.
1 parent 631b9b6 commit eb90719

File tree

7 files changed

+43
-6
lines changed

7 files changed

+43
-6
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/CommonConfigurationKeysPublic.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,10 @@ public class CommonConfigurationKeysPublic {
736736
*/
737737
public static final String HADOOP_RPC_PROTECTION =
738738
"hadoop.rpc.protection";
739+
public static final String HADOOP_SECURITY_SASL_MECHANISM_KEY
740+
= "hadoop.security.sasl.mechanism";
741+
public static final String HADOOP_SECURITY_SASL_MECHANISM_DEFAULT
742+
= "DIGEST-MD5";
739743
public static final String HADOOP_SECURITY_SASL_CUSTOMIZEDCALLBACKHANDLER_CLASS_KEY
740744
= "hadoop.security.sasl.CustomizedCallbackHandler.class";
741745
/** Class to override Sasl Properties for a connection */

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Server.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2610,7 +2610,7 @@ private RpcSaslProto buildSaslNegotiateResponse()
26102610
// accelerate token negotiation by sending initial challenge
26112611
// in the negotiation response
26122612
if (enabledAuthMethods.contains(AuthMethod.TOKEN)
2613-
&& SaslConstants.SASL_MECHANISM_DEFAULT.equals(AuthMethod.TOKEN.getMechanismName())) {
2613+
&& SaslConstants.isDefaultMechanism(AuthMethod.TOKEN.getMechanismName())) {
26142614
saslServer = createSaslServer(AuthMethod.TOKEN);
26152615
byte[] challenge = saslServer.evaluateResponse(new byte[0]);
26162616
RpcSaslProto.Builder negotiateBuilder =

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/CustomizedCallbackHandler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ private static synchronized CustomizedCallbackHandler getSynchronously(
4848
//cache miss
4949
final Class<?> clazz = conf.getClass(key, DefaultHandler.class);
5050
LOG.info("{} = {}", key, clazz);
51+
if (clazz == DefaultHandler.class) {
52+
return DefaultHandler.INSTANCE;
53+
}
54+
5155
final Object created;
5256
try {
5357
created = clazz.newInstance();
5458
} catch (Exception e) {
55-
throw new IllegalStateException("Failed to create a new instance of " + clazz, e);
59+
LOG.warn("Failed to create a new instance of {}, fallback to {}",
60+
clazz, DefaultHandler.class, e);
61+
return DefaultHandler.INSTANCE;
5662
}
5763

5864
final CustomizedCallbackHandler handler = created instanceof CustomizedCallbackHandler ?
@@ -74,6 +80,8 @@ private Cache() { }
7480
}
7581

7682
class DefaultHandler implements CustomizedCallbackHandler {
83+
private static final DefaultHandler INSTANCE = new DefaultHandler();
84+
7785
@Override
7886
public void handleCallbacks(List<Callback> callbacks, String username, char[] password)
7987
throws UnsupportedCallbackException {

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/SaslConstants.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919

2020
import org.apache.hadoop.classification.InterfaceAudience;
2121
import org.apache.hadoop.classification.InterfaceStability;
22+
import org.apache.hadoop.conf.Configuration;
2223
import org.slf4j.Logger;
2324
import org.slf4j.LoggerFactory;
2425

26+
import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY_SASL_MECHANISM_DEFAULT;
27+
import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY_SASL_MECHANISM_KEY;
28+
2529
/**
2630
* SASL related constants.
2731
*/
@@ -32,14 +36,25 @@ public class SaslConstants {
3236

3337
private static final String SASL_MECHANISM_ENV = "HADOOP_SASL_MECHANISM";
3438
public static final String SASL_MECHANISM;
35-
public static final String SASL_MECHANISM_DEFAULT = "DIGEST-MD5";
3639

3740
static {
38-
final String mechanism = System.getenv(SASL_MECHANISM_ENV);
41+
// env
42+
String mechanism = System.getenv(SASL_MECHANISM_ENV);
3943
LOG.debug("{} = {} (env)", SASL_MECHANISM_ENV, mechanism);
40-
SASL_MECHANISM = mechanism != null? mechanism : SASL_MECHANISM_DEFAULT;
44+
45+
if (mechanism == null) {
46+
// conf
47+
final Configuration conf = new Configuration();
48+
mechanism = conf.get(HADOOP_SECURITY_SASL_MECHANISM_KEY, HADOOP_SECURITY_SASL_MECHANISM_DEFAULT);
49+
}
50+
51+
SASL_MECHANISM = mechanism != null? mechanism : HADOOP_SECURITY_SASL_MECHANISM_DEFAULT;
4152
LOG.debug("{} = {} (effective)", SASL_MECHANISM_ENV, SASL_MECHANISM);
4253
}
4354

55+
public static boolean isDefaultMechanism(String mechanism) {
56+
return HADOOP_SECURITY_SASL_MECHANISM_DEFAULT.equals(mechanism);
57+
}
58+
4459
private SaslConstants() {}
4560
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ public enum AuthMethod {
236236
private AuthMethod(byte code, String mechanismName) {
237237
this.code = code;
238238
this.mechanismName = mechanismName;
239+
LOG.info("{} {}: code={}, mechanism=\"{}\"",
240+
getClass().getSimpleName(), name(), code, mechanismName);
239241
}
240242

241243
private static final int FIRST_CODE = values()[0].code;

hadoop-common-project/hadoop-common/src/main/resources/core-default.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,14 @@
732732
</description>
733733
</property>
734734

735+
<property>
736+
<name>hadoop.security.sasl.mechanism</name>
737+
<value>DIGEST-MD5</value>
738+
<description>
739+
The SASL mechanism used in Hadoop.
740+
</description>
741+
</property>
742+
735743
<property>
736744
<name>hadoop.security.sasl.CustomizedCallbackHandler.class</name>
737745
<value></value>

hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/protocol/datatransfer/sasl/SaslParticipant.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private SaslParticipant(SaslClient saslClient) {
127127
}
128128

129129
byte[] createFirstMessage() throws SaslException {
130-
return MECHANISM_ARRAY[0].equals(SaslConstants.SASL_MECHANISM_DEFAULT) ? EMPTY_BYTE_ARRAY
130+
return SaslConstants.isDefaultMechanism(MECHANISM_ARRAY[0]) ? EMPTY_BYTE_ARRAY
131131
: evaluateChallengeOrResponse(EMPTY_BYTE_ARRAY);
132132
}
133133

0 commit comments

Comments
 (0)