Modify GenerateZoneFilesAction to create output files more in line with standard DNS format

In standard DNS format, the first thing on an A, NS or DS definition line is a domain label relative to the zone, which in our case is a TLD. However, the generate_zone_files command prints out fully qualified host and domain names, resulting in a discrepancy when compared to the contents of the DNS subsystem. This CL removes the TLD suffix, which should remove one preprocessing step before file comparison.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=166103705
This commit is contained in:
mountford 2017-08-22 13:15:35 -07:00 committed by Ben McIlwain
parent e94ab94d13
commit 91d4fdb9a8
2 changed files with 34 additions and 17 deletions

View file

@ -223,7 +223,7 @@ public class GenerateZoneFilesAction implements Runnable, JsonActionRunner.JsonA
HostResource host = loadAtPointInTime(unprojectedHost, exportTime).now(); HostResource host = loadAtPointInTime(unprojectedHost, exportTime).now();
// A null means the host was deleted (or not created) at this time. // A null means the host was deleted (or not created) at this time.
if ((host != null) && subordinateHosts.contains(host.getFullyQualifiedHostName())) { if ((host != null) && subordinateHosts.contains(host.getFullyQualifiedHostName())) {
String stanza = hostStanza(host, dnsDefaultATtl); String stanza = hostStanza(host, dnsDefaultATtl, domain.getTld());
if (!stanza.isEmpty()) { if (!stanza.isEmpty()) {
emit(domain.getTld(), stanza); emit(domain.getTld(), stanza);
getContext().incrementCounter(domain.getTld() + " hosts"); getContext().incrementCounter(domain.getTld() + " hosts");
@ -274,10 +274,10 @@ public class GenerateZoneFilesAction implements Runnable, JsonActionRunner.JsonA
/** /**
* Generates DNS records for a domain (NS and DS). * Generates DNS records for a domain (NS and DS).
* *
* These look like this: * For domain foo.tld, these look like this:
* {@code * {@code
* foo.tld 180 IN NS ns.example.com. * foo 180 IN NS ns.example.com.
* foo.tld 86400 IN DS 1 2 3 000102 * foo 86400 IN DS 1 2 3 000102
* } * }
*/ */
private static String domainStanza( private static String domainStanza(
@ -286,10 +286,11 @@ public class GenerateZoneFilesAction implements Runnable, JsonActionRunner.JsonA
Duration dnsDefaultNsTtl, Duration dnsDefaultNsTtl,
Duration dnsDefaultDsTtl) { Duration dnsDefaultDsTtl) {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
String domainLabel = stripTld(domain.getFullyQualifiedDomainName(), domain.getTld());
for (HostResource nameserver : ofy().load().keys(domain.getNameservers()).values()) { for (HostResource nameserver : ofy().load().keys(domain.getNameservers()).values()) {
result.append(String.format( result.append(String.format(
NS_FORMAT, NS_FORMAT,
domain.getFullyQualifiedDomainName(), domainLabel,
dnsDefaultNsTtl.getStandardSeconds(), dnsDefaultNsTtl.getStandardSeconds(),
// Load the nameservers at the export time in case they've been renamed or deleted. // Load the nameservers at the export time in case they've been renamed or deleted.
loadAtPointInTime(nameserver, exportTime).now().getFullyQualifiedHostName())); loadAtPointInTime(nameserver, exportTime).now().getFullyQualifiedHostName()));
@ -297,7 +298,7 @@ public class GenerateZoneFilesAction implements Runnable, JsonActionRunner.JsonA
for (DelegationSignerData dsData : domain.getDsData()) { for (DelegationSignerData dsData : domain.getDsData()) {
result.append(String.format( result.append(String.format(
DS_FORMAT, DS_FORMAT,
domain.getFullyQualifiedDomainName(), domainLabel,
dnsDefaultDsTtl.getStandardSeconds(), dnsDefaultDsTtl.getStandardSeconds(),
dsData.getKeyTag(), dsData.getKeyTag(),
dsData.getAlgorithm(), dsData.getAlgorithm(),
@ -316,18 +317,34 @@ public class GenerateZoneFilesAction implements Runnable, JsonActionRunner.JsonA
* ns.foo.tld 3600 IN AAAA 0:0:0:0:0:0:0:1 * ns.foo.tld 3600 IN AAAA 0:0:0:0:0:0:0:1
* } * }
*/ */
private static String hostStanza(HostResource host, Duration dnsDefaultATtl) { private static String hostStanza(HostResource host, Duration dnsDefaultATtl, String tld) {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
for (InetAddress addr : host.getInetAddresses()) { for (InetAddress addr : host.getInetAddresses()) {
// must be either IPv4 or IPv6 // must be either IPv4 or IPv6
String rrSetClass = (addr instanceof Inet4Address) ? "A" : "AAAA"; String rrSetClass = (addr instanceof Inet4Address) ? "A" : "AAAA";
result.append(String.format( result.append(String.format(
A_FORMAT, A_FORMAT,
host.getFullyQualifiedHostName(), stripTld(host.getFullyQualifiedHostName(), tld),
dnsDefaultATtl.getStandardSeconds(), dnsDefaultATtl.getStandardSeconds(),
rrSetClass, rrSetClass,
addr.getHostAddress())); addr.getHostAddress()));
} }
return result.toString(); return result.toString();
} }
/**
* Removes the TLD, if present, from a fully-qualified name.
*
* <p>This would not work if a fully qualified host name in a different TLD were passed. But
* we only generate glue records for in-bailiwick name servers, meaning that the TLD will always
* match.
*
* If, for some unforeseen reason, the TLD is not present, indicate an error condition, so that
* our process for comparing Datastore and DNS data will realize that something is amiss.
*/
private static String stripTld(String fullyQualifiedName, String tld) {
return fullyQualifiedName.endsWith(tld)
? fullyQualifiedName.substring(0, fullyQualifiedName.length() - tld.length() - 1)
: (fullyQualifiedName + "***");
}
} }

View file

@ -1,14 +1,14 @@
$ORIGIN tld. $ORIGIN tld.
bar.tld 222 IN NS ns.bar.tld. bar 222 IN NS ns.bar.tld.
bar.tld 222 IN NS ns.foo.tld. bar 222 IN NS ns.foo.tld.
ns.bar.tld 11 IN A 127.0.0.1 ns.bar 11 IN A 127.0.0.1
ns.bar.tld 11 IN AAAA 0:0:0:0:0:0:0:1 ns.bar 11 IN AAAA 0:0:0:0:0:0:0:1
ns-only.tld 222 IN NS ns.foo.tld. ns-only 222 IN NS ns.foo.tld.
ns-only.tld 222 IN NS ns.bar.tld. ns-only 222 IN NS ns.bar.tld.
ns-and-ds.tld 222 IN NS ns.foo.tld. ns-and-ds 222 IN NS ns.foo.tld.
ns-and-ds.tld 222 IN NS ns.bar.tld. ns-and-ds 222 IN NS ns.bar.tld.
ns-and-ds.tld 3333 IN DS 1 2 3 000102 ns-and-ds 3333 IN DS 1 2 3 000102