Skip to content

Commit 99b1487

Browse files
committed
Rename SaslConstants to SaslMechanismFactory.
1 parent 23dd123 commit 99b1487

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
import org.apache.hadoop.ipc.protobuf.RpcHeaderProtos.RPCTraceInfoProto;
107107
import org.apache.hadoop.net.NetUtils;
108108
import org.apache.hadoop.security.AccessControlException;
109-
import org.apache.hadoop.security.SaslConstants;
109+
import org.apache.hadoop.security.SaslMechanismFactory;
110110
import org.apache.hadoop.security.SaslPropertiesResolver;
111111
import org.apache.hadoop.security.SaslRpcServer;
112112
import org.apache.hadoop.security.SaslRpcServer.AuthMethod;
@@ -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.isDefaultMechanism(AuthMethod.TOKEN.getMechanismName())) {
2613+
&& SaslMechanismFactory.isDefaultMechanism(AuthMethod.TOKEN.getMechanismName())) {
26142614
saslServer = createSaslServer(AuthMethod.TOKEN);
26152615
byte[] challenge = saslServer.evaluateResponse(new byte[0]);
26162616
RpcSaslProto.Builder negotiateBuilder =
Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.hadoop.security;
1919

20+
import org.apache.hadoop.HadoopIllegalArgumentException;
2021
import org.apache.hadoop.classification.InterfaceAudience;
2122
import org.apache.hadoop.classification.InterfaceStability;
2223
import org.apache.hadoop.conf.Configuration;
@@ -31,31 +32,44 @@
3132
*/
3233
@InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})
3334
@InterfaceStability.Evolving
34-
public class SaslConstants {
35-
public static final Logger LOG = LoggerFactory.getLogger(SaslConstants.class);
35+
public class SaslMechanismFactory {
36+
static final Logger LOG = LoggerFactory.getLogger(SaslMechanismFactory.class);
3637

3738
private static final String SASL_MECHANISM_ENV = "HADOOP_SASL_MECHANISM";
38-
public static final String SASL_MECHANISM;
39+
private static final String SASL_MECHANISM;
3940

4041
static {
4142
// env
42-
String mechanism = System.getenv(SASL_MECHANISM_ENV);
43-
LOG.debug("{} = {} (env)", SASL_MECHANISM_ENV, mechanism);
44-
45-
if (mechanism == null) {
46-
// conf
47-
final Configuration conf = new Configuration();
48-
mechanism = conf.get(HADOOP_SECURITY_SASL_MECHANISM_KEY,
49-
HADOOP_SECURITY_SASL_MECHANISM_DEFAULT);
43+
final String envValue = System.getenv(SASL_MECHANISM_ENV);
44+
LOG.debug("{} = {} (env)", SASL_MECHANISM_ENV, envValue);
45+
46+
// conf
47+
final Configuration conf = new Configuration();
48+
final String confValue = conf.get(HADOOP_SECURITY_SASL_MECHANISM_KEY,
49+
HADOOP_SECURITY_SASL_MECHANISM_DEFAULT);
50+
LOG.debug("{} = {} (conf)", HADOOP_SECURITY_SASL_MECHANISM_KEY, confValue);
51+
52+
if (envValue != null && confValue != null) {
53+
if (!envValue.equals(confValue)) {
54+
throw new HadoopIllegalArgumentException("SASL Mechanism mismatched: env "
55+
+ SASL_MECHANISM_ENV + " is " + envValue + " but conf "
56+
+ HADOOP_SECURITY_SASL_MECHANISM_KEY + " is " + confValue);
57+
}
5058
}
5159

52-
SASL_MECHANISM = mechanism != null? mechanism : HADOOP_SECURITY_SASL_MECHANISM_DEFAULT;
53-
LOG.debug("{} = {} (effective)", SASL_MECHANISM_ENV, SASL_MECHANISM);
60+
SASL_MECHANISM = envValue != null ? envValue
61+
: confValue != null ? confValue
62+
: HADOOP_SECURITY_SASL_MECHANISM_DEFAULT;
63+
LOG.debug("SASL_MECHANISM = {} (effective)", SASL_MECHANISM);
64+
}
65+
66+
public static String getMechanism() {
67+
return SASL_MECHANISM;
5468
}
5569

5670
public static boolean isDefaultMechanism(String mechanism) {
5771
return HADOOP_SECURITY_SASL_MECHANISM_DEFAULT.equals(mechanism);
5872
}
5973

60-
private SaslConstants() {}
74+
private SaslMechanismFactory() {}
6175
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ public enum AuthMethod {
225225
SIMPLE((byte) 80, ""),
226226
KERBEROS((byte) 81, "GSSAPI"),
227227
@Deprecated
228-
DIGEST((byte) 82, SaslConstants.SASL_MECHANISM),
229-
TOKEN((byte) 82, SaslConstants.SASL_MECHANISM),
228+
DIGEST((byte) 82, SaslMechanismFactory.getMechanism()),
229+
TOKEN((byte) 82, SaslMechanismFactory.getMechanism()),
230230
PLAIN((byte) 83, "PLAIN");
231231

232232
/** The code for this method. */

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestSaslRPC.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ public void handle(Callback[] callbacks)
536536
private static Pattern BadToken =
537537
Pattern.compile("^" + RemoteException.class.getName() +
538538
"\\("+ SaslException.class.getName() + "\\): " +
539-
SaslConstants.SASL_MECHANISM + ": digest response format violation.*");
539+
SaslMechanismFactory.getMechanism() + ": digest response format violation.*");
540540
private static Pattern KrbFailed =
541541
Pattern.compile(".*Failed on local exception:.* " +
542542
"Failed to specify server's Kerberos principal name.*");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import org.apache.hadoop.hdfs.protocol.datatransfer.IOStreamPair;
3434
import org.apache.hadoop.security.FastSaslClientFactory;
3535
import org.apache.hadoop.security.FastSaslServerFactory;
36-
import org.apache.hadoop.security.SaslConstants;
36+
import org.apache.hadoop.security.SaslMechanismFactory;
3737
import org.apache.hadoop.security.SaslInputStream;
3838
import org.apache.hadoop.security.SaslOutputStream;
3939

@@ -52,7 +52,7 @@ class SaslParticipant {
5252
// a short string.
5353
private static final String SERVER_NAME = "0";
5454
private static final String PROTOCOL = "hdfs";
55-
private static final String[] MECHANISM_ARRAY = {SaslConstants.SASL_MECHANISM};
55+
private static final String[] MECHANISM_ARRAY = {SaslMechanismFactory.getMechanism()};
5656
private static final byte[] EMPTY_BYTE_ARRAY = {};
5757

5858
// One of these will always be null.
@@ -127,7 +127,7 @@ private SaslParticipant(SaslClient saslClient) {
127127
}
128128

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

0 commit comments

Comments
 (0)