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
This commit is contained in:
1 parent
e2cac52a12
commit
4e377923db
4 files changed
+50
-3
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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:
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user