main
py 97 lines 3.33 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.duo import CollectDuo
10 from app.integrations.modules.schema.duo import DuoAuthKeys
11 from app.integrations.modules.schema.duo import InvokeDuoRequest
12 from app.integrations.modules.schema.duo import InvokeDuoResponse
13 from app.integrations.modules.services.duo import post_to_copilot_duo_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_duo_router = APIRouter()
20
21
22 async def get_duo_auth_keys(customer_integration) -> DuoAuthKeys:
23 """
24 Extract the Duo authentication keys from the CustomerIntegration.
25
26 Args:
27 customer_integration (CustomerIntegration): The CustomerIntegration containing the
28 Duo authentication keys.
29
30 Returns:
31 DuoAuthKeys: The extracted Duo authentication keys.
32 """
33 duo_auth_keys = extract_auth_keys(
34 customer_integration,
35 service_name="DUO",
36 )
37
38 return DuoAuthKeys(**duo_auth_keys)
39
40
41 async def get_collect_duo_data(duo_request, session, auth_keys):
42 return CollectDuo(
43 integration="duo",
44 customer_code=duo_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 integration_key=auth_keys.INTEGRATION_KEY,
56 secret_key=auth_keys.SECRET_KEY,
57 api_host=auth_keys.API_HOSTNAME,
58 api_endpoint="/admin/v2/logs/authentication",
59 range="15m",
60 )
61
62
63 @module_duo_router.post(
64 "",
65 response_model=InvokeDuoResponse,
66 description="Invoke the Duo module.",
67 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
68 )
69 async def collect_duo_route(duo_request: InvokeDuoRequest, session: AsyncSession = Depends(get_db)):
70 """Pull down Duo Events."""
71 logger.info(f"Invoke Duo Request: {duo_request}")
72 try:
73 customer_integration_response = await get_customer_integration_response(
74 duo_request.customer_code,
75 session,
76 )
77
78 # ! SWITCH TO CAPS DUE TO HOW THAT IS STORED IN DB ! #
79 duo_request.integration_name = "DUO"
80
81 customer_integration = await find_customer_integration(
82 duo_request.customer_code,
83 duo_request.integration_name,
84 customer_integration_response,
85 )
86
87 auth_keys = await get_duo_auth_keys(customer_integration)
88
89 collect_duo_data = await get_collect_duo_data(duo_request, session, auth_keys)
90
91 await post_to_copilot_duo_module(data=collect_duo_data)
92
93 except Exception as e:
94 logger.error(f"Error during DB session: {str(e)}")
95 return InvokeDuoResponse(success=False, message=str(e))
96
97 return InvokeDuoResponse(success=True, message="Duo Events collected successfully.")