|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.sql.core; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.Objects; |
| 21 | +import javax.naming.NameNotFoundException; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +/** |
| 26 | + * An implementation of InstanceConnectionNameResolver that uses DNS TXT records to resolve an |
| 27 | + * instance name from a domain name. |
| 28 | + */ |
| 29 | +class DnsInstanceConnectionNameResolver implements InstanceConnectionNameResolver { |
| 30 | + private static final Logger logger = |
| 31 | + LoggerFactory.getLogger(DnsInstanceConnectionNameResolver.class); |
| 32 | + |
| 33 | + private final DnsResolver dnsResolver; |
| 34 | + |
| 35 | + public DnsInstanceConnectionNameResolver(DnsResolver dnsResolver) { |
| 36 | + this.dnsResolver = dnsResolver; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public CloudSqlInstanceName resolve(final String name) { |
| 41 | + if (CloudSqlInstanceName.isValidInstanceName(name)) { |
| 42 | + // name contains a well-formed instance name. |
| 43 | + return new CloudSqlInstanceName(name); |
| 44 | + } |
| 45 | + |
| 46 | + if (CloudSqlInstanceName.isValidDomain(name)) { |
| 47 | + // name contains a well-formed domain name. |
| 48 | + return resolveDomainName(name); |
| 49 | + } |
| 50 | + |
| 51 | + // name is not well-formed, and therefore cannot be resolved. |
| 52 | + throw new IllegalArgumentException( |
| 53 | + String.format( |
| 54 | + "Unable to resolve database instance for \"%s\". It should be a " |
| 55 | + + "well-formed instance name or domain name.", |
| 56 | + name)); |
| 57 | + } |
| 58 | + |
| 59 | + private CloudSqlInstanceName resolveDomainName(String name) { |
| 60 | + // Next, attempt to resolve DNS name. |
| 61 | + Collection<String> instanceNames; |
| 62 | + try { |
| 63 | + instanceNames = this.dnsResolver.resolveTxt(name); |
| 64 | + } catch (NameNotFoundException ne) { |
| 65 | + // No DNS record found. This is not a valid instance name. |
| 66 | + throw new IllegalArgumentException( |
| 67 | + String.format( |
| 68 | + "Unable to resolve TXT record containing the instance name for " |
| 69 | + + "domain name \"%s\".", |
| 70 | + name)); |
| 71 | + } |
| 72 | + |
| 73 | + // Use the first valid instance name from the list |
| 74 | + // or throw an IllegalArgumentException if none of the values can be parsed. |
| 75 | + return instanceNames.stream() |
| 76 | + .map( |
| 77 | + target -> { |
| 78 | + try { |
| 79 | + return new CloudSqlInstanceName(target, name); |
| 80 | + } catch (IllegalArgumentException e) { |
| 81 | + logger.info( |
| 82 | + "Unable to parse instance name in TXT record for " |
| 83 | + + "domain name \"{}\" with target \"{}\"", |
| 84 | + name, |
| 85 | + target, |
| 86 | + e); |
| 87 | + return null; |
| 88 | + } |
| 89 | + }) |
| 90 | + .filter(Objects::nonNull) |
| 91 | + .findFirst() |
| 92 | + .orElseThrow( |
| 93 | + () -> |
| 94 | + new IllegalArgumentException( |
| 95 | + String.format("Unable to parse values of TXT record for \"%s\".", name))); |
| 96 | + } |
| 97 | +} |
0 commit comments