From 4e377923db9fb2946362a65b62bfbe242c02d1fb Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 21:42:56 +0100 Subject: [PATCH] fix(connection): probe the API port from base_url in is_alive is_alive always opened port 443 on the hostname, so a firewall whose API is reached through base_url on another port (https://fw:8443) read as dead. The probe now takes host and port from base_url, falling back to the scheme's default port. Closes #15 --- CHANGELOG.md | 3 +++ napalm_opnsense/api_endpoint.py | 20 ++++++++++++++++++++ napalm_opnsense/opnsense.py | 6 +++--- tests/unit/test_driver.py | 24 ++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 napalm_opnsense/api_endpoint.py diff --git a/CHANGELOG.md b/CHANGELOG.md index aaa2e1f..0ffc10a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ All notable changes to this driver. The format follows 3.12 venv no longer has. ### Fixed +- `is_alive()` checks the host and port named in `base_url` (`https://fw:8443`) instead + of always port 443 on the hostname, so a firewall whose API listens elsewhere no + longer reads as dead (#15). - `configure_snmp_agent()` works on OPNsense 26.x again. The configure step sent fields the os-net-snmp model does not have (`contact`, `location`, `sysobjid`, `bindip`) and `listen` in the shape `get` returns rather than as a string, which diff --git a/napalm_opnsense/api_endpoint.py b/napalm_opnsense/api_endpoint.py new file mode 100644 index 0000000..1904a9a --- /dev/null +++ b/napalm_opnsense/api_endpoint.py @@ -0,0 +1,20 @@ +"""Where the OPNsense REST API listens, read from the driver's ``base_url``.""" + +from __future__ import annotations + +from urllib.parse import urlsplit + +#: The port the API listens on when ``base_url`` names none, per scheme. +DEFAULT_PORTS = {"https": 443, "http": 80} +#: Seconds the driver's reachability probe waits for the API port to accept. +ALIVE_TIMEOUT = 5 + + +def api_endpoint(base_url: str, fallback_host: str) -> tuple[str, int]: + """``https://fw:8443`` -> ``("fw", 8443)``: the host and port of the REST API. + + Without a port the scheme's default applies; without a host, *fallback_host*. + """ + parts = urlsplit(base_url) + port = parts.port or DEFAULT_PORTS.get(parts.scheme.lower(), DEFAULT_PORTS["https"]) + return parts.hostname or fallback_host, port diff --git a/napalm_opnsense/opnsense.py b/napalm_opnsense/opnsense.py index 0ee1c94..6efb55d 100644 --- a/napalm_opnsense/opnsense.py +++ b/napalm_opnsense/opnsense.py @@ -62,6 +62,7 @@ from napalm_device_types.models import ( ) from requests.exceptions import RequestException +from napalm_opnsense.api_endpoint import ALIVE_TIMEOUT, api_endpoint from napalm_opnsense.ha import config_sync, dhcp_ha from napalm_opnsense.listening import parse_socket_statistics, wireguard_listen_ports from napalm_opnsense.ospf_mixin import OPNsenseOspfMixin @@ -190,9 +191,8 @@ class OPNsenseDriver( if self.session is None: return {"is_alive": False} try: - host = self.hostname - port = 443 - with socket.create_connection((host, port), timeout=5): + endpoint = api_endpoint(self.base_url, self.hostname) + with socket.create_connection(endpoint, timeout=ALIVE_TIMEOUT): pass return {"is_alive": True} except OSError: diff --git a/tests/unit/test_driver.py b/tests/unit/test_driver.py index 3bc782d..c6550f8 100644 --- a/tests/unit/test_driver.py +++ b/tests/unit/test_driver.py @@ -164,6 +164,30 @@ class TestIsAlive: result = driver.is_alive() assert result == {"is_alive": False} + @pytest.mark.parametrize( + ("base_url", "expected"), + [ + (None, ("opnsense.example.com", 443)), + ("https://fw.example.com:8443", ("fw.example.com", 8443)), + ("https://fw.example.com:8443/", ("fw.example.com", 8443)), + ("https://fw.example.com", ("fw.example.com", 443)), + ("http://fw.example.com", ("fw.example.com", 80)), + ("https://[2001:db8::1]:4443", ("2001:db8::1", 4443)), + ], + ) + def test_probes_the_host_and_port_of_base_url(self, base_url, expected): + """#15: an API on another port than 443 must not read as dead.""" + args = {"verify": False} + if base_url is not None: + args["base_url"] = base_url + drv = OPNsenseDriver("opnsense.example.com", "u", "p", optional_args=args) + drv.session = MagicMock() + with patch("napalm_opnsense.opnsense.socket.create_connection") as mock_conn: + mock_conn.return_value.__enter__ = MagicMock(return_value=None) + mock_conn.return_value.__exit__ = MagicMock(return_value=False) + assert drv.is_alive() == {"is_alive": True} + assert mock_conn.call_args.args[0] == expected + # --------------------------------------------------------------------------- # _get()