diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e5098c..574cb1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,16 @@ All notable changes to this driver. The format follows - `get_arp_table()` reports an unknown age as `-1.0` (napalm-device-types' `ARP_AGE_UNKNOWN`) instead of `0.0`, which means "confirmed just now": the host list says a host is active, not when the gateway last saw it (NetOrk/netork#1327). +- The HTTP fingerprint no longer counts `gs1920` and `gs2210` as evidence for this + driver: they are Zyxel switches, and discovery offered this gateway driver for + them (#4). +- Every HTTP request to the gateway carries the driver's `timeout`. It was set on + the session, where requests ignores it, so an unresponsive gateway could hang a + call indefinitely (#3). +- `ping()` no longer reports every target as alive. The DAL only acknowledges a + `PINGTEST` request and never says whether the target answered, so `ping()` now + returns an error and the driver opts out of ping sweeps (`SUPPORTS_PING = False`): + a LAN scan through this gateway marked every address alive (#2). ## [0.3.0] - 2026-10-23 diff --git a/README.md b/README.md index 918e4e8..8551658 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,6 @@ with driver("192.0.2.1", "admin", "secret", optional_args={"use_tls": True}) as | `get_port_forwards`, `get_nat_translations` | DAL `NAT_PORTMAP`, else the status page | | `get_wireless_clients`, `get_ssids`, `get_radio_status` | DAL, else the wireless page | | `get_diagnostics` | DAL `DIAGNOSTICS` | -| `ping` | DAL `PINGTEST` | | `is_alive` | TCP connect to the web port | | `reboot_host` | DAL `REBOOT` | | `restart_wifi` | DAL `WIFI_RESTART` | @@ -54,6 +53,9 @@ Configuration changes (`load_merge_candidate` and `load_replace_candidate` raise `NotImplementedError`), `get_config`, LLDP, BGP, NTP, SNMP, users, routes, MAC address table, optics, environment, traceroute, VPN tunnels and the CLI. +`ping` returns an error and `SUPPORTS_PING` is `False`: the DAL accepts a `PINGTEST` +request but does not report whether the target answered. + ## Development The rules are in [STANDARDS.md](https://git.netork.io/NAPALM/driver-standard/src/branch/main/STANDARDS.md). diff --git a/napalm_zyxel/zyxel_driver.py b/napalm_zyxel/zyxel_driver.py index cab9c88..80fb9b6 100644 --- a/napalm_zyxel/zyxel_driver.py +++ b/napalm_zyxel/zyxel_driver.py @@ -33,6 +33,8 @@ from napalm_device_types.models import ( WirelessClientDict, ) +_PING_UNSUPPORTED = "Not supported on Zyxel VMG: the DAL does not report ping replies" + class ZyxelCrypto: """AES-256-CBC encryption wrapper for Zyxel DAL protocol payloads.""" @@ -95,9 +97,10 @@ class ZyxelVMGDriver(ResidentialGatewayDriver): OUI_PREFIXES = ["00:13:49", "1C:74:0D", "C8:6C:87", "B0:B2:DC", "28:28:5D"] HTTP_FINGERPRINT = [ FingerprintRule("zyxel", weight=7.0, mandatory=True), - FingerprintRule("gs1920", weight=6.0), - FingerprintRule("gs2210", weight=6.0), ] + #: The DAL accepts a PINGTEST request but its answer carries no reply count or + #: round-trip time, so this driver cannot tell an answering host from a silent one. + SUPPORTS_PING = False _LOGIN_PATH = "UserLogin" _LOGOUT_PATH = "cgi-bin/UserLogout" @@ -137,7 +140,6 @@ class ZyxelVMGDriver(ResidentialGatewayDriver): """Open a connection and authenticate with the Zyxel device.""" self._session = requests.Session() self._session.verify = self.verify - self._session.timeout = self.timeout self._session.trust_env = False info = self._get_basic_info() @@ -166,6 +168,7 @@ class ZyxelVMGDriver(ResidentialGatewayDriver): resp = self._session.post( self._url(self._LOGIN_PATH), json=enc_payload, + timeout=self.timeout, ) resp.raise_for_status() result = crypto.decrypt_response(resp.json()) @@ -187,6 +190,7 @@ class ZyxelVMGDriver(ResidentialGatewayDriver): self._session.get( self._url(self._LOGOUT_PATH), params={"sessionKey": self._sessionkey}, + timeout=self.timeout, ) except requests.RequestException: pass @@ -208,7 +212,7 @@ class ZyxelVMGDriver(ResidentialGatewayDriver): if self._session is None: return None try: - resp = self._session.get(self._url(self._BASIC_INFO_PATH)) + resp = self._session.get(self._url(self._BASIC_INFO_PATH), timeout=self.timeout) resp.raise_for_status() return resp.json() except (requests.RequestException, json.JSONDecodeError, TypeError): @@ -218,7 +222,7 @@ class ZyxelVMGDriver(ResidentialGatewayDriver): if self._session is None: return None try: - resp = self._session.get(self._url(self._RSA_KEY_PATH)) + resp = self._session.get(self._url(self._RSA_KEY_PATH), timeout=self.timeout) resp.raise_for_status() return resp.json().get("RSAPublicKey") except (requests.RequestException, json.JSONDecodeError, TypeError): @@ -235,6 +239,7 @@ class ZyxelVMGDriver(ResidentialGatewayDriver): self._url(self._DAL_PATH), params={"oid": oid, "sessionkey": self._sessionkey}, json=enc, + timeout=self.timeout, ) resp.raise_for_status() return self._crypto.decrypt_response(resp.json()) @@ -248,6 +253,7 @@ class ZyxelVMGDriver(ResidentialGatewayDriver): resp = self._session.get( self._url(path), params={"sessionKey": self._sessionkey}, + timeout=self.timeout, ) resp.raise_for_status() return resp.text @@ -389,28 +395,12 @@ class ZyxelVMGDriver(ResidentialGatewayDriver): vrf: str = "", source_interface: str = "", ) -> dict[str, Any]: - """Ping a destination via the device's DAL diagnostics.""" - result = self._dal_query( - "PINGTEST", - { - "ProtocolVersion": "IPv4", - "Host": destination, - "DiagnosticsState": "Requested", - "DNSServer": "", - "NumberOfRepetitions": count, - "type": 0, - }, - ) - if result is None: - return {"success": {}, "error": "Ping not supported via DAL"} - return { - "success": { - "results": [{"rtt": 0}], - "probes_sent": count, - "probes_received": count, - }, - "error": "", - } + """Not supported: the DAL does not report whether a target answered. + + Returns NAPALM's ``error`` reply without contacting the gateway; see + :attr:`SUPPORTS_PING`. + """ + return {"error": _PING_UNSUPPORTED} def traceroute( self, diff --git a/tests/test_zyxel.py b/tests/test_zyxel.py index ed80de2..ce43f8d 100644 --- a/tests/test_zyxel.py +++ b/tests/test_zyxel.py @@ -421,3 +421,87 @@ class TestZyxelVMGDriver: def test_cli(self): self._setup_mocked_http() assert self.driver.cli(["show version"]) == {} + + +@pytest.mark.parametrize("switch_model", ["gs1920", "gs2210"]) +def test_a_zyxel_switch_page_adds_no_evidence_for_this_gateway(switch_model): + """GS1920 and GS2210 are Zyxel switches, not VMG gateways.""" + page = f"zyxel {switch_model}-24hp web configurator" + matching = [ + rule.pattern + for rule in ZyxelVMGDriver.HTTP_FINGERPRINT + if not rule.negative and rule.pattern != "zyxel" and rule.pattern in page + ] + assert matching == [] + + +class TestEveryRequestHasATimeout: + """requests ignores a ``timeout`` attribute on a Session; it must go with each call.""" + + def _driver_with_session(self): + driver = ZyxelVMGDriver("192.168.1.1", "admin", "password", timeout=7) + session = MagicMock(spec=requests.Session) + session.get.return_value = MockResponse(MOCK_BASIC_INFO) + session.post.return_value = MockResponse(MOCK_LOGIN_RESPONSE) + session.put.return_value = MockResponse({}) + return driver, session + + @staticmethod + def _timeouts(session): + calls = session.get.call_args_list + session.post.call_args_list + calls += session.put.call_args_list + assert calls + return {call.kwargs.get("timeout") for call in calls} + + def test_open_login_and_logout(self): + driver, session = self._driver_with_session() + crypto = MagicMock() + crypto.encrypt_json.return_value = {"content": "ENC", "iv": "IV", "key": ""} + crypto.decrypt_response.return_value = {"result": "ZCFG_SUCCESS", "sessionkey": "1"} + session.get.side_effect = lambda url, **kw: MockResponse( + {"RSAPublicKey": MOCK_RSA_KEY} if "getRSAPublickKey" in url else MOCK_BASIC_INFO + ) + with ( + patch("napalm_zyxel.zyxel_driver.ZyxelCrypto", return_value=crypto), + patch("napalm_zyxel.zyxel_driver.requests.Session", return_value=session), + ): + driver.open() + driver.close() + assert self._timeouts(session) == {7} + + def test_dal_query_and_page_fetch(self): + driver, session = self._driver_with_session() + driver._session = session + driver._sessionkey = "1" + driver._crypto = MagicMock() + driver._dal_query("LAN_HOSTS") + driver._fetch_page("Status.htm") + assert self._timeouts(session) == {7} + + +class TestPingReportsNoTargetAlive: + """The DAL PINGTEST call only acknowledges the request; it carries no reply count.""" + + def _connected_driver(self): + driver = ZyxelVMGDriver("192.168.1.1", "admin", "password") + driver._session = MagicMock(spec=requests.Session) + driver._session.put.return_value = MockResponse({}) + driver._sessionkey = "1" + driver._crypto = MagicMock() + driver._crypto.decrypt_response.return_value = {"result": "ZCFG_SUCCESS"} + return driver + + def test_the_driver_opts_out_of_ping_sweeps(self): + assert ZyxelVMGDriver.supports_ping() is False + + def test_an_accepted_request_is_not_a_reply(self): + driver = self._connected_driver() + reply = driver.ping("192.0.2.77") + assert "success" not in reply + assert reply["error"] + assert driver._parse_ping_reply("192.0.2.77", reply)["alive"] is False + + def test_ping_starts_no_diagnostic_on_the_gateway(self): + driver = self._connected_driver() + driver.ping("192.0.2.77") + driver._session.put.assert_not_called()