Skip to content

Commit

Permalink
Get zone domain name dynamically
Browse files Browse the repository at this point in the history
Before this commit, the zone's domain name had to be passed as a
configuration parameter to Dyna3. This is no longer the case, it
suffices to pass the zone id, and the domain name is resolved
dynamically from the zone id via Route53 API call.
  • Loading branch information
ahachete committed Nov 26, 2022
1 parent d366f13 commit e4d21a5
Showing 1 changed file with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
import com.ongres.labs.dyna53.route53.exception.Route53Exception;
import com.ongres.labs.dyna53.route53.exception.TimeoutException;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.services.route53.Route53AsyncClient;
import software.amazon.awssdk.services.route53.model.*;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.Optional;
Expand All @@ -33,25 +36,47 @@ public class Route53Manager {

private static final int MAX_VALUES_PER_RESOURCE_RECORD = 400;

private static final Logger LOGGER = LoggerFactory.getLogger(Route53Manager.class);

@ConfigProperty(name = "hosted_zone")
String hostedZone;

@ConfigProperty(name = "zone_domain_name")
String zoneDomainName;

@Inject
Route53AsyncClient route53AsyncClient;

@PostConstruct
void getZoneDomainNameFromHostedZone() {
var getHostedZoneRequest = GetHostedZoneRequest.builder()
.id(hostedZone)
.build();

route53AsyncClient.getHostedZone(getHostedZoneRequest)
.orTimeout(ROUTE53_API_CALLS_TIMEOUT_SECONDS, TimeUnit.SECONDS)
.whenComplete(
(response, throwable) -> {
if(null == response) {
throw new RuntimeException("Invalid hosted zone id " + hostedZone + " or error querying it");
}

zoneDomainName = response.hostedZone().name();
LOGGER.info("Setting zone domain name to " + zoneDomainName);
}
)
.join();
}

private String recordNameDotEnded(String label) {
return label + "." + zoneDomainName + ".";
return label + "." + zoneDomainName;
}

private String labelFrom(String subLabel, String label) {
return subLabel + "." + label;
}

private String route53FullLabel2Label(String value) {
return value.substring(0, value.length() - ("." + zoneDomainName).length() - 1);
return value.substring(0, value.length() - ("." + zoneDomainName).length());
}

private CompletableFuture<ChangeResourceRecordSetsResponse> actionAsyncResourceRecord(
Expand Down Expand Up @@ -243,7 +268,7 @@ public Stream<String> listSRVRecordsLabel(int priority, int weight, int port) {
public Stream<String> listTXTRecordsWithLabel(String label, String namePrefixRegex) {
return listAllRecords(RRType.TXT)
.filter(resourceRecordSet ->
resourceRecordSet.name().endsWith("." + label + "." + zoneDomainName + ".")
resourceRecordSet.name().endsWith("." + label + "." + zoneDomainName)
&& resourceRecordSet.name().split("\\.")[0].matches(namePrefixRegex)
).flatMap(resourceRecordSet -> resourceRecordSet.resourceRecords().stream())
.map(resourceRecord -> txtResourceValue2String(resourceRecord.value()))
Expand Down

0 comments on commit e4d21a5

Please sign in to comment.