diff --git a/backend/src/api/analysis_api.py b/backend/src/api/analysis_api.py index e52fc1f..ca679f1 100644 --- a/backend/src/api/analysis_api.py +++ b/backend/src/api/analysis_api.py @@ -95,7 +95,7 @@ async def analysis_interface_hosts( try: rows = await db.infer_interface_hosts(since=since, limit_per_interface=limit_per_interface) except Exception as exc: - raise HTTPException(status_code=500, detail="Failed to infer interface host mapping") from exc + raise HTTPException(status_code=500, detail=f"Failed to infer interface host mapping: {exc}") from exc interfaces = [InterfaceAttachment(**row) for row in rows] return InterfaceHostAnalysisResponse(since=since, interfaces=interfaces) @@ -138,7 +138,7 @@ async def analysis_interface_host_protocols( limit_protocols_per_host=limit_protocols_per_host, ) except Exception as exc: - raise HTTPException(status_code=500, detail="Failed to infer interface host protocol mapping") from exc + raise HTTPException(status_code=500, detail=f"Failed to infer interface host protocol mapping: {exc}") from exc interfaces = [InterfaceProtocolAttachment(**row) for row in rows] return InterfaceHostProtocolAnalysisResponse(since=since, interfaces=interfaces) diff --git a/backend/src/utilities/database.py b/backend/src/utilities/database.py index bff624a..481bbb5 100644 --- a/backend/src/utilities/database.py +++ b/backend/src/utilities/database.py @@ -642,9 +642,10 @@ class DatabasePool: if self._pool is None: await self.init_pool() - async with self._pool.acquire() as conn: - rows = await conn.fetch( - """ + try: + async with self._pool.acquire() as conn: + rows = await conn.fetch( + """ WITH observations AS ( SELECT ingress_if AS iface, @@ -713,9 +714,12 @@ class DatabasePool: WHERE row_num <= $2 ORDER BY iface, packet_count DESC, last_seen DESC, ip_address, mac_address """, - since, - limit_per_interface, - ) + since, + limit_per_interface, + ) + except Exception: + logger.exception("DB interface-host analysis failed") + raise grouped: Dict[str, List[Dict[str, Any]]] = {} for row in rows: @@ -744,16 +748,17 @@ class DatabasePool: if self._pool is None: await self.init_pool() - async with self._pool.acquire() as conn: - rows = await conn.fetch( - """ + try: + async with self._pool.acquire() as conn: + rows = await conn.fetch( + """ WITH observations AS ( SELECT ingress_if AS iface, src_ip::text AS ip_address, src_mac::text AS mac_address, - COALESCE(NULLIF(app_protocol, ''), NULLIF(app_master_protocol, ''), NULLIF(ip_proto, ''), NULLIF(eth_type, ''), 'UNKNOWN') AS protocol_name, - COALESCE(NULLIF(verdict, ''), 'unknown') AS verdict_name, + COALESCE(NULLIF(app_protocol::text, ''), NULLIF(app_master_protocol::text, ''), NULLIF(ip_proto::text, ''), NULLIF(eth_type::text, ''), 'UNKNOWN') AS protocol_name, + COALESCE(NULLIF(verdict::text, ''), 'unknown') AS verdict_name, timestamp, 'source_on_ingress' AS evidence FROM packets @@ -767,8 +772,8 @@ class DatabasePool: egress_if AS iface, dst_ip::text AS ip_address, dst_mac::text AS mac_address, - COALESCE(NULLIF(app_protocol, ''), NULLIF(app_master_protocol, ''), NULLIF(ip_proto, ''), NULLIF(eth_type, ''), 'UNKNOWN') AS protocol_name, - COALESCE(NULLIF(verdict, ''), 'unknown') AS verdict_name, + COALESCE(NULLIF(app_protocol::text, ''), NULLIF(app_master_protocol::text, ''), NULLIF(ip_proto::text, ''), NULLIF(eth_type::text, ''), 'UNKNOWN') AS protocol_name, + COALESCE(NULLIF(verdict::text, ''), 'unknown') AS verdict_name, timestamp, 'destination_on_egress' AS evidence FROM packets @@ -872,11 +877,14 @@ class DatabasePool: ranked_protocols.packet_count DESC NULLS LAST, ranked_protocols.last_seen DESC NULLS LAST, ranked_protocols.protocol_name - """, - since, - limit_per_interface, - limit_protocols_per_host, - ) + """, + since, + limit_per_interface, + limit_protocols_per_host, + ) + except Exception: + logger.exception("DB interface-host-protocol analysis failed") + raise grouped: Dict[str, Dict[str, Dict[str, Any]]] = {} for row in rows: