Correctly handle TXT records with multiple heritages mixed with actual TXT targets

This commit is contained in:
Aron Tsang 2025-04-26 16:28:11 +08:00
parent 2481c07e95
commit cb586229f5

View File

@ -145,18 +145,22 @@ func (im *TXTRegistry) Records(ctx context.Context) ([]*endpoint.Endpoint, error
endpoints = append(endpoints, record) endpoints = append(endpoints, record)
continue continue
} }
// We simply assume that TXT records for the registry will always have only one target.
// If there are no targets (e.g for routing policy based records in google), direct targets will be empty // If there are no targets (e.g for routing policy based records in google), direct targets will be empty
if len(record.Targets) == 0 { if len(record.Targets) == 0 {
log.Errorf("TXT record has no targets %s", record.DNSName) log.Errorf("TXT record has no targets %s", record.DNSName)
continue continue
} }
labels, err := endpoint.NewLabelsFromString(record.Targets[0], im.txtEncryptAESKey)
// Some of the targets in the TXT records may be heritage labels
// filter them out and add them to the txtRecordsMap
txtTargets := record.Targets
record.Targets = endpoint.Targets{}
for _, target := range txtTargets {
labels, err := endpoint.NewLabelsFromString(target, im.txtEncryptAESKey)
if errors.Is(err, endpoint.ErrInvalidHeritage) { if errors.Is(err, endpoint.ErrInvalidHeritage) {
// if no heritage is found or it is invalid // add target back into record.Targets as
// case when value of txt record cannot be identified // this is not a heritage/label entry
// record will not be removed as it will have empty owner record.Targets = append(record.Targets, target)
endpoints = append(endpoints, record)
continue continue
} }
if err != nil { if err != nil {
@ -173,6 +177,11 @@ func (im *TXTRegistry) Records(ctx context.Context) ([]*endpoint.Endpoint, error
txtRecordsMap[record.DNSName] = struct{}{} txtRecordsMap[record.DNSName] = struct{}{}
} }
if len(record.Targets) != 0 {
endpoints = append(endpoints, record)
}
}
for _, ep := range endpoints { for _, ep := range endpoints {
if ep.Labels == nil { if ep.Labels == nil {
ep.Labels = endpoint.NewLabels() ep.Labels = endpoint.NewLabels()