HADOOP-10848. Cleanup calling of sun.security.krb5.Config.

This commit is contained in:
Akira Ajisaka 2019-04-08 10:02:34 +09:00
parent ec143cbf67
commit 0d47d283a6
No known key found for this signature in database
GPG Key ID: C1EDBB9CA400FD50
2 changed files with 29 additions and 37 deletions

View File

@ -24,7 +24,6 @@
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -44,6 +43,7 @@
import org.ietf.jgss.Oid; import org.ietf.jgss.Oid;
import javax.security.auth.Subject; import javax.security.auth.Subject;
import javax.security.auth.kerberos.KerberosPrincipal;
import javax.security.auth.kerberos.KerberosTicket; import javax.security.auth.kerberos.KerberosTicket;
import javax.security.auth.kerberos.KeyTab; import javax.security.auth.kerberos.KeyTab;
@ -90,36 +90,37 @@ public static Oid getOidInstance(String oidName)
return (Oid)oidField.get(oidClass); return (Oid)oidField.get(oidClass);
} }
/**
* Return the default realm for this JVM.
*
* @return The default realm
* @throws IllegalArgumentException If the default realm does not exist.
* @throws ClassNotFoundException Not thrown. Exists for compatibility.
* @throws NoSuchMethodException Not thrown. Exists for compatibility.
* @throws IllegalAccessException Not thrown. Exists for compatibility.
* @throws InvocationTargetException Not thrown. Exists for compatibility.
*/
public static String getDefaultRealm() public static String getDefaultRealm()
throws ClassNotFoundException, NoSuchMethodException, throws ClassNotFoundException, NoSuchMethodException,
IllegalArgumentException, IllegalAccessException, IllegalArgumentException, IllegalAccessException,
InvocationTargetException { InvocationTargetException {
Object kerbConf; // Any name is okay.
Class<?> classRef; return new KerberosPrincipal("tmp", 1).getRealm();
Method getInstanceMethod;
Method getDefaultRealmMethod;
if (IBM_JAVA) {
classRef = Class.forName("com.ibm.security.krb5.internal.Config");
} else {
classRef = Class.forName("sun.security.krb5.Config");
}
getInstanceMethod = classRef.getMethod("getInstance", new Class[0]);
kerbConf = getInstanceMethod.invoke(classRef, new Object[0]);
getDefaultRealmMethod = classRef.getDeclaredMethod("getDefaultRealm",
new Class[0]);
return (String)getDefaultRealmMethod.invoke(kerbConf, new Object[0]);
} }
/**
* Return the default realm for this JVM.
* If the default realm does not exist, this method returns null.
*
* @return The default realm
*/
public static String getDefaultRealmProtected() { public static String getDefaultRealmProtected() {
String realmString = null;
try { try {
realmString = getDefaultRealm(); return getDefaultRealm();
} catch (RuntimeException rte) {
//silently catch everything
} catch (Exception e) { } catch (Exception e) {
//silently return null //silently catch everything
return null;
} }
return realmString;
} }
/* /*

View File

@ -42,7 +42,6 @@
import javax.security.auth.login.AppConfigurationEntry; import javax.security.auth.login.AppConfigurationEntry;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -1039,19 +1038,11 @@ private static String describeProperty(String name, String def) {
* could be determined * could be determined
*/ */
public static String getDefaultRealmInJVM() { public static String getDefaultRealmInJVM() {
try { String realm = KerberosUtil.getDefaultRealmProtected();
return KerberosUtil.getDefaultRealm(); if (realm == null) {
// JDK7 realm = "";
} catch (ClassNotFoundException ignored) {
// ignored
} catch (NoSuchMethodException ignored) {
// ignored
} catch (IllegalAccessException ignored) {
// ignored
} catch (InvocationTargetException ignored) {
// ignored
} }
return ""; return realm;
} }
/** /**