【问题标题】:Specify client certificate with Ruby Typhoeus使用 Ruby Typhoeus 指定客户端证书
【发布时间】:2021-05-04 21:02:37
【问题描述】:

我正在尝试使用 Typhoeus 发出需要客户端证书的 HTTP 请求。 Typhoeus README 根本没有提到使用客户端证书。我能找到的关于使用 Typhoeus 指定客户端证书的唯一讨论是this GitHub issue discussion。基于该讨论,这是我提出的无效代码(实际 URL 和文件名已更改):

require 'openssl'
require 'typhoeus'

cert = OpenSSL::X509::Certificate.new(File.read("cert.pem"))
key = OpenSSL::PKey::RSA.new(File.read("cert-key.pem"))
ca = OpenSSL::X509::Certificate.new(File.read("cert-ca.pem"))

t = Typhoeus.get(
  "https://example.com/",
  ssl_verifyhost: 0,
  ssl_verifypeer: false,
  sslcert: cert,
  sslkey: key,
  cainfo: ca
)

p t.return_code

这将返回 :ssl_certproblem。响应正文为空,:response_code 为 0。

我确认我的证书文件和 URL 是正确的。此 curl 命令返回我期望的响应正文:

curl --key cert-key.pem --cert cert.pem --cacert cert-ca.pem https://example.com

我收到了使用Faraday 完成的请求,但是,我还需要并行发出请求。 Faraday's way of doing this 是使用 Typhoeus 作为适配器,仍然会导致客户端证书错误。似乎只要涉及 Typhoeus,我就无法使用客户端证书进行身份验证,而且我不知道另一个 HTTP gem 可以为我处理并行请求。现在,我将不得不接受与 Faraday 串联发送请求,这使得我的脚本的执行速度变慢了很多。我可能最终会用另一种语言重写脚本。

这就是我使用法拉第提出请求的方式:

require 'faraday'
require 'openssl'

ssl_opts = {
  :client_cert => OpenSSL::X509::Certificate.new(File.read("cert.pem")),
  :client_key => OpenSSL::PKey::RSA.new(File.read("cert-key.pem")),
  :ca_file => "cert-ca.pem"
}

f = Faraday.new(
  "https://example.com",
  :ssl => ssl_opts
).get

使用 Typhoeus 发出需要客户端证书的 HTTP 请求的正确方法是什么?我愿意使用 Typhoeus 的替代品,但我需要能够发出并行请求。

【问题讨论】:

  • 我建议不要并行运行,除非您拥有正在访问的服务器。消耗其他人的带宽是被禁止的好方法。我已经为我的工作编写了很多蜘蛛和爬虫,即使在慢速机器上,一个不受限制的 HTTP 客户端也能产生如此多的流量。

标签: ruby openssl client-certificates faraday typhoeus


【解决方案1】:

在链接的 Github 问题中,有一个提示对我有用。

我跳过了cainfo,但sslcertsslkey 需要是文件路径的纯字符串。

类似的东西对我有用:

t = Typhoeus.get(
  "https://example.com/",
  ssl_verifyhost: 0,
  ssl_verifypeer: false,
  sslcert: "/path/to/project/cert.pem",
  sslkey: "/path/to/project/cert-key.pem"
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    相关资源
    最近更新 更多