Skip to content

Commit 5a9b3f2

Browse files
committed
fix: send PostgreSQL v3 StartupMessage in proactive IAM refresh probe
1 parent 46d1cf0 commit 5a9b3f2

4 files changed

Lines changed: 128 additions & 30 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,9 @@ async def connect_async(
439439
f"['{instance_connection_string}']: Truncated IAM database username from {kwargs['user']} to {formatted_user}"
440440
)
441441
kwargs["user"] = formatted_user
442+
if conn_info.database_version.startswith("POSTGRES"):
443+
db_name = kwargs.get("db") or kwargs.get("database") or kwargs["user"]
444+
monitored_cache.record_principal(kwargs["user"], str(db_name))
442445

443446
try:
444447
last_ex = None

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

Lines changed: 88 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,21 @@ def __init__(
8484
rate=1 / 30,
8585
)
8686
self._refresh_in_progress = asyncio.locks.Event()
87+
self._iam_principals: list[tuple[str, str]] = []
8788
self._current: asyncio.Task = self._schedule_refresh(0)
8889
self._next: asyncio.Task = self._current
8990
self._closed = False
9091

92+
def record_principal(self, user: str, database: str) -> None:
93+
"""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)
101+
91102
@property
92103
def conn_name(self) -> ConnectionName:
93104
return self._conn_name
@@ -175,37 +186,69 @@ async def _probe_connection(self, conn_info: ConnectionInfo) -> None:
175186
)
176187
return
177188

178-
for target in targets:
179-
try:
180-
logger.debug(
181-
f"['{self._conn_name}']: Probing IAM token refresh on {target}:{port}"
182-
)
183-
_, writer = await asyncio.wait_for(
184-
asyncio.open_connection(
185-
host=target,
186-
port=port,
187-
ssl=ssl_context,
188-
server_hostname=(
189-
self._conn_name.domain_name
190-
if self._conn_name.domain_name
191-
else None
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+
),
192216
),
193-
),
194-
timeout=float(self._timeout),
195-
)
196-
writer.close()
197-
await writer.wait_closed()
198-
logger.debug(
199-
f"['{self._conn_name}']: Proactive IAM token refresh probe successful"
200-
)
201-
return
202-
except Exception as e: # noqa: BLE001
203-
logger.debug(
204-
f"['{self._conn_name}']: Probing IAM token refresh on {target}:{port} failed: {e!s}"
205-
)
206-
logger.debug(
207-
f"['{self._conn_name}']: Proactive IAM token refresh probe encountered error across all targets"
208-
)
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+
)
209252

210253
def _schedule_refresh(self, delay: int) -> asyncio.Task:
211254
"""
@@ -292,3 +335,18 @@ async def close(self) -> None:
292335
tasks = asyncio.gather(self._current, self._next, return_exceptions=True)
293336
await asyncio.wait_for(tasks, timeout=2.0)
294337
self._closed = True
338+
339+
340+
def _build_postgres_startup_packet(user: str, database: str) -> bytes:
341+
"""Builds a PostgreSQL v3.0 StartupMessage for (user, database)."""
342+
if not database:
343+
database = user
344+
body = (
345+
b"user\x00"
346+
+ user.encode("utf-8")
347+
+ b"\x00database\x00"
348+
+ database.encode("utf-8")
349+
+ b"\x00\x00"
350+
)
351+
total_len = 8 + len(body)
352+
return total_len.to_bytes(4, byteorder="big") + b"\x00\x03\x00\x00" + body

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ async def force_refresh(self) -> None:
139139
return
140140
return await self.cache.force_refresh()
141141

142+
def record_principal(self, user: str, database: str) -> None:
143+
if hasattr(self.cache, "record_principal"):
144+
self.cache.record_principal(user, database)
145+
142146
async def close(self) -> None:
143147
# Cancel domain name ticker task.
144148
if self.domain_name_ticker:

‎tests/unit/test_instance.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,36 @@ async def test_ConnectionInfo_create_ssl_context_no_tls1_3_warning() -> None:
391391
in mock_logger.warning.call_args[0][0]
392392
)
393393

394+
395+
@pytest.mark.asyncio
396+
async def test_probe_connection_postgres_startup_packet(
397+
cache: RefreshAheadCache,
398+
) -> None:
399+
"""Test that _probe_connection sends PostgreSQL v3 StartupMessage and Terminate when IAM principal is recorded."""
400+
from unittest.mock import MagicMock
401+
from google.cloud.sql.connector.instance import _build_postgres_startup_packet
402+
403+
cache._enable_iam_auth = True
404+
cache.record_principal("iam-user@example.com", "mydb")
405+
conn_info = await cache.connect_info()
406+
407+
mock_reader = AsyncMock()
408+
mock_reader.read = AsyncMock(return_value=b"R\x00\x00\x00\x08\x00\x00\x00\x00")
409+
mock_writer = MagicMock()
410+
mock_writer.drain = AsyncMock()
411+
mock_writer.wait_closed = AsyncMock()
412+
413+
with patch(
414+
"google.cloud.sql.connector.instance.asyncio.open_connection",
415+
AsyncMock(return_value=(mock_reader, mock_writer)),
416+
) as mock_open_conn:
417+
await cache._probe_connection(conn_info)
418+
419+
mock_open_conn.assert_awaited_once()
420+
written_packets = [call.args[0] for call in mock_writer.write.call_args_list]
421+
expected_startup = _build_postgres_startup_packet("iam-user@example.com", "mydb")
422+
assert written_packets == [expected_startup, b"X\x00\x00\x00\x04"]
423+
mock_reader.read.assert_awaited_once()
424+
mock_writer.close.assert_called_once()
425+
mock_writer.wait_closed.assert_awaited_once()
426+

0 commit comments

Comments
 (0)