Vuln sync manual run (#374)
* Normalize customer code to lowercase in vulnerability index filtering * Add background task support for syncing agent vulnerabilities * Remove logging of customer vulnerabilities indices in sync_agent_vulnerabilities function
taylor_socfortress committed
Dec 13, 2024 at 12:06 UTC
d8ed295fa1c839b641cf78470824a5eeae4a6904
2 files changed
+6
-5
backend/app/agents/routes/agents.py
+4
-3
@@ -5,7 +5,7 @@ import io
5
# from fastapi import BackgroundTasks
6
from fastapi import APIRouter
7
from fastapi import Depends
8
-from fastapi import HTTPException
8
+from fastapi import HTTPException, BackgroundTasks
9
from fastapi import Path
10
from fastapi import Security
11
from fastapi.responses import StreamingResponse
@@ -828,6 +828,7 @@ async def sync_vulnerabilities_route(
828
)
829
async def sync_vulnerabilities_customer_code_route(
830
customer_code: str,
831
+ background_tasks: BackgroundTasks,
832
session: AsyncSession = Depends(get_db),
833
):
834
logger.info("Syncing agent vulnerabilities")
@@ -836,8 +837,8 @@ async def sync_vulnerabilities_customer_code_route(
837
if agent.customer_code is None:
838
logger.info(f"Skipping agent {agent.hostname} due to missing customer code")
839
continue
839
- await sync_agent_vulnerabilities(agent.hostname, customer_code)
840
- return {"success": True, "message": "Agent vulnerabilities synced successfully"}
840
+ background_tasks.add_task(sync_agent_vulnerabilities, agent.hostname, customer_code)
841
+ return {"success": True, "message": "Agent vulnerabilities sync initiated successfully"}
842
843
844
# ! TODO: CURRENTLY UPDATES IN THE DB BUT NEED TO UPDATE IN WAZUH # !
backend/app/agents/wazuh/services/vulnerabilities.py
+2
-2
@@ -119,7 +119,8 @@ def filter_vulnerabilities_indices_sync(indices_list, customer_code):
119
Filter the indices list to only include the vulnerability indices which are relevant to the customer.
120
Notice the missing `states` in the index name.
121
"""
122
- return [index for index in indices_list if index.startswith(f"wazuh-vulnerabilities-{customer_code}")]
122
+ # ! Make the customer code lowercase ! #
123
+ return [index for index in indices_list if index.startswith(f"wazuh-vulnerabilities-{customer_code.lower()}")]
124
125
126
async def collect_vulnerabilities(es, vulnerabilities_indices, agent_id, vulnerability_severity="Critical"):
@@ -308,7 +309,6 @@ async def sync_agent_vulnerabilities(agent_name: str, customer_code: str):
309
processed_vulnerabilities = process_agent_vulnerabilities_new(agent_vulnerabilities)
310
311
customer_vulnerabilities_indices = filter_vulnerabilities_indices_sync(indices.indices_list, customer_code)
311
- logger.info(f"Customer vulnerabilities indices: {customer_vulnerabilities_indices}")
312
313
if customer_vulnerabilities_indices:
314
logger.info("Customer vulnerabilities index already exists")