main
py 96 lines 3.59 KB
Raw
1 from fastapi import APIRouter
2 from fastapi import Depends
3 from fastapi import HTTPException
4 from fastapi import Security
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.routes import find_customer_integration
10 from app.integrations.routes import get_customer_integrations_by_customer_code
11 from app.integrations.schema import CustomerIntegrations
12 from app.integrations.schema import CustomerIntegrationsResponse
13 from app.integrations.socfortress_mdr.schema.provision import (
14 ProvisionSOCFortressMDRRequest,
15 )
16 from app.integrations.socfortress_mdr.schema.provision import (
17 ProvisionSOCFortressMDRResponse,
18 )
19 from app.integrations.socfortress_mdr.services.provision import INTEGRATION_NAME
20 from app.integrations.socfortress_mdr.services.provision import (
21 provision_socfortress_mdr,
22 )
23
24 integration_socfortress_mdr_router = APIRouter()
25
26
27 async def get_customer_integration_response(
28 customer_code: str,
29 session: AsyncSession,
30 ) -> CustomerIntegrationsResponse:
31 """Retrieve the integration settings for a customer (404 if none)."""
32 customer_integration_response = await get_customer_integrations_by_customer_code(
33 customer_code,
34 session,
35 )
36 if customer_integration_response.available_integrations == []:
37 raise HTTPException(
38 status_code=404,
39 detail="Customer integration settings not found.",
40 )
41 return customer_integration_response
42
43
44 def extract_collector_uuid(customer_integration: CustomerIntegrations) -> str:
45 """Pull the COLLECTOR_UUID auth-key value off the SOCFortress MDR subscription."""
46 for subscription in customer_integration.integration_subscriptions:
47 if subscription.integration_service.service_name == INTEGRATION_NAME:
48 for auth_key in subscription.integration_auth_keys:
49 if auth_key.auth_key_name == "COLLECTOR_UUID":
50 return auth_key.auth_value
51 raise HTTPException(
52 status_code=404,
53 detail=(
54 "COLLECTOR_UUID auth key not found for the SOCFortress MDR integration. "
55 "Add the integration with a COLLECTOR_UUID before deploying."
56 ),
57 )
58
59
60 @integration_socfortress_mdr_router.post(
61 "/provision",
62 response_model=ProvisionSOCFortressMDRResponse,
63 description="Provision SOCFortress MDR integration for a customer.",
64 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
65 )
66 async def provision_socfortress_mdr_route(
67 provision_request: ProvisionSOCFortressMDRRequest,
68 session: AsyncSession = Depends(get_db),
69 ) -> ProvisionSOCFortressMDRResponse:
70 """
71 Provision SOCFortress MDR for a customer: validate the COLLECTOR_UUID auth key
72 is present, then mark the integration deployed (enabling alert forwarding).
73 """
74 customer_integration_response = await get_customer_integration_response(
75 provision_request.customer_code,
76 session,
77 )
78
79 customer_integration = await find_customer_integration(
80 provision_request.customer_code,
81 provision_request.integration_name,
82 customer_integration_response,
83 )
84
85 collector_uuid = extract_collector_uuid(customer_integration)
86 if not collector_uuid or not collector_uuid.strip():
87 raise HTTPException(
88 status_code=400,
89 detail="COLLECTOR_UUID is empty. Provide the MDR collector UUID before deploying.",
90 )
91
92 return await provision_socfortress_mdr(
93 customer_code=provision_request.customer_code,
94 collector_uuid=collector_uuid,
95 session=session,
96 )