-1

我有一个 Web 服务器,它在本地数据库中维护其大部分内容,但需要查询后端目录服务来检索用户信息。目录查询作为独立操作可以正常工作,但是当 Web 服务器进行查询时,ssl 会返回错误。

服务器基于CentOS 7-2.1511 / Django 1.9.3 / PostgreSQL 9.2.15 / Apache 2.4.6-40 / mod_ssl 1:2.4.6-40 / OpenSSL 1:1.0.1 / Python 3.4.3. Apache 使用 mod_ssl 服务来自客户端(浏览器)的 https: 请求,我假设 Python 的 ssl.py 使用相同的引擎向目录服务器发出 https: 请求。 Python SSL说它的实现是基于 OpenSSL 的。 Yum显然无法列出 mod_ssl 的依赖项,但我认为它也使用已安装的 openssl 版本。

以下代码将获取用户的专有名称(从 SSL_CLIENT_CERT 中提取)并使用 RESTful 接口在目录服务器中查询用户的属性:

import requests, urllib

URL = 'https://example.com/rest/user_info/'

def get_user_info(dn)
    query = URL + urllib.parse.quote(dn)
    return requests.get(query, cert=('server.crt', 'server.key'),
                        verify='ca_bundle.crt').json()

当我在服务器的 WSGI 目录中作为用户 apache 在服务器上运行时,该例程正确返回一个包含用户属性的字典:

$ python
>>> import auth
>>> dn='cn=Me,o=Company,c=US'
>>> attr = auth.get_user_info(dn)

但是,当 Apache 从它的 WSGI 脚本 ( ) 调用具有相同 DN 的相同函数时views.py,它会引发 OSError:

OSError(0, 'Error')
Line 810, /lib64/python3.4/ssl.py

803  def do_handshake(self, block=False):
804      """Perform a TLS/SSL handshake."""
805      self._check_connected()
806      timeout = self.gettimeout()
807      try:
808          if timeout == 0.0 and block:
809              self.settimeout(None)
810          self._sslobj.do_handshake()   

我将按照OpenSSL的建议开始研究锁定(因为我想不出其他任何会导致这些错误的东西),但很难相信使用 SSL 进行后端查询的网络服务器还不是一条成熟的道路。问题:

  1. 多线程/锁定/重入是否因为这些错误的原因而在正确的树上吠叫?
  2. 是否已经有使用 SSL 进行网络服务器后端连接的工作示例?
4

1 回答 1

0

我放弃。以下内容可靠地工作,但以牺牲美观和效率为代价。它首先尝试requests查询,如果查询失败,它会wget在子进程中启动和调用。它返回一个元项$method,让页面视图知道内联请求是否失败。

def get_user_info(dn, sub=True, ttl=300):
    query = URL + urllib.parse.quote(dn)
    try:
        info = requests.get(query, cert=(SERVER_CERT, SERVER_KEY),
                            verify=CA_CERT).json()
        info['$method'] = 'requests'
        return info
    except OSError:
        if sub:
            args = ['wget', '-O', '-',
                '--certificate=' + SERVER_CERT,
                '--private-key=' + SERVER_KEY,
                query]
            bytes = subprocess.check_output(args, timeout=5)
            info = json.loads(bytes.decode(encoding='utf-8'))
            info['$method'] = 'subprocess'
            return info
        else:
            raise

如果 OpenSSL 使用上下文而不是全局变量,那肯定会很好。

于 2016-04-06T18:47:43.527 回答