Create notes.py
taylor_socfortress committed
Jul 10, 2023 at 16:42 UTC
84161f19bcafa56126e2ea00812666574f9fd2bd
1 file changed
+108
backend/app/services/DFIR_IRIS/notes.py
new
+108
@@ -0,0 +1,108 @@
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 NotesService:
12
+ """
13
+ A service class that encapsulates the logic for pulling case notes 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 get_case_notes(self, search_term: str, cid: int) -> Dict[str, object]:
27
+ """
28
+ Gets a case's notes from DFIR-IRIS and return the ID and Title
29
+
30
+ ARGS:
31
+ cid: The case ID to search for
32
+ search_term: The search term to use
33
+
34
+ Returns:
35
+ dict: A dictionary containing the success status, a message and potentially the notes of a given case.
36
+ """
37
+ if self.iris_session is None:
38
+ return {"success": False, "message": "DFIR-IRIS session was not successfully created."}
39
+
40
+ logger.info(f"Collecting case {cid} from DFIR-IRIS")
41
+ case = Case(session=self.iris_session)
42
+ result = self.universal_service.fetch_and_parse_data(self.iris_session, case.search_notes, search_term, cid)
43
+
44
+ if not result["success"]:
45
+ return {"success": False, "message": "Failed to collect notes from DFIR-IRIS"}
46
+
47
+ # Loop through the notes and get the details
48
+ for note in result['data']:
49
+ note_details = self._get_case_note_details(note['note_id'], cid)
50
+ if not note_details['success']:
51
+ return {"success": False, "message": "Failed to collect notes from DFIR-IRIS"}
52
+ note['note_details'] = note_details['notes']
53
+
54
+ return result
55
+
56
+ def _get_case_note_details(self, note_id: int, cid: int) -> Dict[str, object]:
57
+ """
58
+ Gets a case's notes from DFIR-IRIS and returns the note details such as the content
59
+
60
+ ARGS:
61
+ cid: The case ID to search for
62
+ note_id: The note ID to search for
63
+
64
+ Returns:
65
+ dict: A dictionary containing the success status, a message and potentially the notes of a given case.
66
+ """
67
+ if self.iris_session is None:
68
+ return {"success": False, "message": "DFIR-IRIS session was not successfully created."}
69
+
70
+ logger.info(f"Collecting case {cid} from DFIR-IRIS")
71
+ case = Case(session=self.iris_session)
72
+ result = self.universal_service.fetch_and_parse_data(self.iris_session, case.get_note, note_id, cid)
73
+
74
+ if not result["success"]:
75
+ return {"success": False, "message": "Failed to collect notes from DFIR-IRIS"}
76
+
77
+ return {"success": True, "message": "Successfully collected notes from DFIR-IRIS", "notes": result["data"]}
78
+
79
+ def create_case_note(self, cid: int, note_title: str, note_content: str) -> Dict[str, object]:
80
+ """
81
+ Creates a case note in DFIR-IRIS
82
+
83
+ ARGS:
84
+ cid: The case ID to search for
85
+ title: The title of the note
86
+ content: The content of the note
87
+
88
+ Returns:
89
+ dict: A dictionary containing the success status, a message and potentially the notes of a given case.
90
+ """
91
+ if self.iris_session is None:
92
+ return {"success": False, "message": "DFIR-IRIS session was not successfully created."}
93
+
94
+ logger.info(f"Creating case {cid} note in DFIR-IRIS")
95
+ case = Case(session=self.iris_session)
96
+ # Creating Group for New Note
97
+ note_group = self.universal_service.fetch_and_parse_data(self.iris_session, case.add_notes_group, note_title, cid)
98
+
99
+ if not note_group["success"]:
100
+ return {"success": False, "message": "Failed to create note in DFIR-IRIS"}
101
+ note_group_id = note_group['data']['group_id']
102
+ custom_attributes = {}
103
+ result = self.universal_service.fetch_and_parse_data(self.iris_session, case.add_note, note_title, note_content, note_group_id, custom_attributes, cid)
104
+
105
+ if not result["success"]:
106
+ return {"success": False, "message": "Failed to create note in DFIR-IRIS"}
107
+
108
+ return {"success": True, "message": "Successfully created note in DFIR-IRIS", "notes": result["data"]}