| 1 | from fastapi import APIRouter |
| 2 | from fastapi import Depends |
| 3 | from fastapi import HTTPException |
| 4 | from fastapi import Security |
| 5 | from loguru import logger |
| 6 | from sqlalchemy.ext.asyncio import AsyncSession |
| 7 | from sqlalchemy.future import select |
| 8 | |
| 9 | from app.auth.models.users import User |
| 10 | from app.auth.utils import AuthHandler |
| 11 | from app.db.db_session import get_db |
| 12 | from app.db.universal_models import Customers |
| 13 | from app.middleware.customer_access import customer_access_handler |
| 14 | from app.siem.schema.event_sources import EventSourceCreate |
| 15 | from app.siem.schema.event_sources import EventSourceDeleteResponse |
| 16 | from app.siem.schema.event_sources import EventSourceOperationResponse |
| 17 | from app.siem.schema.event_sources import EventSourceResponse |
| 18 | from app.siem.schema.event_sources import EventSourcesListResponse |
| 19 | from app.siem.schema.event_sources import EventSourceUpdate |
| 20 | from app.siem.services.event_sources import create_event_source |
| 21 | from app.siem.services.event_sources import delete_event_source |
| 22 | from app.siem.services.event_sources import get_event_sources_by_customer |
| 23 | from app.siem.services.event_sources import update_event_source |
| 24 | |
| 25 | event_sources_router = APIRouter() |
| 26 | |
| 27 | |
| 28 | async def verify_customer_exists(customer_code: str, db: AsyncSession) -> None: |
| 29 | result = await db.execute( |
| 30 | select(Customers).filter(Customers.customer_code == customer_code), |
| 31 | ) |
| 32 | if not result.scalars().first(): |
| 33 | raise HTTPException( |
| 34 | status_code=404, |
| 35 | detail=f"Customer with customer_code {customer_code} not found", |
| 36 | ) |
| 37 | |
| 38 | |
| 39 | @event_sources_router.get( |
| 40 | "/{customer_code}", |
| 41 | response_model=EventSourcesListResponse, |
| 42 | description="Get all event sources for a customer", |
| 43 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst", "customer_user"))], |
| 44 | ) |
| 45 | async def get_event_sources_endpoint( |
| 46 | customer_code: str, |
| 47 | current_user: User = Depends(AuthHandler().get_current_user), |
| 48 | db: AsyncSession = Depends(get_db), |
| 49 | ) -> EventSourcesListResponse: |
| 50 | logger.info(f"Getting event sources for customer {customer_code}") |
| 51 | if not await customer_access_handler.check_customer_access(current_user, customer_code, db): |
| 52 | raise HTTPException(status_code=403, detail=f"Access denied to customer {customer_code}") |
| 53 | await verify_customer_exists(customer_code, db) |
| 54 | event_sources = await get_event_sources_by_customer(customer_code, db) |
| 55 | return EventSourcesListResponse( |
| 56 | event_sources=[EventSourceResponse.from_orm(es) for es in event_sources], |
| 57 | success=True, |
| 58 | message="Event sources retrieved successfully", |
| 59 | ) |
| 60 | |
| 61 | |
| 62 | @event_sources_router.post( |
| 63 | "", |
| 64 | response_model=EventSourceOperationResponse, |
| 65 | description="Create a new event source for a customer", |
| 66 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 67 | ) |
| 68 | async def create_event_source_endpoint( |
| 69 | event_source: EventSourceCreate, |
| 70 | db: AsyncSession = Depends(get_db), |
| 71 | ) -> EventSourceOperationResponse: |
| 72 | logger.info(f"Creating event source for customer {event_source.customer_code}") |
| 73 | await verify_customer_exists(event_source.customer_code, db) |
| 74 | created = await create_event_source(event_source, db) |
| 75 | return EventSourceOperationResponse( |
| 76 | event_source=EventSourceResponse.from_orm(created), |
| 77 | success=True, |
| 78 | message="Event source created successfully", |
| 79 | ) |
| 80 | |
| 81 | |
| 82 | @event_sources_router.put( |
| 83 | "/{event_source_id}", |
| 84 | response_model=EventSourceOperationResponse, |
| 85 | description="Update an existing event source", |
| 86 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 87 | ) |
| 88 | async def update_event_source_endpoint( |
| 89 | event_source_id: int, |
| 90 | update_data: EventSourceUpdate, |
| 91 | db: AsyncSession = Depends(get_db), |
| 92 | ) -> EventSourceOperationResponse: |
| 93 | logger.info(f"Updating event source {event_source_id}") |
| 94 | updated = await update_event_source(event_source_id, update_data, db) |
| 95 | return EventSourceOperationResponse( |
| 96 | event_source=EventSourceResponse.from_orm(updated), |
| 97 | success=True, |
| 98 | message="Event source updated successfully", |
| 99 | ) |
| 100 | |
| 101 | |
| 102 | @event_sources_router.delete( |
| 103 | "/{event_source_id}", |
| 104 | response_model=EventSourceDeleteResponse, |
| 105 | description="Delete an event source", |
| 106 | dependencies=[Security(AuthHandler().require_any_scope("admin"))], |
| 107 | ) |
| 108 | async def delete_event_source_endpoint( |
| 109 | event_source_id: int, |
| 110 | db: AsyncSession = Depends(get_db), |
| 111 | ) -> EventSourceDeleteResponse: |
| 112 | logger.info(f"Deleting event source {event_source_id}") |
| 113 | await delete_event_source(event_source_id, db) |
| 114 | return EventSourceDeleteResponse( |
| 115 | success=True, |
| 116 | message="Event source deleted successfully", |
| 117 | ) |