fix(radius): the getters raise when the firewall does not answer (#49)
driver / standard (pull_request) Failing after 17s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m0s
driver / test (3.13) (pull_request) Successful in 1m0s
driver / test (3.12) (pull_request) Successful in 1m12s
driver / lowest (pull_request) Successful in 32s
driver / build (pull_request) Successful in 29s
driver / security (pull_request) Successful in 1m3s
CI / driver (pull_request) Failing after 2m21s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Failing after 17s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m0s
driver / test (3.13) (pull_request) Successful in 1m0s
driver / test (3.12) (pull_request) Successful in 1m12s
driver / lowest (pull_request) Successful in 32s
driver / build (pull_request) Successful in 29s
driver / security (pull_request) Successful in 1m3s
CI / driver (pull_request) Failing after 2m21s
driver / publish (pull_request) Skipped
get_radius_clients() and get_radius_users() caught every exception and returned [], so a caller could not tell no entries from no answer. netOrk asks them whether an entry whose add was cut off is on the firewall, and an empty list would have made it forget a NAS client still live there. Both now raise for a failed request or an answer without rows; an empty list is the firewall's answer, which RADIUS_READS_RAISE = True declares. A saved add whose id lookup fails still answers success, with no id. Closes #49
This commit is contained in:
1 parent
521f7c5d73
commit
9c31afd5d7
3 files changed
+72
-18
No files matched your search
+29
-16
@@ -99,6 +99,14 @@ def _opnsense_version(versions: Any) -> str:
|
||||
return ""
|
||||
|
||||
|
||||
def _search_rows(data: Any, search: str) -> list[dict[str, Any]]:
|
||||
"""The ``rows`` of a FreeRADIUS search answer; raises for an answer without them."""
|
||||
rows = data.get("rows") if isinstance(data, dict) else None
|
||||
if not isinstance(rows, list):
|
||||
raise RuntimeError(f"FreeRADIUS {search} answered without rows: {data!r:.200}")
|
||||
return [row for row in rows if isinstance(row, dict)]
|
||||
|
||||
|
||||
class OPNsenseDriver(
|
||||
OPNsenseConfigRevisionsMixin,
|
||||
OPNsenseTimeSyncMixin,
|
||||
@@ -117,6 +125,9 @@ class OPNsenseDriver(
|
||||
DRIVER_NAME = "opnsense"
|
||||
# Everything runs over the REST API; there is no SSH session to open.
|
||||
USES_SSH = False
|
||||
# get_radius_clients()/get_radius_users() raise when the firewall does not answer,
|
||||
# so an empty list is its answer (#49): a caller may take it as "none there".
|
||||
RADIUS_READS_RAISE = True
|
||||
SNMP_FINGERPRINT = [
|
||||
FingerprintRule("opnsense", weight=8.0, mandatory=True),
|
||||
]
|
||||
@@ -1206,13 +1217,11 @@ class OPNsenseDriver(
|
||||
def get_radius_clients(self) -> list[dict[str, Any]]:
|
||||
"""Return configured FreeRADIUS NAS clients.
|
||||
|
||||
Calls ``GET /api/freeradius/client/search_client``.
|
||||
Calls ``GET /api/freeradius/client/search_client``. Raises when the
|
||||
request fails or the answer holds no ``rows`` list: an empty list means
|
||||
the firewall has no clients, never that it did not answer (#49).
|
||||
"""
|
||||
try:
|
||||
data = self._get("/api/freeradius/client/search_client")
|
||||
except Exception as exc:
|
||||
logger.warning("get_radius_clients() failed: %s", exc)
|
||||
return []
|
||||
data = self._get("/api/freeradius/client/search_client")
|
||||
return [
|
||||
{
|
||||
"id": row.get("uuid", ""),
|
||||
@@ -1220,7 +1229,7 @@ class OPNsenseDriver(
|
||||
"ip": row.get("ip", ""),
|
||||
"enabled": row.get("enabled") == "1",
|
||||
}
|
||||
for row in data.get("rows", [])
|
||||
for row in _search_rows(data, "search_client")
|
||||
]
|
||||
|
||||
def create_radius_client(self, name: str, ip: str, secret: str) -> dict[str, Any]:
|
||||
@@ -1238,7 +1247,11 @@ class OPNsenseDriver(
|
||||
if result.get("result") != "saved":
|
||||
return {"success": False, "validations": result.get("validations", {})}
|
||||
self._post("/api/freeradius/service/reconfigure")
|
||||
created = next((c for c in self.get_radius_clients() if c["name"] == name), None)
|
||||
try:
|
||||
created = next((c for c in self.get_radius_clients() if c["name"] == name), None)
|
||||
except Exception as exc: # the add went through; only its id is unknown
|
||||
logger.warning("create_radius_client(): looking up the new client failed: %s", exc)
|
||||
created = None
|
||||
return {"success": True, "id": created["id"] if created else None}
|
||||
|
||||
def delete_radius_client(self, client_id: str) -> dict[str, Any]:
|
||||
@@ -1253,20 +1266,16 @@ class OPNsenseDriver(
|
||||
"""Return configured FreeRADIUS users.
|
||||
|
||||
Calls ``GET /api/freeradius/user/search_user``. Never includes the
|
||||
password field.
|
||||
password field. Raises like ``get_radius_clients()`` (#49).
|
||||
"""
|
||||
try:
|
||||
data = self._get("/api/freeradius/user/search_user")
|
||||
except Exception as exc:
|
||||
logger.warning("get_radius_users() failed: %s", exc)
|
||||
return []
|
||||
data = self._get("/api/freeradius/user/search_user")
|
||||
return [
|
||||
{
|
||||
"id": row.get("uuid", ""),
|
||||
"username": row.get("username", ""),
|
||||
"enabled": row.get("enabled") == "1",
|
||||
}
|
||||
for row in data.get("rows", [])
|
||||
for row in _search_rows(data, "search_user")
|
||||
]
|
||||
|
||||
def create_radius_user(self, username: str, password: str) -> dict[str, Any]:
|
||||
@@ -1283,7 +1292,11 @@ class OPNsenseDriver(
|
||||
if result.get("result") != "saved":
|
||||
return {"success": False, "validations": result.get("validations", {})}
|
||||
self._post("/api/freeradius/service/reconfigure")
|
||||
created = next((u for u in self.get_radius_users() if u["username"] == username), None)
|
||||
try:
|
||||
created = next((u for u in self.get_radius_users() if u["username"] == username), None)
|
||||
except Exception as exc: # the add went through; only its id is unknown
|
||||
logger.warning("create_radius_user(): looking up the new user failed: %s", exc)
|
||||
created = None
|
||||
return {"success": True, "id": created["id"] if created else None}
|
||||
|
||||
def delete_radius_user(self, user_id: str) -> dict[str, Any]:
|
||||
|
||||
Reference in new issue
Block a user