| 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.utils import AuthHandler |
| 8 | from app.db.db_session import get_db |
| 9 | from app.integrations.mimecast.schema.mimecast import MimecastAuthKeys |
| 10 | from app.integrations.mimecast.schema.mimecast import MimecastRequest |
| 11 | from app.integrations.mimecast.schema.mimecast import MimecastResponse |
| 12 | from app.integrations.mimecast.schema.mimecast import MimecastTTPURLSRequest |
| 13 | from app.integrations.mimecast.services.mimecast import get_ttp_urls |
| 14 | from app.integrations.mimecast.services.mimecast import invoke_mimecast |
| 15 | from app.integrations.routes import find_customer_integration |
| 16 | from app.integrations.utils.utils import extract_auth_keys |
| 17 | from app.integrations.utils.utils import get_customer_integration_response |
| 18 | |
| 19 | integration_mimecast_router = APIRouter() |
| 20 | |
| 21 | |
| 22 | @integration_mimecast_router.post( |
| 23 | "/invoke", |
| 24 | response_model=MimecastResponse, |
| 25 | description="Invoke a mimecast integration.", |
| 26 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 27 | ) |
| 28 | async def invoke_mimecast_route( |
| 29 | mimecast_request: MimecastRequest, |
| 30 | session: AsyncSession = Depends(get_db), |
| 31 | ) -> MimecastResponse: |
| 32 | """ |
| 33 | Invoke a mimecast integration. |
| 34 | |
| 35 | This function is responsible for handling the POST request to invoke a mimecast integration. |
| 36 | It requires the following parameters: |
| 37 | - mimecast_request: The request payload containing the necessary information for the integration. |
| 38 | - session: The database session used for querying customer integration data. |
| 39 | |
| 40 | It performs the following steps: |
| 41 | 1. Retrieves the customer integration response from the database based on the customer code. |
| 42 | 2. Finds the specific customer integration based on the customer code and integration name. |
| 43 | 3. Extracts the mimecast authentication keys from the customer integration. |
| 44 | 4. Invokes the mimecast integration using the provided request payload and authentication keys. |
| 45 | |
| 46 | Returns: |
| 47 | - MimecastResponse: The response model containing the result of the mimecast integration invocation. |
| 48 | """ |
| 49 | customer_integration_response = await get_customer_integration_response( |
| 50 | mimecast_request.customer_code, |
| 51 | session, |
| 52 | ) |
| 53 | |
| 54 | customer_integration = await find_customer_integration( |
| 55 | mimecast_request.customer_code, |
| 56 | mimecast_request.integration_name, |
| 57 | customer_integration_response, |
| 58 | ) |
| 59 | |
| 60 | mimecast_auth_keys = extract_auth_keys(customer_integration, service_name="Mimecast") |
| 61 | |
| 62 | auth_keys = MimecastAuthKeys(**mimecast_auth_keys) |
| 63 | |
| 64 | return await invoke_mimecast(mimecast_request, auth_keys) |
| 65 | |
| 66 | |
| 67 | @integration_mimecast_router.post( |
| 68 | "/ttp/urls", |
| 69 | response_model=MimecastResponse, |
| 70 | description="Pull down Mimecast TTP URLs for a given time range. " |
| 71 | "Link to docs: https://integrations.mimecast.com/documentation/endpoint-reference/logs-and-statistics/get-ttp-url-logs/ ", |
| 72 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 73 | ) |
| 74 | async def mimecast_ttp_url_route( |
| 75 | mimecast_request: MimecastRequest, |
| 76 | session: AsyncSession = Depends(get_db), |
| 77 | ): |
| 78 | logger.info("Mimecast TTP URL request received") |
| 79 | customer_code = mimecast_request.customer_code |
| 80 | customer_integration_response = await get_customer_integration_response( |
| 81 | mimecast_request.customer_code, |
| 82 | session, |
| 83 | ) |
| 84 | |
| 85 | customer_integration = await find_customer_integration( |
| 86 | mimecast_request.customer_code, |
| 87 | mimecast_request.integration_name, |
| 88 | customer_integration_response, |
| 89 | ) |
| 90 | |
| 91 | mimecast_auth_keys = extract_auth_keys(customer_integration, service_name="Mimecast") |
| 92 | |
| 93 | auth_keys = MimecastAuthKeys(**mimecast_auth_keys) |
| 94 | |
| 95 | mimecast_request = MimecastTTPURLSRequest( |
| 96 | ApplicationID=auth_keys.APP_ID, |
| 97 | ApplicationKey=auth_keys.APP_KEY, |
| 98 | AccessKey=auth_keys.ACCESS_KEY, |
| 99 | SecretKey=auth_keys.SECRET_KEY, |
| 100 | EmailAddress=auth_keys.EMAIL_ADDRESS, |
| 101 | time_range=mimecast_request.time_range, |
| 102 | ) |
| 103 | logger.info(f"Mimecast TTP URL request: {mimecast_request}") |
| 104 | |
| 105 | return await get_ttp_urls(mimecast_request, customer_code=customer_code) |