Browse Source

HUE-7623 [editor] Test connection on create_session

jdesjean 8 years ago
parent
commit
1fe6f45

+ 22 - 0
desktop/libs/librdbms/src/librdbms/jdbc.py

@@ -19,7 +19,9 @@ import logging
 import os
 import sys
 
+from desktop.lib.i18n import force_unicode, smart_str
 from notebook.conf import DBPROXY_EXTRA_CLASSPATH
+from notebook.connectors.base import AuthenticationRequired
 
 LOG = logging.getLogger(__name__)
 
@@ -43,6 +45,11 @@ def query_and_fetch(db, statement, n=None):
       return data, meta
     finally:
       curs.close()
+  except Exception, e:
+    message = force_unicode(smart_str(e))
+    if 'Access denied' in message:
+      raise AuthenticationRequired()
+    raise
   finally:
     db.close()
 
@@ -66,6 +73,21 @@ class Jdbc():
 
     self.conn = None
 
+  def test_connection(self, throw_exception=True):
+    try:
+      self.connect()
+      return True
+    except Exception, e:
+      message = force_unicode(smart_str(e))
+      if throw_exception:
+        if 'Access denied' in message:
+          raise AuthenticationRequired()
+        raise
+      else:
+        return False
+    finally:
+      self.close()
+
   def connect(self):
     if self.conn is None:
       self.gateway.jvm.Class.forName(self.jdbc_driver)

+ 4 - 1
desktop/libs/notebook/src/notebook/connectors/jdbc.py

@@ -43,6 +43,8 @@ def query_error_handler(func):
       message = force_unicode(smart_str(e))
       if 'error occurred while trying to connect to the Java server' in message:
         raise QueryError(_('%s: is the DB Proxy server running?') % message)
+      elif 'Access denied' in message:
+        raise AuthenticationRequired()
       else:
         raise QueryError(message)
   return decorator
@@ -70,11 +72,12 @@ class JdbcApi(Api):
     properties = dict([(p['name'], p['value']) for p in properties]) if properties is not None else {}
     props['properties'] = {} # We don't store passwords
 
-    if self.db is None:
+    if self.db is None or not self.db.test_connection(throw_exception='password' not in properties):
       if 'password' in properties:
         user = properties.get('user') or self.options.get('user')
         props['properties'] = {'user': user}
         self.db = API_CACHE[self.cache_key] = Jdbc(self.options['driver'], self.options['url'], user, properties.pop('password'))
+        self.db.test_connection(throw_exception=True)
 
     if self.db is None:
       raise AuthenticationRequired()