2026-05-21 20:33:45 +02:00
|
|
|
"""Engagement CRUD endpoints (flat, sprint 0)."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from flask import Blueprint, abort, jsonify
|
chore(backend): mypy strict clean + ruff format pass
Pre-merge sanity per devops checklist (ruff format --check, mypy --strict).
Type fixes:
- ORM models: `Mapped[dict]` → `Mapped[dict[str, Any]]` (audit, scenario, run,
report, ttp, detection.artifact_files_json). Equivalent on Pydantic DTOs
(TtpBase.params_schema_json, ScenarioStepBase.params_override_json).
- Rename `TtpRead.current_version` → `TtpRead.version` to mirror the ORM
column (which itself was renamed in D-009 cleanup).
- Flask blueprints: add `-> ResponseReturnValue` to every view, plus typed
UUID params on `_validate_step_consistency`.
- `templating/filters.py`: rewrite the conditional re2 import so mypy can
narrow the union (`ModuleType | None`); the runtime branch on `_re2 is not
None` removes the unused-ignore that was triggered by warn_unused_ignores.
- `pyproject.toml`: add `flask_login.*` and `pythonjsonlogger.*` to the
`[[tool.mypy.overrides]]` `ignore_missing_imports` list (both ship without
typed marker).
- Misc: drop stale `# type: ignore` comments (`app.py:36`,
`rbac/decorators.py:35`) flagged by `warn_unused_ignores`. Keep
`logging.JsonFormatter` ignore because the symbol exists at runtime but is
not re-exported through the typed surface.
Formatting:
- `ruff format` applied (15 files normalized; line-length unchanged at 100).
Verification on this commit:
- `ruff check` → All checks passed.
- `ruff format --check` → 68 files already formatted.
- `mypy --strict src` → Success: no issues found in 54 source files.
- `pytest tests/unit` → 49 passed.
2026-05-22 05:10:51 +02:00
|
|
|
from flask.typing import ResponseReturnValue
|
2026-05-21 20:33:45 +02:00
|
|
|
from sqlalchemy import select
|
|
|
|
|
|
|
|
|
|
from mimic.api._helpers import jsonify_model, parse_body, parse_uuid
|
|
|
|
|
from mimic.db.models import Engagement
|
|
|
|
|
from mimic.db.types import EngagementStatus
|
|
|
|
|
from mimic.extensions import db
|
|
|
|
|
from mimic.rbac import Permission, require_perm
|
|
|
|
|
from mimic.schemas import EngagementCreate, EngagementRead, EngagementUpdate
|
|
|
|
|
|
|
|
|
|
bp = Blueprint("engagements", __name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.get("")
|
|
|
|
|
@require_perm(Permission.ENGAGEMENT_READ)
|
chore(backend): mypy strict clean + ruff format pass
Pre-merge sanity per devops checklist (ruff format --check, mypy --strict).
Type fixes:
- ORM models: `Mapped[dict]` → `Mapped[dict[str, Any]]` (audit, scenario, run,
report, ttp, detection.artifact_files_json). Equivalent on Pydantic DTOs
(TtpBase.params_schema_json, ScenarioStepBase.params_override_json).
- Rename `TtpRead.current_version` → `TtpRead.version` to mirror the ORM
column (which itself was renamed in D-009 cleanup).
- Flask blueprints: add `-> ResponseReturnValue` to every view, plus typed
UUID params on `_validate_step_consistency`.
- `templating/filters.py`: rewrite the conditional re2 import so mypy can
narrow the union (`ModuleType | None`); the runtime branch on `_re2 is not
None` removes the unused-ignore that was triggered by warn_unused_ignores.
- `pyproject.toml`: add `flask_login.*` and `pythonjsonlogger.*` to the
`[[tool.mypy.overrides]]` `ignore_missing_imports` list (both ship without
typed marker).
- Misc: drop stale `# type: ignore` comments (`app.py:36`,
`rbac/decorators.py:35`) flagged by `warn_unused_ignores`. Keep
`logging.JsonFormatter` ignore because the symbol exists at runtime but is
not re-exported through the typed surface.
Formatting:
- `ruff format` applied (15 files normalized; line-length unchanged at 100).
Verification on this commit:
- `ruff check` → All checks passed.
- `ruff format --check` → 68 files already formatted.
- `mypy --strict src` → Success: no issues found in 54 source files.
- `pytest tests/unit` → 49 passed.
2026-05-22 05:10:51 +02:00
|
|
|
def list_engagements() -> ResponseReturnValue:
|
2026-05-21 20:33:45 +02:00
|
|
|
stmt = select(Engagement).order_by(Engagement.created_at.desc())
|
|
|
|
|
rows = db.session.execute(stmt).scalars().all()
|
|
|
|
|
return jsonify([EngagementRead.model_validate(row).model_dump(mode="json") for row in rows])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.post("")
|
|
|
|
|
@require_perm(Permission.ENGAGEMENT_CREATE)
|
chore(backend): mypy strict clean + ruff format pass
Pre-merge sanity per devops checklist (ruff format --check, mypy --strict).
Type fixes:
- ORM models: `Mapped[dict]` → `Mapped[dict[str, Any]]` (audit, scenario, run,
report, ttp, detection.artifact_files_json). Equivalent on Pydantic DTOs
(TtpBase.params_schema_json, ScenarioStepBase.params_override_json).
- Rename `TtpRead.current_version` → `TtpRead.version` to mirror the ORM
column (which itself was renamed in D-009 cleanup).
- Flask blueprints: add `-> ResponseReturnValue` to every view, plus typed
UUID params on `_validate_step_consistency`.
- `templating/filters.py`: rewrite the conditional re2 import so mypy can
narrow the union (`ModuleType | None`); the runtime branch on `_re2 is not
None` removes the unused-ignore that was triggered by warn_unused_ignores.
- `pyproject.toml`: add `flask_login.*` and `pythonjsonlogger.*` to the
`[[tool.mypy.overrides]]` `ignore_missing_imports` list (both ship without
typed marker).
- Misc: drop stale `# type: ignore` comments (`app.py:36`,
`rbac/decorators.py:35`) flagged by `warn_unused_ignores`. Keep
`logging.JsonFormatter` ignore because the symbol exists at runtime but is
not re-exported through the typed surface.
Formatting:
- `ruff format` applied (15 files normalized; line-length unchanged at 100).
Verification on this commit:
- `ruff check` → All checks passed.
- `ruff format --check` → 68 files already formatted.
- `mypy --strict src` → Success: no issues found in 54 source files.
- `pytest tests/unit` → 49 passed.
2026-05-22 05:10:51 +02:00
|
|
|
def create_engagement() -> ResponseReturnValue:
|
2026-05-21 20:33:45 +02:00
|
|
|
payload = parse_body(EngagementCreate)
|
|
|
|
|
engagement = Engagement(
|
|
|
|
|
client_name=payload.client_name,
|
|
|
|
|
description=payload.description,
|
|
|
|
|
c2_type=payload.c2_type,
|
|
|
|
|
start_date=payload.start_date,
|
|
|
|
|
end_date=payload.end_date,
|
|
|
|
|
status=EngagementStatus.DRAFT,
|
|
|
|
|
)
|
|
|
|
|
db.session.add(engagement)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return jsonify_model(EngagementRead.model_validate(engagement), status=201)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.get("/<eid>")
|
|
|
|
|
@require_perm(Permission.ENGAGEMENT_READ)
|
chore(backend): mypy strict clean + ruff format pass
Pre-merge sanity per devops checklist (ruff format --check, mypy --strict).
Type fixes:
- ORM models: `Mapped[dict]` → `Mapped[dict[str, Any]]` (audit, scenario, run,
report, ttp, detection.artifact_files_json). Equivalent on Pydantic DTOs
(TtpBase.params_schema_json, ScenarioStepBase.params_override_json).
- Rename `TtpRead.current_version` → `TtpRead.version` to mirror the ORM
column (which itself was renamed in D-009 cleanup).
- Flask blueprints: add `-> ResponseReturnValue` to every view, plus typed
UUID params on `_validate_step_consistency`.
- `templating/filters.py`: rewrite the conditional re2 import so mypy can
narrow the union (`ModuleType | None`); the runtime branch on `_re2 is not
None` removes the unused-ignore that was triggered by warn_unused_ignores.
- `pyproject.toml`: add `flask_login.*` and `pythonjsonlogger.*` to the
`[[tool.mypy.overrides]]` `ignore_missing_imports` list (both ship without
typed marker).
- Misc: drop stale `# type: ignore` comments (`app.py:36`,
`rbac/decorators.py:35`) flagged by `warn_unused_ignores`. Keep
`logging.JsonFormatter` ignore because the symbol exists at runtime but is
not re-exported through the typed surface.
Formatting:
- `ruff format` applied (15 files normalized; line-length unchanged at 100).
Verification on this commit:
- `ruff check` → All checks passed.
- `ruff format --check` → 68 files already formatted.
- `mypy --strict src` → Success: no issues found in 54 source files.
- `pytest tests/unit` → 49 passed.
2026-05-22 05:10:51 +02:00
|
|
|
def get_engagement(eid: str) -> ResponseReturnValue:
|
2026-05-21 20:33:45 +02:00
|
|
|
engagement = db.session.get(Engagement, parse_uuid(eid))
|
|
|
|
|
if engagement is None:
|
|
|
|
|
abort(404)
|
|
|
|
|
return jsonify_model(EngagementRead.model_validate(engagement))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.put("/<eid>")
|
|
|
|
|
@require_perm(Permission.ENGAGEMENT_UPDATE)
|
chore(backend): mypy strict clean + ruff format pass
Pre-merge sanity per devops checklist (ruff format --check, mypy --strict).
Type fixes:
- ORM models: `Mapped[dict]` → `Mapped[dict[str, Any]]` (audit, scenario, run,
report, ttp, detection.artifact_files_json). Equivalent on Pydantic DTOs
(TtpBase.params_schema_json, ScenarioStepBase.params_override_json).
- Rename `TtpRead.current_version` → `TtpRead.version` to mirror the ORM
column (which itself was renamed in D-009 cleanup).
- Flask blueprints: add `-> ResponseReturnValue` to every view, plus typed
UUID params on `_validate_step_consistency`.
- `templating/filters.py`: rewrite the conditional re2 import so mypy can
narrow the union (`ModuleType | None`); the runtime branch on `_re2 is not
None` removes the unused-ignore that was triggered by warn_unused_ignores.
- `pyproject.toml`: add `flask_login.*` and `pythonjsonlogger.*` to the
`[[tool.mypy.overrides]]` `ignore_missing_imports` list (both ship without
typed marker).
- Misc: drop stale `# type: ignore` comments (`app.py:36`,
`rbac/decorators.py:35`) flagged by `warn_unused_ignores`. Keep
`logging.JsonFormatter` ignore because the symbol exists at runtime but is
not re-exported through the typed surface.
Formatting:
- `ruff format` applied (15 files normalized; line-length unchanged at 100).
Verification on this commit:
- `ruff check` → All checks passed.
- `ruff format --check` → 68 files already formatted.
- `mypy --strict src` → Success: no issues found in 54 source files.
- `pytest tests/unit` → 49 passed.
2026-05-22 05:10:51 +02:00
|
|
|
def update_engagement(eid: str) -> ResponseReturnValue:
|
2026-05-21 20:33:45 +02:00
|
|
|
engagement = db.session.get(Engagement, parse_uuid(eid))
|
|
|
|
|
if engagement is None:
|
|
|
|
|
abort(404)
|
|
|
|
|
payload = parse_body(EngagementUpdate)
|
|
|
|
|
for field, value in payload.model_dump(exclude_unset=True).items():
|
|
|
|
|
setattr(engagement, field, value)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return jsonify_model(EngagementRead.model_validate(engagement))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.delete("/<eid>")
|
|
|
|
|
@require_perm(Permission.ENGAGEMENT_DELETE)
|
chore(backend): mypy strict clean + ruff format pass
Pre-merge sanity per devops checklist (ruff format --check, mypy --strict).
Type fixes:
- ORM models: `Mapped[dict]` → `Mapped[dict[str, Any]]` (audit, scenario, run,
report, ttp, detection.artifact_files_json). Equivalent on Pydantic DTOs
(TtpBase.params_schema_json, ScenarioStepBase.params_override_json).
- Rename `TtpRead.current_version` → `TtpRead.version` to mirror the ORM
column (which itself was renamed in D-009 cleanup).
- Flask blueprints: add `-> ResponseReturnValue` to every view, plus typed
UUID params on `_validate_step_consistency`.
- `templating/filters.py`: rewrite the conditional re2 import so mypy can
narrow the union (`ModuleType | None`); the runtime branch on `_re2 is not
None` removes the unused-ignore that was triggered by warn_unused_ignores.
- `pyproject.toml`: add `flask_login.*` and `pythonjsonlogger.*` to the
`[[tool.mypy.overrides]]` `ignore_missing_imports` list (both ship without
typed marker).
- Misc: drop stale `# type: ignore` comments (`app.py:36`,
`rbac/decorators.py:35`) flagged by `warn_unused_ignores`. Keep
`logging.JsonFormatter` ignore because the symbol exists at runtime but is
not re-exported through the typed surface.
Formatting:
- `ruff format` applied (15 files normalized; line-length unchanged at 100).
Verification on this commit:
- `ruff check` → All checks passed.
- `ruff format --check` → 68 files already formatted.
- `mypy --strict src` → Success: no issues found in 54 source files.
- `pytest tests/unit` → 49 passed.
2026-05-22 05:10:51 +02:00
|
|
|
def delete_engagement(eid: str) -> ResponseReturnValue:
|
2026-05-21 20:33:45 +02:00
|
|
|
engagement = db.session.get(Engagement, parse_uuid(eid))
|
|
|
|
|
if engagement is None:
|
|
|
|
|
abort(404)
|
|
|
|
|
engagement.status = EngagementStatus.ARCHIVED
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return "", 204
|