14
from sqlalchemy.orm import joinedload
15
16
from app.auth.utils import AuthHandler
17
+from app.connectors.grafana.services.folders import delete_folder
18
+from app.connectors.graylog.services.management import delete_index_by_id
19
+from app.connectors.graylog.services.streams import delete_stream
20
from app.db.db_session import get_db
21
from app.db.universal_models import Customers
22
from app.db.universal_models import CustomersMeta
520
return processed_customer_integrations
521
522
523
+def generate_integration_response(customer_code: str, integration_name: str) -> CustomerIntegrationCreateResponse:
524
+ additional_info_map = {
525
+ "Office365": (
526
+ "Make sure to update the Office365 integration block in the Wazuh Manager ossec.conf file and restart the Wazuh Manager service. "
527
+ "Also make sure to update the Office365 Graylog stream rule for this customer with the new organization ID if this has changed. "
528
+ "YouTube video: https://youtu.be/ihj2F2rA6BQ?si=p4c8Xnk6PX8r29IB"
529
+ ),
530
+ "Crowdstrike": (
531
+ "Make sure to update the Crowdstrike docker application with the new connection details and restart the docker container. "
532
+ "YouTube video: https://youtu.be/YOVUOpZDEzM?si=jzpHw8vcnqnfVPzt"
533
+ ),
534
+ "BitDefender": (
535
+ "Make sure to update the BitDefender docker application with the new connection details and restart the docker container."
536
+ ),
537
+ }
538
+
539
+ additional_info = additional_info_map.get(integration_name, "")
540
+ if additional_info == "":
541
+ additional_info = None
542
+
543
+ return CustomerIntegrationCreateResponse(
544
+ message=f"Customer integration {customer_code} {integration_name} successfully updated.",
545
+ success=True,
546
+ additional_info=additional_info,
547
+ )
548
+
549
+
550
+def generate_decommission_response(customer_code: str, integration_name: str) -> CustomerIntegrationDeleteResponse:
551
+ additional_info_map = {
552
+ "Office365": (
553
+ "Make sure to remove the Office365 integration block from the Wazuh Manager ossec.conf file and restart the Wazuh Manager service. "
554
+ ),
555
+ "Crowdstrike": ("Make sure to remove the Crowdstrike docker application."),
556
+ "BitDefender": ("Make sure to remove the BitDefender docker application."),
557
+ }
558
+
559
+ additional_info = additional_info_map.get(integration_name, "")
560
+ if additional_info == "":
561
+ additional_info = None
562
+
563
+ return CustomerIntegrationDeleteResponse(
564
+ message=f"Customer integration {customer_code} {integration_name} successfully deleted.",
565
+ success=True,
566
+ additional_info=additional_info,
567
+ )
568
+
569
+
570
@integration_settings_router.get(
571
"/available_integrations",
572
response_model=AvailableIntegrationsResponse,
848
session,
849
)
850
801
- subscription_id = get_subscription_id(
802
- customer_integration,
803
- customer_integration_update.integration_name,
804
- customer_integration_update.integration_auth_keys[0].auth_key_name,
805
- )
806
-
807
- if not subscription_id:
808
- raise HTTPException(
809
- status_code=404,
810
- detail=f"Integration auth key {customer_integration_update.integration_auth_keys[0].auth_key_name} not found.",
851
+ for auth_key in customer_integration_update.integration_auth_keys:
852
+ subscription_id = get_subscription_id(
853
+ customer_integration,
854
+ customer_integration_update.integration_name,
855
+ auth_key.auth_key_name,
856
)
857
813
- await session.execute(
814
- update(IntegrationAuthKeys)
815
- .where(IntegrationAuthKeys.subscription_id == subscription_id)
816
- .values(
817
- auth_value=customer_integration_update.integration_auth_keys[0].auth_value,
818
- ),
819
- )
858
+ if not subscription_id:
859
+ raise HTTPException(
860
+ status_code=404,
861
+ detail=f"Integration auth key {auth_key.auth_key_name} not found.",
862
+ )
863
+
864
+ await session.execute(
865
+ update(IntegrationAuthKeys)
866
+ .where(IntegrationAuthKeys.subscription_id == subscription_id)
867
+ .values(
868
+ auth_value=auth_key.auth_value,
869
+ ),
870
+ )
871
872
await session.commit()
873
823
- return CustomerIntegrationCreateResponse(
824
- message=f"Customer integration {customer_code} {customer_integration_update.integration_name} successfully updated.",
825
- success=True,
826
- )
874
+ return generate_integration_response(customer_code, customer_integration_update.integration_name)
875
876
877
@integration_settings_router.put(
912
)
913
914
915
+async def fetch_customer_integration_meta(session: AsyncSession, customer_code: str, integration_name: str):
916
+ """
917
+ Fetches customer integrations metadata from the database.
918
+ """
919
+ stmt = select(CustomerIntegrationsMeta).where(
920
+ CustomerIntegrationsMeta.customer_code == customer_code,
921
+ CustomerIntegrationsMeta.integration_name == integration_name,
922
+ )
923
+ result = await session.execute(stmt)
924
+ return result.scalars().first()
925
+
926
+
927
+async def delete_customer_integration_meta(session: AsyncSession, customer_code: str, integration_name: str):
928
+ """
929
+ Deletes customer integrations metadata from the database.
930
+ """
931
+ await session.execute(
932
+ delete(CustomerIntegrationsMeta).where(
933
+ CustomerIntegrationsMeta.customer_code == customer_code,
934
+ CustomerIntegrationsMeta.integration_name == integration_name,
935
+ ),
936
+ )
937
+
938
+
939
@integration_settings_router.delete(
940
"/delete_integration",
941
response_model=CustomerIntegrationDeleteResponse,
973
detail="No subscriptions found for customer integration",
974
)
975
976
+ stream_id = (await fetch_customer_integration_meta(session, customer_code, integration_name)).graylog_stream_id
977
+ logger.info(f"stream_id: {stream_id}")
978
+ await delete_stream(stream_id=stream_id)
979
+
980
+ index_id = (await fetch_customer_integration_meta(session, customer_code, integration_name)).graylog_index_id
981
+ logger.info(f"index_id: {index_id}")
982
+ await delete_index_by_id(index_id=index_id)
983
+
984
+ # Delete the folder in Grafana
985
+ grafana_org_id = (await fetch_customer_integration_meta(session, customer_code, integration_name)).grafana_org_id
986
+ grafana_dashboard_folder_id = (
987
+ await fetch_customer_integration_meta(session, customer_code, integration_name)
988
+ ).grafana_dashboard_folder_id
989
+
990
+ await delete_folder(grafana_org_id, int(grafana_dashboard_folder_id))
991
+
992
await delete_metadata(session, subscription_ids)
993
await delete_subscriptions(session, subscription_ids)
994
await delete_configs(session, integration_service_id)
995
await delete_integration_service(session, integration_service_id)
996
await delete_customer_integration_record(session, customer_id)
997
+ await delete_customer_integration_meta(session, customer_code, integration_name)
998
999
await session.commit()
1000
912
- return CustomerIntegrationDeleteResponse(
913
- message=f"Customer integration {customer_code} {integration_name} successfully deleted.",
914
- success=True,
915
- )
916
-
917
-
918
-@integration_settings_router.delete(
919
- "/delete_integration_meta",
920
- response_model=CustomerIntegrationsMetaResponse,
921
- description="Delete a customer integration metadata.",
922
- dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
923
-)
924
-async def delete_integration_meta(
925
- customer_integration_meta: CustomerIntegrationsMetaSchema,
926
- session: AsyncSession = Depends(get_db),
927
-):
928
- """
929
- Endpoint to delete a customer integration metadata.
930
- """
931
- try:
932
- stmt = delete(CustomerIntegrationsMeta).where(
933
- CustomerIntegrationsMeta.customer_code == customer_integration_meta.customer_code,
934
- CustomerIntegrationsMeta.integration_name == customer_integration_meta.integration_name,
935
- )
936
- await session.execute(stmt)
937
- await session.commit()
938
- return CustomerIntegrationsMetaResponse(
939
- message="Customer integration metadata successfully deleted.",
940
- success=True,
941
- )
942
- except Exception as e:
943
- logger.error(f"Error while deleting customer integration metadata: {e}")
944
- return CustomerIntegrationsMetaResponse(
945
- customer_integrations_meta=None,
946
- message="Error while deleting customer integration metadata.",
947
- success=False,
948
- )
1001
+ return generate_decommission_response(customer_code, integration_name)
1002
+
1003
+
1004
+# @integration_settings_router.delete(
1005
+# "/delete_integration_meta",
1006
+# response_model=CustomerIntegrationsMetaResponse,
1007
+# description="Delete a customer integration metadata.",
1008
+# dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
1009
+# )
1010
+# async def delete_integration_meta(
1011
+# customer_integration_meta: CustomerIntegrationsMetaSchema,
1012
+# session: AsyncSession = Depends(get_db),
1013
+# ):
1014
+# """
1015
+# Endpoint to delete a customer integration metadata.
1016
+# """
1017
+# try:
1018
+# stmt = delete(CustomerIntegrationsMeta).where(
1019
+# CustomerIntegrationsMeta.customer_code == customer_integration_meta.customer_code,
1020
+# CustomerIntegrationsMeta.integration_name == customer_integration_meta.integration_name,
1021
+# )
1022
+# await session.execute(stmt)
1023
+# await session.commit()
1024
+# return CustomerIntegrationsMetaResponse(
1025
+# message="Customer integration metadata successfully deleted.",
1026
+# success=True,
1027
+# )
1028
+# except Exception as e:
1029
+# logger.error(f"Error while deleting customer integration metadata: {e}")
1030
+# return CustomerIntegrationsMetaResponse(
1031
+# customer_integrations_meta=None,
1032
+# message="Error while deleting customer integration metadata.",
1033
+# success=False,
1034
+# )