main
py 137 lines 5.3 KB
Raw
1 from typing import Dict
2
3 from fastapi import APIRouter
4 from fastapi import Depends
5 from fastapi import HTTPException
6 from fastapi import Security
7 from sqlalchemy.ext.asyncio import AsyncSession
8
9 from app.auth.utils import AuthHandler
10 from app.db.db_session import get_db
11 from app.integrations.defender_for_endpoint.schema.provision import (
12 DefenderForEndpointCustomerDetails,
13 )
14 from app.integrations.defender_for_endpoint.schema.provision import (
15 ProvisionDefenderForEndpointAuthKeys,
16 )
17 from app.integrations.defender_for_endpoint.schema.provision import (
18 ProvisionDefenderForEndpointRequest,
19 )
20 from app.integrations.defender_for_endpoint.schema.provision import (
21 ProvisionDefenderForEndpointResponse,
22 )
23 from app.integrations.defender_for_endpoint.services.provision import (
24 provision_defender_for_endpoint,
25 )
26 from app.integrations.routes import find_customer_integration
27 from app.integrations.routes import get_customer_integrations_by_customer_code
28 from app.integrations.schema import CustomerIntegrations
29 from app.integrations.schema import CustomerIntegrationsResponse
30
31 integration_defender_for_endpoint_router = APIRouter()
32
33
34 async def get_customer_integration_response(
35 customer_code: str,
36 session: AsyncSession,
37 ) -> CustomerIntegrationsResponse:
38 """
39 Retrieves the integration response for a customer.
40
41 Args:
42 customer_code (str): The code of the customer.
43 session (AsyncSession): The async session object for database operations.
44
45 Returns:
46 CustomerIntegrationsResponse: The integration response for the customer.
47
48 Raises:
49 HTTPException: If the customer integration settings are not found.
50 """
51 customer_integration_response = await get_customer_integrations_by_customer_code(
52 customer_code,
53 session,
54 )
55 if customer_integration_response.available_integrations == []:
56 raise HTTPException(
57 status_code=404,
58 detail="Customer integration settings not found.",
59 )
60 return customer_integration_response
61
62
63 def extract_defender_for_endpoint_auth_keys(
64 customer_integration: CustomerIntegrations,
65 ) -> Dict[str, str]:
66 """
67 Extracts the authentication keys for defender_for_endpoint integration from the given customer integration.
68
69 Args:
70 customer_integration (CustomerIntegrations): The customer integration object.
71
72 Returns:
73 Dict[str, str]: A dictionary containing the authentication keys for defender_for_endpoint integration.
74
75 Raises:
76 HTTPException: If no authentication keys are found for defender_for_endpoint integration.
77 """
78 defender_for_endpoint_auth_keys = {}
79 for subscription in customer_integration.integration_subscriptions:
80 if subscription.integration_service.service_name == "DefenderForEndpoint":
81 for auth_key in subscription.integration_auth_keys:
82 defender_for_endpoint_auth_keys[auth_key.auth_key_name] = auth_key.auth_value
83 if not defender_for_endpoint_auth_keys:
84 raise HTTPException(
85 status_code=404,
86 detail="No auth keys found for defender_for_endpoint integration. Please create auth keys for defender_for_endpoint integration.",
87 )
88 return defender_for_endpoint_auth_keys
89
90
91 @integration_defender_for_endpoint_router.post(
92 "/provision",
93 response_model=ProvisionDefenderForEndpointResponse,
94 description="Provision DefenderForEndpoint integration for a customer.",
95 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
96 )
97 async def provision_defender_for_endpoint_route(
98 provision_defender_for_endpoint_request: ProvisionDefenderForEndpointRequest,
99 session: AsyncSession = Depends(get_db),
100 ) -> ProvisionDefenderForEndpointResponse:
101 """
102 Provisions DefenderForEndpoint integration for a customer.
103
104 Args:
105 provision_defender_for_endpoint_request (ProvisionDefenderForEndpointRequest): The request object containing the necessary information for provisioning.
106 session (AsyncSession, optional): The database session. Defaults to Depends(get_db).
107
108 Returns:
109 ProvisionDefenderForEndpointResponse: The response object containing the result of the provisioning.
110 """
111 customer_integration_response = await get_customer_integration_response(
112 provision_defender_for_endpoint_request.customer_code,
113 session,
114 )
115
116 customer_integration = await find_customer_integration(
117 provision_defender_for_endpoint_request.customer_code,
118 provision_defender_for_endpoint_request.integration_name,
119 customer_integration_response,
120 )
121
122 defender_for_endpoint_auth_keys = extract_defender_for_endpoint_auth_keys(customer_integration)
123
124 auth_keys = ProvisionDefenderForEndpointAuthKeys(**defender_for_endpoint_auth_keys)
125
126 return await provision_defender_for_endpoint(
127 customer_details=DefenderForEndpointCustomerDetails(
128 customer_code=provision_defender_for_endpoint_request.customer_code,
129 customer_name=customer_integration.customer_name,
130 protocal_type="TCP",
131 syslog_port=int(auth_keys.SYSLOG_PORT),
132 hot_data_retention=provision_defender_for_endpoint_request.hot_data_retention,
133 index_replicas=provision_defender_for_endpoint_request.index_replicas,
134 ),
135 keys=auth_keys,
136 session=session,
137 )