Ruby的grpc(v1.3.2)gem SSL / TLS连接问题与grpc服务器完全用golang构建

时间:2017-06-19 10:11:36

标签: ruby-on-rails ruby ssl go grpc

最近,我尝试使用rubygem grpc 1.3.2版作为clinet并连接到由golang构建的grpc服务器。我浏览了GRPC.IO处的文档并将其用于我的代码中。

    irb(main):017:0> GRPC::Core::Credentials.new(File.read(CA_FILE_PATH))
NameError: uninitialized constant GRPC::Core::Credentials
        from (irb):17
        from /usr/local/share/gems/gems/railties-4.2.1/lib/rails/commands/console.rb:110:in `start'
        from /usr/local/share/gems/gems/railties-4.2.1/lib/rails/commands/console.rb:9:in `start'
        from /usr/local/share/gems/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:68:in `console'
        from /usr/local/share/gems/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from /usr/local/share/gems/gems/railties-4.2.1/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

然而,他们的文件明确指出,

creds = GRPC::Core::Credentials.new(load_certs)  # load_certs typically loads a CA roots file
stub = Helloworld::Greeter::Stub.new('myservice.example.com', creds)

然后我遇到了 ChannelCredentials ,而信用卡应该是 ChannelCredentials 对象或符号(例如:this_channel_is_insecure < / em>的)。因此,我也尝试了一下。

我从grpc gem的源代码本身中获取了以下函数。在rspec测试用例中调用此函数来加载证书:

def load_certs
      data_dir = "#{Rails.root}/certs"
      files = ['ca.pem', 'server.key', 'server.pem']
      files.map { |f| File.open(File.join(data_dir, f)).read }
end

然后我试了一下,

channel_creds = GRPC::Core::ChannelCredentials.new(load_certs)
stub = Helloworld::Greeter::Stub.new('myservice.example.com', channel_creds)

但上面的失败

E0619 09:59:10.410575570   14208 ssl_transport_security.c:601] Could not load any root certificate.
E0619 09:59:10.410604954   14208 ssl_transport_security.c:1315] Cannot load server root certificates.
E0619 09:59:10.410622519   14208 security_connector.c:837]   Handshaker factory creation failed with TSI_INVALID_ARGUMENT.

我也尝试过:

channel_creds = GRPC::Core::ChannelCredentials.new(File.read(CA_FILE_PATH))
stub = Helloworld::Greeter::Stub.new('myservice.example.com', creds)

但我得到的只是日志或rpc服务器的错误:

2017/06/16 10:52:34 transport: http2Server.HandleStreams failed to receive the preface from client: EOF
2017/06/16 10:53:35 transport: http2Server.HandleStreams failed to receive the preface from client: EOF
2017/06/16 10:53:59 transport: http2Server.HandleStreams failed to receive the preface from client: EOF
2017/06/16 10:55:06 transport: http2Server.HandleStreams failed to receive the preface from client: EOF

有没有人成功尝试过启用SSL / TLS的这个Ruby客户端Golang服务器组合?

2 个答案:

答案 0 :(得分:1)

  

信用证应该是ChannelCredentials对象或符号

是客户端存根构造函数的第二个参数(creds参数),应该是GRPC::Core::ChannelCredentials对象或具体 ::this_channel_is_insecure符号(如果后者通过) ,将使用不安全的连接。)

我注意到使用

的测试
def load_certs
  data_dir = "#{Rails.root}/certs"
  files = ['ca.pem', 'server.key', 'server.pem']
  files.map { |f| File.open(File.join(data_dir, f)).read }
end

实际上可能会产生误导,因为只使用客户端的私钥和证书链构建通道凭证才有意义(我认为特定的测试不使用密钥和证书链)。 / p>

GRPC::Core::ChannelCredentials构造函数上:

有三种形式可以使用,(https://github.com/grpc/grpc/blob/master/src/ruby/ext/grpc/rb_channel_credentials.c#L128中的构造函数代码上方有一条注释可以覆盖它们),但选项包括:

  • Credentials.new()

  • Credentials.new(pem_root_certs)

  • Credentials.new(pem_root_certs, pem_private_key, pem_cert_chain)

在所有情况下,根文件,私钥和证书链参数都是pem编码的字符串。

请注意,如果未传递任何参数(使用Credentials.new()),则将按this header comment中所述找到服务器根证书(请参阅服务器根证书参数为null时的行为说明) )。如果您希望客户端使用私钥和证书链,则只需要最后一个构造函数。

答案 1 :(得分:0)

我可以确认这是有效的。

channel_creds = GRPC::Core::ChannelCredentials.new(File.read("/home/user/.lnd/tls.cert"))
stub = Lnrpc::Lightning::Stub.new("127.0.0.1:10009", channel_creds)
obj = Lnrpc::GetInfoRequest.new
pp stub.get_info(obj)