diff options
author | Zuul <zuul@review.opendev.org> | 2020-10-22 18:16:44 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2020-10-22 18:16:44 +0000 |
commit | 8f667f8e3dc05bb5839cc4890bf2d7e36e780d7b (patch) | |
tree | ccd06a260d4e5cff2a0c20bb65b6db73f0787bc3 | |
parent | 445f1c42d90a5c36403c5420aeca797c7a666325 (diff) | |
parent | 6480356928c9ae6169ea1e5a5b5f1df3d6e0dc75 (diff) | |
download | oslo-config-8f667f8e3dc05bb5839cc4890bf2d7e36e780d7b.tar.gz |
Merge "Add a new type HostDomain."
-rw-r--r-- | oslo_config/tests/test_types.py | 24 | ||||
-rw-r--r-- | oslo_config/types.py | 47 | ||||
-rw-r--r-- | releasenotes/notes/add-HostDomain-implement-rfc1033-c985a3054f824e9d.yaml | 9 |
3 files changed, 76 insertions, 4 deletions
diff --git a/oslo_config/tests/test_types.py b/oslo_config/tests/test_types.py index 644a239..8ab4a09 100644 --- a/oslo_config/tests/test_types.py +++ b/oslo_config/tests/test_types.py @@ -766,6 +766,27 @@ class HostAddressTypeTests(TypeTestHelper, unittest.TestCase): self.assertConvertedValue('abc.0-0', 'abc.0-0') +class HostDomainTypeTests(TypeTestHelper, unittest.TestCase): + type = types.HostDomain() + + def test_invalid_host_addresses(self): + self.assertInvalid('-1') + self.assertInvalid('3.14') + self.assertInvalid('10.0') + self.assertInvalid('host..name') + self.assertInvalid('org.10') + self.assertInvalid('0.0.00') + + def test_valid_host_addresses(self): + self.assertConvertedValue('_foo', '_foo') + self.assertConvertedValue('host_name', 'host_name') + self.assertConvertedValue( + 'overcloud-novacompute_edge1-0.internalapi.localdomain', + 'overcloud-novacompute_edge1-0.internalapi.localdomain') + self.assertConvertedValue('host_01.co.uk', 'host_01.co.uk') + self.assertConvertedValue('_site01001', '_site01001') + + class HostnameTypeTests(TypeTestHelper, unittest.TestCase): type = types.Hostname() @@ -796,8 +817,8 @@ class HostnameTypeTests(TypeTestHelper, unittest.TestCase): self.assertInvalid("h'ost'") self.assertInvalid("h'ost") self.assertInvalid("h$ost") - self.assertInvalid("h%ost") self.assertInvalid("host_01.co.uk") + self.assertInvalid("h%ost") self.assertInvalid("host;name=99") self.assertInvalid('___site0.1001') self.assertInvalid('_site01001') @@ -808,6 +829,7 @@ class HostnameTypeTests(TypeTestHelper, unittest.TestCase): def test_invalid_hostnames_with_numeric_characters(self): self.assertInvalid("10.0.0.0") self.assertInvalid("3.14") + self.assertInvalid('___site0.1001') self.assertInvalid("org.10") self.assertInvalid('0.0.00') diff --git a/oslo_config/types.py b/oslo_config/types.py index dfd89bc..a1f75c9 100644 --- a/oslo_config/types.py +++ b/oslo_config/types.py @@ -761,11 +761,12 @@ class Hostname(ConfigType): :param type_name: Type name to be used in the sample config file. """ + HOSTNAME_REGEX = '(?!-)[A-Z0-9-]{1,63}(?<!-)$' def __init__(self, type_name='hostname value'): super(Hostname, self).__init__(type_name=type_name) - def __call__(self, value): + def __call__(self, value, regex=HOSTNAME_REGEX): """Check hostname is valid. Ensures that each segment @@ -789,7 +790,7 @@ class Hostname(ConfigType): % value) if value.endswith("."): value = value[:-1] - allowed = re.compile("(?!-)[A-Z0-9-]{1,63}(?<!-)$", re.IGNORECASE) + allowed = re.compile(regex, re.IGNORECASE) if not re.search('[a-zA-Z-]', value.split(".")[-1]): raise ValueError('%s contains no non-numeric characters in the ' 'top-level domain part of the host name and is ' @@ -844,7 +845,8 @@ class HostAddress(ConfigType): try: value = self.hostname(value) except ValueError: - raise ValueError("%s is not a valid host address" % (value,)) + raise ValueError( + "%s is not a valid host address" % (value,)) return value def __repr__(self): @@ -857,6 +859,45 @@ class HostAddress(ConfigType): return value +class HostDomain(HostAddress): + """Host Domain type. + + Like HostAddress with the support of _ character. + + :param version: defines which version should be explicitly + checked (4 or 6) in case of an IP address + :param type_name: Type name to be used in the sample config file. + """ + # DOMAIN_REGEX is HOSTNAME_REGEX with the _ character added + DOMAIN_REGEX = '(?!-)[A-Z0-9-_]{1,63}(?<!-)$' + + def __call__(self, value): + """Checks if is a valid IP/hostname. + + If not a valid IP, makes sure it is not a mistyped IP before + performing checks for it as a hostname. + + """ + + try: + value = super(HostDomain, self).__call__(value) + except ValueError: + # Check if domain is valid + # Add support of underscore + # https://www.ietf.org/rfc/rfc1912, + # http://domainkeys.sourceforge.net/underscore.html + # https://bugs.launchpad.net/oslo.config/+bug/1892044 + try: + value = self.hostname(value, regex=self.DOMAIN_REGEX) + except ValueError: + raise ValueError( + "%s is not a valid host address" % (value,)) + return value + + def __repr__(self): + return 'HostDomain' + + class URI(ConfigType): """URI type diff --git a/releasenotes/notes/add-HostDomain-implement-rfc1033-c985a3054f824e9d.yaml b/releasenotes/notes/add-HostDomain-implement-rfc1033-c985a3054f824e9d.yaml new file mode 100644 index 0000000..1f8d153 --- /dev/null +++ b/releasenotes/notes/add-HostDomain-implement-rfc1033-c985a3054f824e9d.yaml @@ -0,0 +1,9 @@ +--- +features: + - | + Add ``HostDomain`` to handle address with underscore. Underscore are + allowed in domain by RFC1033 [1][2][3]. + + - [1] https://www.ietf.org/rfc/rfc1912.txt + - [2] https://www.ietf.org/rfc/rfc1033.txt + - [3] http://domainkeys.sourceforge.net/underscore.html |