-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for DNS names with Connector (#1204)
The Connector may be configured to use a DNS name to look up the instance name instead of configuring the connector with the instance connection name directly. Add a DNS TXT record for the Cloud SQL instance to a private DNS server or a private Google Cloud DNS Zone used by your application. For example: Record type: TXT Name: prod-db.mycompany.example.com – This is the domain name used by the application Value: my-project:my-region:my-instance – This is the instance connection name Configure the Connector to use a DNS name via setting resolver=DnsResolver
- Loading branch information
1 parent
11f9fe9
commit 1a8f274
Showing
14 changed files
with
309 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import dns.asyncresolver | ||
|
||
from google.cloud.sql.connector.connection_name import _parse_instance_connection_name | ||
from google.cloud.sql.connector.connection_name import ConnectionName | ||
from google.cloud.sql.connector.exceptions import DnsResolutionError | ||
|
||
|
||
class DefaultResolver: | ||
"""DefaultResolver simply validates and parses instance connection name.""" | ||
|
||
async def resolve(self, connection_name: str) -> ConnectionName: | ||
return _parse_instance_connection_name(connection_name) | ||
|
||
|
||
class DnsResolver(dns.asyncresolver.Resolver): | ||
""" | ||
DnsResolver resolves domain names into instance connection names using | ||
TXT records in DNS. | ||
""" | ||
|
||
async def resolve(self, dns: str) -> ConnectionName: # type: ignore | ||
try: | ||
conn_name = _parse_instance_connection_name(dns) | ||
except ValueError: | ||
# The connection name was not project:region:instance format. | ||
# Attempt to query a TXT record to get connection name. | ||
conn_name = await self.query_dns(dns) | ||
return conn_name | ||
|
||
async def query_dns(self, dns: str) -> ConnectionName: | ||
try: | ||
# Attempt to query the TXT records. | ||
records = await super().resolve(dns, "TXT", raise_on_no_answer=True) | ||
# Sort the TXT record values alphabetically, strip quotes as record | ||
# values can be returned as raw strings | ||
rdata = [record.to_text().strip('"') for record in records] | ||
rdata.sort() | ||
# Attempt to parse records, returning the first valid record. | ||
for record in rdata: | ||
try: | ||
conn_name = _parse_instance_connection_name(record) | ||
return conn_name | ||
except Exception: | ||
continue | ||
# If all records failed to parse, throw error | ||
raise DnsResolutionError( | ||
f"Unable to parse TXT record for `{dns}` -> `{rdata[0]}`" | ||
) | ||
# Don't override above DnsResolutionError | ||
except DnsResolutionError: | ||
raise | ||
except Exception as e: | ||
raise DnsResolutionError(f"Unable to resolve TXT record for `{dns}`") from e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
aiofiles==24.1.0 | ||
aiohttp==3.11.9 | ||
cryptography==44.0.0 | ||
dnspython==2.7.0 | ||
Requests==2.32.3 | ||
google-auth==2.36.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.