diff --git a/registry/mapper/mapper.go b/registry/mapper/mapper.go index 594348b4d6..37e02ec985 100644 --- a/registry/mapper/mapper.go +++ b/registry/mapper/mapper.go @@ -19,6 +19,8 @@ package mapper import ( "strings" + log "github.com/sirupsen/logrus" + "sigs.k8s.io/external-dns/endpoint" ) @@ -73,20 +75,22 @@ func (a AffixNameMapper) ToEndpointName(dns string) (string, string) { // drop suffix if a.isSuffix() { dc := strings.Count(a.suffix, ".") - DNSName := strings.SplitN(lowerDNSName, ".", 2+dc) - domainWithSuffix := strings.Join(DNSName[:1+dc], ".") - - r, rType := a.dropAffixExtractType(domainWithSuffix) - if !strings.Contains(lowerDNSName, ".") { + parts := strings.SplitN(lowerDNSName, ".", 2+dc) + if len(parts) <= dc { + log.Debugf("skipping TXT record %q: too few labels for suffix %q", dns, a.suffix) + return "", "" + } + r, rType := a.dropAffixExtractType(strings.Join(parts[:1+dc], ".")) + if len(parts) <= 1+dc { return r, rType } - return r + "." + DNSName[1+dc], rType + return r + "." + parts[1+dc], rType } return "", "" } func (a AffixNameMapper) ToTXTName(dns, recordType string) string { - DNSName := strings.SplitN(dns, ".", 2) + parts := strings.SplitN(dns, ".", 2) recordType = strings.ToLower(recordType) recordT := recordType + "-" @@ -94,19 +98,19 @@ func (a AffixNameMapper) ToTXTName(dns, recordType string) string { suffix := a.normalizeAffixTemplate(a.suffix, recordType) // If specified, replace a leading asterisk in the generated txt record name with some other string - if a.wildcardReplacement != "" && DNSName[0] == "*" { - DNSName[0] = a.wildcardReplacement + if a.wildcardReplacement != "" && parts[0] == "*" { + parts[0] = a.wildcardReplacement } if !a.recordTypeInAffix() { - DNSName[0] = recordT + DNSName[0] + parts[0] = recordT + parts[0] } - if len(DNSName) < 2 { - return prefix + DNSName[0] + suffix + if len(parts) < 2 { + return prefix + parts[0] + suffix } - return prefix + DNSName[0] + suffix + "." + DNSName[1] + return prefix + parts[0] + suffix + "." + parts[1] } func (a AffixNameMapper) recordTypeInAffix() bool { diff --git a/registry/mapper/mapper_test.go b/registry/mapper/mapper_test.go index 9e17866bff..202b0a67bb 100644 --- a/registry/mapper/mapper_test.go +++ b/registry/mapper/mapper_test.go @@ -107,6 +107,27 @@ func TestAffixNameMapper_ToEndpointName(t *testing.T) { wantEndpointName: "foo.example.com", wantRecordType: endpoint.RecordTypeCNAME, }, + { + name: "suffix with multiple dots and trailing labels", + mapper: NewAffixNameMapper("", ".foo.bar", ""), + input: "a-example.foo.bar.com", + wantEndpointName: "example.com", + wantRecordType: endpoint.RecordTypeA, + }, + { + name: "suffix with multiple dots and no trailing labels", + mapper: NewAffixNameMapper("", ".foo.bar", ""), + input: "a-example.foo.bar", + wantEndpointName: "example", + wantRecordType: endpoint.RecordTypeA, + }, + { + name: "suffix with multiple dots and too few labels does not panic", + mapper: NewAffixNameMapper("", ".foo.bar", ""), + input: "a-example.foo", + wantEndpointName: "", + wantRecordType: "", + }, { name: "no affix with A record", mapper: NewAffixNameMapper("", "", ""),