77from urllib .parse import urlparse
88
99import grpc
10+ from anyio import fail_after
1011from anyio .to_thread import run_sync
1112
1213from jumpstarter .common .exceptions import ConfigurationError , ConnectionError
1314
1415
15- async def ssl_channel_credentials (target : str , tls_config ):
16+ async def ssl_channel_credentials (target : str , tls_config , timeout = 5 ):
1617 configure_grpc_env ()
1718 if tls_config .insecure or os .getenv ("JUMPSTARTER_GRPC_INSECURE" ) == "1" :
1819 try :
@@ -22,12 +23,15 @@ async def ssl_channel_credentials(target: str, tls_config):
2223 raise ConfigurationError (f"Failed parsing { target } " ) from e
2324
2425 try :
25- root_certificates = await run_sync (ssl .get_server_certificate , (parsed .hostname , port ))
26+ with fail_after (timeout ):
27+ root_certificates = await run_sync (ssl .get_server_certificate , (parsed .hostname , port ))
2628 return grpc .ssl_channel_credentials (root_certificates = root_certificates .encode ())
2729 except socket .gaierror as e :
2830 raise ConnectionError (f"Failed resolving { parsed .hostname } " ) from e
2931 except ConnectionRefusedError as e :
3032 raise ConnectionError (f"Failed connecting to { parsed .hostname } :{ port } " ) from e
33+ except TimeoutError as e :
34+ raise ConnectionError (f"Timeout connecting to { parsed .hostname } :{ port } " ) from e
3135
3236 elif tls_config .ca != "" :
3337 ca_certificate = base64 .b64decode (tls_config .ca )
0 commit comments