| 1 | from fastapi import APIRouter |
| 2 | from fastapi import Depends |
| 3 | from fastapi import Security |
| 4 | from sqlalchemy.ext.asyncio import AsyncSession |
| 5 | |
| 6 | from app.auth.routes.auth import AuthHandler |
| 7 | from app.db.db_session import get_db |
| 8 | from app.integrations.mimecast.schema.mimecast import MimecastScheduledResponse |
| 9 | from app.integrations.mimecast.schema.provision import ProvisionMimecastRequest |
| 10 | from app.integrations.mimecast.schema.provision import ProvisionMimecastResponse |
| 11 | from app.integrations.mimecast.services.provision import provision_mimecast |
| 12 | from app.integrations.utils.utils import get_customer_integration_response |
| 13 | from app.schedulers.models.scheduler import CreateSchedulerRequest |
| 14 | from app.schedulers.scheduler import add_scheduler_jobs |
| 15 | |
| 16 | integration_mimecast_scheduler_router = APIRouter() |
| 17 | |
| 18 | |
| 19 | @integration_mimecast_scheduler_router.post( |
| 20 | "/provision", |
| 21 | response_model=ProvisionMimecastResponse, |
| 22 | description="Provision a mimecast integration.", |
| 23 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 24 | ) |
| 25 | async def provision_mimecast_route( |
| 26 | provision_mimecast_request: ProvisionMimecastRequest, |
| 27 | session: AsyncSession = Depends(get_db), |
| 28 | ) -> ProvisionMimecastResponse: |
| 29 | """ |
| 30 | Provisions a mimecast integration. |
| 31 | |
| 32 | Args: |
| 33 | provision_mimecast_request (ProvisionMimecastRequest): The request object containing the necessary data for provisioning. |
| 34 | session (AsyncSession, optional): The database session. Defaults to Depends(get_db). |
| 35 | |
| 36 | Returns: |
| 37 | ProvisionMimecastResponse: The response object indicating the success or failure of the provisioning process. |
| 38 | """ |
| 39 | # Check if the customer integration settings are available and can be provisioned |
| 40 | await get_customer_integration_response( |
| 41 | provision_mimecast_request.customer_code, |
| 42 | session, |
| 43 | ) |
| 44 | await provision_mimecast(provision_mimecast_request, session) |
| 45 | await add_scheduler_jobs( |
| 46 | CreateSchedulerRequest( |
| 47 | function_name="invoke_mimecast_integration_ttp", |
| 48 | time_interval=15, |
| 49 | job_id="invoke_mimecast_integration_ttp", |
| 50 | ), |
| 51 | ) |
| 52 | await add_scheduler_jobs( |
| 53 | CreateSchedulerRequest( |
| 54 | function_name="invoke_mimecast_integration", |
| 55 | time_interval=5, |
| 56 | job_id="invoke_mimecast_integration", |
| 57 | ), |
| 58 | ) |
| 59 | return ProvisionMimecastResponse( |
| 60 | success=True, |
| 61 | message="Mimecast integration provisioned.", |
| 62 | ) |
| 63 | |
| 64 | |
| 65 | @integration_mimecast_scheduler_router.post( |
| 66 | "/invoke/scheduler/siem", |
| 67 | description="Invoke a mimecast integration.", |
| 68 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 69 | ) |
| 70 | async def invoke_mimecast_siem_schedule_create( |
| 71 | time_interval: int, |
| 72 | ) -> MimecastScheduledResponse: |
| 73 | """ |
| 74 | Invoke a mimecast integration and schedule it based on the specified time interval. |
| 75 | |
| 76 | Args: |
| 77 | time_interval (int): The time interval in seconds for scheduling the integration. |
| 78 | |
| 79 | Returns: |
| 80 | MimecastScheduledResponse: The response indicating the success or failure of the scheduling operation. |
| 81 | """ |
| 82 | await add_scheduler_jobs( |
| 83 | CreateSchedulerRequest( |
| 84 | function_name="invoke_mimecast_integration", |
| 85 | time_interval=time_interval, |
| 86 | job_id="invoke_mimecast_integration", |
| 87 | ), |
| 88 | ) |
| 89 | return MimecastScheduledResponse( |
| 90 | success=True, |
| 91 | message="Mimecast integration scheduled.", |
| 92 | ) |
| 93 | |
| 94 | |
| 95 | @integration_mimecast_scheduler_router.post( |
| 96 | "/invoke/scheduler/ttp", |
| 97 | description="Invoke a mimecast integration.", |
| 98 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 99 | ) |
| 100 | async def invoke_mimecast_ttp_schedule_create( |
| 101 | time_interval: int, |
| 102 | ) -> MimecastScheduledResponse: |
| 103 | """ |
| 104 | Invoke a Mimecast integration with a specified time interval. |
| 105 | |
| 106 | Args: |
| 107 | time_interval (int): The time interval in seconds. |
| 108 | |
| 109 | Returns: |
| 110 | MimecastScheduledResponse: The response indicating the success and message of the scheduled integration. |
| 111 | """ |
| 112 | await add_scheduler_jobs( |
| 113 | CreateSchedulerRequest( |
| 114 | function_name="invoke_mimecast_integration_ttp", |
| 115 | time_interval=time_interval, |
| 116 | job_id="invoke_mimecast_integration", |
| 117 | ), |
| 118 | ) |
| 119 | return MimecastScheduledResponse( |
| 120 | success=True, |
| 121 | message="Mimecast integration scheduled.", |
| 122 | ) |