-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Automatically configure connections using DNS. Part of #2043.
- Loading branch information
Showing
8 changed files
with
238 additions
and
15 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
87 changes: 87 additions & 0 deletions
87
core/src/main/java/com/google/cloud/sql/core/DnsInstanceConnectionNameResolver.java
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,87 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.google.cloud.sql.core; | ||
|
||
import java.util.Collection; | ||
import java.util.Objects; | ||
import javax.naming.NameNotFoundException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* An implementation of InstanceConnectionNameResolver that uses DNS SRV records to resolve an | ||
* instance name from a domain name. | ||
*/ | ||
class DnsInstanceConnectionNameResolver implements InstanceConnectionNameResolver { | ||
private static final Logger logger = | ||
LoggerFactory.getLogger(DnsInstanceConnectionNameResolver.class); | ||
|
||
private final DnsResolver dnsResolver; | ||
|
||
public DnsInstanceConnectionNameResolver(DnsResolver dnsResolver) { | ||
this.dnsResolver = dnsResolver; | ||
} | ||
|
||
@Override | ||
public CloudSqlInstanceName resolve(final String name) { | ||
// Attempt to parse the instance name | ||
try { | ||
return new CloudSqlInstanceName(name); | ||
} catch (IllegalArgumentException e) { | ||
// Not a well-formed instance name. | ||
} | ||
|
||
// Next, attempt to resolve DNS name. | ||
Collection<DnsSrvRecord> instanceNames; | ||
try { | ||
instanceNames = this.dnsResolver.resolveSrv(name); | ||
} catch (NameNotFoundException ne) { | ||
// No DNS record found. This is not a valid instance name. | ||
throw new IllegalArgumentException( | ||
String.format("Unable to resolve SRV record for \"%s\".", name)); | ||
} | ||
|
||
// Use the first valid instance name from the list | ||
// or throw an IllegalArgumentException if none of the values can be parsed. | ||
return instanceNames.stream() | ||
.map( | ||
r -> { | ||
String target = r.getTarget(); | ||
// Trim trailing '.' from target field | ||
if (target.endsWith(".")) { | ||
target = target.substring(0, target.length() - 1); | ||
} | ||
try { | ||
return new CloudSqlInstanceName(target); | ||
} catch (IllegalArgumentException e) { | ||
logger.info( | ||
"Unable to parse instance name in SRV record for " | ||
+ "domain name \"{}\" with target \"{}\"", | ||
name, | ||
target, | ||
e); | ||
return null; | ||
} | ||
}) | ||
.filter(Objects::nonNull) | ||
.findFirst() | ||
.orElseThrow( | ||
() -> | ||
new IllegalArgumentException( | ||
String.format("Unable to parse values of SRV record for \"%s\".", name))); | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
core/src/main/java/com/google/cloud/sql/core/InstanceConnectionNameResolver.java
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,30 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.google.cloud.sql.core; | ||
|
||
/** Resolves the Cloud SQL Instance from the configuration name. */ | ||
interface InstanceConnectionNameResolver { | ||
|
||
/** | ||
* Resolves the CloudSqlInstanceName from a configuration string value. | ||
* | ||
* @param name the configuration string | ||
* @return the CloudSqlInstanceName | ||
* @throws IllegalArgumentException if the name cannot be resolved. | ||
*/ | ||
CloudSqlInstanceName resolve(String name); | ||
} |
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