Merge "Add utility function to validate NO_PROXY"

This commit is contained in:
Jenkins 2015-12-18 13:33:03 +00:00 committed by Gerrit Code Review
commit 32646bddb6
2 changed files with 65 additions and 0 deletions

View File

@ -231,6 +231,43 @@ def is_hostname_safe(hostname):
return _is_hostname_safe_re.match(hostname) is not None
def is_valid_no_proxy(no_proxy):
"""Check no_proxy validity
Check if no_proxy value that will be written to environment variable by
ironic-python-agent is valid.
:param no_proxy: the value that requires validity check. Expected to be a
comma-separated list of host names, IP addresses and domain names
(with optional :port).
:returns: True if no_proxy is valid, False otherwise.
"""
if not isinstance(no_proxy, six.string_types):
return False
hostname_re = re.compile('(?!-)[A-Z\d-]{1,63}(?<!-)$', re.IGNORECASE)
for hostname in no_proxy.split(','):
hostname = hostname.strip().split(':')[0]
if not hostname:
continue
max_length = 253
if hostname.startswith('.'):
# It is allowed to specify a dot in the beginning of the value to
# indicate that it is a domain name, which means there will be at
# least one additional character in full hostname. *. is also
# possible but may be not supported by some clients, so is not
# considered valid here.
hostname = hostname[1:]
max_length = 251
if len(hostname) > max_length:
return False
if not all(hostname_re.match(part) for part in hostname.split('.')):
return False
return True
def validate_and_normalize_mac(address):
"""Validate a MAC address and return normalized form.

View File

@ -443,6 +443,34 @@ class GenericUtilsTestCase(base.TestCase):
utils.unix_file_modification_datetime('foo'))
mtime_mock.assert_called_once_with('foo')
def test_is_valid_no_proxy(self):
proxy0 = 'a' * 63 + '.' + '0' * 63 + '.c.' + 'd' * 61 + '.' + 'e' * 61
proxy1 = 'A' * 63 + '.' + '0' * 63 + '.C.' + 'D' * 61 + '.' + 'E' * 61
proxy2 = ('A' * 64 + '.' + '0' * 63 + '.C.' + 'D' * 61 + '.' +
'E' * 61) # too long (> 253)
proxy3 = 'a' * 100
proxy4 = 'a..com'
proxy5 = ('.' + 'a' * 62 + '.' + '0' * 62 + '.c.' + 'd' * 61 + '.' +
'e' * 61)
proxy6 = ('.' + 'a' * 63 + '.' + '0' * 62 + '.c.' + 'd' * 61 + '.' +
'e' * 61) # too long (> 251 after deleting .)
proxy7 = ('*.' + 'a' * 60 + '.' + '0' * 60 + '.c.' + 'd' * 61 + '.' +
'e' * 61) # starts with *.
proxy8 = 'c.-a.com'
proxy9 = 'c.a-.com'
proxy10 = ',,example.com:3128,'
valid_with_whitespaces = ' , '.join([proxy0, proxy1, proxy5, proxy10])
all_valid = ','.join([proxy0, proxy1, proxy5, proxy10])
self.assertTrue(utils.is_valid_no_proxy(all_valid))
self.assertTrue(utils.is_valid_no_proxy(valid_with_whitespaces))
self.assertFalse(utils.is_valid_no_proxy(proxy2))
self.assertFalse(utils.is_valid_no_proxy(proxy3))
self.assertFalse(utils.is_valid_no_proxy(proxy4))
self.assertFalse(utils.is_valid_no_proxy(proxy6))
self.assertFalse(utils.is_valid_no_proxy(proxy7))
self.assertFalse(utils.is_valid_no_proxy(proxy8))
self.assertFalse(utils.is_valid_no_proxy(proxy9))
class MkfsTestCase(base.TestCase):