fix(http): pass the timeout with every request

requests ignores a timeout attribute on a Session, so open() setting
one left every call without a timeout and an unresponsive gateway
could hang a worker indefinitely. Each get/post/put now carries the
driver's timeout.

Closes #3
This commit is contained in:
Christian Manivong committed 2026-11-01 21:55:36 +01:00
1 parent d5362fd382
commit f7002114ae
3 files changed
+53 -3

No files matched your search

+3
View File
@@ -19,6 +19,9 @@ All notable changes to this driver. The format follows
- The HTTP fingerprint no longer counts `gs1920` and `gs2210` as evidence for this - 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 driver: they are Zyxel switches, and discovery offered this gateway driver for
them (#4). 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).
## [0.3.0] - 2026-10-23 ## [0.3.0] - 2026-10-23
+6 -3
View File
@@ -135,7 +135,6 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
"""Open a connection and authenticate with the Zyxel device.""" """Open a connection and authenticate with the Zyxel device."""
self._session = requests.Session() self._session = requests.Session()
self._session.verify = self.verify self._session.verify = self.verify
self._session.timeout = self.timeout
self._session.trust_env = False self._session.trust_env = False
info = self._get_basic_info() info = self._get_basic_info()
@@ -164,6 +163,7 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
resp = self._session.post( resp = self._session.post(
self._url(self._LOGIN_PATH), self._url(self._LOGIN_PATH),
json=enc_payload, json=enc_payload,
timeout=self.timeout,
) )
resp.raise_for_status() resp.raise_for_status()
result = crypto.decrypt_response(resp.json()) result = crypto.decrypt_response(resp.json())
@@ -185,6 +185,7 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
self._session.get( self._session.get(
self._url(self._LOGOUT_PATH), self._url(self._LOGOUT_PATH),
params={"sessionKey": self._sessionkey}, params={"sessionKey": self._sessionkey},
timeout=self.timeout,
) )
except requests.RequestException: except requests.RequestException:
pass pass
@@ -206,7 +207,7 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
if self._session is None: if self._session is None:
return None return None
try: 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() resp.raise_for_status()
return resp.json() return resp.json()
except (requests.RequestException, json.JSONDecodeError, TypeError): except (requests.RequestException, json.JSONDecodeError, TypeError):
@@ -216,7 +217,7 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
if self._session is None: if self._session is None:
return None return None
try: 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() resp.raise_for_status()
return resp.json().get("RSAPublicKey") return resp.json().get("RSAPublicKey")
except (requests.RequestException, json.JSONDecodeError, TypeError): except (requests.RequestException, json.JSONDecodeError, TypeError):
@@ -233,6 +234,7 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
self._url(self._DAL_PATH), self._url(self._DAL_PATH),
params={"oid": oid, "sessionkey": self._sessionkey}, params={"oid": oid, "sessionkey": self._sessionkey},
json=enc, json=enc,
timeout=self.timeout,
) )
resp.raise_for_status() resp.raise_for_status()
return self._crypto.decrypt_response(resp.json()) return self._crypto.decrypt_response(resp.json())
@@ -246,6 +248,7 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
resp = self._session.get( resp = self._session.get(
self._url(path), self._url(path),
params={"sessionKey": self._sessionkey}, params={"sessionKey": self._sessionkey},
timeout=self.timeout,
) )
resp.raise_for_status() resp.raise_for_status()
return resp.text return resp.text
+44
View File
@@ -433,3 +433,47 @@ def test_a_zyxel_switch_page_adds_no_evidence_for_this_gateway(switch_model):
if not rule.negative and rule.pattern != "zyxel" and rule.pattern in page if not rule.negative and rule.pattern != "zyxel" and rule.pattern in page
] ]
assert matching == [] 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}