Create cases.py
taylor_socfortress committed
Jul 10, 2023 at 16:42 UTC
68a3580edb8df3393469309aa0592b22161351d8
1 file changed
+80
backend/app/services/DFIR_IRIS/cases.py
new
+80
@@ -0,0 +1,80 @@
1
+from typing import Dict
2
+import requests
3
+from loguru import logger
4
+from app.services.DFIR_IRIS.universal import UniversalService
5
+from dfir_iris_client.case import Case
6
+from dfir_iris_client.helper.utils import assert_api_resp
7
+from dfir_iris_client.helper.utils import get_data_from_resp
8
+from dfir_iris_client.session import ClientSession
9
+
10
+
11
+class CasesService:
12
+ """
13
+ A service class that encapsulates the logic for pulling cases from DFIR-IRIS.
14
+ """
15
+
16
+ def __init__(self):
17
+ self.universal_service = UniversalService("DFIR-IRIS")
18
+ session_result = self.universal_service.create_session()
19
+
20
+ if not session_result['success']:
21
+ logger.error(session_result['message'])
22
+ self.iris_session = None
23
+ else:
24
+ self.iris_session = session_result['session']
25
+
26
+ def list_cases(self) -> Dict[str, object]:
27
+ """
28
+ Lists all cases from DFIR-IRIS
29
+
30
+ Returns:
31
+ dict: A dictionary containing the success status, a message and potentially the cases.
32
+ """
33
+ if self.iris_session is None:
34
+ return {
35
+ "success": False,
36
+ "message": "DFIR-IRIS session was not successfully created.",
37
+ }
38
+
39
+ logger.info("Collecting cases from DFIR-IRIS")
40
+ case = Case(session=self.iris_session)
41
+ result = self.universal_service.fetch_and_parse_data(self.iris_session, case.list_cases)
42
+
43
+ if not result["success"]:
44
+ return {"success": False, "message": "Failed to collect cases from DFIR-IRIS"}
45
+
46
+ return {"success": True, "message": "Successfully collected cases from DFIR-IRIS", "cases": result["data"]}
47
+
48
+ def get_case(self, case_id: int) -> bool:
49
+ """
50
+ Gets a case from DFIR-IRIS and returns all the details
51
+
52
+ Returns:
53
+ dict: A dictionary containing the success status, a message and potentially the case.
54
+ """
55
+ if self.iris_session is None:
56
+ return {"success": False, "message": "DFIR-IRIS session was not successfully created."}
57
+
58
+ logger.info(f"Collecting case {case_id} from DFIR-IRIS")
59
+ case = Case(session=self.iris_session)
60
+ result = self.universal_service.fetch_and_parse_data(self.iris_session, case.get_case, case_id)
61
+
62
+ if not result["success"]:
63
+ return {"success": False, "message": f"Failed to collect case {case_id} from DFIR-IRIS"}
64
+
65
+ return {"success": True, "message": f"Successfully collected case {case_id} from DFIR-IRIS", "case": result["data"]}
66
+
67
+ def check_case_id(self, case_id: int) -> bool:
68
+ """
69
+ Checks if a case exists in DFIR-IRIS
70
+
71
+ Returns:
72
+ dict: A dictionary containing the success status, a message and potentially the case.
73
+ """
74
+ return self.get_case(case_id)
75
+
76
+
77
+
78
+
79
+
80
+