Skip to content

Commit bed74b3

Browse files
authored
cmd/devp2p: fix comparison of TXT record value (#22572)
* cmd/devp2p: fix comparison of TXT record value The AWS API returns quoted DNS strings, so we must encode the new value before comparing it against the existing record content. * cmd/devp2p: add test * cmd/devp2p: fix typo and rename val -> newValue
1 parent 0fda25e commit bed74b3

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

cmd/devp2p/dns_route53.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (c *route53Client) deploy(name string, t *dnsdisc.Tree) error {
131131
}
132132
}
133133

134-
// wait for all change batches to propagate
134+
// Wait for all change batches to propagate.
135135
for _, change := range changesToCheck {
136136
log.Info(fmt.Sprintf("Waiting for change request %s", *change.ChangeInfo.Id))
137137
wreq := &route53.GetChangeInput{Id: change.ChangeInfo.Id}
@@ -196,24 +196,29 @@ func (c *route53Client) computeChanges(name string, records map[string]string, e
196196
records = lrecords
197197

198198
var changes []types.Change
199-
for path, val := range records {
199+
for path, newValue := range records {
200+
prevRecords, exists := existing[path]
201+
prevValue := strings.Join(prevRecords.values, "")
202+
203+
// prevValue contains quoted strings, encode newValue to compare.
204+
newValue = splitTXT(newValue)
205+
206+
// Assign TTL.
200207
ttl := int64(rootTTL)
201208
if path != name {
202209
ttl = int64(treeNodeTTL)
203210
}
204211

205-
prevRecords, exists := existing[path]
206-
prevValue := strings.Join(prevRecords.values, "")
207212
if !exists {
208213
// Entry is unknown, push a new one
209-
log.Info(fmt.Sprintf("Creating %s = %q", path, val))
210-
changes = append(changes, newTXTChange("CREATE", path, ttl, splitTXT(val)))
211-
} else if prevValue != val || prevRecords.ttl != ttl {
214+
log.Info(fmt.Sprintf("Creating %s = %s", path, newValue))
215+
changes = append(changes, newTXTChange("CREATE", path, ttl, newValue))
216+
} else if prevValue != newValue || prevRecords.ttl != ttl {
212217
// Entry already exists, only change its content.
213-
log.Info(fmt.Sprintf("Updating %s from %q to %q", path, prevValue, val))
214-
changes = append(changes, newTXTChange("UPSERT", path, ttl, splitTXT(val)))
218+
log.Info(fmt.Sprintf("Updating %s from %s to %s", path, prevValue, newValue))
219+
changes = append(changes, newTXTChange("UPSERT", path, ttl, newValue))
215220
} else {
216-
log.Info(fmt.Sprintf("Skipping %s = %q", path, val))
221+
log.Debug(fmt.Sprintf("Skipping %s = %s", path, newValue))
217222
}
218223
}
219224

@@ -288,21 +293,19 @@ func changeCount(ch types.Change) int {
288293

289294
// collectRecords collects all TXT records below the given name.
290295
func (c *route53Client) collectRecords(name string) (map[string]recordSet, error) {
291-
log.Info(fmt.Sprintf("Retrieving existing TXT records on %s (%s)", name, c.zoneID))
292296
var req route53.ListResourceRecordSetsInput
293297
req.HostedZoneId = &c.zoneID
294298
existing := make(map[string]recordSet)
295-
for {
299+
for page := 0; ; page++ {
300+
log.Info("Loading existing TXT records", "name", name, "zone", c.zoneID, "page", page)
296301
resp, err := c.api.ListResourceRecordSets(context.TODO(), &req)
297302
if err != nil {
298303
return existing, err
299304
}
300-
301305
for _, set := range resp.ResourceRecordSets {
302306
if !isSubdomain(*set.Name, name) || set.Type != types.RRTypeTxt {
303307
continue
304308
}
305-
306309
s := recordSet{ttl: *set.TTL}
307310
for _, rec := range set.ResourceRecords {
308311
s.values = append(s.values, *rec.Value)
@@ -314,14 +317,12 @@ func (c *route53Client) collectRecords(name string) (map[string]recordSet, error
314317
if !resp.IsTruncated {
315318
break
316319
}
317-
318-
// Set the cursor to the next batc. From the AWS docs:
320+
// Set the cursor to the next batch. From the AWS docs:
319321
//
320322
// To display the next page of results, get the values of NextRecordName,
321323
// NextRecordType, and NextRecordIdentifier (if any) from the response. Then submit
322324
// another ListResourceRecordSets request, and specify those values for
323325
// StartRecordName, StartRecordType, and StartRecordIdentifier.
324-
325326
req.StartRecordIdentifier = resp.NextRecordIdentifier
326327
req.StartRecordName = resp.NextRecordName
327328
req.StartRecordType = resp.NextRecordType

cmd/devp2p/dns_route53_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,29 @@ func TestRoute53ChangeSort(t *testing.T) {
162162
}
163163
}
164164

165+
// This test checks that computeChanges compares the quoted value of the records correctly.
166+
func TestRoute53NoChange(t *testing.T) {
167+
// Existing record set.
168+
testTree0 := map[string]recordSet{
169+
"n": {ttl: rootTTL, values: []string{
170+
`"enrtree-root:v1 e=JWXYDBPXYWG6FX3GMDIBFA6CJ4 l=C7HRFPF3BLGF3YR4DY5KX3SMBE seq=1 sig=o908WmNp7LibOfPsr4btQwatZJ5URBr2ZAuxvK4UWHlsB9sUOTJQaGAlLPVAhM__XJesCHxLISo94z5Z2a463gA"`,
171+
}},
172+
"2xs2367yhaxjfglzhvawlqd4zy.n": {ttl: treeNodeTTL, values: []string{
173+
`"enr:-HW4QOFzoVLaFJnNhbgMoDXPnOvcdVuj7pDpqRvh6BRDO68aVi5ZcjB3vzQRZH2IcLBGHzo8uUN3snqmgTiE56CH3AMBgmlkgnY0iXNlY3AyNTZrMaECC2_24YYkYHEgdzxlSNKQEnHhuNAbNlMlWJxrJxbAFvA"`,
174+
}},
175+
}
176+
// New set.
177+
testTree1 := map[string]string{
178+
"n": "enrtree-root:v1 e=JWXYDBPXYWG6FX3GMDIBFA6CJ4 l=C7HRFPF3BLGF3YR4DY5KX3SMBE seq=1 sig=o908WmNp7LibOfPsr4btQwatZJ5URBr2ZAuxvK4UWHlsB9sUOTJQaGAlLPVAhM__XJesCHxLISo94z5Z2a463gA",
179+
"2XS2367YHAXJFGLZHVAWLQD4ZY.n": "enr:-HW4QOFzoVLaFJnNhbgMoDXPnOvcdVuj7pDpqRvh6BRDO68aVi5ZcjB3vzQRZH2IcLBGHzo8uUN3snqmgTiE56CH3AMBgmlkgnY0iXNlY3AyNTZrMaECC2_24YYkYHEgdzxlSNKQEnHhuNAbNlMlWJxrJxbAFvA",
180+
}
181+
182+
var client route53Client
183+
changes := client.computeChanges("n", testTree1, testTree0)
184+
if len(changes) > 0 {
185+
t.Fatalf("wrong changes (got %d, want 0)", len(changes))
186+
}
187+
}
188+
165189
func sp(s string) *string { return &s }
166190
func ip(i int64) *int64 { return &i }

0 commit comments

Comments
 (0)