Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set TLS/SNI option in gRPC client of Python/C++?

Tags:

ssl

grpc

sni

How do I set the TLS/SNI (https://en.wikipedia.org/wiki/Server_Name_Indication) in the Python/C++ gRPC client API?

In other words, what's the equivalent of setting the -servername in openssl s_client?

I have verified my TLS server works by using the correct flags on openssl s_client:

 openssl s_client -connect "myserver.tunnel.dev:4443" -servername "myserver.tunnel.dev" 

However, I wasn't able to setup the credentials correct with the Python /C++ API:

uri = "myserver.tunnel.dev:4443"
hostname = "myserver.tunnel.dev"

creds = grpc.ssl_channel_credentials(
    root_certificates=dev_cert)
    # root_certificates=certificate_chain)
    # certificate_chain=certificate_chain)
channel = grpc.secure_channel(uri, creds,
    options=(('grpc.ssl_target_name_override', hostname),)
)

This throws:

grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNAVAILABLE, Connect Failed)>

In the ChannelOptions struct, the closest option I could find is ssl_target_name_override, which doesn't work either.

like image 220
steveyang Avatar asked Sep 21 '18 00:09

steveyang


1 Answers

Setting options=(('grpc.ssl_target_name_override', hostname),) should work. This is the right way to override host name. In this case, it seems unnecessary as your uri host and override host are the same. You could turn on some tracing by using the environment variables listed here and see if the handshake is failing or the is there some other reason for connection failure.

like image 157
srini Avatar answered Sep 28 '22 01:09

srini