UWSGI 是否会影响来自底层 Django 应用程序的 LDAPS 请求?

Does UWSGI influence LDAPS Requests from an underlying Django app?

我已将此 class 作为 django 中的身份验证后端插入

class ActiveDirectoryBackend(object):

    logger = Messenger.infrastructure

    @classmethod
    def authenticate(cls, username=None, password=None):

        try:
            bind_dn = "%s@%s" % (username, settings.AD_DNS_NAME)

            try:
                cls.logger.debug('Initializing: %s' % settings.AD_LDAP_URL)
                l = ldap.initialize(settings.AD_LDAP_URL)
                l.protocol_version = ldap.VERSION3
                l.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
                cls.logger.debug('Binding user %s' % bind_dn)
                l.simple_bind_s(bind_dn, password)
                result = l.search_ext_s(settings.AD_SEARCH_DN, ldap.SCOPE_SUBTREE,
                                        "sAMAccountName=%s" % username, settings.AD_SEARCH_FIELDS)[0][1]
                email = result.get('mail', (None,))[0]
            except ldap.SERVER_DOWN, ex:
                cls.logger.warning('LDAP-Sever Down (%s)' % ex.message)
                raise PermissionDenied
            except ldap.INVALID_CREDENTIALS:
                cls.logger.warning('LDAP-Server: Rejected login for user %s due to invalid credentials' % username)
                raise PermissionDenied

            cls.logger.debug('User %s was successfully authorized.' % username)

            l.unbind_s()

        except Exception:
            raise PermissionDenied

        try:
            user = User.objects.get(email=email)
            cls.logger.debug('User %s found for email %s ' % (user.username, email))

        except ObjectDoesNotExist, ex:
            cls.logger.debug('User for email %s could not be found.' % email)
            raise PermissionDenied

        return user

整个过程 运行 在 Apache + uWSGI 下进行。 当我 运行 Django 独立(manage.py 运行 服务器)时,LDAP 和 LDAPS 一切正常。

但是当运行使用 LDAPS 在 uWSGI 下使用它时,它总是抛出 "Server Down"。 LDAP (w/o 's') 有效。 使用 tcpdump 我可以看到数据包在 AD 和我的服务器之间双向传输。

uWSGI 如何影响 python-ldap 或底层库与 Active Directory 之间的 LDAPS 通信?

uWSGI 在生成它的 workers 时默认做一些 "clever" tricks

uWSGI tries to (ab)use the Copy On Write semantics of the fork() call whenever possible. By default it will fork after having loaded your applications to share as much of their memory as possible. If this behavior is undesirable for some reason, use the lazy-apps option. This will instruct uWSGI to load the applications after each worker’s fork().

根据我的经验,这确实会影响某些库(如 FreeTDS)。尝试提到的 lazy-apps 选项。

通过使用命令行 ldapsearch 上的选项,我和一位同事找到了解决问题的方法 - 但不是首先导致问题的确切原因。

通过显式设置 HOST 选项,我们让它工作。

在 python 中看起来像这样:

l.set_option(ldap.OPT_HOST_NAME, settings.AD_DNS_NAME)