Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New task report endpoint and UI changes #1520

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"python.analysis.extraPaths": [
"./turbinia/api/client",
"./turbinia/api/cli"
]
}
87 changes: 62 additions & 25 deletions turbinia/api/api_server_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,44 @@ class testTurbiniaAPIServer(unittest.TestCase):
""" Test Turbinia API server."""

_TASK_TEST_DATA = {
'id': 'c8f73a5bc5084086896023c12c7cc026',
'evidence_name': 'test_data/artifact_disk.dd',
'evidence_id': '084d5904f3d2412b99dc29ed34853a16',
'job_id': '1db0dc47d8f244f5b4fa7e15b8a87861',
'start_time': '2022-04-01T19:15:14.791074Z',
'last_update': '2022-04-01T19:17:14.791074Z',
'name': 'YaraAnalysisTask',
'request_id': '41483253079448e59685d88f37ab91f7',
'requester': 'root',
'group_id': '1234',
'worker_name': '95153920ab11',
'report_data': 'No issues found in crontabs',
'report_priority': 80,
'run_time': 46.003234,
'status': 'No issues found in crontabs',
'saved_paths': '/tmp/worker-log.txt',
'successful': True,
'output_manager': '',
'instance': 'turbinia-jleaniz-test'
'id':
'c8f73a5bc5084086896023c12c7cc026',
'evidence_name':
'test_data/artifact_disk.dd',
'evidence_id':
'084d5904f3d2412b99dc29ed34853a16',
'job_id':
'1db0dc47d8f244f5b4fa7e15b8a87861',
'start_time':
'2022-04-01T19:15:14.791074Z',
'last_update':
'2022-04-01T19:17:14.791074Z',
'name':
'YaraAnalysisTask',
'request_id':
'41483253079448e59685d88f37ab91f7',
'requester':
'root',
'group_id':
'1234',
'worker_name':
'95153920ab11',
'report_data':
'### YaraAnalysisTask (LOW PRIORITY): No issues found in crontabs',
'report_priority':
80,
'run_time':
46.003234,
'status':
'No issues found in crontabs',
'saved_paths':
'/tmp/worker-log.txt',
'successful':
True,
'output_manager':
'',
'instance':
'turbinia-jleaniz-test'
}

_REQUEST_TEST_DATA = {
Expand Down Expand Up @@ -181,8 +200,7 @@ class testTurbiniaAPIServer(unittest.TestCase):
]
}

_REQUEST_REPORT = b'## Request ID: 41483253079448e59685d88f37ab91f7\n* Last Update: 2022-04-01T19:17:14.791074Z\n* Requester: root\n* Reason:\n* Status: successful\n* Failed tasks: 0\n* Running tasks: 0\n* Successful tasks: 1\n* Task Count: 1\n* Queued tasks: 0\n* Evidence Name: test_data/artifact_disk.dd\n* Evidence ID: 084d5904f3d2412b99dc29ed34853a16\n\n### YaraAnalysisTask (LOW PRIORITY): No issues found in crontabs'

_REQUEST_REPORT = '## Request ID: 41483253079448e59685d88f37ab91f7\n* Last Update: 2022-04-01T19:17:14.791074Z\n* Requester: root\n* Reason:\n* Status: successful\n* Failed tasks: 0\n* Running tasks: 0\n* Successful tasks: 1\n* Task Count: 1\n* Queued tasks: 0\n* Evidence Name: test_data/artifact_disk.dd\n* Evidence ID: 084d5904f3d2412b99dc29ed34853a16\n\n### YaraAnalysisTask (LOW PRIORITY): No issues found in crontabs'
_COUNT_SUMMARY = 3

@mock.patch('redis.StrictRedis')
Expand Down Expand Up @@ -252,11 +270,11 @@ def testGetTaskStatus(self, testTaskData):
expected_result_str = json.dumps(expected_result_dict)

redis_client.set(
'TurbiniaTask:41483253079448e59685d88f37ab91f7', expected_result_str)
'TurbiniaTask:c8f73a5bc5084086896023c12c7cc026', expected_result_str)

testTaskData.return_value = [
json.loads(
redis_client.get('TurbiniaTask:41483253079448e59685d88f37ab91f7'))
redis_client.get('TurbiniaTask:c8f73a5bc5084086896023c12c7cc026'))
]

result = self.client.get(f"/api/task/{self._TASK_TEST_DATA.get('id')}")
Expand Down Expand Up @@ -420,10 +438,29 @@ def testGetRequestReport(self, testKeyExists, testTaskData, testRequestData):
]

result = self.client.get(
f"/api/request/report?request_id={self._REQUEST_STATUS_TEST_DATA.get('request_id')}"
f"/api/request/report/{self._REQUEST_STATUS_TEST_DATA.get('request_id')}"
)
result = result.content
self.assertEqual(expected_result, result)
self.assertEqual(expected_result, result.decode())

@mock.patch('turbinia.state_manager.RedisStateManager.get_task')
def testGetTaskReport(self, testTaskData):
"""Test getting task report."""
redis_client = fakeredis.FakeStrictRedis()
input_task = TurbiniaTask().deserialize(self._TASK_TEST_DATA)
task_data_dict = input_task.serialize()
task_data_str = json.dumps(task_data_dict)
expected_result = task_data_dict.get('report_data')

redis_client.set(
'TurbiniaTask:c8f73a5bc5084086896023c12c7cc026', task_data_str)

testTaskData.return_value = json.loads(
redis_client.get('TurbiniaTask:c8f73a5bc5084086896023c12c7cc026'))

result = self.client.get(
f"/api/task/report/{self._TASK_TEST_DATA.get('id')}")
self.assertEqual(expected_result, result.content.decode())

@mock.patch('turbinia.state_manager.RedisStateManager.get_task_data')
def testTaskNotFound(self, testTaskData):
Expand Down
2 changes: 1 addition & 1 deletion turbinia/api/routes/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def get_requests_summary(request: Request):
detail='Error retrieving requests summary') from exception


@router.get('/report')
@router.get('/report/{request_id}')
async def get_request_report(request: Request, request_id: str):
"""Retrieves the MarkDown report of a Turbinia request.

Expand Down
27 changes: 26 additions & 1 deletion turbinia/api/routes/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from collections import OrderedDict

from fastapi import HTTPException, APIRouter
from fastapi.responses import JSONResponse
from fastapi.responses import JSONResponse, PlainTextResponse
from fastapi.requests import Request
from fastapi.encoders import jsonable_encoder

Expand All @@ -29,6 +29,8 @@
from turbinia import state_manager
from turbinia.api.models import workers_status
from turbinia.api.models import tasks_statistics
from turbinia.api.models import request_status
from turbinia.api.cli.turbinia_client.helpers.formatter import TaskMarkdownReport

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -100,3 +102,26 @@ async def get_task_status(request: Request, task_id: str):
raise HTTPException(
status_code=500,
detail='Error retrieving task information') from exception


@router.get('/report/{task_id}')
async def get_task_report(request: Request, task_id: str):
"""Retrieves the MarkDown report of a Turbinia task.

Raises:
HTTPException: if another exception is caught.
"""
try:
task_data = state_manager.get_state_manager().get_task(task_id=task_id)
if not task_data:
raise HTTPException(status_code=404, detail='Task not found.')
markdownreport = TaskMarkdownReport(
request_data=task_data).generate_markdown()

return PlainTextResponse(content=markdownreport, status_code=200)
except (json.JSONDecodeError, TypeError, ValueError, AttributeError,
ValidationError) as exception:
log.error(f'Error retrieving markdown report: {exception!s}', exc_info=True)
raise HTTPException(
status_code=500,
detail='Error retrieving markdown report') from exception
Loading