Rag*_*ngh 6 ruby ssl ruby-on-rails 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>'
Run Code Online (Sandbox Code Playgroud)
然而他们的文件明确指出,
creds = GRPC::Core::Credentials.new(load_certs) # load_certs typically loads a CA roots file
stub = Helloworld::Greeter::Stub.new('myservice.example.com', creds)
Run Code Online (Sandbox Code Playgroud)
然后我遇到了ChannelCredentials,信用应该是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
Run Code Online (Sandbox Code Playgroud)
然后我尝试了一下,
channel_creds = GRPC::Core::ChannelCredentials.new(load_certs)
stub = Helloworld::Greeter::Stub.new('myservice.example.com', channel_creds)
Run Code Online (Sandbox Code Playgroud)
但以上失败了
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.
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
channel_creds = GRPC::Core::ChannelCredentials.new(File.read(CA_FILE_PATH))
stub = Helloworld::Greeter::Stub.new('myservice.example.com', creds)
Run Code Online (Sandbox Code Playgroud)
但我得到的只是来自日志或 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
Run Code Online (Sandbox Code Playgroud)
有人成功尝试过启用 SSL/TLS 的 Ruby 客户端 Golang 服务器组合吗?
小智 4
creds 应该是 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
Run Code Online (Sandbox Code Playgroud)
实际上可能会产生误导,因为只有使用客户端的私钥和证书链构建通道凭证才有意义(我认为该特定测试不使用密钥和证书链)。
在构造函数上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()中的描述找到服务器根证书(请参阅服务器根证书参数为空时的行为描述)。仅当您希望客户端使用私钥和证书链时才需要最后一个构造函数。
| 归档时间: |
|
| 查看次数: |
2172 次 |
| 最近记录: |