暂无描述

main.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. from fastapi import FastAPI, HTTPException
  2. from app.adapters.iris import IrisAdapter
  3. from app.adapters.pagerduty import PagerDutyAdapter
  4. from app.adapters.shuffle import ShuffleAdapter
  5. from app.adapters.wazuh import WazuhAdapter
  6. from app.config import settings
  7. from app.db import init_schema
  8. from app.models import (
  9. ActionCreateIncidentRequest,
  10. ApiResponse,
  11. ShuffleLoginRequest,
  12. ShuffleProxyRequest,
  13. TriggerShuffleRequest,
  14. WazuhIngestRequest,
  15. )
  16. from app.repositories.mvp_repo import MvpRepository
  17. from app.routes.mvp import build_mvp_router
  18. from app.security import require_internal_api_key
  19. from app.services.mvp_service import MvpService
  20. app = FastAPI(title=settings.app_name, version="0.1.0")
  21. wazuh_adapter = WazuhAdapter(
  22. base_url=settings.wazuh_base_url,
  23. username=settings.wazuh_username,
  24. password=settings.wazuh_password,
  25. )
  26. shuffle_adapter = ShuffleAdapter(
  27. base_url=settings.shuffle_base_url,
  28. api_key=settings.shuffle_api_key,
  29. )
  30. pagerduty_adapter = PagerDutyAdapter(
  31. base_url=settings.pagerduty_base_url,
  32. api_key=settings.pagerduty_api_key,
  33. )
  34. iris_adapter = IrisAdapter(
  35. base_url=settings.iris_base_url,
  36. api_key=settings.iris_api_key,
  37. )
  38. repo = MvpRepository()
  39. mvp_service = MvpService(
  40. repo=repo,
  41. wazuh_adapter=wazuh_adapter,
  42. shuffle_adapter=shuffle_adapter,
  43. iris_adapter=iris_adapter,
  44. pagerduty_adapter=pagerduty_adapter,
  45. )
  46. app.include_router(build_mvp_router(mvp_service, require_internal_api_key))
  47. @app.on_event("startup")
  48. async def startup() -> None:
  49. init_schema()
  50. repo.ensure_policy()
  51. @app.get("/health", response_model=ApiResponse)
  52. async def health() -> ApiResponse:
  53. return ApiResponse(
  54. data={
  55. "service": settings.app_name,
  56. "env": settings.app_env,
  57. "targets": {
  58. "wazuh": settings.wazuh_base_url,
  59. "shuffle": settings.shuffle_base_url,
  60. "pagerduty": settings.pagerduty_base_url,
  61. "iris": settings.iris_base_url,
  62. },
  63. }
  64. )
  65. @app.post("/ingest/wazuh-alert", response_model=ApiResponse)
  66. async def ingest_wazuh_alert(payload: WazuhIngestRequest) -> ApiResponse:
  67. normalized = {
  68. "source": payload.source,
  69. "alert_id": payload.alert_id,
  70. "rule_id": payload.rule_id,
  71. "severity": payload.severity,
  72. "title": payload.title,
  73. "payload": payload.payload,
  74. }
  75. return ApiResponse(data={"normalized": normalized})
  76. @app.post("/action/create-incident", response_model=ApiResponse)
  77. async def create_incident(payload: ActionCreateIncidentRequest) -> ApiResponse:
  78. incident_payload = {
  79. "title": payload.title,
  80. "urgency": payload.severity,
  81. "incident_key": payload.dedupe_key,
  82. "body": payload.payload,
  83. "source": payload.source,
  84. }
  85. try:
  86. pd_result = await pagerduty_adapter.create_incident(incident_payload)
  87. except Exception as exc:
  88. raise HTTPException(status_code=502, detail=f"PagerDuty call failed: {exc}") from exc
  89. return ApiResponse(data={"pagerduty": pd_result})
  90. @app.post("/action/trigger-shuffle", response_model=ApiResponse)
  91. async def trigger_shuffle(payload: TriggerShuffleRequest) -> ApiResponse:
  92. try:
  93. shuffle_result = await shuffle_adapter.trigger_workflow(
  94. workflow_id=payload.workflow_id,
  95. payload=payload.execution_argument,
  96. )
  97. except Exception as exc:
  98. raise HTTPException(status_code=502, detail=f"Shuffle call failed: {exc}") from exc
  99. return ApiResponse(data={"shuffle": shuffle_result})
  100. @app.get("/shuffle/health", response_model=ApiResponse)
  101. async def shuffle_health() -> ApiResponse:
  102. try:
  103. result = await shuffle_adapter.health()
  104. except Exception as exc:
  105. raise HTTPException(status_code=502, detail=f"Shuffle call failed: {exc}") from exc
  106. return ApiResponse(data={"shuffle": result})
  107. @app.get("/shuffle/auth-test", response_model=ApiResponse)
  108. async def shuffle_auth_test() -> ApiResponse:
  109. try:
  110. result = await shuffle_adapter.auth_test()
  111. except Exception as exc:
  112. raise HTTPException(status_code=502, detail=f"Shuffle call failed: {exc}") from exc
  113. return ApiResponse(data={"shuffle": result})
  114. @app.post("/shuffle/login", response_model=ApiResponse)
  115. async def shuffle_login(payload: ShuffleLoginRequest) -> ApiResponse:
  116. try:
  117. result = await shuffle_adapter.login(payload.username, payload.password)
  118. except Exception as exc:
  119. raise HTTPException(status_code=502, detail=f"Shuffle call failed: {exc}") from exc
  120. return ApiResponse(data={"shuffle": result})
  121. @app.post("/shuffle/generate-apikey", response_model=ApiResponse)
  122. async def shuffle_generate_apikey(payload: ShuffleLoginRequest | None = None) -> ApiResponse:
  123. username = payload.username if payload else settings.shuffle_username
  124. password = payload.password if payload else settings.shuffle_password
  125. if not username or not password:
  126. raise HTTPException(
  127. status_code=400,
  128. detail="Missing shuffle credentials. Provide username/password in body or set SHUFFLE_USERNAME and SHUFFLE_PASSWORD.",
  129. )
  130. try:
  131. result = await shuffle_adapter.generate_apikey_from_login(username, password)
  132. except Exception as exc:
  133. raise HTTPException(status_code=502, detail=f"Shuffle call failed: {exc}") from exc
  134. return ApiResponse(data={"shuffle": result})
  135. @app.get("/shuffle/workflows", response_model=ApiResponse)
  136. async def shuffle_workflows() -> ApiResponse:
  137. try:
  138. result = await shuffle_adapter.list_workflows()
  139. except Exception as exc:
  140. raise HTTPException(status_code=502, detail=f"Shuffle call failed: {exc}") from exc
  141. return ApiResponse(data={"shuffle": result})
  142. @app.get("/shuffle/workflows/{workflow_id}", response_model=ApiResponse)
  143. async def shuffle_workflow(workflow_id: str) -> ApiResponse:
  144. try:
  145. result = await shuffle_adapter.get_workflow(workflow_id)
  146. except Exception as exc:
  147. raise HTTPException(status_code=502, detail=f"Shuffle call failed: {exc}") from exc
  148. return ApiResponse(data={"shuffle": result})
  149. @app.post("/shuffle/workflows/{workflow_id}/execute", response_model=ApiResponse)
  150. async def shuffle_workflow_execute(
  151. workflow_id: str, payload: dict[str, object]
  152. ) -> ApiResponse:
  153. try:
  154. result = await shuffle_adapter.trigger_workflow(workflow_id=workflow_id, payload=payload)
  155. except Exception as exc:
  156. raise HTTPException(status_code=502, detail=f"Shuffle call failed: {exc}") from exc
  157. return ApiResponse(data={"shuffle": result})
  158. @app.get("/shuffle/apps", response_model=ApiResponse)
  159. async def shuffle_apps() -> ApiResponse:
  160. try:
  161. result = await shuffle_adapter.list_apps()
  162. except Exception as exc:
  163. raise HTTPException(status_code=502, detail=f"Shuffle call failed: {exc}") from exc
  164. return ApiResponse(data={"shuffle": result})
  165. @app.post("/shuffle/proxy", response_model=ApiResponse)
  166. async def shuffle_proxy(payload: ShuffleProxyRequest) -> ApiResponse:
  167. path = payload.path if payload.path.startswith("/api/") else f"/api/v1/{payload.path.lstrip('/')}"
  168. try:
  169. result = await shuffle_adapter.proxy(
  170. method=payload.method,
  171. path=path,
  172. params=payload.params,
  173. payload=payload.payload,
  174. )
  175. except Exception as exc:
  176. raise HTTPException(status_code=502, detail=f"Shuffle call failed: {exc}") from exc
  177. return ApiResponse(data={"shuffle": result})
  178. @app.post("/action/create-iris-case", response_model=ApiResponse)
  179. async def create_iris_case(payload: ActionCreateIncidentRequest) -> ApiResponse:
  180. # IRIS v2 expects case_name, case_description, case_customer, case_soc_id.
  181. case_payload = {
  182. "case_name": payload.title,
  183. "case_description": payload.payload.get("description", "Created by soc-integrator"),
  184. "case_customer": payload.payload.get("case_customer", settings.iris_default_customer_id),
  185. "case_soc_id": payload.payload.get("case_soc_id", settings.iris_default_soc_id),
  186. }
  187. try:
  188. iris_result = await iris_adapter.create_case(case_payload)
  189. except Exception as exc:
  190. raise HTTPException(status_code=502, detail=f"IRIS call failed: {exc}") from exc
  191. return ApiResponse(data={"iris": iris_result})
  192. @app.get("/sync/wazuh-version", response_model=ApiResponse)
  193. async def sync_wazuh_version() -> ApiResponse:
  194. try:
  195. wazuh_result = await wazuh_adapter.get_version()
  196. except Exception as exc:
  197. raise HTTPException(status_code=502, detail=f"Wazuh call failed: {exc}") from exc
  198. return ApiResponse(data={"wazuh": wazuh_result})
  199. @app.get("/wazuh/auth-test", response_model=ApiResponse)
  200. async def wazuh_auth_test() -> ApiResponse:
  201. try:
  202. result = await wazuh_adapter.auth_test()
  203. except Exception as exc:
  204. raise HTTPException(status_code=502, detail=f"Wazuh auth failed: {exc}") from exc
  205. return ApiResponse(data={"wazuh": result})
  206. @app.get("/wazuh/manager-info", response_model=ApiResponse)
  207. async def wazuh_manager_info() -> ApiResponse:
  208. try:
  209. result = await wazuh_adapter.get_manager_info()
  210. except Exception as exc:
  211. raise HTTPException(status_code=502, detail=f"Wazuh call failed: {exc}") from exc
  212. return ApiResponse(data={"wazuh": result})
  213. @app.get("/wazuh/agents", response_model=ApiResponse)
  214. async def wazuh_agents(
  215. limit: int = 50,
  216. offset: int = 0,
  217. select: str | None = None,
  218. ) -> ApiResponse:
  219. try:
  220. result = await wazuh_adapter.list_agents(limit=limit, offset=offset, select=select)
  221. except Exception as exc:
  222. raise HTTPException(status_code=502, detail=f"Wazuh call failed: {exc}") from exc
  223. return ApiResponse(data={"wazuh": result})
  224. @app.get("/wazuh/alerts", response_model=ApiResponse)
  225. async def wazuh_alerts(
  226. limit: int = 50,
  227. offset: int = 0,
  228. q: str | None = None,
  229. sort: str | None = None,
  230. ) -> ApiResponse:
  231. try:
  232. # In this Wazuh build, API alerts are exposed via manager logs.
  233. result = await wazuh_adapter.list_manager_logs(
  234. limit=limit, offset=offset, q=q, sort=sort
  235. )
  236. except Exception as exc:
  237. raise HTTPException(status_code=502, detail=f"Wazuh call failed: {exc}") from exc
  238. return ApiResponse(data={"wazuh": result})
  239. @app.get("/wazuh/manager-logs", response_model=ApiResponse)
  240. async def wazuh_manager_logs(
  241. limit: int = 50,
  242. offset: int = 0,
  243. q: str | None = None,
  244. sort: str | None = None,
  245. ) -> ApiResponse:
  246. try:
  247. result = await wazuh_adapter.list_manager_logs(
  248. limit=limit, offset=offset, q=q, sort=sort
  249. )
  250. except Exception as exc:
  251. raise HTTPException(status_code=502, detail=f"Wazuh call failed: {exc}") from exc
  252. return ApiResponse(data={"wazuh": result})