| 1 | from fastapi import HTTPException |
| 2 | from loguru import logger |
| 3 | |
| 4 | from app.connectors.graylog.schema.events import AlertEvent |
| 5 | from app.connectors.graylog.schema.events import AlertQuery |
| 6 | from app.connectors.graylog.schema.events import Alerts |
| 7 | from app.connectors.graylog.schema.events import Context |
| 8 | from app.connectors.graylog.schema.events import EventDefinition |
| 9 | from app.connectors.graylog.schema.events import GraylogAlertsResponse |
| 10 | from app.connectors.graylog.schema.events import GraylogEventDefinitionsResponse |
| 11 | from app.connectors.graylog.schema.events import Parameters |
| 12 | from app.connectors.graylog.utils.universal import send_get_request |
| 13 | from app.connectors.graylog.utils.universal import send_post_request |
| 14 | |
| 15 | |
| 16 | async def get_event_definitions() -> GraylogEventDefinitionsResponse: |
| 17 | """Get event definitions from Graylog. |
| 18 | |
| 19 | Returns: |
| 20 | GraylogEventDefinitionsResponse: The response containing the event definitions. |
| 21 | """ |
| 22 | logger.info("Getting event definitions from Graylog") |
| 23 | event_definitions_collected = await send_get_request( |
| 24 | endpoint="/api/events/definitions", |
| 25 | ) |
| 26 | if event_definitions_collected["success"]: |
| 27 | try: |
| 28 | event_definitions_data = event_definitions_collected["data"]["event_definitions"] |
| 29 | except KeyError: |
| 30 | raise HTTPException( |
| 31 | status_code=500, |
| 32 | detail="Failed to collect event definitions key", |
| 33 | ) |
| 34 | |
| 35 | # Convert the dictionary to a list of GraylogIndexItem |
| 36 | event_definitions_list = [EventDefinition(**event_definition_data) for event_definition_data in event_definitions_data] |
| 37 | |
| 38 | return GraylogEventDefinitionsResponse( |
| 39 | event_definitions=event_definitions_list, |
| 40 | success=True, |
| 41 | message="Event definitions collected successfully", |
| 42 | ) |
| 43 | else: |
| 44 | return GraylogEventDefinitionsResponse( |
| 45 | event_definitions=[], |
| 46 | success=False, |
| 47 | message="Failed to collect event definitions", |
| 48 | ) |
| 49 | |
| 50 | |
| 51 | async def get_alerts(alert_query: AlertQuery) -> GraylogAlertsResponse: |
| 52 | """ |
| 53 | Retrieves alerts from Graylog based on the provided alert query. |
| 54 | |
| 55 | Args: |
| 56 | alert_query (AlertQuery): The query parameters for retrieving alerts. |
| 57 | |
| 58 | Returns: |
| 59 | GraylogAlertsResponse: The response containing the collected alerts. |
| 60 | |
| 61 | Raises: |
| 62 | HTTPException: If there is an error collecting the alerts. |
| 63 | """ |
| 64 | logger.info("Getting alerts from Graylog") |
| 65 | response = await send_post_request( |
| 66 | endpoint="/api/events/search", |
| 67 | data=alert_query.model_dump(), |
| 68 | ) |
| 69 | |
| 70 | if response["success"]: |
| 71 | try: |
| 72 | raw_alerts_data = response["data"] |
| 73 | except KeyError: |
| 74 | raise HTTPException(status_code=500, detail="Failed to collect data key") |
| 75 | # Convert raw event data to Event objects |
| 76 | event_objects = [AlertEvent(**event_data) for event_data in raw_alerts_data["events"]] |
| 77 | |
| 78 | # Build the Alerts object |
| 79 | alerts = Alerts( |
| 80 | context=Context(**raw_alerts_data["context"]), |
| 81 | duration=raw_alerts_data["duration"], |
| 82 | events=event_objects, |
| 83 | parameters=Parameters(**raw_alerts_data["parameters"]), |
| 84 | total_events=raw_alerts_data["total_events"], |
| 85 | used_indices=raw_alerts_data["used_indices"], |
| 86 | ) |
| 87 | |
| 88 | # Build the final GraylogAlertsResponse |
| 89 | final_response = GraylogAlertsResponse( |
| 90 | alerts=alerts, |
| 91 | message="Successfully collected alerts", |
| 92 | success=True, |
| 93 | ) |
| 94 | |
| 95 | logger.info(f"Events collected: {event_objects}") |
| 96 | return final_response |
| 97 | else: |
| 98 | return GraylogAlertsResponse( |
| 99 | alerts=Alerts(events=[]), |
| 100 | success=False, |
| 101 | message="Failed to collect alerts", |
| 102 | ) |