如何在没有客户端 SSL 证书的情况下在 python gRPC 客户端中打开安全通道

prn*_*vbn 5 python ssl grpc grpc-python grpc-go

我有一个 grpc 服务器(在 Go 中),它具有有效的 TLS 证书,并且不需要客户端 TLS。由于某种原因,我无法在 Python 中实现没有 mTLS 的客户端,尽管我可以在 Golang 中实现。

在Python中我有

os.environ["GRPC_VERBOSITY"] = "DEBUG"
# os.environ["GRPC_DEFAULT_SSL_ROOTS_FILE_PATH"] = "/etc/ssl/certs/ca-bundle.crt"

channel = grpc.secure_channel(ADDR, grpc.ssl_channel_credentials())
grpc.channel_ready_future(channel).result(timeout=10)
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误

D0513 08:02:08.147319164   21092 security_handshaker.cc:181] Security handshake failed: {"created":"@1652446928.147311309","description":"Handshake failed","file":"src/core/lib/security/transport/security_handshaker.cc","file_line":377,"tsi_code":10,"tsi_error":"TSI_PROTOCOL_FAILURE"}
Run Code Online (Sandbox Code Playgroud)

如果我通过取消注释行来使用 SSL 证书,我可以让它工作。我知道我的服务器不会请求、要求或验证客户端证书,因为以下 Go 代码可以完美运行

D0513 08:02:08.147319164   21092 security_handshaker.cc:181] Security handshake failed: {"created":"@1652446928.147311309","description":"Handshake failed","file":"src/core/lib/security/transport/security_handshaker.cc","file_line":377,"tsi_code":10,"tsi_error":"TSI_PROTOCOL_FAILURE"}
Run Code Online (Sandbox Code Playgroud)

小智 1

https://grpc.github.io/grpc/python/_modules/grpc.html#secure_channelchannel = grpc.secure_channel(ORBIUM_ADDR, grpc.ssl_channel_credentials()). 此函数依赖于类通道,请参阅文档https://grpc.github.io/grpc/python/_modules/grpc/aio/_channel.html

基本上,Channel 类包装 C 代码以提供安全通道。封装的 C 代码需要证书。如果您可以用 C 语言实现,那么更改 C 代码可能是最简单的方法。