简体   繁体   English

Ruby on Rails为SSL连接正确配置faraday http客户端

[英]Ruby on Rails configure faraday http client correctly for SSL connections

I am constantly getting Failed to open TCP connection to :80 (Cannot assign requested address - connect(2) for nil port 80) (Errno::EADDRNOTAVAIL) while using the ruby faraday gem. 在使用ruby faraday gem时,我始终Failed to open TCP connection to :80 (Cannot assign requested address - connect(2) for nil port 80) (Errno::EADDRNOTAVAIL) I don't have alot of experience with ruby on rails. 我没有很多关于红宝石的经验。

I have a docker ruby on rails service running on elastic beanstalk that is using puma with ssl. 我在使用bean和ssl的弹性beantalk上运行的docker ruby​​ on rails服务上。 CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"] I have a network load balancer configured with it to forward 443 -> 8443 (i've experimented with both self signed certs and real wild card certs). CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]我配置了网络负载平衡器,以转发CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"] 443 -> 8443 (我已经尝试过两个签名证书和真实通配符证书)。

ssl_bind '0.0.0.0', '8443', {
  key: '/var/app/ssl/something.key',
  cert: '/var/app/ssl/something.crt'
}

This configuration works as expected 此配置按预期工作

Puma starting in single mode...
* Version 3.12.0 (ruby 2.5.1-p57), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: staging
* Listening on tcp://0.0.0.0:3000
* Listening on ssl://0.0.0.0:8443?cert=/var/app/ssl/something.crt&key=/var/app/ssl/something.key&verify_mode=none

and I can get the healthz status page with both types of certs. 我可以同时获得两种证书的healthz状态页面。 Using httpie and --verify=no for the self signed. 使用httpie--verify=no进行自签名。

$ http https://backend.something.com/healthz
HTTP/1.1 200 OK
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/json; charset=utf-8

{
    "name": "my-backend-service",
    "version": "0.0.1"
}

I have another ruby on rails backend service that makes api requests to this service using faraday. 我在rails后端服务上有另一个ruby,它使用faraday向该服务发出api请求。 Ive removed some of the request/response code from my actual. 我已经从我的实际中删除了一些request/response代码。

def connection(baseUrl, options = {})
    conn = Faraday.new(url: baseUrl) do |c|

  # dont really know if this is needed or not
  # http.use_ssl? is always false
  c.adapter :net_http do |http|
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE # if http.use_ssl?
  end
end

I don't know if this http.verify_mode is actually working. 我不知道这个http.verify_mode是否真的有效。 I cant really find that method anywhere around here on ruby-doc.org 我真的无法在ruby-doc.org上的任何地方找到该方法

If you try to make a request it will just end up faiing. 如果您尝试发出请求,它将最终失败。

conn = connection(https://backend.something.com)
response = conn.post '/foo', params[:foo].to_json

The logs show from starting to parameters and then the http.rb:939 error. 日志显示从开始到参数,然后显示http.rb:939错误。 I realize the parameters aren't valid here but they aren't my problem. 我知道参数在这里无效,但这不是我的问题。

Started POST "/foo"
Processing by FooController#create as */*
Parameters: {"paramter"=>"something", "paramter"=>"something", "paramter"=>"something"}

ERROR -- : /usr/local/lib/ruby/2.5.0/net/http.rb:939:in `rescue in block in connect': Failed to open TCP connection to :80 (Cannot assign requested address - connect(2) for nil port 80) (Errno::EADDRNOTAVAIL)

If I make the same request from httpie or curl to this service I get the expected results over both http/https . 如果我从httpie或curl向此服务提出相同的请求,则通过http/https都可以获得预期的结果。

$ http POST https://backend.something.com parameter="something" parameter="something" parameter="something"
HTTP/1.1 201 Created
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/json; charset=utf-8

If you inspect the conn object it kinda still seems like it is sitting on its default value for @url_prefix = http:/ . 如果您检查conn对象,它似乎仍然位于@url_prefix = http:/默认值上。 Found in the docs above, but I don't know if I'm looking at the correct thing the correct ruby way. 在上面的文档中找到,但是我不知道我是否在以正确的方式查看正确的事物。 I imagined that Faraday.new(url: baseUrl) would parse the correct schema, which is https . 我以为Faraday.new(url: baseUrl)会解析正确的模式,即https

#<Faraday::Connection:0x0000565351701430 @parallel_manager=nil, @headers={"User-Agent"=>"Faraday v0.15.4"}, @params={}, @options=#<Faraday::RequestOptions (empty)>, @ssl=#<Faraday::SSLOptions (empty)>, @default_parallel_manager=nil, @builder=#<Faraday::RackBuilder:0x0000565351700f58 @handlers=[FaradayMiddleware::EncodeJson, FaradayMiddleware::ParseJson, Faraday::Adapter::NetHttp]>, @url_prefix=#<URI::HTTP http:/>, @manual_proxy=false, @proxy=nil, @temp_proxy=nil>

It seems that the the address you are trying to connect to is invalid. 您尝试连接的地址似乎无效。 From what I can see in the error: 从错误中我可以看到:

ERROR -- : /usr/local/lib/ruby/2.5.0/net/http.rb:939:in `rescue in block in connect': Failed to open TCP connection to :80 (Cannot assign requested address - connect(2) for nil port 80) (Errno::EADDRNOTAVAIL)

it is nil. 它是零。 Ensure that the host is properly set. 确保正确设置了主机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM