From 8b16003cabe02ed137879f902b0a9284028f19a9 Mon Sep 17 00:00:00 2001 From: twangboy Date: Mon, 26 Aug 2024 15:05:12 -0600 Subject: [PATCH] Fix parsing of IPv4 mapped IPv6 addresses --- changelog/66837.fixed.md | 3 +++ salt/utils/network.py | 8 +++++++- tests/pytests/unit/utils/test_network.py | 12 +++++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 changelog/66837.fixed.md diff --git a/changelog/66837.fixed.md b/changelog/66837.fixed.md new file mode 100644 index 00000000000..ccbe4a1155f --- /dev/null +++ b/changelog/66837.fixed.md @@ -0,0 +1,3 @@ +Issue 66837: Fixes an issue with the `network.local_port_tcp` function +where it was not parsing the IPv4 mapped IPv6 address correctly. The +``::ffff:`` is now removed and only the IP address is returned. diff --git a/salt/utils/network.py b/salt/utils/network.py index f964e25fa54..bfa38e4fe9f 100644 --- a/salt/utils/network.py +++ b/salt/utils/network.py @@ -1740,7 +1740,13 @@ def _netlink_tool_remote_on(port, which_end): continue if which_end == "local_port" and int(local_port) != int(port): continue - remotes.add(remote_host.strip("[]")) + + # Interpret IPv4-mapped IPv6 addresses as IPv4 (strip prefix) + remote_host = remote_host.strip("[]").lower() + if remote_host.startswith("::ffff:"): + remote_host = remote_host[7:] + + remotes.add(remote_host) if valid is False: remotes = None diff --git a/tests/pytests/unit/utils/test_network.py b/tests/pytests/unit/utils/test_network.py index 12d545b0154..cbc7fd3f14a 100644 --- a/tests/pytests/unit/utils/test_network.py +++ b/tests/pytests/unit/utils/test_network.py @@ -11,7 +11,7 @@ from salt._compat import ipaddress from tests.support.mock import MagicMock, create_autospec, mock_open, patch pytestmark = [ - pytest.mark.skip_on_windows, + pytest.mark.windows_whitelisted, ] @@ -722,13 +722,13 @@ def test_netlink_tool_remote_on_a(): with patch("salt.utils.platform.is_linux", return_value=True): with patch("subprocess.check_output", return_value=LINUX_NETLINK_SS_OUTPUT): remotes = network._netlink_tool_remote_on("4506", "local_port") - assert remotes == {"192.168.122.177", "::ffff:127.0.0.1"} + assert remotes == {"192.168.122.177", "127.0.0.1"} def test_netlink_tool_remote_on_b(): with patch("subprocess.check_output", return_value=LINUX_NETLINK_SS_OUTPUT): remotes = network._netlink_tool_remote_on("4505", "remote_port") - assert remotes == {"127.0.0.1", "::ffff:1.2.3.4"} + assert remotes == {"127.0.0.1", "1.2.3.4"} def test_openbsd_remotes_on(): @@ -1430,6 +1430,7 @@ def test_isportopen_false(): assert ret is False +@pytest.mark.skip_on_windows(reason="Do not run on Windows") def test_isportopen(): ret = network.isportopen("127.0.0.1", "22") assert ret == 0 @@ -1445,6 +1446,7 @@ def test_get_socket(): assert ret.type == socket.SOCK_STREAM +@pytest.mark.skip_on_windows(reason="Do not run on Windows") def test_ip_to_host(grains): ret = network.ip_to_host("127.0.0.1") if grains["oscodename"] == "Photon": @@ -1506,6 +1508,7 @@ def test_rpad_ipv4_network(addr, expected): assert network.rpad_ipv4_network(addr) == expected +@pytest.mark.skip_on_windows(reason="Do not run on Windows") def test_hw_addr(linux_interfaces_dict, freebsd_interfaces_dict): with patch( @@ -1531,6 +1534,7 @@ def test_hw_addr(linux_interfaces_dict, freebsd_interfaces_dict): ) +@pytest.mark.skip_on_windows(reason="Do not run on Windows") def test_interface_and_ip(linux_interfaces_dict): with patch( @@ -1557,6 +1561,7 @@ def test_interface_and_ip(linux_interfaces_dict): assert ret == 'Interface "dog" not in available interfaces: "eth0", "lo"' +@pytest.mark.skip_on_windows(reason="Do not run on Windows") def test_subnets(linux_interfaces_dict): with patch( @@ -1581,6 +1586,7 @@ def test_in_subnet(caplog): assert not ret +@pytest.mark.skip_on_windows(reason="Do not run on Windows") def test_ip_addrs(linux_interfaces_dict): with patch( "salt.utils.network.linux_interfaces",