Browse Source

HUE-8252 [jdbc] Provide ability to configure users impersonation with jdbc (#687)

Ivan Dzikovsky 7 years ago
parent
commit
b304fea3fd

+ 2 - 1
desktop/conf.dist/hue.ini

@@ -860,7 +860,8 @@
     #   ## Specific options for connecting to the server.
     #   ## Specific options for connecting to the server.
     #   ## The JDBC connectors, e.g. mysql.jar, need to be in the CLASSPATH environment variable.
     #   ## The JDBC connectors, e.g. mysql.jar, need to be in the CLASSPATH environment variable.
     #   ## If 'user' and 'password' are omitted, they will be prompted in the UI.
     #   ## If 'user' and 'password' are omitted, they will be prompted in the UI.
-    #   options='{"url": "jdbc:mysql://localhost:3306/hue", "driver": "com.mysql.jdbc.Driver", "user": "root", "password": "root"}'
+    #   ## Option 'impersonation_property' used to configure outbound impersonation, e.g. "impersonation_property": "hive.server2.proxy.user".
+    #   options='{"url": "jdbc:mysql://localhost:3306/hue", "driver": "com.mysql.jdbc.Driver", "user": "root", "password": "root", "impersonation_property": ""}'
 
 
 
 
 ###########################################################################
 ###########################################################################

+ 2 - 1
desktop/conf/pseudo-distributed.ini.tmpl

@@ -862,7 +862,8 @@
     #   ## Specific options for connecting to the server.
     #   ## Specific options for connecting to the server.
     #   ## The JDBC connectors, e.g. mysql.jar, need to be in the CLASSPATH environment variable.
     #   ## The JDBC connectors, e.g. mysql.jar, need to be in the CLASSPATH environment variable.
     #   ## If 'user' and 'password' are omitted, they will be prompted in the UI.
     #   ## If 'user' and 'password' are omitted, they will be prompted in the UI.
-    #   options='{"url": "jdbc:mysql://localhost:3306/hue", "driver": "com.mysql.jdbc.Driver", "user": "root", "password": "root"}'
+    #   ## Option 'impersonation_property' used to configure outbound impersonation, e.g. "impersonation_property": "hive.server2.proxy.user".
+    #   options='{"url": "jdbc:mysql://localhost:3306/hue", "driver": "com.mysql.jdbc.Driver", "user": "root", "password": "root", "impersonation_property": ""}'
 
 
 
 
 ###########################################################################
 ###########################################################################

+ 4 - 1
desktop/libs/librdbms/src/librdbms/jdbc.py

@@ -56,7 +56,7 @@ def query_and_fetch(db, statement, n=None):
 
 
 class Jdbc():
 class Jdbc():
 
 
-  def __init__(self, driver_name, url, username, password):
+  def __init__(self, driver_name, url, username, password, impersonation_property=None, impersonation_user=None):
     if 'py4j' not in sys.modules:
     if 'py4j' not in sys.modules:
       raise Exception('Required py4j module is not imported.')
       raise Exception('Required py4j module is not imported.')
 
 
@@ -71,6 +71,9 @@ class Jdbc():
     self.username = username
     self.username = username
     self.password = password
     self.password = password
 
 
+    if impersonation_property and impersonation_user:
+      self.db_url += ";{}={};".format(impersonation_property, impersonation_user)
+
     self.conn = None
     self.conn = None
 
 
   def test_connection(self, throw_exception=True):
   def test_connection(self, throw_exception=True):

+ 3 - 2
desktop/libs/notebook/src/notebook/connectors/jdbc.py

@@ -63,7 +63,8 @@ class JdbcApi(Api):
       self.db = API_CACHE[self.cache_key]
       self.db = API_CACHE[self.cache_key]
     elif 'password' in self.options:
     elif 'password' in self.options:
       username = self.options.get('user') or user.username
       username = self.options.get('user') or user.username
-      self.db = API_CACHE[self.cache_key] = Jdbc(self.options['driver'], self.options['url'], username, self.options['password'])
+      impersonation_property = self.options.get('impersonation_property')
+      self.db = API_CACHE[self.cache_key] = Jdbc(self.options['driver'], self.options['url'], username, self.options['password'], impersonation_property=impersonation_property, impersonation_user=user.username)
 
 
   def create_session(self, lang=None, properties=None):
   def create_session(self, lang=None, properties=None):
     global API_CACHE
     global API_CACHE
@@ -209,4 +210,4 @@ class Assist():
         "CF": "STRING_TYPE",
         "CF": "STRING_TYPE",
         "CV": "CHAR_TYPE",
         "CV": "CHAR_TYPE",
         "DA": "DATE_TYPE",
         "DA": "DATE_TYPE",
-      }.get(name, 'STRING_TYPE')
+      }.get(name, 'STRING_TYPE')