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