You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zeppelin.apache.org by zj...@apache.org on 2021/07/08 01:56:24 UTC

[zeppelin] branch master updated: [ZEPPELIN-5448] Hive job runs under anonymous user instead of default.user

This is an automated email from the ASF dual-hosted git repository.

zjffdu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zeppelin.git


The following commit(s) were added to refs/heads/master by this push:
     new a6fd2c5  [ZEPPELIN-5448] Hive job runs under anonymous user instead of default.user
a6fd2c5 is described below

commit a6fd2c5301c0fe406a1ab4e3afb26e471d32bb02
Author: Jeff Zhang <zj...@apache.org>
AuthorDate: Tue Jul 6 15:57:32 2021 +0800

    [ZEPPELIN-5448] Hive job runs under anonymous user instead of default.user
    
    ### What is this PR for?
    
    Hive job should run under `default.user` instead of `anonymous`.  The root cause of this issue is that we didn't verify it is `anonymous` before passing it to jdbc.
    
    ### What type of PR is it?
    [Bug Fix]
    
    ### Todos
    * [ ] - Task
    
    ### What is the Jira issue?
    * https://issues.apache.org/jira/browse/ZEPPELIN-5448
    
    ### How should this be tested?
    * Manually tested
    
    ### Screenshots (if appropriate)
    
    ![image](https://user-images.githubusercontent.com/164491/124566262-7b884480-de75-11eb-9b54-98a40dfa12cf.png)
    
    ### Questions:
    * Does the licenses files need update? No
    * Is there breaking changes for older versions? No
    * Does this needs documentation? No
    
    Author: Jeff Zhang <zj...@apache.org>
    
    Closes #4163 from zjffdu/ZEPPELIN-5448 and squashes the following commits:
    
    6f915c3fa8 [Jeff Zhang] [ZEPPELIN-5448] Hive job runs under anonymous user instead of default.user
---
 .../org/apache/zeppelin/jdbc/JDBCInterpreter.java  | 43 +++++++++++++++-------
 1 file changed, 30 insertions(+), 13 deletions(-)

diff --git a/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java b/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java
index 62ffd67..1882f1d 100644
--- a/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java
+++ b/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java
@@ -350,6 +350,23 @@ public class JDBCInterpreter extends KerberosInterpreter {
     }
   }
 
+  /* Get user of this sql.
+   * 1. If shiro is enabled, use the login user
+   * 2. Otherwise try to get it from interpreter setting, e.g. default.user
+   */
+  private String getUser(InterpreterContext context) {
+    String user = context.getAuthenticationInfo().getUser();
+    String dbPrefix = getDBPrefix(context);
+
+    if ("anonymous".equalsIgnoreCase(user) && basePropertiesMap.containsKey(dbPrefix)) {
+      String userInProperty = basePropertiesMap.get(dbPrefix).getProperty(USER_KEY);
+      if (StringUtils.isNotBlank(userInProperty)) {
+        user = userInProperty;
+      }
+    }
+    return user;
+  }
+
   private String getEntityName(String replName, String propertyKey) {
     if ("jdbc".equals(replName)) {
       return propertyKey;
@@ -402,8 +419,7 @@ public class JDBCInterpreter extends KerberosInterpreter {
   private void setUserProperty(String dbPrefix, InterpreterContext context)
       throws SQLException, IOException, InterpreterException {
 
-    String user = context.getAuthenticationInfo().getUser();
-
+    String user = getUser(context);
     JDBCUserConfigurations jdbcUserConfigurations = getJDBCConfiguration(user);
     if (basePropertiesMap.get(dbPrefix).containsKey(USER_KEY) &&
         !basePropertiesMap.get(dbPrefix).getProperty(USER_KEY).isEmpty()) {
@@ -416,11 +432,11 @@ public class JDBCInterpreter extends KerberosInterpreter {
     if (existAccountInBaseProperty(dbPrefix)) {
       return;
     }
-    jdbcUserConfigurations.cleanUserProperty(dbPrefix);
 
     UsernamePassword usernamePassword = getUsernamePassword(context,
             getEntityName(context.getReplName(), dbPrefix));
     if (usernamePassword != null) {
+      jdbcUserConfigurations.cleanUserProperty(dbPrefix);
       jdbcUserConfigurations.setUserProperty(dbPrefix, usernamePassword);
     } else {
       closeDBPool(user, dbPrefix);
@@ -500,12 +516,14 @@ public class JDBCInterpreter extends KerberosInterpreter {
 
   public Connection getConnection(String dbPrefix, InterpreterContext context)
       throws ClassNotFoundException, SQLException, InterpreterException, IOException {
-    final String user =  context.getAuthenticationInfo().getUser();
-    Connection connection = null;
+
     if (dbPrefix == null || basePropertiesMap.get(dbPrefix) == null) {
+      LOGGER.warn("No such dbPrefix: {}", dbPrefix);
       return null;
     }
 
+    Connection connection = null;
+    String user = getUser(context);
     JDBCUserConfigurations jdbcUserConfigurations = getJDBCConfiguration(user);
     setUserProperty(dbPrefix, context);
 
@@ -703,7 +721,7 @@ public class JDBCInterpreter extends KerberosInterpreter {
     Statement statement;
     ResultSet resultSet = null;
     String paragraphId = context.getParagraphId();
-    String user = context.getAuthenticationInfo().getUser();
+    String user = getUser(context);
 
     try {
       connection = getConnection(dbPrefix, context);
@@ -950,8 +968,7 @@ public class JDBCInterpreter extends KerberosInterpreter {
 
     LOGGER.info("Cancel current query statement.");
     String paragraphId = context.getParagraphId();
-    JDBCUserConfigurations jdbcUserConfigurations =
-            getJDBCConfiguration(context.getAuthenticationInfo().getUser());
+    JDBCUserConfigurations jdbcUserConfigurations = getJDBCConfiguration(getUser(context));
     try {
       jdbcUserConfigurations.cancelStatement(paragraphId);
     } catch (SQLException e) {
@@ -1014,17 +1031,17 @@ public class JDBCInterpreter extends KerberosInterpreter {
 
   @Override
   public List<InterpreterCompletion> completion(String buf, int cursor,
-      InterpreterContext interpreterContext) throws InterpreterException {
+      InterpreterContext context) throws InterpreterException {
     List<InterpreterCompletion> candidates = new ArrayList<>();
-    String propertyKey = getDBPrefix(interpreterContext);
+    String propertyKey = getDBPrefix(context);
     String sqlCompleterKey =
-        String.format("%s.%s", interpreterContext.getAuthenticationInfo().getUser(), propertyKey);
+        String.format("%s.%s", getUser(context), propertyKey);
     SqlCompleter sqlCompleter = sqlCompletersMap.get(sqlCompleterKey);
 
     Connection connection = null;
     try {
-      if (interpreterContext != null) {
-        connection = getConnection(propertyKey, interpreterContext);
+      if (context != null) {
+        connection = getConnection(propertyKey, context);
       }
     } catch (ClassNotFoundException | SQLException | IOException e) {
       LOGGER.warn("SQLCompleter will created without use connection");