| 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.bitdefender.schema.provision import BitdefenderCustomerDetails |
| 12 | from app.integrations.bitdefender.schema.provision import ProvisionBitdefenderAuthKeys |
| 13 | from app.integrations.bitdefender.schema.provision import ProvisionBitdefenderRequest |
| 14 | from app.integrations.bitdefender.schema.provision import ProvisionBitdefenderResponse |
| 15 | from app.integrations.bitdefender.services.provision import provision_bitdefender |
| 16 | from app.integrations.routes import find_customer_integration |
| 17 | from app.integrations.routes import get_customer_integrations_by_customer_code |
| 18 | from app.integrations.schema import CustomerIntegrations |
| 19 | from app.integrations.schema import CustomerIntegrationsResponse |
| 20 | |
| 21 | integration_bitdefender_router = APIRouter() |
| 22 | |
| 23 | |
| 24 | async def get_customer_integration_response( |
| 25 | customer_code: str, |
| 26 | session: AsyncSession, |
| 27 | ) -> CustomerIntegrationsResponse: |
| 28 | """ |
| 29 | Retrieves the integration response for a customer. |
| 30 | |
| 31 | Args: |
| 32 | customer_code (str): The code of the customer. |
| 33 | session (AsyncSession): The async session object for database operations. |
| 34 | |
| 35 | Returns: |
| 36 | CustomerIntegrationsResponse: The integration response for the customer. |
| 37 | |
| 38 | Raises: |
| 39 | HTTPException: If the customer integration settings are not found. |
| 40 | """ |
| 41 | customer_integration_response = await get_customer_integrations_by_customer_code( |
| 42 | customer_code, |
| 43 | session, |
| 44 | ) |
| 45 | if customer_integration_response.available_integrations == []: |
| 46 | raise HTTPException( |
| 47 | status_code=404, |
| 48 | detail="Customer integration settings not found.", |
| 49 | ) |
| 50 | return customer_integration_response |
| 51 | |
| 52 | |
| 53 | def extract_bitdefender_auth_keys( |
| 54 | customer_integration: CustomerIntegrations, |
| 55 | ) -> Dict[str, str]: |
| 56 | """ |
| 57 | Extracts the authentication keys for Bitdefender integration from the given customer integration. |
| 58 | |
| 59 | Args: |
| 60 | customer_integration (CustomerIntegrations): The customer integration object. |
| 61 | |
| 62 | Returns: |
| 63 | Dict[str, str]: A dictionary containing the authentication keys for Bitdefender integration. |
| 64 | |
| 65 | Raises: |
| 66 | HTTPException: If no authentication keys are found for Bitdefender integration. |
| 67 | """ |
| 68 | bitdefender_auth_keys = {} |
| 69 | for subscription in customer_integration.integration_subscriptions: |
| 70 | if subscription.integration_service.service_name == "BitDefender": |
| 71 | for auth_key in subscription.integration_auth_keys: |
| 72 | bitdefender_auth_keys[auth_key.auth_key_name] = auth_key.auth_value |
| 73 | if not bitdefender_auth_keys: |
| 74 | raise HTTPException( |
| 75 | status_code=404, |
| 76 | detail="No auth keys found for Bitdefender integration. Please create auth keys for Bitdefender integration.", |
| 77 | ) |
| 78 | return bitdefender_auth_keys |
| 79 | |
| 80 | |
| 81 | @integration_bitdefender_router.post( |
| 82 | "/provision", |
| 83 | response_model=ProvisionBitdefenderResponse, |
| 84 | description="Provision Bitdefender integration for a customer.", |
| 85 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 86 | ) |
| 87 | async def provision_bitdefender_route( |
| 88 | provision_bitdefender_request: ProvisionBitdefenderRequest, |
| 89 | session: AsyncSession = Depends(get_db), |
| 90 | ) -> ProvisionBitdefenderResponse: |
| 91 | """ |
| 92 | Provisions Bitdefender integration for a customer. |
| 93 | |
| 94 | Args: |
| 95 | provision_bitdefender_request (ProvisionBitdefenderRequest): The request object containing the necessary information for provisioning. |
| 96 | session (AsyncSession, optional): The database session. Defaults to Depends(get_db). |
| 97 | |
| 98 | Returns: |
| 99 | ProvisionBitdefenderResponse: The response object containing the result of the provisioning. |
| 100 | """ |
| 101 | customer_integration_response = await get_customer_integration_response( |
| 102 | provision_bitdefender_request.customer_code, |
| 103 | session, |
| 104 | ) |
| 105 | |
| 106 | customer_integration = await find_customer_integration( |
| 107 | provision_bitdefender_request.customer_code, |
| 108 | provision_bitdefender_request.integration_name, |
| 109 | customer_integration_response, |
| 110 | ) |
| 111 | |
| 112 | bitdefender_auth_keys = extract_bitdefender_auth_keys(customer_integration) |
| 113 | |
| 114 | auth_keys = ProvisionBitdefenderAuthKeys(**bitdefender_auth_keys) |
| 115 | |
| 116 | return await provision_bitdefender( |
| 117 | customer_details=BitdefenderCustomerDetails( |
| 118 | customer_code=provision_bitdefender_request.customer_code, |
| 119 | customer_name=customer_integration.customer_name, |
| 120 | protocal_type="Tcp", |
| 121 | syslog_port=int(auth_keys.GRAYLOG_PORT), |
| 122 | hot_data_retention=provision_bitdefender_request.hot_data_retention, |
| 123 | index_replicas=provision_bitdefender_request.index_replicas, |
| 124 | ), |
| 125 | keys=auth_keys, |
| 126 | session=session, |
| 127 | ) |