No files matched your search
@@ -13,6 +13,9 @@ All notable changes to this driver. The format follows
|
|||||||
3.12 venv no longer has.
|
3.12 venv no longer has.
|
||||||
|
|
||||||
### Fixed
|
### 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
|
- `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`,
|
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
|
`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 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.ha import config_sync, dhcp_ha
|
||||||
from napalm_opnsense.listening import parse_socket_statistics, wireguard_listen_ports
|
from napalm_opnsense.listening import parse_socket_statistics, wireguard_listen_ports
|
||||||
from napalm_opnsense.ospf_mixin import OPNsenseOspfMixin
|
from napalm_opnsense.ospf_mixin import OPNsenseOspfMixin
|
||||||
@@ -190,9 +191,8 @@ class OPNsenseDriver(
|
|||||||
if self.session is None:
|
if self.session is None:
|
||||||
return {"is_alive": False}
|
return {"is_alive": False}
|
||||||
try:
|
try:
|
||||||
host = self.hostname
|
endpoint = api_endpoint(self.base_url, self.hostname)
|
||||||
port = 443
|
with socket.create_connection(endpoint, timeout=ALIVE_TIMEOUT):
|
||||||
with socket.create_connection((host, port), timeout=5):
|
|
||||||
pass
|
pass
|
||||||
return {"is_alive": True}
|
return {"is_alive": True}
|
||||||
except OSError:
|
except OSError:
|
||||||
|
|||||||
@@ -164,6 +164,30 @@ class TestIsAlive:
|
|||||||
result = driver.is_alive()
|
result = driver.is_alive()
|
||||||
assert result == {"is_alive": False}
|
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()
|
# _get()
|
||||||
|
|||||||
Reference in new issue
Block a user