我有一個具有有效 TLS 證書且不需要客戶端 TLS 的 grpc 服務器(在 Go 中)。出于某種原因,我無法在 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(ORBIUM_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)
}
uj5u.com熱心網友回復:
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 代碼可能是最簡單的。
uj5u.com熱心網友回復:
如果服務器端的證書是公開簽名的,您可以使用:
grpc.secure_channel(ORBIUM_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(ORBIUM_ADDR, creds)
[1] https://grpc.github.io/grpc/python/grpc.html#grpc.ssl_channel_credentials
[2] https://grpc.io/docs/guides/auth/
uj5u.com熱心網友回復:
我的猜測基于 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())
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/480547.html
標籤:Python ssl grpc grpc-python grpc-go
