/
CloudDnsWriterTest.java
492 lines (430 loc) · 18.2 KB
/
CloudDnsWriterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// 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 google.registry.dns.writer.clouddns;
import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.newDomainResource;
import static google.registry.testing.DatastoreHelper.newHostResource;
import static google.registry.testing.DatastoreHelper.persistResource;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.api.services.dns.Dns;
import com.google.api.services.dns.model.Change;
import com.google.api.services.dns.model.ResourceRecordSet;
import com.google.api.services.dns.model.ResourceRecordSetsListResponse;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.common.net.InetAddresses;
import com.google.common.util.concurrent.RateLimiter;
import com.googlecode.objectify.Key;
import google.registry.dns.writer.clouddns.CloudDnsWriter.ZoneStateException;
import google.registry.model.domain.DomainResource;
import google.registry.model.domain.secdns.DelegationSignerData;
import google.registry.model.eppcommon.StatusValue;
import google.registry.model.host.HostResource;
import google.registry.testing.AppEngineRule;
import google.registry.testing.ExceptionRule;
import google.registry.util.Retrier;
import google.registry.util.SystemClock;
import google.registry.util.SystemSleeper;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.concurrent.Callable;
import javax.annotation.Nullable;
import org.joda.time.Duration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
/** Test case for {@link CloudDnsWriter}. */
@RunWith(MockitoJUnitRunner.class)
public class CloudDnsWriterTest {
private static final Inet4Address IPv4 = (Inet4Address) InetAddresses.forString("127.0.0.1");
private static final Inet6Address IPv6 = (Inet6Address) InetAddresses.forString("::1");
private static final DelegationSignerData DS_DATA =
DelegationSignerData.create(12345, 3, 1, base16().decode("1234567890ABCDEF"));
private static final Duration DEFAULT_A_TTL = Duration.standardSeconds(11);
private static final Duration DEFAULT_NS_TTL = Duration.standardSeconds(222);
private static final Duration DEFAULT_DS_TTL = Duration.standardSeconds(3333);
@Mock private Dns dnsConnection;
@Mock private Dns.ResourceRecordSets resourceRecordSets;
@Mock private Dns.ResourceRecordSets.List listResourceRecordSetsRequest;
@Mock private Dns.Changes changes;
@Mock private Dns.Changes.Create createChangeRequest;
@Mock private Callable<Void> mutateZoneCallable;
@Captor ArgumentCaptor<String> recordNameCaptor;
@Captor ArgumentCaptor<Change> changeCaptor;
private CloudDnsWriter writer;
private ImmutableSet<ResourceRecordSet> stubZone;
@Rule public final ExceptionRule thrown = new ExceptionRule();
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
@Before
public void setUp() throws Exception {
createTld("tld");
writer =
new CloudDnsWriter(
dnsConnection,
"projectId",
"zoneName",
DEFAULT_A_TTL,
DEFAULT_NS_TTL,
DEFAULT_DS_TTL,
RateLimiter.create(20),
new SystemClock(),
new Retrier(new SystemSleeper(), 5));
// Create an empty zone.
stubZone = ImmutableSet.of();
when(dnsConnection.changes()).thenReturn(changes);
when(dnsConnection.resourceRecordSets()).thenReturn(resourceRecordSets);
when(resourceRecordSets.list(anyString(), anyString()))
.thenReturn(listResourceRecordSetsRequest);
when(listResourceRecordSetsRequest.setName(recordNameCaptor.capture()))
.thenReturn(listResourceRecordSetsRequest);
// Return records from our stub zone when a request to list the records is executed
when(listResourceRecordSetsRequest.execute())
.thenAnswer(
new Answer<ResourceRecordSetsListResponse>() {
@Override
public ResourceRecordSetsListResponse answer(InvocationOnMock invocationOnMock)
throws Throwable {
return new ResourceRecordSetsListResponse()
.setRrsets(
FluentIterable.from(stubZone)
.filter(
new Predicate<ResourceRecordSet>() {
@Override
public boolean apply(
@Nullable ResourceRecordSet resourceRecordSet) {
if (resourceRecordSet == null) {
return false;
}
return resourceRecordSet
.getName()
.equals(recordNameCaptor.getValue());
}
})
.toList());
}
});
when(changes.create(anyString(), anyString(), changeCaptor.capture()))
.thenReturn(createChangeRequest);
// Change our stub zone when a request to change the records is executed
when(createChangeRequest.execute())
.thenAnswer(
new Answer<Change>() {
@Override
public Change answer(InvocationOnMock invocationOnMock) throws IOException {
Change requestedChange = changeCaptor.getValue();
ImmutableSet<ResourceRecordSet> toDelete =
ImmutableSet.copyOf(requestedChange.getDeletions());
ImmutableSet<ResourceRecordSet> toAdd =
ImmutableSet.copyOf(requestedChange.getAdditions());
// Fail if the records to delete has records that aren't in the stub zone.
// This matches documented Google Cloud DNS behavior.
if (!Sets.difference(toDelete, stubZone).isEmpty()) {
throw new IOException();
}
stubZone =
Sets.union(Sets.difference(stubZone, toDelete).immutableCopy(), toAdd)
.immutableCopy();
return requestedChange;
}
});
}
private void verifyZone(ImmutableSet<ResourceRecordSet> expectedRecords) throws Exception {
// Trigger zone changes
writer.commit();
assertThat(stubZone).containsExactlyElementsIn(expectedRecords);
}
/** Returns a a zone cut with records for a domain */
private static ImmutableSet<ResourceRecordSet> fakeDomainRecords(
String domainName,
int v4InBailiwickNameservers,
int v6InBailiwickNameservers,
int externalNameservers,
int dsRecords) {
ImmutableSet.Builder<ResourceRecordSet> recordSetBuilder = new ImmutableSet.Builder<>();
// Add IPv4 in-bailiwick nameservers
if (v4InBailiwickNameservers > 0) {
ImmutableList.Builder<String> nameserverHostnames = new ImmutableList.Builder<>();
for (int i = 0; i < v4InBailiwickNameservers; i++) {
nameserverHostnames.add(i + ".ip4." + domainName + ".");
}
recordSetBuilder.add(
new ResourceRecordSet()
.setKind("dns#resourceRecordSet")
.setType("NS")
.setName(domainName + ".")
.setTtl((int) DEFAULT_NS_TTL.getStandardSeconds())
.setRrdatas(nameserverHostnames.build()));
// Add glue for IPv4 in-bailiwick nameservers
for (int i = 0; i < v4InBailiwickNameservers; i++) {
recordSetBuilder.add(
new ResourceRecordSet()
.setKind("dns#resourceRecordSet")
.setType("A")
.setName(i + ".ip4." + domainName + ".")
.setTtl((int) DEFAULT_A_TTL.getStandardSeconds())
.setRrdatas(ImmutableList.of(IPv4.toString())));
}
}
// Add IPv6 in-bailiwick nameservers
if (v6InBailiwickNameservers > 0) {
ImmutableList.Builder<String> nameserverHostnames = new ImmutableList.Builder<>();
for (int i = 0; i < v6InBailiwickNameservers; i++) {
nameserverHostnames.add(i + ".ip6." + domainName + ".");
}
recordSetBuilder.add(
new ResourceRecordSet()
.setKind("dns#resourceRecordSet")
.setType("NS")
.setName(domainName + ".")
.setTtl((int) DEFAULT_NS_TTL.getStandardSeconds())
.setRrdatas(nameserverHostnames.build()));
// Add glue for IPv6 in-bailiwick nameservers
for (int i = 0; i < v6InBailiwickNameservers; i++) {
recordSetBuilder.add(
new ResourceRecordSet()
.setKind("dns#resourceRecordSet")
.setType("AAAA")
.setName(i + ".ip6." + domainName + ".")
.setTtl((int) DEFAULT_A_TTL.getStandardSeconds())
.setRrdatas(ImmutableList.of(IPv6.toString())));
}
}
// Add external nameservers
if (externalNameservers > 0) {
ImmutableList.Builder<String> nameserverHostnames = new ImmutableList.Builder<>();
for (int i = 0; i < externalNameservers; i++) {
nameserverHostnames.add(i + ".external.");
}
recordSetBuilder.add(
new ResourceRecordSet()
.setKind("dns#resourceRecordSet")
.setType("NS")
.setName(domainName + ".")
.setTtl((int) DEFAULT_NS_TTL.getStandardSeconds())
.setRrdatas(nameserverHostnames.build()));
}
// Add DS records
if (dsRecords > 0) {
ImmutableList.Builder<String> dsRecordData = new ImmutableList.Builder<>();
for (int i = 0; i < dsRecords; i++) {
dsRecordData.add(
DelegationSignerData.create(
i, DS_DATA.getAlgorithm(), DS_DATA.getDigestType(), DS_DATA.getDigest())
.toRrData());
}
recordSetBuilder.add(
new ResourceRecordSet()
.setKind("dns#resourceRecordSet")
.setType("DS")
.setName(domainName + ".")
.setTtl((int) DEFAULT_DS_TTL.getStandardSeconds())
.setRrdatas(dsRecordData.build()));
}
return recordSetBuilder.build();
}
/** Returns a domain to be persisted in Datastore. */
private static DomainResource fakeDomain(
String domainName, ImmutableSet<HostResource> nameservers, int numDsRecords) {
ImmutableSet.Builder<DelegationSignerData> dsDataBuilder = new ImmutableSet.Builder<>();
for (int i = 0; i < numDsRecords; i++) {
dsDataBuilder.add(
DelegationSignerData.create(
i, DS_DATA.getAlgorithm(), DS_DATA.getDigestType(), DS_DATA.getDigest()));
}
ImmutableSet.Builder<Key<HostResource>> hostResourceRefBuilder = new ImmutableSet.Builder<>();
for (HostResource nameserver : nameservers) {
hostResourceRefBuilder.add(Key.create(nameserver));
}
return newDomainResource(domainName)
.asBuilder()
.setNameservers(hostResourceRefBuilder.build())
.setDsData(dsDataBuilder.build())
.build();
}
/** Returns a nameserver used for its NS record. */
private static HostResource fakeHost(String nameserver, InetAddress... addresses) {
return newHostResource(nameserver)
.asBuilder()
.setInetAddresses(ImmutableSet.copyOf(addresses))
.build();
}
@Test
public void testLoadDomain_nonExistentDomain() throws Exception {
writer.publishDomain("example.tld");
verifyZone(ImmutableSet.<ResourceRecordSet>of());
}
@Test
public void testLoadDomain_noDsDataOrNameservers() throws Exception {
persistResource(fakeDomain("example.tld", ImmutableSet.<HostResource>of(), 0));
writer.publishDomain("example.tld");
verifyZone(fakeDomainRecords("example.tld", 0, 0, 0, 0));
}
@Test
public void testLoadDomain_deleteOldData() throws Exception {
stubZone = fakeDomainRecords("example.tld", 2, 2, 2, 2);
persistResource(fakeDomain("example.tld", ImmutableSet.<HostResource>of(), 0));
writer.publishDomain("example.tld");
verifyZone(fakeDomainRecords("example.tld", 0, 0, 0, 0));
}
@Test
public void testLoadDomain_withExternalNs() throws Exception {
persistResource(
fakeDomain("example.tld", ImmutableSet.of(persistResource(fakeHost("0.external"))), 0));
writer.publishDomain("example.tld");
verifyZone(fakeDomainRecords("example.tld", 0, 0, 1, 0));
}
@Test
public void testLoadDomain_withDsData() throws Exception {
persistResource(
fakeDomain("example.tld", ImmutableSet.of(persistResource(fakeHost("0.external"))), 1));
writer.publishDomain("example.tld");
verifyZone(fakeDomainRecords("example.tld", 0, 0, 1, 1));
}
@Test
public void testLoadDomain_withInBailiwickNs_IPv4() throws Exception {
persistResource(
fakeDomain(
"example.tld",
ImmutableSet.of(persistResource(fakeHost("0.ip4.example.tld", IPv4))),
0))
.asBuilder()
.addSubordinateHost("0.ip4.example.tld")
.build();
writer.publishDomain("example.tld");
verifyZone(fakeDomainRecords("example.tld", 1, 0, 0, 0));
}
@Test
public void testLoadDomain_withInBailiwickNs_IPv6() throws Exception {
persistResource(
fakeDomain(
"example.tld",
ImmutableSet.of(persistResource(fakeHost("0.ip6.example.tld", IPv6))),
0))
.asBuilder()
.addSubordinateHost("0.ip6.example.tld")
.build();
writer.publishDomain("example.tld");
verifyZone(fakeDomainRecords("example.tld", 0, 1, 0, 0));
}
@Test
public void testLoadHost_externalHost() throws Exception {
writer.publishHost("ns1.example.com");
// external hosts should not be published in our zone
verifyZone(ImmutableSet.<ResourceRecordSet>of());
}
@Test
public void testLoadHost_removeStaleNsRecords() throws Exception {
// Initialize the zone with both NS records
stubZone = fakeDomainRecords("example.tld", 2, 0, 0, 0);
// Model the domain with only one NS record -- this is equivalent to creating it
// with two NS records and then deleting one
persistResource(
fakeDomain(
"example.tld",
ImmutableSet.of(persistResource(fakeHost("0.ip4.example.tld", IPv4))),
0))
.asBuilder()
.addSubordinateHost("0.ip4.example.tld")
.build();
// Ask the writer to delete the deleted NS record and glue
writer.publishHost("1.ip4.example.tld");
verifyZone(fakeDomainRecords("example.tld", 1, 0, 0, 0));
}
@Test
@SuppressWarnings("unchecked")
public void retryMutateZoneOnError() throws Exception {
CloudDnsWriter spyWriter = spy(writer);
when(mutateZoneCallable.call()).thenThrow(ZoneStateException.class).thenReturn(null);
when(spyWriter.getMutateZoneCallback(
Matchers.<ImmutableMap<String, ImmutableSet<ResourceRecordSet>>>any()))
.thenReturn(mutateZoneCallable);
spyWriter.commit();
verify(mutateZoneCallable, times(2)).call();
}
@Test
public void testLoadDomain_withClientHold() throws Exception {
persistResource(
fakeDomain(
"example.tld",
ImmutableSet.of(persistResource(fakeHost("0.ip4.example.tld", IPv4))),
0)
.asBuilder()
.addStatusValue(StatusValue.CLIENT_HOLD)
.build());
writer.publishDomain("example.tld");
verifyZone(ImmutableSet.<ResourceRecordSet>of());
}
@Test
public void testLoadDomain_withServerHold() throws Exception {
persistResource(
fakeDomain(
"example.tld",
ImmutableSet.of(persistResource(fakeHost("0.ip4.example.tld", IPv4))),
0)
.asBuilder()
.addStatusValue(StatusValue.SERVER_HOLD)
.build());
writer.publishDomain("example.tld");
verifyZone(ImmutableSet.<ResourceRecordSet>of());
}
@Test
public void testLoadDomain_withPendingDelete() throws Exception {
persistResource(
fakeDomain(
"example.tld",
ImmutableSet.of(persistResource(fakeHost("0.ip4.example.tld", IPv4))),
0)
.asBuilder()
.addStatusValue(StatusValue.PENDING_DELETE)
.build());
writer.publishDomain("example.tld");
verifyZone(ImmutableSet.<ResourceRecordSet>of());
}
@Test
public void testDuplicateRecords() throws Exception {
// In publishing DNS records, we can end up publishing information on the same host twice
// (through a domain change and a host change), so this scenario needs to work.
persistResource(
fakeDomain(
"example.tld",
ImmutableSet.of(persistResource(fakeHost("0.ip4.example.tld", IPv4))),
0))
.asBuilder()
.addSubordinateHost("ns1.example.tld")
.build();
writer.publishDomain("example.tld");
writer.publishHost("0.ip4.example.tld");
verifyZone(fakeDomainRecords("example.tld", 1, 0, 0, 0));
}
}