Skip to content

Commit 8a44dc7

Browse files
committed
refactor: add record_principal to ConnectionInfoCache ABC and probe in LazyRefreshCache
1 parent 5a9b3f2 commit 8a44dc7

6 files changed

Lines changed: 194 additions & 94 deletions

File tree

‎google/cloud/sql/connector/connection_info.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ async def connect_info(self) -> ConnectionInfo:
4545
async def force_refresh(self) -> None:
4646
pass
4747

48+
@abc.abstractmethod
49+
def record_principal(self, user: str, database: str) -> None:
50+
pass
51+
4852
@abc.abstractmethod
4953
async def close(self) -> None:
5054
pass

‎google/cloud/sql/connector/connector.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ async def connect_async(
338338
self._client,
339339
self._keys,
340340
enable_iam_auth,
341+
ip_type=self._ip_type,
342+
timeout=self._timeout,
341343
)
342344
else:
343345
logger.debug(

‎google/cloud/sql/connector/instance.py‎

Lines changed: 115 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,7 @@ def __init__(
9191

9292
def record_principal(self, user: str, database: str) -> None:
9393
"""Records a PostgreSQL (user, database) pair observed on an IAM connection."""
94-
if not user:
95-
return
96-
if not database:
97-
database = user
98-
pair = (user, database)
99-
if pair not in self._iam_principals:
100-
self._iam_principals.append(pair)
94+
_append_iam_principal(self._iam_principals, user, database)
10195

10296
@property
10397
def conn_name(self) -> ConnectionName:
@@ -164,91 +158,14 @@ async def _perform_refresh(self) -> ConnectionInfo:
164158

165159
async def _probe_connection(self, conn_info: ConnectionInfo) -> None:
166160
"""Proactively probes the database to refresh IAM tokens on server-side MCP."""
167-
targets: list[str] = []
168-
if self._conn_name.domain_name:
169-
targets.append(self._conn_name.domain_name)
170-
else:
171-
if self._ip_type.value in conn_info.ip_addrs:
172-
targets.extend(conn_info.ip_addrs[self._ip_type.value])
173-
174-
if not targets:
175-
logger.debug(
176-
f"['{self._conn_name}']: Proactive IAM token refresh probe skipped: no target IP addresses"
177-
)
178-
return
179-
180-
port = SERVER_PROXY_PORT
181-
try:
182-
ssl_context = await conn_info.create_ssl_context(self._enable_iam_auth)
183-
except Exception as e: # noqa: BLE001
184-
logger.debug(
185-
f"['{self._conn_name}']: Failed to create SSL context for probe: {e!s}"
186-
)
187-
return
188-
189-
principals: list[tuple[str, str] | None] = []
190-
if (
191-
conn_info.database_version.startswith("POSTGRES")
192-
and self._iam_principals
193-
):
194-
principals.extend(self._iam_principals)
195-
else:
196-
principals.append(None)
197-
198-
all_succeeded = True
199-
for principal in principals:
200-
probed_principal = False
201-
for target in targets:
202-
try:
203-
logger.debug(
204-
f"['{self._conn_name}']: Probing IAM token refresh on {target}:{port}"
205-
)
206-
reader, writer = await asyncio.wait_for(
207-
asyncio.open_connection(
208-
host=target,
209-
port=port,
210-
ssl=ssl_context,
211-
server_hostname=(
212-
self._conn_name.domain_name
213-
if self._conn_name.domain_name
214-
else None
215-
),
216-
),
217-
timeout=float(self._timeout),
218-
)
219-
try:
220-
if principal is not None:
221-
user, database = principal
222-
writer.write(
223-
_build_postgres_startup_packet(user, database)
224-
)
225-
await writer.drain()
226-
await asyncio.wait_for(
227-
reader.read(1024),
228-
timeout=float(self._timeout),
229-
)
230-
writer.write(b"X\x00\x00\x00\x04")
231-
await writer.drain()
232-
finally:
233-
writer.close()
234-
await writer.wait_closed()
235-
probed_principal = True
236-
break
237-
except Exception as e: # noqa: BLE001
238-
logger.debug(
239-
f"['{self._conn_name}']: Probing IAM token refresh on {target}:{port} failed: {e!s}"
240-
)
241-
if not probed_principal:
242-
all_succeeded = False
243-
244-
if all_succeeded:
245-
logger.debug(
246-
f"['{self._conn_name}']: Proactive IAM token refresh probe successful"
247-
)
248-
else:
249-
logger.debug(
250-
f"['{self._conn_name}']: Proactive IAM token refresh probe encountered error across all targets"
251-
)
161+
await _probe_instance_connection(
162+
self._conn_name,
163+
conn_info,
164+
self._enable_iam_auth,
165+
self._ip_type,
166+
self._timeout,
167+
self._iam_principals,
168+
)
252169

253170
def _schedule_refresh(self, delay: int) -> asyncio.Task:
254171
"""
@@ -337,6 +254,112 @@ async def close(self) -> None:
337254
self._closed = True
338255

339256

257+
def _append_iam_principal(
258+
principals: list[tuple[str, str]], user: str, database: str
259+
) -> None:
260+
"""Appends (user, database) to principals if not already present."""
261+
if not user:
262+
return
263+
if not database:
264+
database = user
265+
pair = (user, database)
266+
if pair not in principals:
267+
principals.append(pair)
268+
269+
270+
async def _probe_instance_connection(
271+
conn_name: ConnectionName,
272+
conn_info: ConnectionInfo,
273+
enable_iam_auth: bool,
274+
ip_type: IPTypes,
275+
timeout: int,
276+
iam_principals: list[tuple[str, str]],
277+
) -> None:
278+
"""Proactively probes the database to refresh IAM tokens on server-side MCP."""
279+
targets: list[str] = []
280+
if conn_name.domain_name:
281+
targets.append(conn_name.domain_name)
282+
else:
283+
if ip_type.value in conn_info.ip_addrs:
284+
targets.extend(conn_info.ip_addrs[ip_type.value])
285+
286+
if not targets:
287+
logger.debug(
288+
f"['{conn_name}']: Proactive IAM token refresh probe skipped: no target IP addresses"
289+
)
290+
return
291+
292+
port = SERVER_PROXY_PORT
293+
try:
294+
ssl_context = await conn_info.create_ssl_context(enable_iam_auth)
295+
except Exception as e: # noqa: BLE001
296+
logger.debug(
297+
f"['{conn_name}']: Failed to create SSL context for probe: {e!s}"
298+
)
299+
return
300+
301+
principals: list[tuple[str, str] | None] = []
302+
if conn_info.database_version.startswith("POSTGRES") and iam_principals:
303+
principals.extend(iam_principals)
304+
else:
305+
principals.append(None)
306+
307+
all_succeeded = True
308+
for principal in principals:
309+
probed_principal = False
310+
for target in targets:
311+
try:
312+
logger.debug(
313+
f"['{conn_name}']: Probing IAM token refresh on {target}:{port}"
314+
)
315+
reader, writer = await asyncio.wait_for(
316+
asyncio.open_connection(
317+
host=target,
318+
port=port,
319+
ssl=ssl_context,
320+
server_hostname=(
321+
conn_name.domain_name
322+
if conn_name.domain_name
323+
else None
324+
),
325+
),
326+
timeout=float(timeout),
327+
)
328+
try:
329+
if principal is not None:
330+
user, database = principal
331+
writer.write(
332+
_build_postgres_startup_packet(user, database)
333+
)
334+
await writer.drain()
335+
await asyncio.wait_for(
336+
reader.read(1024),
337+
timeout=float(timeout),
338+
)
339+
writer.write(b"X\x00\x00\x00\x04")
340+
await writer.drain()
341+
finally:
342+
writer.close()
343+
await writer.wait_closed()
344+
probed_principal = True
345+
break
346+
except Exception as e: # noqa: BLE001
347+
logger.debug(
348+
f"['{conn_name}']: Probing IAM token refresh on {target}:{port} failed: {e!s}"
349+
)
350+
if not probed_principal:
351+
all_succeeded = False
352+
353+
if all_succeeded:
354+
logger.debug(
355+
f"['{conn_name}']: Proactive IAM token refresh probe successful"
356+
)
357+
else:
358+
logger.debug(
359+
f"['{conn_name}']: Proactive IAM token refresh probe encountered error across all targets"
360+
)
361+
362+
340363
def _build_postgres_startup_packet(user: str, database: str) -> bytes:
341364
"""Builds a PostgreSQL v3.0 StartupMessage for (user, database)."""
342365
if not database:

‎google/cloud/sql/connector/lazy.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
from google.cloud.sql.connector.connection_info import ConnectionInfo
2525
from google.cloud.sql.connector.connection_info import ConnectionInfoCache
2626
from google.cloud.sql.connector.connection_name import ConnectionName
27+
from google.cloud.sql.connector.enums import IPTypes
28+
from google.cloud.sql.connector.instance import _append_iam_principal
29+
from google.cloud.sql.connector.instance import _probe_instance_connection
30+
from google.cloud.sql.connector.instance import DEFAULT_CONNECT_TIMEOUT
2731
from google.cloud.sql.connector.refresh_utils import _refresh_buffer
2832

2933
logger = logging.getLogger(name=__name__)
@@ -44,6 +48,8 @@ def __init__(
4448
client: CloudSQLClient,
4549
keys: asyncio.Future,
4650
enable_iam_auth: bool = False,
51+
ip_type: IPTypes | str = IPTypes.PUBLIC,
52+
timeout: int = DEFAULT_CONNECT_TIMEOUT,
4753
) -> None:
4854
"""Initializes a LazyRefreshCache instance.
4955
@@ -56,16 +62,27 @@ def __init__(
5662
enable_iam_auth (bool): Enables automatic IAM database authentication
5763
(Postgres and MySQL) as the default authentication method for all
5864
connections.
65+
ip_type (IPTypes | str): Preferred IP type used to connect to the instance.
66+
timeout (int): Connect timeout in seconds.
5967
"""
6068
self._conn_name = conn_name
6169
self._enable_iam_auth = enable_iam_auth
70+
if isinstance(ip_type, str):
71+
ip_type = IPTypes._from_str(ip_type)
72+
self._ip_type = ip_type
73+
self._timeout = timeout
6274
self._keys = keys
6375
self._client = client
6476
self._lock = asyncio.Lock()
77+
self._iam_principals: list[tuple[str, str]] = []
6578
self._cached: ConnectionInfo | None = None
6679
self._needs_refresh = False
6780
self._closed = False
6881

82+
def record_principal(self, user: str, database: str) -> None:
83+
"""Records a PostgreSQL (user, database) pair observed on an IAM connection."""
84+
_append_iam_principal(self._iam_principals, user, database)
85+
6986
@property
7087
def conn_name(self) -> ConnectionName:
7188
return self._conn_name
@@ -82,6 +99,17 @@ async def force_refresh(self) -> None:
8299
async with self._lock:
83100
self._needs_refresh = True
84101

102+
async def _probe_connection(self, conn_info: ConnectionInfo) -> None:
103+
"""Proactively probes the database to refresh IAM tokens on server-side MCP."""
104+
await _probe_instance_connection(
105+
self._conn_name,
106+
conn_info,
107+
self._enable_iam_auth,
108+
self._ip_type,
109+
self._timeout,
110+
self._iam_principals,
111+
)
112+
85113
async def connect_info(self) -> ConnectionInfo:
86114
"""Retrieves ConnectionInfo instance for establishing a secure
87115
connection to the Cloud SQL instance.
@@ -110,6 +138,8 @@ async def connect_info(self) -> ConnectionInfo:
110138
self._keys,
111139
self._enable_iam_auth,
112140
)
141+
if self._enable_iam_auth:
142+
await self._probe_connection(conn_info)
113143
except Exception as e:
114144
logger.debug(
115145
f"['{self._conn_name}']: Connection info "

‎google/cloud/sql/connector/monitored_cache.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ async def force_refresh(self) -> None:
140140
return await self.cache.force_refresh()
141141

142142
def record_principal(self, user: str, database: str) -> None:
143-
if hasattr(self.cache, "record_principal"):
144-
self.cache.record_principal(user, database)
143+
self.cache.record_principal(user, database)
145144

146145
async def close(self) -> None:
147146
# Cancel domain name ticker task.

‎tests/unit/test_lazy.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,45 @@ async def test_LazyRefreshCache_connect_info_error(
111111

112112
await cache.close()
113113

114+
115+
async def test_LazyRefreshCache_probe_connection_postgres_startup_packet(
116+
fake_client: CloudSQLClient,
117+
) -> None:
118+
"""
119+
Test that LazyRefreshCache.connect_info probes the instance with a PostgreSQL
120+
StartupMessage and Terminate when enable_iam_auth=True and a principal is recorded.
121+
"""
122+
from unittest.mock import MagicMock, patch
123+
from google.cloud.sql.connector.instance import _build_postgres_startup_packet
124+
125+
keys = asyncio.create_task(generate_keys())
126+
cache = LazyRefreshCache(
127+
ConnectionName("test-project", "test-region", "test-instance"),
128+
client=fake_client,
129+
keys=keys,
130+
enable_iam_auth=True,
131+
)
132+
cache.record_principal("iam-user@example.com", "mydb")
133+
134+
mock_reader = AsyncMock()
135+
mock_reader.read = AsyncMock(return_value=b"R\x00\x00\x00\x08\x00\x00\x00\x00")
136+
mock_writer = MagicMock()
137+
mock_writer.drain = AsyncMock()
138+
mock_writer.wait_closed = AsyncMock()
139+
140+
with patch(
141+
"google.cloud.sql.connector.instance.asyncio.open_connection",
142+
AsyncMock(return_value=(mock_reader, mock_writer)),
143+
) as mock_open_conn:
144+
await cache.connect_info()
145+
146+
mock_open_conn.assert_awaited_once()
147+
written_packets = [call.args[0] for call in mock_writer.write.call_args_list]
148+
expected_startup = _build_postgres_startup_packet("iam-user@example.com", "mydb")
149+
assert written_packets == [expected_startup, b"X\x00\x00\x00\x04"]
150+
mock_reader.read.assert_awaited_once()
151+
mock_writer.close.assert_called_once()
152+
mock_writer.wait_closed.assert_awaited_once()
153+
await cache.close()
154+
155+

0 commit comments

Comments
 (0)