feat(backend): c2 callback history + task import (sprint 8 M4)
Command source decision: extended C2TaskStatus with command: str | None (default None). Added command_name to _GET_TASK_QUERY so get_task() returns command in a single round-trip — no separate history fetch needed on import. 4-line change, zero cascading test impact. adapter.py: - C2TaskStatus: add command: str | None = None field - C2HistoricalTask: new dataclass (display_id, command, params, status, completed, timestamp) for history rows - C2TaskPage.items: typed as list[C2HistoricalTask] (was list[dict]) mythic.py: - _GET_TASK_QUERY: add command_name field - _LIST_CALLBACK_TASKS_QUERY: new query (order_by id desc, limit/offset) - _COUNT_CALLBACK_TASKS_QUERY: new aggregate query for total - get_task(): surfaces command_name as status.command - list_callback_tasks(): two _post() calls (tasks + count), allow_redirects=False fake.py: - _FAKE_HISTORY: frozen deterministic history (cb1=12, cb2=0, cb3=5 tasks) - list_callback_tasks(): serves from _FAKE_HISTORY, pagination applied - get_task(): returns command from _tasks dict api/c2.py: - GET /api/engagements/<eid>/c2/callbacks/<cid>/history: page+page_size defaults 1/25, cap 100, reject <1, 502 on adapter error - POST /api/simulations/<sid>/c2/import: idempotent per (sim,mythic_id) pair, source=import, completed tasks get output+mapping_applied, incomplete tasks stored for poll-on-read pickup, auto-transition pending→in_progress 60 new tests (456 total); pytest/ruff/mypy all green Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -21,6 +21,7 @@ from backend.app.services.c2.adapter import (
|
||||
C2Callback,
|
||||
C2Error,
|
||||
C2Health,
|
||||
C2HistoricalTask,
|
||||
C2TaskPage,
|
||||
C2TaskStatus,
|
||||
decode_response_text,
|
||||
@@ -61,6 +62,7 @@ _GET_TASK_QUERY = """
|
||||
query GetTask($display_id: Int!) {
|
||||
task(where: {display_id: {_eq: $display_id}}) {
|
||||
display_id
|
||||
command_name
|
||||
status
|
||||
completed
|
||||
timestamp
|
||||
@@ -68,6 +70,34 @@ query GetTask($display_id: Int!) {
|
||||
}
|
||||
"""
|
||||
|
||||
_LIST_CALLBACK_TASKS_QUERY = """
|
||||
query ListCallbackTasks($callback_display_id: Int!, $limit: Int!, $offset: Int!) {
|
||||
task(
|
||||
where: {callback: {display_id: {_eq: $callback_display_id}}}
|
||||
order_by: {id: desc}
|
||||
limit: $limit
|
||||
offset: $offset
|
||||
) {
|
||||
display_id
|
||||
command_name
|
||||
params
|
||||
status
|
||||
completed
|
||||
timestamp
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
_COUNT_CALLBACK_TASKS_QUERY = """
|
||||
query CountCallbackTasks($callback_display_id: Int!) {
|
||||
task_aggregate(where: {callback: {display_id: {_eq: $callback_display_id}}}) {
|
||||
aggregate {
|
||||
count
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
_GET_TASK_OUTPUT_QUERY = """
|
||||
query GetTaskOutput($display_id: Int!) {
|
||||
response(
|
||||
@@ -197,6 +227,7 @@ class MythicAdapter(C2Adapter):
|
||||
status=row["status"],
|
||||
completed=bool(row.get("completed", False)),
|
||||
completed_at=completed_at,
|
||||
command=row.get("command_name") or None,
|
||||
)
|
||||
|
||||
def get_task_output(self, task_display_id: int) -> str:
|
||||
@@ -222,4 +253,41 @@ class MythicAdapter(C2Adapter):
|
||||
page: int = 1,
|
||||
page_size: int = 25,
|
||||
) -> C2TaskPage:
|
||||
raise NotImplementedError("M4")
|
||||
"""Return a paginated, most-recent-first history of tasks for a callback."""
|
||||
offset = (page - 1) * page_size
|
||||
try:
|
||||
data = self._post({
|
||||
"query": _LIST_CALLBACK_TASKS_QUERY,
|
||||
"variables": {
|
||||
"callback_display_id": callback_display_id,
|
||||
"limit": page_size,
|
||||
"offset": offset,
|
||||
},
|
||||
})
|
||||
count_data = self._post({
|
||||
"query": _COUNT_CALLBACK_TASKS_QUERY,
|
||||
"variables": {"callback_display_id": callback_display_id},
|
||||
})
|
||||
except requests.RequestException as exc:
|
||||
raise C2Error(str(exc)) from exc
|
||||
|
||||
rows = data.get("data", {}).get("task", [])
|
||||
total: int = (
|
||||
count_data.get("data", {})
|
||||
.get("task_aggregate", {})
|
||||
.get("aggregate", {})
|
||||
.get("count", 0)
|
||||
)
|
||||
|
||||
items = [
|
||||
C2HistoricalTask(
|
||||
display_id=r["display_id"],
|
||||
command=r.get("command_name") or "",
|
||||
params=r.get("params") or None,
|
||||
status=r.get("status") or "",
|
||||
completed=bool(r.get("completed", False)),
|
||||
timestamp=r.get("timestamp") or None,
|
||||
)
|
||||
for r in rows
|
||||
]
|
||||
return C2TaskPage(items=items, total=total, page=page, page_size=page_size)
|
||||
|
||||
Reference in New Issue
Block a user