【问题标题】:How do I get an HTTPS request with SSL client cert to work with Ruby EventMachine?如何使用 SSL 客户端证书获取 HTTPS 请求以使用 Ruby EventMachine?
【发布时间】:2010-10-27 23:49:39
【问题描述】:

我正在尝试使用 Ruby EventMachine 访问使用 SSL 证书身份验证的 HTTPS Web 服务,但我没有让它工作。

我编写了以下简单的代码块来对其进行端到端测试:

require 'rubygems'
require 'em-http'

EventMachine.run do
  url = 'https://foobar.com/'
  ssl_opts = {:private_key_file => '/tmp/private.key',
    :cert_chain_file => '/tmp/ca.pem',
    :verify_peer => false}
  http = EventMachine::HttpRequest.new(url).get :ssl => ssl_opts

  http.callback do
    p http.response_header.status
    p http.response_header
    p http.response
    EventMachine.stop
  end

  http.errback do
    EventMachine.stop
    fail "Request failed"
  end
end

运行上述输出 <SSL_incomp> 后跟引发的 RuntimeError 消息。我尝试将 :verify_peer 设置为 true 和 false 运行,它给了我同样的错误。在没有 :ssl 选项的情况下运行 EventMachine::HttpRequest#get 也是如此。

我还尝试在没有 :ssl 选项(即没有证书的普通 HTTPS)的情况下向 GMail (https://mail.google.com) 发送请求,并且可以输出状态代码 200、标题和正文。

我尝试使用 curl 对 Web 服务执行相同的请求,并且效果很好:

curl --silent --cert /tmp/private.key --cacert /tmp/ca.pem https://foobar.com/

我认为我错误地使用了 em-http-request gem 或 EventMachine,或者 SSL 文件的格式适用于 curl 但不适用于 EventMachine。

如果有人知道如何解决上述示例或直接使用 EventMachine 提供类似示例,将不胜感激!

【问题讨论】:

    标签: ruby ssl-certificate eventmachine


    【解决方案1】:

    传递给 curl 的 --cert 的文件包含证书和密钥(除非您单独传递 --key)。只需使用/tmp/private.key 作为:private_key_file:cert_chain_file 的参数

    请参阅http://github.com/eventmachine/eventmachine/issues/#issue/115 了解有关该问题的更多详细信息以及暴露潜在错误的补丁程序(而不仅仅是打印出 SSL_incomp)。

    【讨论】:

      猜你喜欢
      • 2012-02-17
      • 1970-01-01
      • 2018-11-12
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多