瀏覽代碼

HUE-7989 [useradmin] Provide better UI message and message in logs when ldap server down

Ying Chen 7 年之前
父節點
當前提交
7eb49ee520
共有 2 個文件被更改,包括 41 次插入10 次删除
  1. 15 7
      apps/useradmin/src/useradmin/ldap_access.py
  2. 26 3
      desktop/core/src/desktop/auth/forms.py

+ 15 - 7
apps/useradmin/src/useradmin/ldap_access.py

@@ -158,18 +158,26 @@ class LdapConnection(object):
     if bind_user:
       try:
         self.ldap_handle.simple_bind_s(bind_user, bind_password)
-      except:
-        msg = "Failed to bind to LDAP server as user %s" % bind_user
-        LOG.exception(msg)
-        raise LdapBindException(msg)
+      except Exception, e:
+        self.handle_bind_exception(e, bind_user)
     else:
       try:
         # Do anonymous bind
         self.ldap_handle.simple_bind_s('','')
-      except:
+      except Exception, e:
+        self.handle_bind_exception(e)
+
+  def handle_bind_exception(self, exception, bind_user=None):
+    LOG.error("LDAP access bind error: %s" % exception)
+    if 'Can\'t contact LDAP server' in str(exception):
+      msg = "Can\'t contact LDAP server"
+    else:
+      if bind_user:
+        msg = "Failed to bind to LDAP server as user %s" % bind_user
+      else:
         msg = "Failed to bind to LDAP server anonymously"
-        LOG.exception(msg)
-        raise LdapBindException(msg)
+
+    raise LdapBindException(msg)
 
   def _get_search_params(self, name, attr, find_by_dn=False):
     """

+ 26 - 3
desktop/core/src/desktop/auth/forms.py

@@ -16,6 +16,7 @@
 # limitations under the License.
 
 import datetime
+import logging
 
 from django.conf import settings
 from django.contrib.auth import authenticate, get_backends
@@ -29,6 +30,9 @@ from desktop import conf
 from useradmin.password_policy import get_password_validators
 
 
+LOG = logging.getLogger(__name__)
+
+
 def get_backend_names():
   return get_backends and [backend.__class__.__name__ for backend in get_backends()]
 
@@ -128,10 +132,29 @@ class LdapAuthenticationForm(AuthenticationForm):
       raise ValidationError(self.error_messages['invalid_login'])
 
     if username and password:
-      self.user_cache = authenticate(username=username,
-                                     password=password,
-                                     server=server)
+      try:
+        self.user_cache = authenticate(username=username,
+                                       password=password,
+                                       server=server)
+      except Exception as e:
+        # If bind password incorrect will cause exception when sync group in login, suggest admin to test LDAP connection
+        LOG.error("LDAP auth error: %s" % e)
+        raise ValidationError(_("Please contact your administrator for LDAP connection setup."))
+
       if self.user_cache is None:
+        from useradmin.ldap_access import get_connection as get_ldap_connection
+        try:
+          server_key = ''
+          if conf.LDAP.LDAP_SERVERS.get():
+            if server in conf.LDAP.LDAP_SERVERS.get():
+              server_key = server
+              ldap_config = conf.LDAP.LDAP_SERVERS.get()[server]
+              get_ldap_connection(ldap_config)
+          else:
+            get_ldap_connection(conf.LDAP)
+        except Exception as e:
+          raise ValidationError(_("LDAP server %s Error: %s" % (server_key, str(e))))
+
         raise ValidationError(
           self.error_messages['invalid_login'])
       elif not self.user_cache.is_active: