| 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.darktrace import CollectDarktrace |
| 10 | from app.integrations.modules.schema.darktrace import DarktraceAuthKeys |
| 11 | from app.integrations.modules.schema.darktrace import InvokeDarktraceRequest |
| 12 | from app.integrations.modules.schema.darktrace import InvokeDarktraceResponse |
| 13 | from app.integrations.modules.services.darktrace import post_to_copilot_darktrace_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_darktrace_router = APIRouter() |
| 20 | |
| 21 | |
| 22 | async def get_darktrace_auth_keys(customer_integration) -> DarktraceAuthKeys: |
| 23 | """ |
| 24 | Extract the Darktrace authentication keys from the CustomerIntegration. |
| 25 | |
| 26 | Args: |
| 27 | customer_integration (CustomerIntegration): The CustomerIntegration containing the |
| 28 | Darktrace authentication keys. |
| 29 | |
| 30 | Returns: |
| 31 | DarktraceAuthKeys: The extracted Darktrace authentication keys. |
| 32 | """ |
| 33 | darktrace_auth_keys = extract_auth_keys( |
| 34 | customer_integration, |
| 35 | service_name="Darktrace", |
| 36 | ) |
| 37 | |
| 38 | return DarktraceAuthKeys(**darktrace_auth_keys) |
| 39 | |
| 40 | |
| 41 | async def get_collect_darktrace_data(darktrace_request, session, auth_keys): |
| 42 | return CollectDarktrace( |
| 43 | integration="darktrace", |
| 44 | customer_code=darktrace_request.customer_code, |
| 45 | graylog_host=await get_connector_attribute( |
| 46 | connector_id=14, |
| 47 | column_name="connector_url", |
| 48 | session=session, |
| 49 | ), |
| 50 | graylog_port=await get_connector_attribute( |
| 51 | connector_id=14, |
| 52 | column_name="connector_extra_data", |
| 53 | session=session, |
| 54 | ), |
| 55 | public_token=auth_keys.PUBLIC_TOKEN, |
| 56 | private_token=auth_keys.PRIVATE_TOKEN, |
| 57 | darktrace_host=auth_keys.HOST, |
| 58 | darktrace_port=auth_keys.PORT, |
| 59 | timeframe="15m", |
| 60 | ) |
| 61 | |
| 62 | |
| 63 | @module_darktrace_router.post( |
| 64 | "", |
| 65 | response_model=InvokeDarktraceResponse, |
| 66 | description="Invoke the Darktrace module.", |
| 67 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 68 | ) |
| 69 | async def collect_darktrace_route(darktrace_request: InvokeDarktraceRequest, session: AsyncSession = Depends(get_db)): |
| 70 | """Pull down Darktrace Events.""" |
| 71 | logger.info(f"Invoke Darktrace Request: {darktrace_request}") |
| 72 | try: |
| 73 | customer_integration_response = await get_customer_integration_response( |
| 74 | darktrace_request.customer_code, |
| 75 | session, |
| 76 | ) |
| 77 | |
| 78 | customer_integration = await find_customer_integration( |
| 79 | darktrace_request.customer_code, |
| 80 | darktrace_request.integration_name, |
| 81 | customer_integration_response, |
| 82 | ) |
| 83 | |
| 84 | auth_keys = await get_darktrace_auth_keys(customer_integration) |
| 85 | |
| 86 | collect_darktrace_data = await get_collect_darktrace_data(darktrace_request, session, auth_keys) |
| 87 | |
| 88 | await post_to_copilot_darktrace_module(data=collect_darktrace_data) |
| 89 | |
| 90 | except Exception as e: |
| 91 | logger.error(f"Error during DB session: {str(e)}") |
| 92 | return InvokeDarktraceResponse(success=False, message=str(e)) |
| 93 | |
| 94 | return InvokeDarktraceResponse(success=True, message="Darktrace Events collected successfully.") |