@cryptotaxi247 / CoPilot / commits / 2bea5093

mimecast provision start scheduler

Taylor committed Jan 29, 2024 at 16:47 UTC 2bea5093f502f1f2a38d4a93de2bb23d1d457ab7
5 files changed +84 -24
backend/app/integrations/mimecast/checkpoint/mimecast_00002.checkpoint
+1 -1
@@ -1 +1 @@
1 -eNo9jtFOgzAYRt_lv5XEgm0pS7woyEhnjHEwNxZvsHSswAChkw3ju4teeP2dc_J9waDkuVc6hwWUeNwj7USTmVJ-IVgsQ9q93cb0JvGPsuKuwy5PVfmwStdhvqv6ehNfi7oUnSBH8_Gauw3fjcxvHlfJOsr2InpPQ_ocjDjYSv0ydcZXfcMLUS63-B4saA-HQRlY2BYMWp3qtvi94ZE7jyLPZhbIXmVGJfqkZshFhFFsU-Ix518w127ekAWfqh902_zFMinbczOHIdjE3LZdThB8_wA-O0ou
1 +eNo9js1SwjAURt_lrjtjSolpmHHRCuqUH0GqDoybEm7SaGmwCcXG8d2tLlh_55z5vsGiODWo9zCCl1Qvn-pEEWk-dZ5uku2jyJbGvxZu3qlMCrJatNPp-e3Kefa-cdtCf3VjlpYya0WYzrlY51pU1U7tBscZ8mjG_b6Ui3hypz_uJ9fK-gccn3Ovy9UNBGCktOhgFAZgNR4qo_5ucDrkNIoiGoBosHCY6wP2ECOUU07iQczii-C6Y7-RAFpsrDb1f6wQwpzqPgy3z-skDFlCCfz8AnB4S9w
\ No newline at end of file
backend/app/integrations/mimecast/routes/provision.py
+37 -7
@@ -28,25 +28,32 @@ async def provision_mimecast_route(provision_mimecast_request: ProvisionMimecast
28 Returns:
29 ProvisionOffice365Response: The response object containing the result of the provisioning.
30 """
31 - return await provision_mimecast(provision_mimecast_request, session)
31 + await provision_mimecast(provision_mimecast_request, session)
32 + await add_scheduler_jobs(
33 + CreateSchedulerRequest(
34 + function_name="invoke_mimecast_integration_ttp",
35 + time_interval=15,
36 + job_id="invoke_mimecast_integration_ttp",
37 + ),
38 + )
39 + return ProvisionMimecastResponse(success=True, message="Mimecast integration provisioned.")
40
41
42
43
44 @integration_mimecast_scheduler_router.post(
37 - "/invoke/scheduler",
45 + "/invoke/scheduler/siem",
46 description="Invoke a mimecast integration.",
47 )
40 -async def invoke_mimecast_schedule_create(time_interval: int) -> MimecastScheduledResponse:
48 +async def invoke_mimecast_siem_schedule_create(time_interval: int) -> MimecastScheduledResponse:
49 """
42 - Provisions Office365 integration for a customer.
50 + Invoke a mimecast integration and schedule it based on the specified time interval.
51
52 Args:
45 - provision_office365_request (ProvisionOffice365Request): The request object containing the necessary information for provisioning.
46 - session (AsyncSession, optional): The database session. Defaults to Depends(get_db).
53 + time_interval (int): The time interval in seconds for scheduling the integration.
54
55 Returns:
49 - ProvisionOffice365Response: The response object containing the result of the provisioning.
56 + MimecastScheduledResponse: The response indicating the success or failure of the scheduling operation.
57 """
58 await add_scheduler_jobs(
59 CreateSchedulerRequest(
@@ -56,3 +63,26 @@ async def invoke_mimecast_schedule_create(time_interval: int) -> MimecastSchedul
63 ),
64 )
65 return MimecastScheduledResponse(success=True, message="Mimecast integration scheduled.")
66 +
67 +@integration_mimecast_scheduler_router.post(
68 + "/invoke/scheduler/ttp",
69 + description="Invoke a mimecast integration.",
70 +)
71 +async def invoke_mimecast_ttp_schedule_create(time_interval: int) -> MimecastScheduledResponse:
72 + """
73 + Invoke a Mimecast integration with a specified time interval.
74 +
75 + Args:
76 + time_interval (int): The time interval in seconds.
77 +
78 + Returns:
79 + MimecastScheduledResponse: The response indicating the success and message of the scheduled integration.
80 + """
81 + await add_scheduler_jobs(
82 + CreateSchedulerRequest(
83 + function_name="invoke_mimecast_integration_ttp",
84 + time_interval=time_interval,
85 + job_id="invoke_mimecast_integration",
86 + ),
87 + )
88 + return MimecastScheduledResponse(success=True, message="Mimecast integration scheduled.")
backend/app/integrations/mimecast/services/provision.py
+14 -14
@@ -102,14 +102,15 @@ async def build_event_stream_config(
102 session: AsyncSession,
103 ) -> MimecastEventStream:
104 """
105 - Build the configuration for a Wazuh event stream.
105 + Builds the configuration for a Mimecast event stream.
106
107 Args:
108 - request (ProvisionNewCustomer): The request object containing customer information.
109 - index_set_id (str): The ID of the index set.
108 + customer_code (str): The customer code.
109 + index_set_id (str): The index set ID.
110 + session (AsyncSession): The async session.
111
112 Returns:
112 - Office365EventStream: The configured Wazuh event stream.
113 + MimecastEventStream: The configured Mimecast event stream.
114 """
115 return MimecastEventStream(
116 title=f"Mimecast EVENTS - {(await get_customer(customer_code, session)).customer.customer_name}",
@@ -140,7 +141,7 @@ async def send_event_stream_creation_request(event_stream: MimecastEventStream)
141 Sends a request to create an event stream.
142
143 Args:
143 - event_stream (WazuhEventStream): The event stream to be created.
144 + event_stream (MimecastEventStream): The event stream to be created.
145
146 Returns:
147 StreamCreationResponse: The response containing the created event stream.
@@ -174,15 +175,14 @@ async def create_grafana_datasource(
175 session: AsyncSession,
176 ) -> GrafanaDataSourceCreationResponse:
177 """
177 - Creates a Grafana Wazuh datasource for a new customer using the OpenSearch Data Source.
178 + Creates a Grafana datasource for the specified customer.
179
180 Args:
180 - request (ProvisionNewCustomer): The request object containing customer information.
181 - organization_id (int): The ID of the organization to create the datasource for.
182 - session (AsyncSession): The database session.
181 + customer_code (str): The customer code.
182 + session (AsyncSession): The async session.
183
184 Returns:
185 - GrafanaDataSourceCreationResponse: The response object containing the result of the datasource creation.
185 + GrafanaDataSourceCreationResponse: The response containing the created datasource details.
186 """
187 logger.info("Creating Grafana datasource")
188 grafana_client = await create_grafana_client("Grafana")
@@ -244,8 +244,8 @@ async def provision_mimecast(provision_mimecast_request: ProvisionMimecastReques
244 await start_stream(stream_id=stream_id)
245
246 # Grafana Deployment
247 - office365_datasource_uid = (await create_grafana_datasource(customer_code=provision_mimecast_request.customer_code, session=session)).datasource.uid
248 - grafana_o365_folder_id = (
247 + mimecast_datasource_uid = (await create_grafana_datasource(customer_code=provision_mimecast_request.customer_code, session=session)).datasource.uid
248 + grafana_mimecast_folder_id = (
249 await create_grafana_folder(
250 organization_id=(await get_customer_meta(provision_mimecast_request.customer_code, session)).customer_meta.customer_meta_grafana_org_id,
251 folder_title="MIMECAST",
@@ -255,8 +255,8 @@ async def provision_mimecast(provision_mimecast_request: ProvisionMimecastReques
255 DashboardProvisionRequest(
256 dashboards=[dashboard.name for dashboard in MimecastDashboard],
257 organizationId=(await get_customer_meta(provision_mimecast_request.customer_code, session)).customer_meta.customer_meta_grafana_org_id,
258 - folderId=grafana_o365_folder_id,
259 - datasourceUid=office365_datasource_uid,
258 + folderId=grafana_mimecast_folder_id,
259 + datasourceUid=mimecast_datasource_uid,
260 ),
261 )
262
backend/app/schedulers/scheduler.py
+2 -1
@@ -7,7 +7,7 @@ from app.db.db_session import sync_engine
7 from app.schedulers.models.scheduler import CreateSchedulerRequest
8 from app.schedulers.models.scheduler import JobMetadata
9 from app.schedulers.services.agent_sync import agent_sync
10 -from app.schedulers.services.invoke_mimecast import invoke_mimecast_integration
10 +from app.schedulers.services.invoke_mimecast import invoke_mimecast_integration, invoke_mimecast_integration_ttp
11
12
13 def init_scheduler():
@@ -76,6 +76,7 @@ def get_function_by_name(function_name: str):
76 function_map = {
77 "agent_sync": agent_sync,
78 "invoke_mimecast_integration": invoke_mimecast_integration,
79 + "invoke_mimecast_integration_ttp": invoke_mimecast_integration_ttp,
80 # Add other function mappings here
81 }
82 return function_map.get(function_name, lambda: ValueError(f"Function {function_name} not found"))
backend/app/schedulers/services/invoke_mimecast.py
+30 -1
@@ -6,7 +6,7 @@ from sqlalchemy import select
6
7 from app.db.db_session import get_db_session
8 from app.db.db_session import get_sync_db_session
9 -from app.integrations.mimecast.routes.mimecast import invoke_mimecast_route
9 +from app.integrations.mimecast.routes.mimecast import invoke_mimecast_route, mimecast_ttp_url_route
10 from app.integrations.mimecast.schema.mimecast import MimecastRequest
11 from app.integrations.mimecast.schema.mimecast import MimecastResponse
12 from app.integrations.models.customer_integration_settings import CustomerIntegrations
@@ -43,3 +43,32 @@ async def invoke_mimecast_integration() -> MimecastResponse:
43 # Handle the case where job_metadata does not exist
44 print("JobMetadata for 'invoke_mimecast_integration' not found.")
45 return MimecastResponse(success=True, message="Mimecast integration invoked.")
46 +
47 +async def invoke_mimecast_integration_ttp() -> MimecastResponse:
48 + """
49 + Invokes the Mimecast integration.
50 + """
51 + customer_codes = []
52 + async with get_db_session() as session:
53 + stmt = select(CustomerIntegrations).where(CustomerIntegrations.integration_service_name == "Mimecast")
54 + result = await session.execute(stmt)
55 + customer_codes = [row.customer_code for row in result.scalars()]
56 + logger.info(f"customer_codes: {customer_codes}")
57 + for customer_code in customer_codes:
58 + await mimecast_ttp_url_route(
59 + MimecastRequest(customer_code=customer_code, integration_name="Mimecast"),
60 + session,
61 + )
62 + # Close the session
63 + await session.close()
64 + with get_sync_db_session() as session:
65 + # Synchronous ORM operations
66 + job_metadata = session.query(JobMetadata).filter_by(job_id="invoke_mimecast_integration_ttp").one_or_none()
67 + if job_metadata:
68 + job_metadata.last_success = datetime.utcnow()
69 + session.add(job_metadata)
70 + session.commit()
71 + else:
72 + # Handle the case where job_metadata does not exist
73 + print("JobMetadata for 'invoke_mimecast_integration_ttp' not found.")
74 + return MimecastResponse(success=True, message="Mimecast integration invoked.")