Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions service/src/java/org/apache/hive/service/auth/HiveAuthFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
package org.apache.hive.service.auth;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.net.ssl.SSLServerSocket;
import javax.security.auth.login.LoginException;
Expand Down Expand Up @@ -91,6 +94,29 @@ public String getAuthName() {
public static final String HS2_PROXY_USER = "hive.server2.proxy.user";
public static final String HS2_CLIENT_TOKEN = "hiveserver2ClientToken";

private static Field keytabFile = null;
private static Method getKeytab = null;
static {
Class<?> clz = UserGroupInformation.class;
try {
keytabFile = clz.getDeclaredField("keytabFile");
keytabFile.setAccessible(true);
} catch (NoSuchFieldException nfe) {
LOG.debug("Cannot find private field \"keytabFile\" in class: " +
UserGroupInformation.class.getCanonicalName(), nfe);
keytabFile = null;
}

try {
getKeytab = clz.getDeclaredMethod("getKeytab");
getKeytab.setAccessible(true);
} catch(NoSuchMethodException nme) {
LOG.debug("Cannot find private method \"getKeytab\" in class:" +
UserGroupInformation.class.getCanonicalName(), nme);
getKeytab = null;
}
}

public HiveAuthFactory(HiveConf conf) throws TTransportException {
this.conf = conf;
transportMode = conf.getVar(HiveConf.ConfVars.HIVE_SERVER2_TRANSPORT_MODE);
Expand Down Expand Up @@ -381,4 +407,25 @@ public static void verifyProxyAccess(String realUser, String proxyUser, String i
}
}

public static boolean needUgiLogin(UserGroupInformation ugi, String principal, String keytab) {
return null == ugi || !ugi.hasKerberosCredentials() || !ugi.getUserName().equals(principal) ||
!Objects.equals(keytab, getKeytabFromUgi());
}

private static String getKeytabFromUgi() {
synchronized (UserGroupInformation.class) {
try {
if (keytabFile != null) {
return (String) keytabFile.get(null);
} else if (getKeytab != null) {
return (String) getKeytab.invoke(UserGroupInformation.getCurrentUser());
} else {
return null;
}
} catch (Exception e) {
LOG.debug("Fail to get keytabFile path via reflection", e);
return null;
}
}
}
}