ec26183e2e
- ruff.toml: select E/W/F/I/N rules, line-length=120, drop legacy ignores - Auto-fix: sort 82 import blocks (isort), remove 29 unused imports, strip 6 trailing-whitespace blank lines in docstrings - main.py: move setup_logging and settings imports to top (E402) - errors.py: noqa N818 on DDD exception names (96 call sites, safe) - intel_service.py: noqa N817 for universal ET alias - atomic/elastic/sigma import services: move _MAX_UNCOMPRESSED_SIZE and _MAX_ENTRIES to module level (N806) - compliance_import_service.py: move SAMPLE_CONTROLS / CIS_CONTROLS to module level; wrap long description strings (N806 + E501) - snapshot_service.py: move STATUS_ORDER dict to module level (N806) - sigma_import_service.py: remove dead dedup_key expression (F841) - threat_actor_import_service.py: remove dead stix_to_actor expression (F841) - data_source.py, seed_demo.py, campaign_scheduler_service.py, lolbas_import_service.py: wrap lines exceeding 120 chars (E501) - d3fend_import_service.py: per-file E501 ignore (data file with long strings) All 439 unit tests pass. ruff check app/ → All checks passed! Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
"""D3FEND endpoints — defensive technique listings, mappings, and import trigger."""
|
|
|
|
import logging
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.database import get_db
|
|
from app.dependencies.auth import get_current_user, require_role
|
|
from app.models.user import User
|
|
from app.services.d3fend_import_service import (
|
|
import_d3fend_mappings,
|
|
import_d3fend_techniques,
|
|
)
|
|
from app.services.d3fend_query_service import (
|
|
get_defenses_for_attack_technique,
|
|
list_d3fend_tactics,
|
|
)
|
|
from app.services.d3fend_query_service import (
|
|
list_defensive_techniques as list_defensive_techniques_svc,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
router = APIRouter(prefix="/d3fend", tags=["d3fend"])
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# GET /d3fend — List all defensive techniques
|
|
# ---------------------------------------------------------------------------
|
|
|
|
@router.get("")
|
|
def list_defensive_techniques(
|
|
tactic: Optional[str] = Query(None),
|
|
search: Optional[str] = Query(None),
|
|
offset: int = Query(0, ge=0),
|
|
limit: int = Query(50, ge=1, le=200),
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""List all D3FEND defensive techniques with optional filters."""
|
|
return list_defensive_techniques_svc(
|
|
db, tactic=tactic, search=search, offset=offset, limit=limit
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# GET /d3fend/tactics — List all D3FEND tactics
|
|
# ---------------------------------------------------------------------------
|
|
|
|
@router.get("/tactics")
|
|
def list_d3fend_tactics_endpoint(
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Return a list of all D3FEND tactics with counts."""
|
|
return list_d3fend_tactics(db)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# GET /d3fend/for-technique/{mitre_id} — Defenses for a technique
|
|
# ---------------------------------------------------------------------------
|
|
|
|
@router.get("/for-technique/{mitre_id}")
|
|
def get_defenses_for_attack_technique_endpoint(
|
|
mitre_id: str,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Get all D3FEND defensive techniques mapped to a given ATT&CK technique."""
|
|
return get_defenses_for_attack_technique(db, mitre_id)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# POST /d3fend/import — Trigger D3FEND import (admin only)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
@router.post("/import")
|
|
def trigger_d3fend_import(
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(require_role("admin")),
|
|
):
|
|
"""Import D3FEND techniques and ATT&CK mappings. Admin only."""
|
|
tech_result = import_d3fend_techniques(db)
|
|
mapping_result = import_d3fend_mappings(db)
|
|
|
|
return {
|
|
"techniques": tech_result,
|
|
"mappings": mapping_result,
|
|
}
|