2026-05-26 10:59:14 +02:00
|
|
|
"""Simulation model."""
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import enum
|
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
|
|
|
|
|
|
from backend.app.extensions import db
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SimulationStatus(str, enum.Enum):
|
|
|
|
|
PENDING = "pending"
|
|
|
|
|
IN_PROGRESS = "in_progress"
|
|
|
|
|
REVIEW_REQUIRED = "review_required"
|
|
|
|
|
DONE = "done"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Simulation(db.Model): # type: ignore[name-defined]
|
|
|
|
|
__tablename__ = "simulations"
|
|
|
|
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
engagement_id = db.Column(
|
|
|
|
|
db.Integer,
|
|
|
|
|
db.ForeignKey("engagements.id", ondelete="CASCADE"),
|
|
|
|
|
nullable=False,
|
|
|
|
|
index=True,
|
|
|
|
|
)
|
|
|
|
|
name = db.Column(db.String(255), 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
|
|
|
techniques = db.Column(db.JSON, nullable=False, default=list)
|
feat(backend): sprint 4 — tactic_ids + done guard + engagement auto-status
- Simulation model: add tactic_ids JSON column (nullable=False, default=[])
- Migration 0004: ADD COLUMN tactic_ids (server_default='[]', no batch needed)
- mitre.py: add _TACTIC_IDS map, lookup_tactic(), get_tactic_name()
- simulation_workflow.py: done guard (409) before RBAC; SOC gate += tactic_ids;
_resolve_tactic_ids() validates against hardcoded map; auto-transition += tactic_ids;
transition done→review_required is Reopen (all 3 roles); _maybe_activate_engagement hook
- serializers.py: _enrich_tactics() → serialize_simulation adds tactics:[{id,name}]
- test_simulations_tactics.py: valid/invalid/dedup/SOC gate/auto-transition/no-bundle
- test_simulations_done_readonly.py: 409 all roles, Reopen all roles, invalid transitions, after-reopen ok
- test_engagement_lifecycle.py: planned→active on auto-transition, already active/closed unchanged, migration 0004 round-trip
- Updated test_simulations_patch.py + test_simulations_workflow.py for AC-18 behavior
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-27 19:52:02 +02:00
|
|
|
tactic_ids = db.Column(db.JSON, nullable=False, default=list)
|
2026-05-26 10:59:14 +02:00
|
|
|
description = db.Column(db.Text, nullable=True)
|
|
|
|
|
commands = db.Column(db.Text, nullable=True)
|
|
|
|
|
prerequisites = db.Column(db.Text, nullable=True)
|
|
|
|
|
executed_at = db.Column(db.DateTime, nullable=True)
|
|
|
|
|
execution_result = db.Column(db.Text, nullable=True)
|
|
|
|
|
log_source = db.Column(db.Text, nullable=True)
|
|
|
|
|
logs = db.Column(db.Text, nullable=True)
|
|
|
|
|
soc_comment = db.Column(db.Text, nullable=True)
|
|
|
|
|
incident_number = db.Column(db.String(128), nullable=True)
|
|
|
|
|
status = db.Column(
|
|
|
|
|
db.Enum(SimulationStatus, name="simulation_status"),
|
|
|
|
|
nullable=False,
|
|
|
|
|
default=SimulationStatus.PENDING,
|
|
|
|
|
)
|
|
|
|
|
created_at = db.Column(
|
|
|
|
|
db.DateTime, nullable=False, default=lambda: datetime.now(UTC)
|
|
|
|
|
)
|
|
|
|
|
updated_at = db.Column(db.DateTime, nullable=True)
|
|
|
|
|
created_by_id = db.Column(
|
|
|
|
|
db.Integer,
|
|
|
|
|
db.ForeignKey("users.id", ondelete="RESTRICT"),
|
|
|
|
|
nullable=False,
|
|
|
|
|
index=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
engagement = db.relationship(
|
|
|
|
|
"Engagement",
|
|
|
|
|
backref=db.backref("simulations", cascade="all, delete-orphan", lazy="dynamic"),
|
|
|
|
|
)
|
|
|
|
|
created_by = db.relationship("User", backref="simulations", lazy="joined")
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"<Simulation {self.id} {self.name!r}>"
|