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>
2026-05-27 03:56:02 +02:00
|
|
|
"""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
|
2026-05-27 04:25:20 +02:00
|
|
|
from sqlalchemy.sql import text
|
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>
2026-05-27 03:56:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
revision = "0003"
|
|
|
|
|
down_revision = "0002"
|
|
|
|
|
branch_labels = None
|
|
|
|
|
depends_on = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
2026-05-27 04:25:20 +02:00
|
|
|
with op.batch_alter_table("simulations") as batch_op:
|
|
|
|
|
batch_op.alter_column("techniques", existing_type=sa.Text(), nullable=False)
|
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>
2026-05-27 03:56:02 +02:00
|
|
|
|
|
|
|
|
# 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")
|