Skip to content

Commit 52217fc

Browse files
authored
HADOOP-17432. [JDK 16] KerberosUtil#getOidInstance is broken by JEP 396 (#2546)
Reviewed-by: Steve Loughran <stevel@apache.org>
1 parent 7ef2875 commit 52217fc

File tree

4 files changed

+31
-33
lines changed
  • hadoop-common-project/hadoop-auth/src
  • hadoop-yarn-project/hadoop-yarn
    • hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/src/main/java/org/apache/hadoop/yarn/service/utils
    • hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/util

4 files changed

+31
-33
lines changed

hadoop-common-project/hadoop-auth/src/main/java/org/apache/hadoop/security/authentication/util/KerberosUtil.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.File;
2323
import java.io.IOException;
2424
import java.io.UnsupportedEncodingException;
25-
import java.lang.reflect.Field;
2625
import java.lang.reflect.InvocationTargetException;
2726
import java.net.InetAddress;
2827
import java.net.UnknownHostException;
@@ -73,21 +72,29 @@ private static Oid getNumericOidInstance(String oidName) {
7372
}
7473
}
7574

76-
public static Oid getOidInstance(String oidName)
77-
throws ClassNotFoundException, GSSException, NoSuchFieldException,
78-
IllegalAccessException {
79-
Class<?> oidClass;
80-
if (IBM_JAVA) {
81-
if ("NT_GSS_KRB5_PRINCIPAL".equals(oidName)) {
82-
// IBM JDK GSSUtil class does not have field for krb5 principal oid
83-
return new Oid("1.2.840.113554.1.2.2.1");
84-
}
85-
oidClass = Class.forName("com.ibm.security.jgss.GSSUtil");
86-
} else {
87-
oidClass = Class.forName("sun.security.jgss.GSSUtil");
75+
/**
76+
* Returns the Oid instance from string oidName.
77+
* Use {@link GSS_SPNEGO_MECH_OID}, {@link GSS_KRB5_MECH_OID},
78+
* or {@link NT_GSS_KRB5_PRINCIPAL_OID} instead.
79+
*
80+
* @return Oid instance
81+
* @param oidName The oid Name
82+
* @throws NoSuchFieldException if the input is not supported.
83+
*/
84+
@Deprecated
85+
public static Oid getOidInstance(String oidName)
86+
throws NoSuchFieldException {
87+
switch (oidName) {
88+
case "GSS_SPNEGO_MECH_OID":
89+
return GSS_SPNEGO_MECH_OID;
90+
case "GSS_KRB5_MECH_OID":
91+
return GSS_KRB5_MECH_OID;
92+
case "NT_GSS_KRB5_PRINCIPAL":
93+
return NT_GSS_KRB5_PRINCIPAL_OID;
94+
default:
95+
throw new NoSuchFieldException(
96+
"oidName: " + oidName + " is not supported.");
8897
}
89-
Field oidField = oidClass.getDeclaredField(oidName);
90-
return (Oid)oidField.get(oidClass);
9198
}
9299

93100
/**

hadoop-common-project/hadoop-auth/src/test/java/org/apache/hadoop/security/authentication/server/TestKerberosAuthenticationHandler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,10 @@ public String call() throws Exception {
301301
GSSContext gssContext = null;
302302
try {
303303
String servicePrincipal = KerberosTestUtils.getServerPrincipal();
304-
Oid oid =
305-
KerberosUtil.getOidInstance("NT_GSS_KRB5_PRINCIPAL");
304+
Oid oid = KerberosUtil.NT_GSS_KRB5_PRINCIPAL_OID;
306305
GSSName serviceName = gssManager.createName(servicePrincipal,
307306
oid);
308-
oid = KerberosUtil.getOidInstance("GSS_KRB5_MECH_OID");
307+
oid = KerberosUtil.GSS_KRB5_MECH_OID;
309308
gssContext = gssManager.createContext(serviceName, oid, null,
310309
GSSContext.DEFAULT_LIFETIME);
311310
gssContext.requestCredDeleg(true);

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/src/main/java/org/apache/hadoop/yarn/service/utils/HttpUtil.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@
3030
import org.apache.commons.codec.binary.Base64;
3131
import org.apache.hadoop.security.UserGroupInformation;
3232
import org.apache.hadoop.security.authentication.client.AuthenticationException;
33-
import org.apache.hadoop.security.authentication.util.KerberosUtil;
3433
import org.ietf.jgss.GSSContext;
3534
import org.ietf.jgss.GSSException;
3635
import org.ietf.jgss.GSSManager;
3736
import org.ietf.jgss.GSSName;
38-
import org.ietf.jgss.Oid;
3937
import org.slf4j.Logger;
4038
import org.slf4j.LoggerFactory;
4139

@@ -72,17 +70,16 @@ public static String generateToken(String server) throws
7270
@Override
7371
public String run() throws Exception {
7472
try {
75-
// This Oid for Kerberos GSS-API mechanism.
76-
Oid mechOid = KerberosUtil.getOidInstance("GSS_KRB5_MECH_OID");
7773
GSSManager manager = GSSManager.getInstance();
7874
// GSS name for server
7975
GSSName serverName = manager.createName("HTTP@" + server,
8076
GSSName.NT_HOSTBASED_SERVICE);
8177
// Create a GSSContext for authentication with the service.
8278
// We're passing client credentials as null since we want them to
8379
// be read from the Subject.
80+
// We're passing Oid as null to use the default.
8481
GSSContext gssContext = manager.createContext(
85-
serverName.canonicalize(mechOid), mechOid, null,
82+
serverName.canonicalize(null), null, null,
8683
GSSContext.DEFAULT_LIFETIME);
8784
gssContext.requestMutualAuth(true);
8885
gssContext.requestCredDeleg(true);
@@ -95,9 +92,8 @@ public String run() throws Exception {
9592
LOG.debug("Got valid challenge for host {}", serverName);
9693
return new String(BASE_64_CODEC.encode(outToken),
9794
StandardCharsets.US_ASCII);
98-
} catch (GSSException | IllegalAccessException
99-
| NoSuchFieldException | ClassNotFoundException e) {
100-
LOG.error("Error: {}", e);
95+
} catch (GSSException e) {
96+
LOG.error("Error: ", e);
10197
throw new AuthenticationException(e);
10298
}
10399
}

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/util/YarnClientUtils.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@
3131
import org.apache.hadoop.security.SecurityUtil;
3232
import org.apache.hadoop.security.UserGroupInformation;
3333
import org.apache.hadoop.security.authentication.client.AuthenticationException;
34-
import org.apache.hadoop.security.authentication.util.KerberosUtil;
3534
import org.apache.hadoop.yarn.api.records.NodeLabel;
3635
import org.apache.hadoop.yarn.conf.HAUtil;
3736
import org.apache.hadoop.yarn.conf.YarnConfiguration;
3837
import org.ietf.jgss.GSSContext;
3938
import org.ietf.jgss.GSSException;
4039
import org.ietf.jgss.GSSManager;
4140
import org.ietf.jgss.GSSName;
42-
import org.ietf.jgss.Oid;
4341
import org.slf4j.Logger;
4442
import org.slf4j.LoggerFactory;
4543

@@ -222,17 +220,16 @@ public static String generateToken(String server) throws IOException,
222220
@Override
223221
public String run() throws Exception {
224222
try {
225-
// This Oid for Kerberos GSS-API mechanism.
226-
Oid mechOid = KerberosUtil.getOidInstance("GSS_KRB5_MECH_OID");
227223
GSSManager manager = GSSManager.getInstance();
228224
// GSS name for server
229225
GSSName serverName = manager.createName("HTTP@" + server,
230226
GSSName.NT_HOSTBASED_SERVICE);
231227
// Create a GSSContext for authentication with the service.
232228
// We're passing client credentials as null since we want them to
233229
// be read from the Subject.
230+
// We're passing Oid as null to use the default.
234231
GSSContext gssContext = manager.createContext(
235-
serverName.canonicalize(mechOid), mechOid, null,
232+
serverName.canonicalize(null), null, null,
236233
GSSContext.DEFAULT_LIFETIME);
237234
gssContext.requestMutualAuth(true);
238235
gssContext.requestCredDeleg(true);
@@ -245,8 +242,7 @@ public String run() throws Exception {
245242
LOG.debug("Got valid challenge for host {}", serverName);
246243
return new String(BASE_64_CODEC.encode(outToken),
247244
StandardCharsets.US_ASCII);
248-
} catch (GSSException | IllegalAccessException
249-
| NoSuchFieldException | ClassNotFoundException e) {
245+
} catch (GSSException e) {
250246
LOG.error("Error: ", e);
251247
throw new AuthenticationException(e);
252248
}

0 commit comments

Comments
 (0)