Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ip_bracket can now accept ipv6 addresses with brackets #54823

Merged
merged 1 commit into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions salt/utils/zeromq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(']')
dhiltonp marked this conversation as resolved.
Show resolved Hide resolved
addr = ipaddress.ip_address(addr)
return ('[{}]' if addr.version == 6 else '{}').format(addr)
6 changes: 6 additions & 0 deletions tests/unit/utils/test_zeromq.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# 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
Expand All @@ -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.")
Expand Down