如何使用SSL客户端证书获取HTTPS请求以使用Ruby EventMachine?

jgy*_*len 6 ruby ssl-certificate eventmachine

我试图访问使用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
Run Code Online (Sandbox Code Playgroud)

运行上面的输出,<SSL_incomp>然后是引发的RuntimeError消息.我尝试使用:verify_peerset 运行true和false,它给了我同样的错误.EventMachine::HttpRequest#get没有:ssl选项的运行也是如此.

我也尝试将请求发送到GMail(https://mail.google.com),没有:ssl选项(即没有证书的普通HTTPS),并且可以正常工作,输出状态代码200,标题和正文.

我尝试使用curl对Web服务执行相同的请求,并且可以正常工作:

curl --silent --cert /tmp/private.key --cacert /tmp/ca.pem https://foobar.com/
Run Code Online (Sandbox Code Playgroud)

我想我要么错误地使用em-http-request gem或者EventMachine,要么SSL文件的格式是使用curl而不是EventMachine.

我有人知道如何解决上面的例子或直接使用EventMachine提供类似的例子将非常感激!

tmm*_*mm1 3

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

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