| 1 | from fastapi import APIRouter |
| 2 | from fastapi import Depends |
| 3 | from fastapi import Security |
| 4 | from loguru import logger |
| 5 | from sqlalchemy.ext.asyncio import AsyncSession |
| 6 | |
| 7 | from app.auth.routes.auth import AuthHandler |
| 8 | from app.db.db_session import get_db |
| 9 | from app.integrations.modules.schema.cato import CatoAuthKeys |
| 10 | from app.integrations.modules.schema.cato import CollectCato |
| 11 | from app.integrations.modules.schema.cato import InvokeCatoRequest |
| 12 | from app.integrations.modules.schema.cato import InvokeCatoResponse |
| 13 | from app.integrations.modules.services.cato import post_to_copilot_cato_module |
| 14 | from app.integrations.routes import find_customer_integration |
| 15 | from app.integrations.utils.utils import extract_auth_keys |
| 16 | from app.integrations.utils.utils import get_customer_integration_response |
| 17 | from app.utils import get_connector_attribute |
| 18 | |
| 19 | module_cato_router = APIRouter() |
| 20 | |
| 21 | |
| 22 | async def get_cato_auth_keys(customer_integration) -> CatoAuthKeys: |
| 23 | """ |
| 24 | Extract the cato authentication keys from the CustomerIntegration. |
| 25 | |
| 26 | Args: |
| 27 | customer_integration (CustomerIntegration): The CustomerIntegration containing the |
| 28 | cato authentication keys. |
| 29 | |
| 30 | Returns: |
| 31 | catoAuthKeys: The extracted cato authentication keys. |
| 32 | """ |
| 33 | cato_auth_keys = extract_auth_keys( |
| 34 | customer_integration, |
| 35 | service_name="CATO", |
| 36 | ) |
| 37 | |
| 38 | return CatoAuthKeys(**cato_auth_keys) |
| 39 | |
| 40 | |
| 41 | async def get_collect_cato_data(cato_request, session, auth_keys): |
| 42 | return CollectCato( |
| 43 | integration="cato", |
| 44 | customer_code=cato_request.customer_code, |
| 45 | graylog_host=await get_connector_attribute( |
| 46 | connector_id=10, |
| 47 | column_name="connector_url", |
| 48 | session=session, |
| 49 | ), |
| 50 | graylog_port=await get_connector_attribute( |
| 51 | connector_id=10, |
| 52 | column_name="connector_extra_data", |
| 53 | session=session, |
| 54 | ), |
| 55 | api_key=auth_keys.API_KEY, |
| 56 | account_id=int(auth_keys.ACCOUNT_ID), |
| 57 | event_types=auth_keys.EVENT_TYPES, |
| 58 | event_sub_types=auth_keys.EVENT_SUB_TYPES, |
| 59 | ) |
| 60 | |
| 61 | |
| 62 | @module_cato_router.post( |
| 63 | "", |
| 64 | response_model=InvokeCatoResponse, |
| 65 | description="Invoke the cato module.", |
| 66 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 67 | ) |
| 68 | async def collect_cato_route(cato_request: InvokeCatoRequest, session: AsyncSession = Depends(get_db)): |
| 69 | """Pull down cato Events.""" |
| 70 | logger.info(f"Collecting Cato Events for {cato_request.customer_code}") |
| 71 | try: |
| 72 | customer_integration_response = await get_customer_integration_response( |
| 73 | cato_request.customer_code, |
| 74 | session, |
| 75 | ) |
| 76 | |
| 77 | customer_integration = await find_customer_integration( |
| 78 | cato_request.customer_code, |
| 79 | cato_request.integration_name, |
| 80 | customer_integration_response, |
| 81 | ) |
| 82 | |
| 83 | auth_keys = await get_cato_auth_keys(customer_integration) |
| 84 | |
| 85 | collect_cato_data = await get_collect_cato_data(cato_request, session, auth_keys) |
| 86 | |
| 87 | await post_to_copilot_cato_module(data=collect_cato_data) |
| 88 | |
| 89 | except Exception as e: |
| 90 | logger.error(f"Error during DB session: {str(e)}") |
| 91 | return InvokeCatoResponse(success=False, message=str(e)) |
| 92 | |
| 93 | return InvokeCatoResponse(success=True, message="Cato Events collected successfully.") |