Create inputs.py
taylor_socfortress committed
Jul 10, 2023 at 16:43 UTC
828110654d554846bd1e37d5990a68075a6aab54
1 file changed
+128
backend/app/services/Graylog/inputs.py
new
+128
@@ -0,0 +1,128 @@
1
+from app.models.agents import (
2
+ AgentMetadata,
3
+ agent_metadata_schema,
4
+ agent_metadatas_schema,
5
+)
6
+from typing import Dict, List
7
+from app import db
8
+from datetime import datetime
9
+import requests
10
+from loguru import logger
11
+from app.models.connectors import connector_factory, Connector, GraylogConnector
12
+from app.services.Graylog.universal import UniversalService
13
+
14
+
15
+class InputsService:
16
+ """
17
+ A service class that encapsulates the logic for pulling index data from Graylog
18
+ """
19
+
20
+ HEADERS: Dict[str, str] = {"X-Requested-By": "CoPilot"}
21
+
22
+ def __init__(self):
23
+ (
24
+ self.connector_url,
25
+ self.connector_username,
26
+ self.connector_password,
27
+ ) = UniversalService().collect_graylog_details("Graylog")
28
+
29
+ def collect_running_inputs(self):
30
+ """
31
+ Collects the running inputs that are managed by Graylog.
32
+
33
+ Returns:
34
+ list: A list containing the inputs.
35
+ """
36
+ if (
37
+ self.connector_url is None
38
+ or self.connector_username is None
39
+ or self.connector_password is None
40
+ ):
41
+ return {"message": "Failed to collect Graylog details", "success": False}
42
+
43
+ running_inputs = self._collect_running_inputs()
44
+
45
+ if running_inputs["success"]:
46
+ return running_inputs
47
+
48
+ def _collect_running_inputs(self) -> Dict[str, object]:
49
+ """
50
+ Collects the running inputs that are managed by Graylog.
51
+
52
+ Returns:
53
+ dict: A dictionary containing the success status, a message and potentially the inputs.
54
+ """
55
+ try:
56
+ running_inputs = requests.get(
57
+ f"{self.connector_url}/api/system/inputstates",
58
+ headers=self.HEADERS,
59
+ auth=(self.connector_username, self.connector_password),
60
+ verify=False,
61
+ )
62
+ inputs_list = []
63
+ for input in running_inputs.json()["states"]:
64
+ inputs_list.append(
65
+ {
66
+ "state": input["state"],
67
+ "title": input["message_input"]["title"],
68
+ "port": input["message_input"]["attributes"]["port"],
69
+ },
70
+ )
71
+ return {
72
+ "message": "Successfully collected running inputs",
73
+ "success": True,
74
+ "inputs": inputs_list,
75
+ }
76
+ except Exception as e:
77
+ logger.error(f"Failed to collect running inputs: {e}")
78
+ return {"message": "Failed to collect running inputs", "success": False}
79
+
80
+ def collect_configured_inputs(self):
81
+ """
82
+ Collects the configured inputs that are managed by Graylog.
83
+
84
+ Returns:
85
+ list: A list containing the inputs.
86
+ """
87
+ if (
88
+ self.connector_url is None
89
+ or self.connector_username is None
90
+ or self.connector_password is None
91
+ ):
92
+ return {"message": "Failed to collect Graylog details", "success": False}
93
+
94
+ configured_inputs = self._collect_configured_inputs()
95
+
96
+ if configured_inputs["success"]:
97
+ return configured_inputs
98
+
99
+ def _collect_configured_inputs(self) -> Dict[str, object]:
100
+ """
101
+ Collects the configured inputs that are managed by Graylog.
102
+
103
+ Returns:
104
+ dict: A dictionary containing the success status, a message and potentially the inputs.
105
+ """
106
+ try:
107
+ configured_inputs = requests.get(
108
+ f"{self.connector_url}/api/system/inputs",
109
+ headers=self.HEADERS,
110
+ auth=(self.connector_username, self.connector_password),
111
+ verify=False,
112
+ )
113
+ configured_inputs_list = []
114
+ for input in configured_inputs.json()["inputs"]:
115
+ configured_inputs_list.append(
116
+ {
117
+ "title": input["title"],
118
+ "port": input["attributes"]["port"],
119
+ },
120
+ )
121
+ return {
122
+ "message": "Successfully collected configured inputs",
123
+ "success": True,
124
+ "configured_inputs": configured_inputs_list,
125
+ }
126
+ except Exception as e:
127
+ logger.error(f"Failed to collect configured inputs: {e}")
128
+ return {"message": "Failed to collect configured inputs", "success": False}