feat(backend): sprint 3 — multi-technique simulations + MITRE matrix
- Simulation model: replace mitre_technique_id/name scalars with techniques JSON column [{id, name}]
- Alembic migration 0003: add techniques, backfill from scalars, drop old columns (reversible)
- MITRE service: add get_tactics(), lookup_name(), get_matrix() with canonical tactic order and sub-technique nesting
- serializer: enrich techniques with tactics from service at serialize time (graceful empty tactics if bundle outdated)
- simulation_workflow: PATCH now accepts technique_ids list, validates against bundle, deduplicates preserving order, auto-transitions on non-empty list
- simulations API: add GET /api/mitre/matrix endpoint (503 if bundle absent)
- test_mitre.py: updated _reset_mitre fixture, added T1059.006 sub-technique, 14 new tests for get_tactics/lookup_name/get_matrix/matrix endpoint
- test_simulations_techniques.py: 20 new tests covering AC-13.1 to AC-13.5 (create, PATCH, dedup, auto-transition, SOC blocked, migration backfill logic)
Total: 161 tests passing. ruff clean. mypy: no new errors.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
"""replace scalar MITRE columns with techniques JSON array
|
||||
|
||||
Revision ID: 0003
|
||||
Revises: 0002
|
||||
Create Date: 2026-05-27 00:00:00.000000
|
||||
"""
|
||||
import json
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.sql import column, table, text
|
||||
|
||||
|
||||
revision = "0003"
|
||||
down_revision = "0002"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
# Lightweight table proxies for data migration (no ORM import).
|
||||
_sims = table(
|
||||
"simulations",
|
||||
column("id", sa.Integer),
|
||||
column("mitre_technique_id", sa.String),
|
||||
column("mitre_technique_name", sa.String),
|
||||
column("techniques", sa.Text),
|
||||
)
|
||||
|
||||
|
||||
def upgrade():
|
||||
bind = op.get_bind()
|
||||
|
||||
# 1. Add techniques column (nullable while we backfill).
|
||||
op.add_column("simulations", sa.Column("techniques", sa.Text(), nullable=True))
|
||||
|
||||
# 2. Backfill: scalar → JSON array.
|
||||
rows = bind.execute(
|
||||
text("SELECT id, mitre_technique_id, mitre_technique_name FROM simulations")
|
||||
).fetchall()
|
||||
for row in rows:
|
||||
if row[1]: # mitre_technique_id is not null
|
||||
val = json.dumps([{"id": row[1], "name": row[2] or ""}])
|
||||
else:
|
||||
val = "[]"
|
||||
bind.execute(
|
||||
text("UPDATE simulations SET techniques = :v WHERE id = :id"),
|
||||
{"v": val, "id": row[0]},
|
||||
)
|
||||
|
||||
# 3. Make NOT NULL now that every row has a value.
|
||||
# SQLite doesn't support ALTER COLUMN, so we skip the nullable constraint
|
||||
# change at DDL level — the application model enforces it.
|
||||
|
||||
# 4. Drop old scalar columns.
|
||||
with op.batch_alter_table("simulations") as batch_op:
|
||||
batch_op.drop_column("mitre_technique_id")
|
||||
batch_op.drop_column("mitre_technique_name")
|
||||
|
||||
|
||||
def downgrade():
|
||||
bind = op.get_bind()
|
||||
|
||||
# 1. Re-add scalar columns.
|
||||
with op.batch_alter_table("simulations") as batch_op:
|
||||
batch_op.add_column(sa.Column("mitre_technique_id", sa.String(length=32), nullable=True))
|
||||
batch_op.add_column(sa.Column("mitre_technique_name", sa.String(length=255), nullable=True))
|
||||
|
||||
# 2. Back-fill: take first element of techniques array.
|
||||
rows = bind.execute(text("SELECT id, techniques FROM simulations")).fetchall()
|
||||
for row in rows:
|
||||
techniques = json.loads(row[1] or "[]")
|
||||
if techniques:
|
||||
first = techniques[0]
|
||||
bind.execute(
|
||||
text(
|
||||
"UPDATE simulations SET mitre_technique_id = :tid, mitre_technique_name = :tname WHERE id = :id"
|
||||
),
|
||||
{"tid": first.get("id"), "tname": first.get("name"), "id": row[0]},
|
||||
)
|
||||
|
||||
# 3. Drop techniques column.
|
||||
with op.batch_alter_table("simulations") as batch_op:
|
||||
batch_op.drop_column("techniques")
|
||||
Reference in New Issue
Block a user