Merge pull request #54823 from dhiltonp/maybe-bracket

ip_bracket can now accept ipv6 addresses with brackets
This commit is contained in:
Daniel Wozniak 2019-09-30 18:13:33 -07:00 committed by GitHub
commit 93b1c4d24b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View file

@ -80,8 +80,11 @@ def check_ipc_path_max_len(uri):
def ip_bracket(addr):
'''
Convert IP address representation to ZMQ (URL) format. ZMQ expects
brackets around IPv6 literals, since they are used in URLs.
Ensure IP addresses are URI-compatible - specifically, add brackets
around IPv6 literals if they are not already present.
'''
addr = str(addr)
addr = addr.lstrip('[')
addr = addr.rstrip(']')
addr = ipaddress.ip_address(addr)
return ('[{}]' if addr.version == 6 else '{}').format(addr)

View file

@ -6,6 +6,7 @@ Test salt.utils.zeromq
# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
import zmq
from salt._compat import ipaddress
# Import Salt Testing libs
from tests.support.unit import TestCase, skipIf
@ -24,8 +25,13 @@ class UtilsTestCase(TestCase):
def test_ip_bracket(self):
test_ipv4 = '127.0.0.1'
test_ipv6 = '::1'
test_ipv6_uri = '[::1]'
self.assertEqual(test_ipv4, salt.utils.zeromq.ip_bracket(test_ipv4))
self.assertEqual('[{0}]'.format(test_ipv6), salt.utils.zeromq.ip_bracket(test_ipv6))
self.assertEqual('[{0}]'.format(test_ipv6), salt.utils.zeromq.ip_bracket(test_ipv6_uri))
ip_addr_obj = ipaddress.ip_address(test_ipv4)
self.assertEqual(test_ipv4, salt.utils.zeromq.ip_bracket(ip_addr_obj))
@skipIf(NO_MOCK, NO_MOCK_REASON)
@skipIf(not hasattr(zmq, 'IPC_PATH_MAX_LEN'), "ZMQ does not have max length support.")