Skip to content

Commit 53e40c7

Browse files
fix: set PSC ip type based on pscEnabled (#1158)
With CAS-based instances being supported soon and also using dnsName, we can no longer rely on the presence of dnsName in the connectSettings API as identifying that PSC is enabled on an instance. Instead we should check pscEnabled from the response as source of truth. Check if pscEnabled is True before setting PSC ip type.
1 parent e730212 commit 53e40c7

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

google/cloud/sql/connector/client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,13 @@ async def _get_metadata(
141141
if "ipAddresses" in ret_dict
142142
else {}
143143
)
144-
# Remove trailing period from PSC DNS name.
145-
psc_dns = ret_dict.get("dnsName")
146-
if psc_dns:
147-
ip_addresses["PSC"] = psc_dns.rstrip(".")
144+
# resolve dnsName into IP address for PSC
145+
# Note that we have to check for PSC enablement also because CAS
146+
# instances also set the dnsName field.
147+
# Remove trailing period from DNS name. Required for SSL in Python
148+
dns_name = ret_dict.get("dnsName", "").rstrip(".")
149+
if dns_name and ret_dict.get("pscEnabled"):
150+
ip_addresses["PSC"] = dns_name
148151

149152
return {
150153
"ip_addresses": ip_addresses,

tests/unit/mocks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ def __init__(
234234
self.name = name
235235
self.db_version = db_version
236236
self.ip_addrs = ip_addrs
237+
self.psc_enabled = False
237238
self.cert_before = cert_before
238239
self.cert_expiration = cert_expiration
239240
# create self signed CA cert
@@ -255,6 +256,7 @@ async def connect_settings(self, request: Any) -> web.Response:
255256
"expirationTime": str(self.cert_expiration),
256257
},
257258
"dnsName": "abcde.12345.us-central1.sql.goog",
259+
"pscEnabled": self.psc_enabled,
258260
"ipAddresses": ip_addrs,
259261
"region": self.region,
260262
"databaseVersion": self.db_version,

tests/unit/test_client.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,36 @@
2424

2525

2626
@pytest.mark.asyncio
27-
async def test_get_metadata(fake_client: CloudSQLClient) -> None:
27+
async def test_get_metadata_no_psc(fake_client: CloudSQLClient) -> None:
2828
"""
29-
Test _get_metadata returns successfully.
29+
Test _get_metadata returns successfully and does not include PSC IP type.
3030
"""
3131
resp = await fake_client._get_metadata(
3232
"test-project",
3333
"test-region",
3434
"test-instance",
3535
)
3636
assert resp["database_version"] == "POSTGRES_15"
37+
assert resp["ip_addresses"] == {
38+
"PRIMARY": "127.0.0.1",
39+
"PRIVATE": "10.0.0.1",
40+
}
41+
assert isinstance(resp["server_ca_cert"], str)
42+
43+
44+
@pytest.mark.asyncio
45+
async def test_get_metadata_with_psc(fake_client: CloudSQLClient) -> None:
46+
"""
47+
Test _get_metadata returns successfully with PSC IP type.
48+
"""
49+
# set PSC to enabled on test instance
50+
fake_client.instance.psc_enabled = True
51+
resp = await fake_client._get_metadata(
52+
"test-project",
53+
"test-region",
54+
"test-instance",
55+
)
56+
assert resp["database_version"] == "POSTGRES_15"
3757
assert resp["ip_addresses"] == {
3858
"PRIMARY": "127.0.0.1",
3959
"PRIVATE": "10.0.0.1",

0 commit comments

Comments
 (0)