main
py 132 lines 5.24 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.connectors.graylog.utils.routing import GraylogContext
11 from app.connectors.graylog.utils.routing import clear_graylog_context
12 from app.connectors.graylog.utils.routing import set_graylog_context
13 from app.db.db_session import get_db
14 from app.network_connectors.routes import find_customer_network_connector
15 from app.network_connectors.routes import (
16 get_customer_network_connectors_by_customer_code,
17 )
18 from app.network_connectors.schema import CustomerNetworkConnectors
19 from app.network_connectors.schema import CustomerNetworkConnectorsResponse
20 from app.stack_provisioning.graylog.schema.sonicwall import ProvisionSonicwallKeys
21 from app.stack_provisioning.graylog.schema.sonicwall import ProvisionSonicwallRequest
22 from app.stack_provisioning.graylog.schema.sonicwall import ProvisionSonicwallResponse
23 from app.stack_provisioning.graylog.schema.sonicwall import SonicwallCustomerDetails
24 from app.stack_provisioning.graylog.services.sonicwall import provision_sonicwall
25
26 stack_provisioning_graylog_sonicwall_router = APIRouter()
27
28
29 async def get_customer_integration_response(
30 customer_code: str,
31 session: AsyncSession,
32 ) -> CustomerNetworkConnectorsResponse:
33 """
34 Retrieves the integration response for a customer.
35
36 Args:
37 customer_code (str): The code of the customer.
38 session (AsyncSession): The async session object for database operations.
39
40 Returns:
41 CustomerNetworkConnectorsResponse: The integration response for the customer.
42
43 Raises:
44 HTTPException: If the customer integration settings are not found.
45 """
46 customer_integration_response = await get_customer_network_connectors_by_customer_code(
47 customer_code,
48 session,
49 )
50 if customer_integration_response.available_network_connectors == []:
51 raise HTTPException(
52 status_code=404,
53 detail="Customer integration settings not found.",
54 )
55 return customer_integration_response
56
57
58 def extract_sonicwall_keys(
59 customer_integration: CustomerNetworkConnectors,
60 ) -> Dict[str, str]:
61 """
62 Extracts the authentication keys for SonicWall integration from the given customer integration.
63
64 Args:
65 customer_integration (CustomerNetworkConnectors): The customer integration object.
66
67 Returns:
68 Dict[str, str]: A dictionary containing the authentication keys for SonicWall integration.
69
70 Raises:
71 HTTPException: If no authentication keys are found for SonicWall integration.
72 """
73 sonicwall_keys = {}
74 for subscription in customer_integration.network_connectors_subscriptions:
75 if subscription.network_connectors_service.service_name == "Sonicwall":
76 for auth_key in subscription.network_connectors_keys:
77 sonicwall_keys[auth_key.auth_key_name] = auth_key.auth_value
78 if not sonicwall_keys:
79 raise HTTPException(
80 status_code=404,
81 detail="No auth keys found for Sonicwall integration. Please create auth keys for Sonicwall network connector.",
82 )
83 return sonicwall_keys
84
85
86 @stack_provisioning_graylog_sonicwall_router.post(
87 "/graylog/provision/sonicwall",
88 response_model=ProvisionSonicwallResponse,
89 description="Provision SonicWall for the customer. Uses Graylog-Network instance for all Graylog operations.",
90 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
91 )
92 async def provision_sonicwall_route(
93 provision_sonicwall_request: ProvisionSonicwallRequest,
94 session: AsyncSession = Depends(get_db),
95 ) -> ProvisionSonicwallResponse:
96 """
97 Provision SonicWall for the customer.
98 Uses Graylog-Network instance for all Graylog operations.
99 """
100 # Set the Graylog context for this request - all downstream Graylog calls will use Graylog-Network
101 set_graylog_context(GraylogContext.NETWORK)
102
103 try:
104 customer_integration_response = await get_customer_integration_response(
105 provision_sonicwall_request.customer_code,
106 session,
107 )
108
109 customer_integration = await find_customer_network_connector(
110 provision_sonicwall_request.customer_code,
111 provision_sonicwall_request.integration_name,
112 customer_integration_response,
113 )
114
115 sonicwall_keys = extract_sonicwall_keys(customer_integration)
116
117 return await provision_sonicwall(
118 customer_details=SonicwallCustomerDetails(
119 customer_code=provision_sonicwall_request.customer_code,
120 customer_name=customer_integration.customer_name,
121 tls_cert_file=sonicwall_keys["TLS_CERT_FILE"],
122 tls_key_file=sonicwall_keys["TLS_KEY_FILE"],
123 syslog_port=int(sonicwall_keys["SYSLOG_PORT"]),
124 hot_data_retention=provision_sonicwall_request.hot_data_retention,
125 index_replicas=provision_sonicwall_request.index_replicas,
126 ),
127 keys=ProvisionSonicwallKeys(**sonicwall_keys),
128 session=session,
129 )
130 finally:
131 # Always clear the context when done
132 clear_graylog_context()