diff --git a/octodns/zone.py b/octodns/zone.py index f0df959..ff8252c 100644 --- a/octodns/zone.py +++ b/octodns/zone.py @@ -41,7 +41,8 @@ class Zone(object): self._root_ns = None # optional leading . to match empty hostname # optional trailing . b/c some sources don't have it on their fqdn - self._name_re = re.compile(fr'\.?{name}?$') + self._utf8_name_re = re.compile(fr'\.?{idna_decode(name)}?$') + self._idna_name_re = re.compile(fr'\.?{self.name}?$') # Copy-on-write semantics support, when `not None` this property will # point to a location with records for this `Zone`. Once `hydrated` @@ -63,7 +64,13 @@ class Zone(object): return self._root_ns def hostname_from_fqdn(self, fqdn): - return self._name_re.sub('', fqdn) + try: + fqdn.encode('ascii') + # it's non-idna or idna encoded + return self._idna_name_re.sub('', idna_encode(fqdn)) + except UnicodeEncodeError: + # it has utf8 chars + return self._utf8_name_re.sub('', fqdn) def add_record(self, record, replace=False, lenient=False): if self._origin: diff --git a/tests/test_octodns_zone.py b/tests/test_octodns_zone.py index 708862f..86fd689 100644 --- a/tests/test_octodns_zone.py +++ b/tests/test_octodns_zone.py @@ -47,10 +47,16 @@ class TestZone(TestCase): ('foo.bar', 'foo.bar.unit.tests'), ('foo.unit.tests', 'foo.unit.tests.unit.tests.'), ('foo.unit.tests', 'foo.unit.tests.unit.tests'), + # if we pass utf8 we get utf8 ('déjà', 'déjà.unit.tests'), ('déjà.foo', 'déjà.foo.unit.tests'), ('bar.déjà', 'bar.déjà.unit.tests'), ('bar.déjà.foo', 'bar.déjà.foo.unit.tests'), + # if we pass idna we get idna + ('xn--dj-kia8a', 'xn--dj-kia8a.unit.tests'), + ('xn--dj-kia8a.foo', 'xn--dj-kia8a.foo.unit.tests'), + ('bar.xn--dj-kia8a', 'bar.xn--dj-kia8a.unit.tests'), + ('bar.xn--dj-kia8a.foo', 'bar.xn--dj-kia8a.foo.unit.tests'), ): self.assertEqual(hostname, zone.hostname_from_fqdn(fqdn)) @@ -68,6 +74,10 @@ class TestZone(TestCase): ('déjà.foo', 'déjà.foo.grüßen.de'), ('bar.déjà', 'bar.déjà.grüßen.de'), ('bar.déjà.foo', 'bar.déjà.foo.grüßen.de'), + ('xn--dj-kia8a', 'xn--dj-kia8a.xn--gren-wna7o.de'), + ('xn--dj-kia8a.foo', 'xn--dj-kia8a.foo.xn--gren-wna7o.de'), + ('bar.xn--dj-kia8a', 'bar.xn--dj-kia8a.xn--gren-wna7o.de'), + ('bar.xn--dj-kia8a.foo', 'bar.xn--dj-kia8a.foo.xn--gren-wna7o.de'), ): self.assertEqual(hostname, zone.hostname_from_fqdn(fqdn))