main
py 107 lines 3.86 KB
Raw
1 from fastapi import APIRouter
2 from fastapi import Depends
3 from fastapi import Security
4 from loguru import logger
5 from sqlalchemy.ext.asyncio import AsyncSession
6
7 from app.auth.routes.auth import AuthHandler
8 from app.db.db_session import get_db
9 from app.integrations.modules.schema.huntress import CollectHuntress
10 from app.integrations.modules.schema.huntress import HuntressAuthKeys
11 from app.integrations.modules.schema.huntress import InvokeHuntressRequest
12 from app.integrations.modules.schema.huntress import InvokeHuntressResponse
13 from app.integrations.modules.services.huntress import post_to_copilot_huntress_module
14 from app.integrations.routes import find_customer_integration
15 from app.integrations.utils.utils import extract_auth_keys
16 from app.integrations.utils.utils import get_customer_integration_response
17 from app.utils import get_connector_attribute
18
19 module_huntress_router = APIRouter()
20
21
22 async def get_huntress_auth_keys(customer_integration) -> HuntressAuthKeys:
23 """
24 Extract the Huntress authentication keys from the CustomerIntegration.
25
26 Args:
27 customer_integration (CustomerIntegration): The CustomerIntegration containing the
28 Huntress authentication keys.
29
30 Returns:
31 HuntressAuthKeys: The extracted Huntress authentication keys.
32 """
33 huntress_auth_keys = extract_auth_keys(
34 customer_integration,
35 service_name="Huntress",
36 )
37
38 return HuntressAuthKeys(**huntress_auth_keys)
39
40
41 async def get_collect_huntress_data(huntress_request, session, auth_keys):
42 return CollectHuntress(
43 integration="huntress",
44 customer_code=huntress_request.customer_code,
45 graylog_host=await get_connector_attribute(
46 connector_name="Event Shipper",
47 column_name="connector_url",
48 session=session,
49 ),
50 graylog_port=await get_connector_attribute(
51 connector_name="Event Shipper",
52 column_name="connector_extra_data",
53 session=session,
54 ),
55 wazuh_indexer_host=await get_connector_attribute(
56 connector_id=1,
57 column_name="connector_url",
58 session=session,
59 ),
60 wazuh_indexer_username=await get_connector_attribute(
61 connector_id=1,
62 column_name="connector_username",
63 session=session,
64 ),
65 wazuh_indexer_password=await get_connector_attribute(
66 connector_id=1,
67 column_name="connector_password",
68 session=session,
69 ),
70 api_key=auth_keys.API_KEY,
71 api_secret=auth_keys.API_SECRET,
72 )
73
74
75 @module_huntress_router.post(
76 "",
77 response_model=InvokeHuntressResponse,
78 description="Invoke the Huntress module.",
79 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
80 )
81 async def collect_huntress_route(huntress_request: InvokeHuntressRequest, session: AsyncSession = Depends(get_db)):
82 """Pull down Huntress Events."""
83 try:
84 customer_integration_response = await get_customer_integration_response(
85 huntress_request.customer_code,
86 session,
87 )
88
89 customer_integration = await find_customer_integration(
90 huntress_request.customer_code,
91 huntress_request.integration_name,
92 customer_integration_response,
93 )
94
95 auth_keys = await get_huntress_auth_keys(customer_integration)
96
97 collect_huntress_data = await get_collect_huntress_data(huntress_request, session, auth_keys)
98
99 logger.info(f"Collecting Huntress Events for {huntress_request.customer_code}")
100
101 await post_to_copilot_huntress_module(data=collect_huntress_data)
102
103 except Exception as e:
104 logger.error(f"Error during DB session: {str(e)}")
105 return InvokeHuntressResponse(success=False, message=str(e))
106
107 return InvokeHuntressResponse(success=True, message="Huntress Events collected successfully.")