Cleanup DnsUpdateWriter

- Update DnsUpdateWriter to load nameservers names using
  DomainBase.loadNameserverFullyQualifiedHostNames() to be consistent with
  other uses.
- Pass "whole entity" to helper methods to simplify code.
- Cleanup damage from prior rename of DnsMessageTransport.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=124854517
This commit is contained in:
Hans Ridder 2016-06-14 10:34:03 -07:00 committed by Justine Tunney
parent 9a2b88ee28
commit 2ac5d3694b

View file

@ -16,7 +16,6 @@ package google.registry.dns.writer.dnsupdate;
import static com.google.common.base.Verify.verify; import static com.google.common.base.Verify.verify;
import static google.registry.model.EppResourceUtils.loadByUniqueId; import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.ofy.ObjectifyService.ofy;
import com.google.common.net.InternetDomainName; import com.google.common.net.InternetDomainName;
@ -53,10 +52,9 @@ import javax.inject.Inject;
* A DnsWriter that implements the DNS UPDATE protocol as specified in * A DnsWriter that implements the DNS UPDATE protocol as specified in
* <a href="https://tools.ietf.org/html/rfc2136">RFC 2136</a>. Publishes changes in the * <a href="https://tools.ietf.org/html/rfc2136">RFC 2136</a>. Publishes changes in the
* domain-registry to a (capable) external DNS server, sometimes called a "hidden master". DNS * domain-registry to a (capable) external DNS server, sometimes called a "hidden master". DNS
* UPDATE messages are sent via a "resolver" class which implements the network transport. For each * UPDATE messages are sent via a supplied "transport" class. For each publish call, a single
* publish call, a single UPDATE message is created containing the records required to "synchronize" * UPDATE message is created containing the records required to "synchronize" the DNS with the
* the DNS with the current (at the time of processing) state of the registry, for the supplied * current (at the time of processing) state of the registry, for the supplied domain/host.
* domain/host.
* *
* <p>The general strategy of the publish methods is to delete <em>all</em> resource records of any * <p>The general strategy of the publish methods is to delete <em>all</em> resource records of any
* <em>type</em> that match the exact domain/host name supplied. And then for create/update cases, * <em>type</em> that match the exact domain/host name supplied. And then for create/update cases,
@ -76,23 +74,23 @@ import javax.inject.Inject;
public class DnsUpdateWriter implements DnsWriter { public class DnsUpdateWriter implements DnsWriter {
private final Duration dnsTimeToLive; private final Duration dnsTimeToLive;
private final DnsMessageTransport resolver; private final DnsMessageTransport transport;
private final Clock clock; private final Clock clock;
/** /**
* Class constructor. * Class constructor.
* *
* @param dnsTimeToLive TTL used for any created resource records * @param dnsTimeToLive TTL used for any created resource records
* @param resolver a resolver used to send/receive the UPDATE messages * @param transport the transport used to send/receive the UPDATE messages
* @param clock a source of time * @param clock a source of time
*/ */
@Inject @Inject
public DnsUpdateWriter( public DnsUpdateWriter(
@Config("dnsUpdateTimeToLive") Duration dnsTimeToLive, @Config("dnsUpdateTimeToLive") Duration dnsTimeToLive,
DnsMessageTransport resolver, DnsMessageTransport transport,
Clock clock) { Clock clock) {
this.dnsTimeToLive = dnsTimeToLive; this.dnsTimeToLive = dnsTimeToLive;
this.resolver = resolver; this.transport = transport;
this.clock = clock; this.clock = clock;
} }
@ -103,12 +101,11 @@ public class DnsUpdateWriter implements DnsWriter {
Update update = new Update(toAbsoluteName(findTldFromName(domainName))); Update update = new Update(toAbsoluteName(findTldFromName(domainName)));
update.delete(toAbsoluteName(domainName), Type.ANY); update.delete(toAbsoluteName(domainName), Type.ANY);
if (domain != null && domain.shouldPublishToDns()) { if (domain != null && domain.shouldPublishToDns()) {
update.add(makeNameServerSet( update.add(makeNameServerSet(domain));
domainName, ofy().load().refs(domain.getNameservers()).values())); update.add(makeDelegationSignerSet(domain));
update.add(makeDelegationSignerSet(domainName, domain.getDsData()));
} }
Message response = resolver.send(update); Message response = transport.send(update);
verify( verify(
response.getRcode() == Rcode.NOERROR, response.getRcode() == Rcode.NOERROR,
"DNS server failed domain update for '%s' rcode: %s", "DNS server failed domain update for '%s' rcode: %s",
@ -126,11 +123,11 @@ public class DnsUpdateWriter implements DnsWriter {
Update update = new Update(toAbsoluteName(findTldFromName(hostName))); Update update = new Update(toAbsoluteName(findTldFromName(hostName)));
update.delete(toAbsoluteName(hostName), Type.ANY); update.delete(toAbsoluteName(hostName), Type.ANY);
if (host != null) { if (host != null) {
update.add(makeAddressSet(hostName, host.getInetAddresses())); update.add(makeAddressSet(host));
update.add(makeV6AddressSet(hostName, host.getInetAddresses())); update.add(makeV6AddressSet(host));
} }
Message response = resolver.send(update); Message response = transport.send(update);
verify( verify(
response.getRcode() == Rcode.NOERROR, response.getRcode() == Rcode.NOERROR,
"DNS server failed host update for '%s' rcode: %s", "DNS server failed host update for '%s' rcode: %s",
@ -147,13 +144,12 @@ public class DnsUpdateWriter implements DnsWriter {
@Override @Override
public void close() {} public void close() {}
private RRset makeDelegationSignerSet(String domainName, Iterable<DelegationSignerData> dsData) private RRset makeDelegationSignerSet(DomainResource domain) throws TextParseException {
throws TextParseException {
RRset signerSet = new RRset(); RRset signerSet = new RRset();
for (DelegationSignerData signerData : dsData) { for (DelegationSignerData signerData : domain.getDsData()) {
DSRecord dsRecord = DSRecord dsRecord =
new DSRecord( new DSRecord(
toAbsoluteName(domainName), toAbsoluteName(domain.getFullyQualifiedDomainName()),
DClass.IN, DClass.IN,
dnsTimeToLive.getStandardSeconds(), dnsTimeToLive.getStandardSeconds(),
signerData.getKeyTag(), signerData.getKeyTag(),
@ -165,43 +161,46 @@ public class DnsUpdateWriter implements DnsWriter {
return signerSet; return signerSet;
} }
private RRset makeNameServerSet(String domainName, Iterable<HostResource> nameservers) private RRset makeNameServerSet(DomainResource domain) throws TextParseException {
throws TextParseException {
RRset nameServerSet = new RRset(); RRset nameServerSet = new RRset();
for (HostResource host : nameservers) { for (String hostName : domain.loadNameserverFullyQualifiedHostNames()) {
NSRecord record = NSRecord record =
new NSRecord( new NSRecord(
toAbsoluteName(domainName), toAbsoluteName(domain.getFullyQualifiedDomainName()),
DClass.IN, DClass.IN,
dnsTimeToLive.getStandardSeconds(), dnsTimeToLive.getStandardSeconds(),
toAbsoluteName(host.getFullyQualifiedHostName())); toAbsoluteName(hostName));
nameServerSet.addRR(record); nameServerSet.addRR(record);
} }
return nameServerSet; return nameServerSet;
} }
private RRset makeAddressSet(String hostName, Iterable<InetAddress> addresses) private RRset makeAddressSet(HostResource host) throws TextParseException {
throws TextParseException {
RRset addressSet = new RRset(); RRset addressSet = new RRset();
for (InetAddress address : addresses) { for (InetAddress address : host.getInetAddresses()) {
if (address instanceof Inet4Address) { if (address instanceof Inet4Address) {
ARecord record = ARecord record =
new ARecord( new ARecord(
toAbsoluteName(hostName), DClass.IN, dnsTimeToLive.getStandardSeconds(), address); toAbsoluteName(host.getFullyQualifiedHostName()),
DClass.IN,
dnsTimeToLive.getStandardSeconds(),
address);
addressSet.addRR(record); addressSet.addRR(record);
} }
} }
return addressSet; return addressSet;
} }
private RRset makeV6AddressSet(String hostName, Iterable<InetAddress> addresses) private RRset makeV6AddressSet(HostResource host) throws TextParseException {
throws TextParseException {
RRset addressSet = new RRset(); RRset addressSet = new RRset();
for (InetAddress address : addresses) { for (InetAddress address : host.getInetAddresses()) {
if (address instanceof Inet6Address) { if (address instanceof Inet6Address) {
AAAARecord record = AAAARecord record =
new AAAARecord( new AAAARecord(
toAbsoluteName(hostName), DClass.IN, dnsTimeToLive.getStandardSeconds(), address); toAbsoluteName(host.getFullyQualifiedHostName()),
DClass.IN,
dnsTimeToLive.getStandardSeconds(),
address);
addressSet.addRR(record); addressSet.addRR(record);
} }
} }