A GET of the DAL answers sealed ({"content", "iv"}), like a PUT does.
_read_dal_or_page parsed the envelope as it came, so hosts, the ARP
table, port forwards and the Wi-Fi getters found nothing on every
gateway, and since nothing raised the HTML fallback never ran either.
Every DAL GET now goes through one helper that decrypts the answer,
checks for ZCFG_SUCCESS and returns its Object list; the status page
and the System page share the decryption. Hosts are read from
lanhosts and port forwards from nat, the names the gateway's web
interface uses: LAN_HOSTS and NAT_PORTMAP are answered like any
unknown name, with an empty Object. The new parsers live in
napalm_zyxel/parsers.py, under strict mypy.
Tests seal recorded, anonymised answers of a VMG4005-B50A
(V5.17(ABQA.3.1)C0) with a real ZyxelCrypto, so the driver decrypts
them. The Wi-Fi DAL names stay unverified: that gateway has no Wi-Fi.
Closes #25
83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
"""A logged-in driver whose gateway answers GETs from recorded fixtures, sealed.
|
|
|
|
The fixtures in ``fixtures/`` are a VMG4005-B50A's (firmware V5.17(ABQA.3.1)C0)
|
|
answers, anonymised: addresses under 192.0.2.0/24, MAC addresses replaced by
|
|
placeholders, the serial number replaced, the provisioning key and PLOAM password
|
|
emptied. The gateway seals a DAL answer as ``{"content", "iv"}``; :func:`driver`
|
|
seals each fixture with a real :class:`ZyxelCrypto` and the driver's own key, so
|
|
what the driver reads has gone through the decryption.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Any
|
|
from unittest.mock import MagicMock
|
|
|
|
import requests
|
|
|
|
from napalm_zyxel import ZyxelVMGDriver
|
|
from napalm_zyxel.zyxel_driver import ZyxelCrypto
|
|
|
|
FIXTURES = Path(__file__).parent / "fixtures"
|
|
FIRMWARE = "vmg4005-b50a-ABQA.3.1"
|
|
AES_KEY = bytes(range(32))
|
|
|
|
|
|
def fixture(name: str) -> Any:
|
|
"""The recorded answer *name* (``cardpage_status``, ``lanhosts``, ...)."""
|
|
return json.loads((FIXTURES / f"{FIRMWARE}-{name}.json").read_text())
|
|
|
|
|
|
def dal(oid: str) -> str:
|
|
"""The path a DAL GET of *oid* is sent to."""
|
|
return f"cgi-bin/DAL?oid={oid}"
|
|
|
|
|
|
def response(status: int, text: str = "") -> requests.Response:
|
|
resp = requests.Response()
|
|
resp.status_code = status
|
|
resp._content = text.encode()
|
|
resp.url = "http://192.0.2.1/"
|
|
return resp
|
|
|
|
|
|
def _crypto() -> ZyxelCrypto:
|
|
crypto = ZyxelCrypto()
|
|
crypto.aes_key = AES_KEY
|
|
return crypto
|
|
|
|
|
|
def sealed(answer: Any) -> requests.Response:
|
|
"""*answer* the way the gateway sends it: AES-encrypted, ``{"content", "iv"}``."""
|
|
envelope = _crypto().encrypt_json(answer)
|
|
return response(200, json.dumps({"content": envelope["content"], "iv": envelope["iv"]}))
|
|
|
|
|
|
def driver(answers: dict[str, Any]) -> ZyxelVMGDriver:
|
|
"""A logged-in driver whose gateway answers a GET of each path in *answers*:
|
|
a response as it is, anything else sealed. Any other path answers 404."""
|
|
drv = ZyxelVMGDriver("192.0.2.1", "admin", "password")
|
|
# NAPALM's __del__ asks is_alive(), which opens a TCP connection: not in a test.
|
|
drv.is_alive = lambda: {"is_alive": False} # type: ignore[method-assign]
|
|
|
|
def get(url: str, params: dict[str, Any] | None = None, **_: Any) -> requests.Response:
|
|
for path, answer in answers.items():
|
|
if url.endswith(f"/{path}"):
|
|
return answer if isinstance(answer, requests.Response) else sealed(answer)
|
|
return response(404)
|
|
|
|
session = MagicMock(spec=requests.Session)
|
|
session.get.side_effect = get
|
|
drv._session = session
|
|
drv._sessionkey = "1"
|
|
drv._crypto = _crypto()
|
|
drv._device_info = fixture("getBasicInformation")
|
|
return drv
|
|
|
|
|
|
def requested(drv: ZyxelVMGDriver) -> list[str]:
|
|
"""The URLs the driver sent a GET to, in order."""
|
|
return [call.args[0] for call in drv._session.get.call_args_list] # type: ignore[union-attr]
|