| 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.mimecast import CollectMimecast |
| 10 | from app.integrations.modules.schema.mimecast import InvokeMimecastRequest |
| 11 | from app.integrations.modules.schema.mimecast import InvokeMimecastResponse |
| 12 | from app.integrations.modules.schema.mimecast import MimecastAuthKeys |
| 13 | from app.integrations.modules.services.mimecast import post_to_copilot_mimecast_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.middleware.license import get_license |
| 18 | from app.utils import get_connector_attribute |
| 19 | |
| 20 | module_mimecast_router = APIRouter() |
| 21 | |
| 22 | |
| 23 | async def get_mimecast_auth_keys(customer_integration) -> MimecastAuthKeys: |
| 24 | """ |
| 25 | Extract the Mimecast authentication keys from the CustomerIntegration. |
| 26 | |
| 27 | Args: |
| 28 | customer_integration (CustomerIntegration): The CustomerIntegration containing the |
| 29 | Mimecast authentication keys. |
| 30 | |
| 31 | Returns: |
| 32 | MimecastAuthKeys: The extracted Huntress authentication keys. |
| 33 | """ |
| 34 | mimecast_auth_keys = extract_auth_keys( |
| 35 | customer_integration, |
| 36 | service_name="Mimecast", |
| 37 | ) |
| 38 | |
| 39 | return MimecastAuthKeys(**mimecast_auth_keys) |
| 40 | |
| 41 | |
| 42 | async def get_collect_mimecast_data(mimecast_request, session, auth_keys): |
| 43 | return CollectMimecast( |
| 44 | integration="mimecast", |
| 45 | customer_code=mimecast_request.customer_code, |
| 46 | graylog_host=await get_connector_attribute( |
| 47 | connector_id=14, |
| 48 | column_name="connector_url", |
| 49 | session=session, |
| 50 | ), |
| 51 | graylog_port=await get_connector_attribute( |
| 52 | connector_id=14, |
| 53 | column_name="connector_extra_data", |
| 54 | session=session, |
| 55 | ), |
| 56 | app_id=auth_keys.APP_ID, |
| 57 | app_key=auth_keys.APP_KEY, |
| 58 | email_address=auth_keys.EMAIL_ADDRESS, |
| 59 | access_key=auth_keys.ACCESS_KEY, |
| 60 | secret_key=auth_keys.SECRET_KEY, |
| 61 | uri=auth_keys.URI, |
| 62 | time_range="15m", |
| 63 | ) |
| 64 | |
| 65 | |
| 66 | @module_mimecast_router.post( |
| 67 | "", |
| 68 | response_model=InvokeMimecastResponse, |
| 69 | description="Invoke the Huntress module.", |
| 70 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 71 | ) |
| 72 | async def collect_huntress_route(mimecast_request: InvokeMimecastRequest, session: AsyncSession = Depends(get_db)): |
| 73 | """Pull down Huntress Events.""" |
| 74 | try: |
| 75 | customer_integration_response = await get_customer_integration_response( |
| 76 | mimecast_request.customer_code, |
| 77 | session, |
| 78 | ) |
| 79 | |
| 80 | customer_integration = await find_customer_integration( |
| 81 | mimecast_request.customer_code, |
| 82 | mimecast_request.integration_name, |
| 83 | customer_integration_response, |
| 84 | ) |
| 85 | |
| 86 | auth_keys = await get_mimecast_auth_keys(customer_integration) |
| 87 | |
| 88 | collect_huntress_data = await get_collect_mimecast_data(mimecast_request, session, auth_keys) |
| 89 | |
| 90 | license = await get_license(session) |
| 91 | |
| 92 | await post_to_copilot_mimecast_module(data=collect_huntress_data, license_key=license.license_key) |
| 93 | |
| 94 | except Exception as e: |
| 95 | logger.error(f"Error during DB session: {str(e)}") |
| 96 | return InvokeMimecastResponse(success=False, message=str(e)) |
| 97 | |
| 98 | return InvokeMimecastResponse(success=True, message="Mimecast Events collected successfully.") |