【问题标题】:Are client side certificates supported in App EngineApp Engine 是否支持客户端证书
【发布时间】:2013-02-05 13:57:42
【问题描述】:

我正在开发一个谷歌应用引擎应用程序,我需要使用 SOAP 连接到一个网络服务。我正在使用 pysimplesoap(使用找到的代码修补 here)来解析 xml,并使用客户端证书触发请求。 当我在本地环境中的简单单元测试中执行此操作时,它可以工作,并且我从 web 服务中得到了正确的响应。 但是,当我在应用引擎中运行完全相同的代码时,我得到了这个:

  File "/Users/me/Documents/workspace/blixem/model/communicate/communication_channel.py", line 60, in generate_soap_message_pysimplesoap
    response = client.SendDocument('LA.XML', 'TESTCASE', 'data')
  File "/Users/me/Documents/workspace/blixem/lib/pysimplesoap/client.py", line 152, in <lambda>
    return lambda *args, **kwargs: self.wsdl_call(attr,*args,**kwargs)
  File "/Users/me/Documents/workspace/blixem/lib/pysimplesoap/client.py", line 320, in wsdl_call
    response = self.call(method, *params)
  File "/Users/me/Documents/workspace/blixem/lib/pysimplesoap/client.py", line 215, in call
    self.xml_response = self.send(method, self.xml_request)
  File "/Users/me/Documents/workspace/blixem/lib/pysimplesoap/client.py", line 241, in send
    location,"POST", body=xml, headers=headers )
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/httplib2/httplib2/__init__.py", line 1457, in request
    self.disable_ssl_certificate_validation)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/httplib2/httplib2/__init__.py", line 1143, in __init__
    strict, timeout, proxy_info, ca_certs, disable_ssl_certificate_validation)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/httplib2/httplib2/__init__.py", line 1092, in __init__
    raise NotSupportedOnThisPlatform()
NotSupportedOnThisPlatform

我做了一些阅读,发现 urlfetch 服务还不支持客户端证书。现在还是这样吗?如果是这样,是否有解决方法?

【问题讨论】:

  • Thanx Ingo,我将研究这个出站套接字支持功能。不支持太糟糕的客户端证书。
  • Ingo,我想对您的评论进行投票,但我对在 stackoverflow 上提出问题还很陌生,所以请告诉我如何对您的评论进行投票(我们不应该投票而不是 cmets?)
  • 你是对的。我删除了评论并将其添加为答案:)

标签: python google-app-engine pysimplesoap


【解决方案1】:

要扩展BooTooMany's answer,现在可以使用出色的requests library 来实现这一点,只要底层代码使用sockets。请按照以下步骤使用它:

  1. 套接字是only available to paid apps。确保您的应用已启用结算功能。
  2. requests 安装到您的lib/ 目录中。我目前正在使用 v2.21.0。尽管the docs 建议使用requests-toolbelt 提供的AppEngine 适配器,但请确保requests 进行monkeypatch。这使得requests 使用urlfetch 而不是套接字,后者可用于免费应用,但目前不支持客户端证书。
  3. 在您的 app.yaml 中,启用 SSL 库:
libraries:
- name: ssl
  version: latest
  1. 在您的app.yaml 中,为开发服务器启用套接字:
env_variables:
  GAE_USE_SOCKETS_HTTPLIB: 'yes'

现在您可以使用客户端证书发出请求了:

import requests

def make_request(url):
    cert = ('cert_file.pem', 'key_file.pem')
    server_cert_file = 'server_cert_file.pem'
    return requests.get(url=url, cert=cert, verify=server_cert_file)

【讨论】:

    【解决方案2】:

    GAE 目前不支持客户端证书。您可以通过 HTTPS 使用 URLFetch 服务。但是您不能使用客户端证书。您应该尝试Outbound Sockets Support feature,它目前在trusted tester program 中可用。它可能会将您正在寻找的功能列入白名单。我问a similar question for GAE/J before

    如果您确实需要它,请使用出站套接字功能或在EC2 中运行代理。

    【讨论】:

    • 这不再是真的 - 请参阅下面的答案。
    【解决方案3】:

    GAE 现在支持 Python SSL - 请参阅 https://cloud.google.com/appengine/docs/standard/python/sockets/ssl_support

    所以现在可以使用客户端证书。从那个网页:

     # Example of a dynamic key and cert.
      datastore_record_k = ndb.Key('Employee', 'asalieri', 'Address', 1)
      datastore_record = datastore_record_k.get()
      key_str = datastore_record.key_str
      cert_str = datastore_record.cert
      ssl_server = ssl.wrap_socket(server_sock,
                                  server_side=False,
                                  keyfile=StringIO.StringIO(key_str),
                                  certfile=StringIO.StringIO(cert_str),
                                  cert_reqs=ssl.CERT_REQUIRED,
                                  ssl_version=ssl.PROTOCOL_SSLv23,
                                  ca_certs=CERTIFICATE_FILE)
    

    【讨论】:

    • 如问题所述,如何使用ssl.wrap_socket 创建 HTTPS 请求?这是否需要实现大量低级 HTTP 通信逻辑,或者 requests 或其他库可以以某种方式使用它?
    猜你喜欢
    • 1970-01-01
    • 2021-02-02
    • 2012-09-29
    • 2019-11-18
    • 2013-04-05
    • 2017-01-29
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    相关资源
    最近更新 更多