2323import os
2424import random
2525import socket
26+ import ssl
2627from threading import Thread
2728import time
2829from types import TracebackType
3839from google .cloud .sql .connector import pymysql
3940from google .cloud .sql .connector import pytds
4041from google .cloud .sql .connector .client import CloudSQLClient
42+ from google .cloud .sql .connector .connection_info import ConnectionInfo
4143from google .cloud .sql .connector .connection_name import ConnectionName
4244from google .cloud .sql .connector .enums import DriverMapping
4345from google .cloud .sql .connector .enums import IPTypes
4951from google .cloud .sql .connector .instance import RefreshAheadCache
5052from google .cloud .sql .connector .lazy import LazyRefreshCache
5153from google .cloud .sql .connector .monitored_cache import MonitoredCache
54+ from google .cloud .sql .connector .postgres_connection import PostgresConnection
5255from google .cloud .sql .connector .resolver import DefaultResolver
5356from google .cloud .sql .connector .resolver import DnsResolver
5457from google .cloud .sql .connector .sqldata_client import SqlDataClient
@@ -346,6 +349,7 @@ def _get_or_create_cache(
346349 self ,
347350 conn_name : ConnectionName ,
348351 enable_iam_auth : bool ,
352+ ip_type : IPTypes | None = None ,
349353 ) -> MonitoredCache :
350354 assert self ._client is not None , "client must be initialized before creating cache"
351355 assert self ._keys is not None , "keys must be initialized before creating cache"
@@ -355,6 +359,13 @@ def _get_or_create_cache(
355359 ].closed :
356360 return self ._cache [(str (conn_name ), enable_iam_auth )]
357361
362+ effective_ip_type = ip_type if ip_type is not None else self ._ip_type
363+ dial_func = partial (
364+ self ._dial_probe_connection ,
365+ enable_iam_auth = enable_iam_auth ,
366+ ip_type = effective_ip_type ,
367+ )
368+
358369 if self ._refresh_strategy == RefreshStrategy .LAZY :
359370 logger .debug (
360371 f"['{ conn_name } ']: Refresh strategy is set to lazy refresh"
@@ -364,6 +375,9 @@ def _get_or_create_cache(
364375 self ._client ,
365376 self ._keys ,
366377 enable_iam_auth ,
378+ ip_type = effective_ip_type ,
379+ timeout = self ._timeout ,
380+ dial_func = dial_func ,
367381 )
368382 else :
369383 logger .debug (
@@ -374,6 +388,9 @@ def _get_or_create_cache(
374388 self ._client ,
375389 self ._keys ,
376390 enable_iam_auth ,
391+ ip_type = effective_ip_type ,
392+ timeout = self ._timeout ,
393+ dial_func = dial_func ,
377394 )
378395 # wrap cache as a MonitoredCache
379396 monitored_cache = MonitoredCache (
@@ -452,7 +469,9 @@ async def connect_async(
452469
453470 conn_name = await self ._resolver .resolve (instance_connection_string )
454471 if ip_type != IPTypes .SQL_DATA :
455- monitored_cache = self ._get_or_create_cache (conn_name , enable_iam_auth )
472+ monitored_cache = self ._get_or_create_cache (
473+ conn_name , enable_iam_auth , ip_type
474+ )
456475
457476 connect_func = {
458477 "pymysql" : pymysql .connect ,
@@ -587,33 +606,7 @@ async def get_conn_info():
587606 await self ._remove_cached (str (conn_name ), enable_iam_auth )
588607 raise
589608
590- targets = []
591- if conn_info .conn_name .domain_name and isinstance (self ._resolver , DnsResolver ):
592- try :
593- ips = await self ._resolver .resolve_a_record (conn_info .conn_name .domain_name )
594- if ips :
595- targets .extend (ips )
596- logger .debug (
597- f"['{ instance_connection_string } ']: Custom DNS name "
598- f"'{ conn_info .conn_name .domain_name } ' resolved to '{ ips } ', "
599- "using it to connect"
600- )
601- else :
602- logger .debug (
603- f"['{ instance_connection_string } ']: Custom DNS name "
604- f"'{ conn_info .conn_name .domain_name } ' resolved but returned no "
605- f"entries, using '{ preferred_ips } ' from instance metadata"
606- )
607- targets .extend (preferred_ips )
608- except Exception as e : # noqa: BLE001
609- logger .debug (
610- f"['{ instance_connection_string } ']: Custom DNS name "
611- f"'{ conn_info .conn_name .domain_name } ' did not resolve to an IP "
612- f"address: { e } , using '{ preferred_ips } ' from instance metadata"
613- )
614- targets .extend (preferred_ips )
615- else :
616- targets .extend (preferred_ips )
609+ targets = await self ._resolve_targets (conn_info , preferred_ips )
617610 # format `user` param for automatic IAM database authn
618611 if enable_iam_auth :
619612 formatted_user = format_database_user (
@@ -636,30 +629,53 @@ async def get_conn_info():
636629 await conn_info .create_ssl_context (enable_iam_auth ),
637630 ** kwargs ,
638631 )
632+ if (
633+ enable_iam_auth
634+ and conn_info .database_version .startswith ("POSTGRES" )
635+ ):
636+ db_name = kwargs .get ("database" )
637+ if db_name is None :
638+ db_name = kwargs .get ("db" )
639+ user = kwargs .get ("user" )
640+ if user :
641+ monitored_cache .record_principal (
642+ str (user ),
643+ str (db_name ) if db_name else str (user ),
644+ )
639645 last_ex = None
640646 return conn
641647
642648 # Create socket with SSLContext for sync drivers
643649 ctx = await conn_info .create_ssl_context (enable_iam_auth )
644650 raw_sock = socket .create_connection ((target_ip , SERVER_PROXY_PORT ))
651+ sync_sock : socket .socket | ssl .SSLSocket | PostgresConnection
645652 try :
646- sock = ctx .wrap_socket (
653+ sync_sock = ctx .wrap_socket (
647654 raw_sock ,
648655 server_hostname = target_ip ,
649656 )
650657 except Exception :
651658 raw_sock .close ()
652659 raise
653660
661+ if (
662+ enable_iam_auth
663+ and conn_info .database_version .startswith ("POSTGRES" )
664+ ):
665+ sync_sock = PostgresConnection (
666+ sync_sock ,
667+ on_startup = monitored_cache .record_principal ,
668+ )
669+
654670 # If this connection was opened using a domain name, then store it
655671 # for later in case we need to forcibly close it on failover.
656672 if conn_info .conn_name .domain_name :
657- monitored_cache .sockets .append (sock )
673+ monitored_cache .sockets .append (sync_sock )
658674 # Synchronous drivers are blocking and run using executor
659675 connect_partial = partial (
660676 connector ,
661677 target_ip ,
662- sock ,
678+ sync_sock ,
663679 ** kwargs ,
664680 )
665681 conn = await self ._loop .run_in_executor (None , connect_partial )
@@ -670,7 +686,6 @@ async def get_conn_info():
670686 f"['{ conn_info .conn_name } ']: Connection to { target_ip } failed: { e } "
671687 )
672688 last_ex = e
673-
674689 if last_ex :
675690 raise last_ex
676691
@@ -694,6 +709,87 @@ async def _remove_cached(
694709 cache = self ._cache .pop ((instance_connection_string , enable_iam_auth ))
695710 await cache .close ()
696711
712+ async def _resolve_targets (
713+ self ,
714+ conn_info : ConnectionInfo ,
715+ preferred_ips : list [str ],
716+ ) -> list [str ]:
717+ """Resolves target IP addresses for a Cloud SQL instance, with DNS fallback."""
718+ targets : list [str ] = []
719+ if conn_info .conn_name .domain_name and isinstance (self ._resolver , DnsResolver ):
720+ try :
721+ ips = await self ._resolver .resolve_a_record (
722+ conn_info .conn_name .domain_name
723+ )
724+ if ips :
725+ targets .extend (ips )
726+ logger .debug (
727+ f"['{ conn_info .conn_name } ']: Custom DNS name "
728+ f"'{ conn_info .conn_name .domain_name } ' resolved to '{ ips } ', "
729+ "using it to connect"
730+ )
731+ else :
732+ logger .debug (
733+ f"['{ conn_info .conn_name } ']: Custom DNS name "
734+ f"'{ conn_info .conn_name .domain_name } ' resolved but returned no "
735+ f"entries, using '{ preferred_ips } ' from instance metadata"
736+ )
737+ targets .extend (preferred_ips )
738+ except Exception as e : # noqa: BLE001
739+ logger .debug (
740+ f"['{ conn_info .conn_name } ']: Custom DNS name "
741+ f"'{ conn_info .conn_name .domain_name } ' did not resolve to an IP "
742+ f"address: { e } , using '{ preferred_ips } ' from instance metadata"
743+ )
744+ targets .extend (preferred_ips )
745+ else :
746+ targets .extend (preferred_ips )
747+ return targets
748+
749+ async def _dial_probe_connection (
750+ self ,
751+ conn_info : ConnectionInfo ,
752+ enable_iam_auth : bool ,
753+ ip_type : IPTypes | None = None ,
754+ ) -> tuple [asyncio .StreamReader , asyncio .StreamWriter ]:
755+ """Dials a TLS probe connection using the connector's IP resolution and SSLContext."""
756+ effective_ip_type = ip_type if ip_type is not None else self ._ip_type
757+ preferred_ips = conn_info .get_preferred_ips (effective_ip_type )
758+ targets = await self ._resolve_targets (conn_info , preferred_ips )
759+
760+ if not targets :
761+ raise ConnectionError (
762+ f"No target IP addresses found for { conn_info .conn_name } "
763+ )
764+
765+ ssl_context = await conn_info .create_ssl_context (enable_iam_auth )
766+ server_hostname = (
767+ conn_info .conn_name .domain_name
768+ if conn_info .conn_name .domain_name
769+ else None
770+ )
771+
772+ last_err : Exception | None = None
773+ for target in targets :
774+ try :
775+ reader , writer = await asyncio .wait_for (
776+ asyncio .open_connection (
777+ host = target ,
778+ port = SERVER_PROXY_PORT ,
779+ ssl = ssl_context ,
780+ server_hostname = server_hostname ,
781+ ),
782+ timeout = float (max (1 , self ._timeout )),
783+ )
784+ return reader , writer
785+ except Exception as e : # noqa: BLE001
786+ last_err = e
787+ if last_err :
788+ raise last_err
789+ raise ConnectionError (
790+ f"Failed to connect to any target for { conn_info .conn_name } "
791+ )
792+
697793 def __enter__ (self ) -> Any :
698794 """Enter context manager by returning Connector object"""
699795 return self
0 commit comments