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

问题描述 投票:0回答:4

我有一个 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)

这给了我以下错误

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"}

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

conn, err := grpc.DialContext(
    ctx,
    gRPCAddr,
    grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")),
)
dummyClient := dummy.NewDummyServiceClient(conn)
if _, err := dummyClient.Ping(context.Background(), &dummy.PingRequest{
    Ping: "go client ping",
}); err != nil {
    return fmt.Errorf("failed to ping: %w", err)
}
python ssl grpc grpc-python grpc-go
4个回答
3
投票

如果服务器端的证书是公开签名的,您可以使用:

grpc.secure_channel(ADDR, grpc.ssl_channel_credentials())

但这似乎对你不起作用,所以我猜服务器证书是由你拥有的根证书签名的。您可以将根证书传递到

root_certificates
字段 [1],并将其他两个字段留空。此用例记录在我们的身份验证指南 [2] 中。

with open(os.environ["GRPC_DEFAULT_SSL_ROOTS_FILE_PATH"], 'rb') as f:
    creds = grpc.ssl_channel_credentials(f.read())

channel = grpc.secure_channel(ADDR, creds)

[1] https://grpc.github.io/grpc/python/grpc.html#grpc.ssl_channel_credentials

[2] https://grpc.io/docs/guides/auth/


1
投票

https://grpc.github.io/grpc/python/_modules/grpc.html#secure_channel

channel = 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 代码可能是最简单的。


0
投票

@former_Epsilon 给出的答案回答了我的问题,但是我针对该问题提出的解决方案是不同的,我最终使用了

secure_channel
,所以我也想发布一个答案。

import os
import grpc

# configure this dict for your systems
system_certs_map = {
    "Windows": "<Path to system cert>",
    "Darwin": "$REQUESTS_CA_BUNDLE",
    "Linux": "/etc/ssl/certs/ca-bundle.crt",
}

os.environ["GRPC_DEFAULT_SSL_ROOTS_FILE_PATH"] = system_certs_map[platform.system()]
channel_credentials = grpc.ssl_channel_credentials()

-4
投票

我基于Python GRPC文档的猜测https://grpc.github.io/grpc/python/grpc.html

channel = grpc.insecure_channel(ORBIUM_ADDR)

而不是:

channel = grpc.secure_channel(ORBIUM_ADDR, grpc.ssl_channel_credentials())
© www.soinside.com 2019 - 2024. All rights reserved.