apply KerberosInterpreter to JDBCInterpreter

This commit is contained in:
Prabhjyot Singh 2017-06-27 14:25:13 +05:30
parent 341883d9ed
commit 57ea80c0c3

View file

@ -50,6 +50,7 @@ import org.apache.zeppelin.interpreter.InterpreterContext;
import org.apache.zeppelin.interpreter.InterpreterException;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.interpreter.InterpreterResult.Code;
import org.apache.zeppelin.interpreter.KerberosInterpreter;
import org.apache.zeppelin.interpreter.ResultMessages;
import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion;
import org.apache.zeppelin.jdbc.security.JDBCSecurityImpl;
@ -89,7 +90,7 @@ import static org.apache.hadoop.security.UserGroupInformation.AuthenticationMeth
* }
* </p>
*/
public class JDBCInterpreter extends Interpreter {
public class JDBCInterpreter extends KerberosInterpreter {
private Logger logger = LoggerFactory.getLogger(JDBCInterpreter.class);
@ -147,6 +148,24 @@ public class JDBCInterpreter extends Interpreter {
maxLineResults = MAX_LINE_DEFAULT;
}
@Override
protected boolean runKerberosLogin() {
try {
UserGroupInformation.AuthenticationMethod authType = JDBCSecurityImpl.getAuthtype(property);
if (authType.equals(KERBEROS)) {
if (UserGroupInformation.isLoginKeytabBased()) {
UserGroupInformation.getLoginUser().reloginFromKeytab();
} else if (UserGroupInformation.isLoginTicketBased()) {
UserGroupInformation.getLoginUser().reloginFromTicketCache();
}
}
} catch (Exception e) {
logger.error("Unable to run kinit for zeppelin", e);
return false;
}
return true;
}
public HashMap<String, Properties> getPropertiesMap() {
return basePropretiesMap;
}
@ -188,6 +207,11 @@ public class JDBCInterpreter extends Interpreter {
logger.debug("JDBC PropretiesMap: {}", basePropretiesMap);
setMaxLineResults();
UserGroupInformation.AuthenticationMethod authType = JDBCSecurityImpl.getAuthtype(property);
if (authType.equals(KERBEROS)) {
startKerberosLoginThread();
}
}
private void setMaxLineResults() {
@ -709,51 +733,19 @@ public class JDBCInterpreter extends Interpreter {
}
getJDBCConfiguration(user).removeStatement(paragraphId);
} catch (Throwable e) {
if (e.getCause() instanceof TTransportException &&
Throwables.getStackTraceAsString(e).contains("GSS") &&
getJDBCConfiguration(user).isConnectionInDBDriverPoolSuccessful(propertyKey)) {
return reLoginFromKeytab(propertyKey, sql, interpreterContext, interpreterResult);
} else {
logger.error("Cannot run " + sql, e);
String errorMsg = Throwables.getStackTraceAsString(e);
try {
closeDBPool(user, propertyKey);
} catch (SQLException e1) {
logger.error("Cannot close DBPool for user, propertyKey: " + user + propertyKey, e1);
}
interpreterResult.add(errorMsg);
return new InterpreterResult(Code.ERROR, interpreterResult.message());
logger.error("Cannot run " + sql, e);
String errorMsg = Throwables.getStackTraceAsString(e);
try {
closeDBPool(user, propertyKey);
} catch (SQLException e1) {
logger.error("Cannot close DBPool for user, propertyKey: " + user + propertyKey, e1);
}
interpreterResult.add(errorMsg);
return new InterpreterResult(Code.ERROR, interpreterResult.message());
}
return interpreterResult;
}
private InterpreterResult reLoginFromKeytab(String propertyKey, String sql,
InterpreterContext interpreterContext, InterpreterResult interpreterResult) {
String user = interpreterContext.getAuthenticationInfo().getUser();
try {
closeDBPool(user, propertyKey);
} catch (SQLException e) {
logger.error("Error, could not close DB pool in reLoginFromKeytab ", e);
}
UserGroupInformation.AuthenticationMethod authType =
JDBCSecurityImpl.getAuthtype(property);
if (authType.equals(KERBEROS)) {
try {
if (UserGroupInformation.isLoginKeytabBased()) {
UserGroupInformation.getLoginUser().reloginFromKeytab();
} else if (UserGroupInformation.isLoginTicketBased()) {
UserGroupInformation.getLoginUser().reloginFromTicketCache();
}
} catch (IOException e) {
logger.error("Cannot reloginFromKeytab " + sql, e);
interpreterResult.add(e.getMessage());
return new InterpreterResult(Code.ERROR, interpreterResult.message());
}
}
return executeSql(propertyKey, sql, interpreterContext);
}
/**
* For %table response replace Tab and Newline characters from the content.
*/