Skip to content

Commit 73246b8

Browse files
committed
refactor: Use new ConnectSettings.DnsNames field to validate the server TLS certificate.
1 parent fb8c21c commit 73246b8

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ venv
55
.python-version
66
cloud_sql_python_connector.egg-info/
77
dist/
8+
.idea

google/cloud/sql/connector/client.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,19 @@ async def _get_metadata(
157157
# Note that we have to check for PSC enablement also because CAS
158158
# instances also set the dnsName field.
159159
# Remove trailing period from DNS name. Required for SSL in Python
160-
dns_name = ret_dict.get("dnsName", "").rstrip(".")
161-
if dns_name and ret_dict.get("pscEnabled"):
160+
if ret_dict.get("pscEnabled"):
161+
psc_dns_names = [
162+
d["name"]
163+
for d in ret_dict.get("dnsNames", [])
164+
if d["connectionType"] == "PRIVATE_SERVICE_CONNECT"
165+
and d["dnsScope"] == "INSTANCE"
166+
]
167+
168+
dns_name = psc_dns_names[0] if psc_dns_names else None
169+
170+
if dns_name is None:
171+
dns_name = ret_dict.get("dnsName", "").rstrip(".")
172+
162173
ip_addresses["PSC"] = dns_name
163174

164175
return {

tests/unit/mocks.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@ async def connect_settings(self, request: Any) -> web.Response:
256256
"expirationTime": str(self.cert_expiration),
257257
},
258258
"dnsName": "abcde.12345.us-central1.sql.goog",
259+
"dnsNames": [
260+
{
261+
"name": "abcde.12345.us-central1.sql.goog",
262+
"connectionType": "PRIVATE_SERVICE_CONNECT",
263+
"dnsScope": "INSTANCE",
264+
}
265+
],
259266
"pscEnabled": self.psc_enabled,
260267
"ipAddresses": ip_addrs,
261268
"region": self.region,

0 commit comments

Comments
 (0)