Няма описание

wazuh.py 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from typing import Any
  2. import httpx
  3. class WazuhAdapter:
  4. def __init__(self, base_url: str, username: str, password: str) -> None:
  5. self.base_url = base_url.rstrip("/")
  6. self.username = username
  7. self.password = password
  8. async def _authenticate(self, client: httpx.AsyncClient) -> str:
  9. auth_url = f"{self.base_url}/security/user/authenticate?raw=true"
  10. response = await client.post(auth_url, auth=(self.username, self.password))
  11. response.raise_for_status()
  12. token = response.text.strip()
  13. if not token:
  14. raise RuntimeError("Wazuh authentication returned an empty token.")
  15. return token
  16. async def _get_with_bearer(
  17. self,
  18. client: httpx.AsyncClient,
  19. token: str,
  20. path: str,
  21. params: dict[str, Any] | None = None,
  22. ) -> dict[str, Any]:
  23. url = f"{self.base_url}{path}"
  24. response = await client.get(
  25. url,
  26. headers={"Authorization": f"Bearer {token}"},
  27. params=params,
  28. )
  29. response.raise_for_status()
  30. return response.json() if response.content else {"status_code": response.status_code}
  31. async def auth_test(self) -> dict[str, Any]:
  32. async with httpx.AsyncClient(verify=False, timeout=15.0) as client:
  33. token = await self._authenticate(client)
  34. return {"authenticated": True, "token_prefix": token[:12]}
  35. async def get_version(self) -> dict[str, Any]:
  36. async with httpx.AsyncClient(verify=False, timeout=15.0) as client:
  37. token = await self._authenticate(client)
  38. # API root should return API metadata when authenticated.
  39. try:
  40. return await self._get_with_bearer(client, token, "/")
  41. except httpx.HTTPStatusError:
  42. # Fallback endpoint for environments restricting root access.
  43. return await self._get_with_bearer(client, token, "/manager/info")
  44. async def get_manager_info(self) -> dict[str, Any]:
  45. async with httpx.AsyncClient(verify=False, timeout=15.0) as client:
  46. token = await self._authenticate(client)
  47. return await self._get_with_bearer(client, token, "/manager/info")
  48. async def list_agents(
  49. self, limit: int = 50, offset: int = 0, select: str | None = None
  50. ) -> dict[str, Any]:
  51. params: dict[str, Any] = {"limit": limit, "offset": offset}
  52. if select:
  53. params["select"] = select
  54. async with httpx.AsyncClient(verify=False, timeout=20.0) as client:
  55. token = await self._authenticate(client)
  56. return await self._get_with_bearer(client, token, "/agents", params=params)
  57. async def list_manager_logs(
  58. self,
  59. limit: int = 50,
  60. offset: int = 0,
  61. q: str | None = None,
  62. sort: str | None = None,
  63. ) -> dict[str, Any]:
  64. params: dict[str, Any] = {"limit": limit, "offset": offset}
  65. if q:
  66. params["q"] = q
  67. if sort:
  68. params["sort"] = sort
  69. async with httpx.AsyncClient(verify=False, timeout=20.0) as client:
  70. token = await self._authenticate(client)
  71. return await self._get_with_bearer(client, token, "/manager/logs", params=params)