Merge pull request #51173 from dwoz/issue51160

Avoid exceptions by passing bytes to idna
This commit is contained in:
Daniel Wozniak 2019-01-15 09:32:30 -07:00 committed by GitHub
commit 58eede65a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 25 deletions

View file

@ -137,23 +137,11 @@ def _generate_minion_id():
def first(self):
return self and self[0] or None
hosts = DistinctList([])
try:
hosts.append(socket.getfqdn())
except ValueError:
pass
try:
hosts.append(platform.node())
except ValueError:
pass
try:
hosts.append(socket.gethostname())
except ValueError:
pass
hostname = socket.gethostname()
hosts = DistinctList().append(
salt.utils.stringutils.to_unicode(socket.getfqdn(salt.utils.stringutils.to_bytes(hostname)))
).append(platform.node()).append(hostname)
if not hosts:
try:
for a_nfo in socket.getaddrinfo(hosts.first() or 'localhost', None, socket.AF_INET,

View file

@ -182,15 +182,6 @@ class NetworkTestCase(TestCase):
with patch('salt.utils.files.fopen', fopen_mock):
assert 'thisismyhostname' in network._generate_minion_id()
def test_generate_minion_id_with_long_hostname(self):
'''
Test that hostnames longer than 63 characters do not raise
an exception when generating the minion ID
'''
with patch('socket.gethostbyaddr') as mock_gethostbyname:
mock_gethostbyname.side_effect = UnicodeError('encoding with \'idna\' codec failed')
self.assertTrue(network.generate_minion_id())
def test_is_ip(self):
self.assertTrue(network.is_ip('10.10.0.3'))
self.assertFalse(network.is_ip('0.9.800.1000'))
@ -573,3 +564,15 @@ class NetworkTestCase(TestCase):
self.assertRaises(ValueError, network.mac_str_to_bytes, 'a0:b0:c0:d0:e0:fg')
self.assertEqual(b'\x10\x08\x06\x04\x02\x00', network.mac_str_to_bytes('100806040200'))
self.assertEqual(b'\xf8\xe7\xd6\xc5\xb4\xa3', network.mac_str_to_bytes('f8e7d6c5b4a3'))
def test_generate_minion_id_with_long_hostname(self):
'''
Validate the fix for:
https://github.com/saltstack/salt/issues/51160
'''
long_name = 'localhost-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz'
with patch('socket.gethostname', MagicMock(return_value=long_name)):
# An exception is raised if unicode is passed to socket.getfqdn
minion_id = network.generate_minion_id()
assert minion_id != '', minion_id