Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /Ap6pack_malwar /src /malwar /api /routes /campaigns.py
| """Campaign API endpoints.""" | |
| from __future__ import annotations | |
| import json | |
| from fastapi import APIRouter, Depends, HTTPException | |
| from pydantic import BaseModel | |
| from malwar.api.auth import require_api_key # noqa: F401 — kept for backward compat | |
| from malwar.api.rbac import require_campaign_manage | |
| router = APIRouter() | |
| # --------------------------------------------------------------------------- | |
| # Response models | |
| # --------------------------------------------------------------------------- | |
| class CampaignResponse(BaseModel): | |
| id: str | |
| name: str | |
| description: str | |
| first_seen: str | |
| last_seen: str | |
| attributed_to: str | None = None | |
| iocs: list[str] | |
| total_skills_affected: int | |
| status: str | |
| class CampaignDetailResponse(CampaignResponse): | |
| signature_count: int = 0 | |
| # --------------------------------------------------------------------------- | |
| # Helpers | |
| # --------------------------------------------------------------------------- | |
| def _row_to_response(row: dict) -> CampaignResponse: | |
| iocs_raw = row.get("iocs", "[]") | |
| iocs = json.loads(iocs_raw) if isinstance(iocs_raw, str) else iocs_raw | |
| return CampaignResponse( | |
| id=row["id"], | |
| name=row["name"], | |
| description=row["description"], | |
| first_seen=row["first_seen"], | |
| last_seen=row["last_seen"], | |
| attributed_to=row.get("attributed_to"), | |
| iocs=iocs, | |
| total_skills_affected=row.get("total_skills_affected", 0), | |
| status=row["status"], | |
| ) | |
| # --------------------------------------------------------------------------- | |
| # Endpoints | |
| # --------------------------------------------------------------------------- | |
| async def list_campaigns( | |
| _auth: object = Depends(require_campaign_manage), | |
| ) -> list[CampaignResponse]: | |
| """List all active campaigns.""" | |
| from malwar.storage.database import get_db | |
| from malwar.storage.repositories.campaigns import CampaignRepository | |
| db = await get_db() | |
| repo = CampaignRepository(db) | |
| rows = await repo.list_active() | |
| return [_row_to_response(row) for row in rows] | |
| async def get_campaign( | |
| campaign_id: str, | |
| _auth: object = Depends(require_campaign_manage), | |
| ) -> CampaignDetailResponse: | |
| """Retrieve a single campaign with details.""" | |
| from malwar.storage.database import get_db | |
| from malwar.storage.repositories.campaigns import CampaignRepository | |
| db = await get_db() | |
| repo = CampaignRepository(db) | |
| row = await repo.get(campaign_id) | |
| if row is None: | |
| raise HTTPException( | |
| status_code=404, detail=f"Campaign {campaign_id} not found" | |
| ) | |
| iocs_raw = row.get("iocs", "[]") | |
| iocs = json.loads(iocs_raw) if isinstance(iocs_raw, str) else iocs_raw | |
| # Count associated signatures | |
| cursor = await db.execute( | |
| "SELECT COUNT(*) FROM signatures WHERE campaign_id = ?", (campaign_id,) | |
| ) | |
| count_row = await cursor.fetchone() | |
| signature_count = count_row[0] if count_row else 0 | |
| return CampaignDetailResponse( | |
| id=row["id"], | |
| name=row["name"], | |
| description=row["description"], | |
| first_seen=row["first_seen"], | |
| last_seen=row["last_seen"], | |
| attributed_to=row.get("attributed_to"), | |
| iocs=iocs, | |
| total_skills_affected=row.get("total_skills_affected", 0), | |
| status=row["status"], | |
| signature_count=signature_count, | |
| ) | |
Xet Storage Details
- Size:
- 3.53 kB
- Xet hash:
- 3ba3b742e1b9ca4a44758a1a24bbf5b3426e055817651d267946859472486af5
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.