| 1 | from typing import List |
| 2 | |
| 3 | from fastapi import HTTPException |
| 4 | from loguru import logger |
| 5 | |
| 6 | from app.agents.wazuh.schema.agents import WazuhAgentScaPolicyResults |
| 7 | from app.agents.wazuh.schema.agents import WazuhAgentScaPolicyResultsResponse |
| 8 | from app.agents.wazuh.schema.agents import WazuhAgentScaResponse |
| 9 | from app.agents.wazuh.schema.agents import WazuhAgentScaResults |
| 10 | from app.connectors.wazuh_manager.utils.universal import send_get_request |
| 11 | |
| 12 | |
| 13 | async def collect_agent_sca(agent_id: str): |
| 14 | """ |
| 15 | Collect agent sca from Wazuh Manager. |
| 16 | |
| 17 | Args: |
| 18 | agent_id (str): The ID of the agent. |
| 19 | |
| 20 | Returns: |
| 21 | WazuhAgentVulnerabilitiesResponse: An object containing the collected sca. |
| 22 | |
| 23 | Raises: |
| 24 | HTTPException: If there is an error collecting the sca. |
| 25 | """ |
| 26 | logger.info(f"Collecting agent {agent_id} sca from Wazuh Manager") |
| 27 | agent_sca = await send_get_request( |
| 28 | endpoint=f"/sca/{agent_id}", |
| 29 | ) |
| 30 | if agent_sca["success"] is False: |
| 31 | raise HTTPException(status_code=500, detail=agent_sca["message"]) |
| 32 | |
| 33 | processed_sca = process_agent_sca( |
| 34 | agent_sca["data"], |
| 35 | ) |
| 36 | logger.info(f"{processed_sca}") |
| 37 | return WazuhAgentScaResponse( |
| 38 | sca=processed_sca, |
| 39 | success=True, |
| 40 | message="SCA collected successfully", |
| 41 | ) |
| 42 | |
| 43 | |
| 44 | def process_agent_sca( |
| 45 | agent_sca: dict, |
| 46 | ) -> List[WazuhAgentScaResults]: |
| 47 | """ |
| 48 | Process agent sca and return a list of WazuhAgentScaResults objects. |
| 49 | |
| 50 | Args: |
| 51 | agent_sca (dict): A dictionary containing agent sca data. |
| 52 | |
| 53 | Returns: |
| 54 | List[WazuhAgentScaResults]: A list of WazuhAgentScaResults objects. |
| 55 | |
| 56 | Raises: |
| 57 | HTTPException: If there is an error processing the agent sca. |
| 58 | """ |
| 59 | try: |
| 60 | sca = agent_sca.get("data", {}).get( |
| 61 | "affected_items", |
| 62 | [], |
| 63 | ) |
| 64 | return [WazuhAgentScaResults(**sca) for sca in sca] |
| 65 | except Exception as e: |
| 66 | raise HTTPException( |
| 67 | status_code=500, |
| 68 | detail=f"Failed to process agent sca: {e}", |
| 69 | ) |
| 70 | |
| 71 | |
| 72 | ########## ! SCA POLICY RESULTS ! ######### |
| 73 | async def collect_agent_sca_policy_results(agent_id: str, policy_id: str): |
| 74 | """ |
| 75 | Collect agent sca from Wazuh Manager. |
| 76 | |
| 77 | Args: |
| 78 | agent_id (str): The ID of the agent. |
| 79 | |
| 80 | Returns: |
| 81 | WazuhAgentScaPolicyResultsResponse: An object containing the collected sca. |
| 82 | |
| 83 | Raises: |
| 84 | HTTPException: If there is an error collecting the sca. |
| 85 | """ |
| 86 | logger.info(f"Collecting agent {agent_id} sca from Wazuh Manager") |
| 87 | agent_sca_policy_results = await send_get_request( |
| 88 | endpoint=f"/sca/{agent_id}/checks/{policy_id}", |
| 89 | ) |
| 90 | if agent_sca_policy_results["success"] is False: |
| 91 | raise HTTPException(status_code=500, detail=agent_sca_policy_results["message"]) |
| 92 | |
| 93 | processed_sca_policy_results = process_agent_sca_policy_results( |
| 94 | agent_sca_policy_results["data"], |
| 95 | ) |
| 96 | logger.info(f"{processed_sca_policy_results}") |
| 97 | return WazuhAgentScaPolicyResultsResponse( |
| 98 | sca_policy_results=processed_sca_policy_results, |
| 99 | success=True, |
| 100 | message="SCA Policy results collected successfully", |
| 101 | ) |
| 102 | |
| 103 | |
| 104 | def process_agent_sca_policy_results( |
| 105 | agent_sca: dict, |
| 106 | ) -> List[WazuhAgentScaPolicyResults]: |
| 107 | """ |
| 108 | Process agent sca and return a list of WazuhAgentScaPolicyResults objects. |
| 109 | |
| 110 | Args: |
| 111 | agent_sca (dict): A dictionary containing agent sca data. |
| 112 | |
| 113 | Returns: |
| 114 | List[WazuhAgentScaPolicyResults]: A list of WazuhAgentScaPolicyResults objects. |
| 115 | |
| 116 | Raises: |
| 117 | HTTPException: If there is an error processing the agent sca. |
| 118 | """ |
| 119 | try: |
| 120 | sca = agent_sca.get("data", {}).get( |
| 121 | "affected_items", |
| 122 | [], |
| 123 | ) |
| 124 | return [WazuhAgentScaPolicyResults(**sca) for sca in sca] |
| 125 | except Exception as e: |
| 126 | raise HTTPException( |
| 127 | status_code=500, |
| 128 | detail=f"Failed to process agent sca: {e}", |
| 129 | ) |