23 tables + alembic_version covering the v1 data model:
- Auth/RBAC (8): users, groups, permissions, user_groups, group_permissions,
invitations, invitation_groups, refresh_tokens.
- MITRE (4): mitre_tactics, mitre_techniques, mitre_subtechniques + the
technique↔tactic many-to-many.
- Templates (4): test_templates, test_template_mitre_tags (3 nullable FKs +
CHECK exactly_one_mitre_fk), scenario_templates, scenario_template_tests
(UUID PK + UNIQUE(scenario_id, position) so a test can appear at multiple
positions).
- Missions (6): missions, mission_members, mission_scenarios, mission_tests,
mission_test_mitre_tags (deliberately denormalised — copies external_id +
name + url, no FK to mitre_* — so a re-sync of the catalogue can't purge
historical tags), mission_categories.
- Evidence/settings/notifications (5): evidence_files, settings (JSONB
value), detection_levels, notifications.
SQLAlchemy 2.x with Mapped[]/mapped_column(), pk_/fk_/ck_/uq_/ix_ naming
convention. Reusable mixins (UuidPkMixin, TimestampMixin, SoftDeleteMixin —
no auto __table_args__ since classes silently clobber the mixin's).
Soft delete: deleted_at + partial indexes ix_<table>_active WHERE deleted_at
IS NULL on 9 tables (users, groups, test_templates, scenario_templates,
missions, mission_scenarios, mission_tests, mission_categories,
evidence_files). Notifications gets ix_..._unread WHERE read_at IS NULL.
CHECK constraints for status / state / opsec_level / mitre_kind enums.
New API endpoint GET /api/v1/diag/db: returns alembic_revision (short hash)
and the public-schema table_count. 503 with {"reachable": false} on a DB
outage. Database card on the SPA home consumes it.
Test stage in backend/Dockerfile (--target test): runtime + dev extras +
tests/. New make test-api spins an ephemeral pytest container against the
live DB on the compose network. backend/tests/test_schema.py: 8 integration
tests (tables, FK pairs, CHECK constraints, partial indexes, alembic-at-head,
negative INSERT proving the exactly_one_mitre_fk CHECK fires).
e2e/tests/m1-db.spec.ts: 4 Playwright tests covering the diag endpoint
contract + the Database card + footer/roadmap labels.
DoD: make clean && make up && make migrate → 23 tables, 32 FKs, 9 CHECKs,
make test-api → 9 passed, make e2e → 12 passed.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
87 lines
3.1 KiB
Python
87 lines
3.1 KiB
Python
"""MITRE ATT&CK reference tables.
|
|
|
|
Read-mostly. Hard delete (no soft-delete) — replaced by the periodic sync job.
|
|
A technique can map to multiple tactics (kill_chain_phases in STIX) hence the
|
|
M2M `technique_tactics` join. Sub-techniques inherit their parent's tactics
|
|
through the parent technique.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
from sqlalchemy import ForeignKey, Index, String, Text, Uuid
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.db.base import Base
|
|
from app.db.mixins import TimestampMixin, UuidPkMixin
|
|
|
|
|
|
class MitreTactic(Base, UuidPkMixin, TimestampMixin):
|
|
__tablename__ = "mitre_tactics"
|
|
|
|
external_id: Mapped[str] = mapped_column(String(16), unique=True, nullable=False)
|
|
short_name: Mapped[str] = mapped_column(String(80), nullable=False)
|
|
name: Mapped[str] = mapped_column(String(120), nullable=False)
|
|
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
url: Mapped[str | None] = mapped_column(String(512), nullable=True)
|
|
|
|
techniques: Mapped[list["MitreTechnique"]] = relationship(
|
|
secondary="mitre_technique_tactics",
|
|
back_populates="tactics",
|
|
)
|
|
|
|
|
|
class MitreTechnique(Base, UuidPkMixin, TimestampMixin):
|
|
__tablename__ = "mitre_techniques"
|
|
|
|
external_id: Mapped[str] = mapped_column(String(16), unique=True, nullable=False)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
url: Mapped[str | None] = mapped_column(String(512), nullable=True)
|
|
|
|
tactics: Mapped[list[MitreTactic]] = relationship(
|
|
secondary="mitre_technique_tactics",
|
|
back_populates="techniques",
|
|
lazy="selectin",
|
|
)
|
|
subtechniques: Mapped[list["MitreSubtechnique"]] = relationship(
|
|
back_populates="technique",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
|
|
__table_args__ = (Index("ix_mitre_techniques_name", "name"),)
|
|
|
|
|
|
class MitreSubtechnique(Base, UuidPkMixin, TimestampMixin):
|
|
__tablename__ = "mitre_subtechniques"
|
|
|
|
external_id: Mapped[str] = mapped_column(String(16), unique=True, nullable=False)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
url: Mapped[str | None] = mapped_column(String(512), nullable=True)
|
|
technique_id: Mapped[uuid.UUID] = mapped_column(
|
|
Uuid(as_uuid=True), ForeignKey("mitre_techniques.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
|
|
technique: Mapped[MitreTechnique] = relationship(back_populates="subtechniques")
|
|
|
|
__table_args__ = (Index("ix_mitre_subtechniques_technique_id", "technique_id"),)
|
|
|
|
|
|
class MitreTechniqueTactic(Base):
|
|
"""Many-to-many: a technique can serve several tactics (STIX kill_chain_phases)."""
|
|
|
|
__tablename__ = "mitre_technique_tactics"
|
|
|
|
technique_id: Mapped[uuid.UUID] = mapped_column(
|
|
Uuid(as_uuid=True),
|
|
ForeignKey("mitre_techniques.id", ondelete="CASCADE"),
|
|
primary_key=True,
|
|
)
|
|
tactic_id: Mapped[uuid.UUID] = mapped_column(
|
|
Uuid(as_uuid=True),
|
|
ForeignKey("mitre_tactics.id", ondelete="CASCADE"),
|
|
primary_key=True,
|
|
)
|