Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: IDNA awareness in the zone finder #5147

Merged
merged 3 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions provider/zonefinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ limitations under the License.

package provider

import "strings"
import (
"strings"

log "github.com/sirupsen/logrus"
"golang.org/x/net/idna"
)

type ZoneIDName map[string]string

Expand All @@ -25,8 +30,13 @@ func (z ZoneIDName) Add(zoneID, zoneName string) {
}

func (z ZoneIDName) FindZone(hostname string) (suitableZoneID, suitableZoneName string) {
name, err := idna.Lookup.ToUnicode(hostname)
if err != nil {
log.Warnf("Failed to convert hostname '%s' to its Unicode form: %v", hostname, err)
name = hostname
}
for zoneID, zoneName := range z {
if hostname == zoneName || strings.HasSuffix(hostname, "."+zoneName) {
if name == zoneName || strings.HasSuffix(name, "."+zoneName) {
if suitableZoneName == "" || len(zoneName) > len(suitableZoneName) {
suitableZoneID = zoneID
suitableZoneName = zoneName
Expand Down
13 changes: 13 additions & 0 deletions provider/zonefinder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@ package provider
import (
"testing"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/external-dns/internal/testutils"
)

func TestZoneIDName(t *testing.T) {
z := ZoneIDName{}
z.Add("123456", "foo.bar")
z.Add("123456", "qux.baz")
z.Add("654321", "foo.qux.baz")
z.Add("987654", "エイミー.みんな")

assert.Equal(t, ZoneIDName{
"123456": "qux.baz",
"654321": "foo.qux.baz",
"987654": "エイミー.みんな",
}, z)

// simple entry in a domain
Expand Down Expand Up @@ -62,4 +66,13 @@ func TestZoneIDName(t *testing.T) {
zoneID, zoneName = z.FindZone("foo.qux.baz")
assert.Equal(t, "foo.qux.baz", zoneName)
assert.Equal(t, "654321", zoneID)

// entry gets normalized before finding
zoneID, zoneName = z.FindZone("xn--eckh0ome.xn--q9jyb4c")
assert.Equal(t, "エイミー.みんな", zoneName)
assert.Equal(t, "987654", zoneID)

b := testutils.LogsToBuffer(log.WarnLevel, t)
zoneID, zoneName = z.FindZone("???")
assert.Contains(t, b.String(), "level=warning msg=\"Failed to convert hostname '???' to its Unicode form: idna: disallowed rune U+003F\"")
}
Loading