最近,我试图使用rubygem grpc 1.3.2版作为clinet,并连接到由golang构建的grpc服务器。我查阅了GRPC.IO的文档,并在我的代码中使用它。Ruby的grpc(v1.3.2)gem SSL/TLS连接问题,完全由golang构建的grpc服务器

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) 

然后,我遇到了ChannelCredentialscreds应该是要么ChannelCredentials对象或符号(如:this_channel_is_insecure )。因此,我也试了一下。

我已经从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) 

B UT所有我得到的是从日志或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 

有没有人成功地尝试这种红宝石客户Golang服务器启用SSL/TLS的组合?

Cyclops

回答

creds应该是要么ChannelCredentials对象或符号

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 

实际上可能会产生误导,因为它才有意义,构建与