dnscontrol/providers/desec/convert.go
Vincent Bernat c44ed88659
GANDI_V5 & DESEC: correctly handle multiple RR in a RRset (#1296)
When having multiple RR in a RRset, only a few of them may be applied.
In my case, when I have two A records, only one of them makes its way
to Gandi.

In `convert.go`, we had:

```go
var zrs []livedns.DomainRecord
// [...]
zrs = append(zrs, zr)
keys[key] = &zrs[len(zrs)-1]
```

If the slice needs to be extended when appending, the reference we got
in `keys[key]` may be outdated because the new slice contains a copy
of the old one. We either need to store references to domain records
in the slice or we need to stop keeping reference of items in the
slice.

I have fixed this with the second solution as I think the order of the
RRsets is not important.
2021-11-01 15:41:37 -04:00

78 lines
2.2 KiB
Go

package desec
// Convert the provider's native record description to models.RecordConfig.
import (
"fmt"
"github.com/StackExchange/dnscontrol/v3/models"
"github.com/StackExchange/dnscontrol/v3/pkg/printer"
)
// nativeToRecord takes a DNS record from deSEC and returns a native RecordConfig struct.
func nativeToRecords(n resourceRecord, origin string) (rcs []*models.RecordConfig) {
// deSEC returns all the values for a given label/rtype pair in each
// resourceRecord. In other words, if there are multiple A
// records for a label, all the IP addresses are listed in
// n.Records rather than having many resourceRecord's.
// We must split them out into individual records, one for each value.
for _, value := range n.Records {
rc := &models.RecordConfig{
TTL: n.TTL,
Original: n,
}
rc.SetLabel(n.Subname, origin)
switch rtype := n.Type; rtype {
default: // "A", "AAAA", "CAA", "NS", "CNAME", "MX", "PTR", "SRV", "TXT"
if err := rc.PopulateFromString(rtype, value, origin); err != nil {
panic(fmt.Errorf("unparsable record received from deSEC: %w", err))
}
}
rcs = append(rcs, rc)
}
return rcs
}
func recordsToNative(rcs []*models.RecordConfig, origin string) []resourceRecord {
// Take a list of RecordConfig and return an equivalent list of resourceRecord.
// deSEC requires one resourceRecord for each label:key tuple, therefore we
// might collapse many RecordConfig into one resourceRecord.
var keys = map[models.RecordKey]*resourceRecord{}
var zrs []resourceRecord
for _, r := range rcs {
label := r.GetLabel()
if label == "@" {
label = ""
}
key := r.Key()
if zr, ok := keys[key]; !ok {
// Allocate a new ZoneRecord:
zr := resourceRecord{
Type: r.Type,
TTL: r.TTL,
Subname: label,
Records: []string{r.GetTargetCombined()},
}
keys[key] = &zr
} else {
zr.Records = append(zr.Records, r.GetTargetCombined())
if r.TTL != zr.TTL {
printer.Warnf("All TTLs for a rrset (%v) must be the same. Using smaller of %v and %v.\n", key, r.TTL, zr.TTL)
if r.TTL < zr.TTL {
zr.TTL = r.TTL
}
}
}
}
for _, zr := range keys {
zrs = append(zrs, *zr)
}
return zrs
}