8f98bdd273
- 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!
122 lines
3.8 KiB
Python
122 lines
3.8 KiB
Python
"""Compliance endpoints — framework status, reports, and gap analysis.
|
|
|
|
Thin HTTP adapter: delegates all data logic to compliance_service.
|
|
|
|
Provides compliance posture assessment by mapping MITRE ATT&CK technique
|
|
coverage to compliance framework controls.
|
|
"""
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from fastapi.responses import StreamingResponse
|
|
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.compliance_import_service import (
|
|
import_cis_controls_v8_mappings,
|
|
import_nist_800_53_mappings,
|
|
)
|
|
from app.services.compliance_service import (
|
|
build_framework_report_csv,
|
|
get_framework_gaps,
|
|
get_framework_status,
|
|
list_frameworks,
|
|
)
|
|
|
|
router = APIRouter(prefix="/compliance", tags=["compliance"])
|
|
|
|
|
|
# ── GET /compliance/frameworks ────────────────────────────────────────
|
|
|
|
|
|
@router.get("/frameworks")
|
|
def list_frameworks_endpoint(
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""List all available compliance frameworks."""
|
|
return list_frameworks(db)
|
|
|
|
|
|
# ── GET /compliance/frameworks/{id}/status ────────────────────────────
|
|
|
|
|
|
@router.get("/frameworks/{framework_id}/status")
|
|
def framework_status(
|
|
framework_id: str,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Get compliance status for each control in a framework."""
|
|
return get_framework_status(db, framework_id)
|
|
|
|
|
|
# ── GET /compliance/frameworks/{id}/report ────────────────────────────
|
|
|
|
|
|
@router.get("/frameworks/{framework_id}/report")
|
|
def framework_report(
|
|
framework_id: str,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Get the full compliance report (same as status but marked as report)."""
|
|
return get_framework_status(db, framework_id)
|
|
|
|
|
|
# ── GET /compliance/frameworks/{id}/report/csv ────────────────────────
|
|
|
|
|
|
@router.get("/frameworks/{framework_id}/report/csv")
|
|
def framework_report_csv(
|
|
framework_id: str,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Export compliance report as CSV."""
|
|
csv_bytes, filename = build_framework_report_csv(db, framework_id)
|
|
return StreamingResponse(
|
|
iter([csv_bytes]),
|
|
media_type="text/csv",
|
|
headers={
|
|
"Content-Disposition": f"attachment; filename={filename}",
|
|
},
|
|
)
|
|
|
|
|
|
# ── GET /compliance/frameworks/{id}/gaps ──────────────────────────────
|
|
|
|
|
|
@router.get("/frameworks/{framework_id}/gaps")
|
|
def framework_gaps(
|
|
framework_id: str,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Get controls with techniques that are not adequately covered."""
|
|
return get_framework_gaps(db, framework_id)
|
|
|
|
|
|
# ── POST /compliance/import/... ────────────────────────────────────────
|
|
|
|
|
|
@router.post("/import/nist-800-53")
|
|
def import_nist(
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(require_role("admin")),
|
|
):
|
|
"""Import NIST 800-53 Rev 5 mappings (admin only)."""
|
|
result = import_nist_800_53_mappings(db)
|
|
return result
|
|
|
|
|
|
@router.post("/import/cis-controls-v8")
|
|
def import_cis(
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(require_role("admin")),
|
|
):
|
|
"""Import CIS Controls v8 mappings (admin only)."""
|
|
result = import_cis_controls_v8_mappings(db)
|
|
return result
|