mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
Pass all user credentials to the interpreter
This commit is contained in:
parent
90d546f7c0
commit
cfe4c8627b
7 changed files with 38 additions and 70 deletions
|
|
@ -36,6 +36,9 @@ import org.apache.zeppelin.interpreter.InterpreterResult;
|
|||
import org.apache.zeppelin.interpreter.InterpreterResult.Code;
|
||||
import org.apache.zeppelin.scheduler.Scheduler;
|
||||
import org.apache.zeppelin.scheduler.SchedulerFactory;
|
||||
import org.apache.zeppelin.user.AuthenticationInfo;
|
||||
import org.apache.zeppelin.user.UserCredentials;
|
||||
import org.apache.zeppelin.user.UsernamePassword;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
|
@ -135,18 +138,15 @@ public class HiveInterpreter extends Interpreter {
|
|||
}
|
||||
|
||||
public InterpreterResult executeSql(String propertyKey, String sql,
|
||||
String user, String password,
|
||||
InterpreterContext interpreterContext) {
|
||||
String executingUser = interpreterContext.getAuthenticationInfo().getDataSourceUser();
|
||||
String password = interpreterContext.getAuthenticationInfo().getDataSourcePassword();
|
||||
Connection connection = null;
|
||||
Statement statement = null;
|
||||
try {
|
||||
Properties properties = propertiesMap.get(propertyKey);
|
||||
Class.forName(properties.getProperty(DRIVER_KEY));
|
||||
String url = properties.getProperty(URL_KEY);
|
||||
String user;
|
||||
user = executingUser;
|
||||
if (null != user) {
|
||||
if (user != null) {
|
||||
connection = DriverManager.getConnection(url, user, password);
|
||||
} else {
|
||||
connection = DriverManager.getConnection(url, properties);
|
||||
|
|
@ -239,7 +239,24 @@ public class HiveInterpreter extends Interpreter {
|
|||
logger.info("PropertyKey: {} User: {} SQL command: '{}'", propertyKey,
|
||||
contextInterpreter.getAuthenticationInfo().getUser(), cmd);
|
||||
|
||||
return executeSql(propertyKey, cmd, contextInterpreter);
|
||||
UsernamePassword usernamePassword = null;
|
||||
String username = null;
|
||||
String password = null;
|
||||
|
||||
AuthenticationInfo authenticationInfo = contextInterpreter.getAuthenticationInfo();
|
||||
UserCredentials userCredentials = authenticationInfo.getUserCredentials();
|
||||
logger.info(userCredentials.toString());
|
||||
if (userCredentials != null) {
|
||||
usernamePassword = userCredentials.getUsernamePassword("hive(" + propertyKey + ")");
|
||||
}
|
||||
if (usernamePassword != null) {
|
||||
username = usernamePassword.getUsername();
|
||||
password = usernamePassword.getPassword();
|
||||
}
|
||||
if (username == null) {
|
||||
username = authenticationInfo.getUser();
|
||||
}
|
||||
return executeSql(propertyKey, cmd, username, password, contextInterpreter);
|
||||
}
|
||||
|
||||
private int getMaxResult() {
|
||||
|
|
|
|||
|
|
@ -24,8 +24,7 @@ package org.apache.zeppelin.user;
|
|||
public class AuthenticationInfo {
|
||||
String user;
|
||||
String ticket;
|
||||
String dataSourceUser;
|
||||
String dataSourcePassword;
|
||||
UserCredentials userCredentials;
|
||||
|
||||
public AuthenticationInfo() {}
|
||||
|
||||
|
|
@ -55,20 +54,12 @@ public class AuthenticationInfo {
|
|||
this.ticket = ticket;
|
||||
}
|
||||
|
||||
public String getDataSourceUser() {
|
||||
return dataSourceUser;
|
||||
public UserCredentials getUserCredentials() {
|
||||
return userCredentials;
|
||||
}
|
||||
|
||||
public void setDataSourceUser(String dataSourceUser) {
|
||||
this.dataSourceUser = dataSourceUser;
|
||||
}
|
||||
|
||||
public String getDataSourcePassword() {
|
||||
return dataSourcePassword;
|
||||
}
|
||||
|
||||
public void setDataSourcePassword(String dataSourcePassword) {
|
||||
this.dataSourcePassword = dataSourcePassword;
|
||||
public void setUserCredentials(UserCredentials userCredentials) {
|
||||
this.userCredentials = userCredentials;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,19 +15,16 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.zeppelin.credential;
|
||||
package org.apache.zeppelin.user;
|
||||
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.zeppelin.conf.ZeppelinConfiguration;
|
||||
|
||||
/**
|
||||
* Class defining credentials for data source authorization
|
||||
*/
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.zeppelin.credential;
|
||||
package org.apache.zeppelin.user;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.zeppelin.credential;
|
||||
package org.apache.zeppelin.user;
|
||||
|
||||
/**
|
||||
* Username and Password POJO
|
||||
|
|
@ -19,9 +19,9 @@ package org.apache.zeppelin.rest;
|
|||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import org.apache.zeppelin.credential.Credentials;
|
||||
import org.apache.zeppelin.credential.UserCredentials;
|
||||
import org.apache.zeppelin.credential.UsernamePassword;
|
||||
import org.apache.zeppelin.user.Credentials;
|
||||
import org.apache.zeppelin.user.UserCredentials;
|
||||
import org.apache.zeppelin.user.UsernamePassword;
|
||||
import org.apache.zeppelin.server.JsonResponse;
|
||||
import org.apache.zeppelin.utils.SecurityUtils;
|
||||
import org.slf4j.Logger;
|
||||
|
|
|
|||
|
|
@ -20,9 +20,9 @@ package org.apache.zeppelin.notebook;
|
|||
import org.apache.zeppelin.display.AngularObject;
|
||||
import org.apache.zeppelin.display.AngularObjectRegistry;
|
||||
import org.apache.zeppelin.user.AuthenticationInfo;
|
||||
import org.apache.zeppelin.credential.Credentials;
|
||||
import org.apache.zeppelin.credential.UserCredentials;
|
||||
import org.apache.zeppelin.credential.UsernamePassword;
|
||||
import org.apache.zeppelin.user.Credentials;
|
||||
import org.apache.zeppelin.user.UserCredentials;
|
||||
import org.apache.zeppelin.user.UsernamePassword;
|
||||
import org.apache.zeppelin.display.GUI;
|
||||
import org.apache.zeppelin.display.Input;
|
||||
import org.apache.zeppelin.interpreter.*;
|
||||
|
|
@ -317,29 +317,6 @@ public class Paragraph extends Job implements Serializable, Cloneable {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the property key of the specific data source
|
||||
* e.g. getDataSourceKey("%hive(vertica)") -> "vertica"
|
||||
* @param cmd
|
||||
* @return property key of data source being queried
|
||||
*/
|
||||
public static String getDataSourceKey(String cmd) {
|
||||
if (cmd == null) {
|
||||
return null;
|
||||
}
|
||||
int firstLineIndex = cmd.indexOf("\n");
|
||||
if (-1 == firstLineIndex) {
|
||||
firstLineIndex = cmd.length();
|
||||
}
|
||||
int configStartIndex = cmd.indexOf("(");
|
||||
int configLastIndex = cmd.indexOf(")");
|
||||
if (configStartIndex != -1 && configLastIndex != -1
|
||||
&& configLastIndex < firstLineIndex && configLastIndex < firstLineIndex) {
|
||||
return cmd.substring(configStartIndex + 1, configLastIndex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private InterpreterContext getInterpreterContext() {
|
||||
AngularObjectRegistry registry = null;
|
||||
ResourcePool resourcePool = null;
|
||||
|
|
@ -358,22 +335,8 @@ public class Paragraph extends Job implements Serializable, Cloneable {
|
|||
final Paragraph self = this;
|
||||
|
||||
Credentials credentials = Credentials.getCredentials();
|
||||
String dataSourceKey = null;
|
||||
try {
|
||||
dataSourceKey = getDataSourceKey(getScriptBody());
|
||||
} catch (NullPointerException e) {
|
||||
logger().info("NPE at dataSourceKey({}). cmd = {}", dataSourceKey, getScriptBody());
|
||||
}
|
||||
logger().info("{} {}", getScriptBody(), dataSourceKey);
|
||||
UserCredentials userCredentials = credentials.getUserCredentials(authenticationInfo.getUser());
|
||||
logger().info(userCredentials.toString());
|
||||
if (userCredentials != null) {
|
||||
UsernamePassword userPassword = userCredentials.getUsernamePassword(dataSourceKey);
|
||||
if (userPassword != null) {
|
||||
authenticationInfo.setDataSourceUser(userPassword.getUsername());
|
||||
authenticationInfo.setDataSourcePassword(userPassword.getPassword());
|
||||
}
|
||||
}
|
||||
authenticationInfo.setUserCredentials(userCredentials);
|
||||
|
||||
InterpreterContext interpreterContext = new InterpreterContext(
|
||||
note.id(),
|
||||
|
|
|
|||
Loading…
Reference in a new issue