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!
104 lines
3.0 KiB
Python
104 lines
3.0 KiB
Python
"""Compliance models — frameworks, controls, and technique mappings.
|
|
|
|
Maps compliance frameworks (NIST 800-53, DORA, NIS2, ISO 27001) to
|
|
MITRE ATT&CK techniques, enabling compliance gap analysis.
|
|
"""
|
|
|
|
import uuid
|
|
|
|
from sqlalchemy import (
|
|
Boolean,
|
|
Column,
|
|
DateTime,
|
|
ForeignKey,
|
|
Index,
|
|
String,
|
|
Text,
|
|
UniqueConstraint,
|
|
func,
|
|
)
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class ComplianceFramework(Base):
|
|
"""A compliance framework (e.g. NIST 800-53, ISO 27001)."""
|
|
__tablename__ = "compliance_frameworks"
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
name = Column(String, unique=True, nullable=False)
|
|
version = Column(String, nullable=True)
|
|
description = Column(Text, nullable=True)
|
|
url = Column(String, nullable=True)
|
|
is_active = Column(Boolean, default=True)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
# Relationships
|
|
controls = relationship(
|
|
"ComplianceControl",
|
|
back_populates="framework",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
|
|
|
|
class ComplianceControl(Base):
|
|
"""A control within a compliance framework (e.g. AC-2, PR.AC-1)."""
|
|
__tablename__ = "compliance_controls"
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
framework_id = Column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("compliance_frameworks.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
)
|
|
control_id = Column(String, nullable=False) # e.g. "AC-2"
|
|
title = Column(String, nullable=False)
|
|
description = Column(Text, nullable=True)
|
|
category = Column(String, nullable=True)
|
|
|
|
# Relationships
|
|
framework = relationship("ComplianceFramework", back_populates="controls")
|
|
technique_mappings = relationship(
|
|
"ComplianceControlMapping",
|
|
back_populates="compliance_control",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
|
|
__table_args__ = (
|
|
Index('ix_compliance_controls_framework', 'framework_id'),
|
|
)
|
|
|
|
|
|
class ComplianceControlMapping(Base):
|
|
"""Maps a compliance control to a MITRE ATT&CK technique."""
|
|
__tablename__ = "compliance_control_mappings"
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
compliance_control_id = Column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("compliance_controls.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
)
|
|
technique_id = Column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("techniques.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
)
|
|
|
|
# Relationships
|
|
compliance_control = relationship(
|
|
"ComplianceControl", back_populates="technique_mappings"
|
|
)
|
|
technique = relationship("Technique")
|
|
|
|
__table_args__ = (
|
|
Index('ix_compliance_mappings_control', 'compliance_control_id'),
|
|
Index('ix_compliance_mappings_technique', 'technique_id'),
|
|
UniqueConstraint(
|
|
'compliance_control_id', 'technique_id',
|
|
name='uq_control_technique',
|
|
),
|
|
)
|