Create agents.py
taylor_socfortress committed
Jul 10, 2023 at 16:39 UTC
ac96fd8d8cc0b83b485562a3b8c9553a213e533e
1 file changed
+137
backend/app/routes/agents.py
new
+137
@@ -0,0 +1,137 @@
1
+from flask import Blueprint, jsonify, request
2
+from loguru import logger
3
+from app.models.connectors import Connector, WazuhManagerConnector
4
+
5
+from app.services.agents.agents import AgentService, AgentSyncService
6
+
7
+from app.services.WazuhManager.universal import UniversalService
8
+from app.services.WazuhManager.agent import WazuhManagerAgentService
9
+from app.services.WazuhManager.vulnerability import VulnerabilityService
10
+
11
+
12
+
13
+bp = Blueprint("agents", __name__)
14
+
15
+
16
+@bp.route("/agents", methods=["GET"])
17
+def get_agents():
18
+ """
19
+ Endpoint to list all available agents.
20
+ It processes each agent to verify the connection and returns the results.
21
+
22
+ Returns:
23
+ json: A JSON response containing the list of all available agents along with their connection verification status.
24
+ """
25
+ service = AgentService()
26
+ universal_service = UniversalService()
27
+ auth_token = universal_service.get_auth_token()
28
+ return jsonify(auth_token)
29
+ agents = service.get_all_agents()
30
+ return agents
31
+
32
+
33
+@bp.route("/agents/<agent_id>", methods=["GET"])
34
+def get_agent(agent_id):
35
+ """
36
+ Endpoint to get the details of a agent.
37
+
38
+ Args:
39
+ id (str): The id of the agent to be fetched.
40
+
41
+ Returns:
42
+ json: A JSON response containing the details of the agent.
43
+ """
44
+ service = AgentService()
45
+ agent = service.get_agent(agent_id)
46
+ return agent
47
+
48
+
49
+@bp.route("/agents/<agent_id>/critical", methods=["POST"])
50
+def mark_as_critical(agent_id):
51
+ """
52
+ Endpoint to mark a agent as critical.
53
+
54
+ Args:
55
+ id (str): The id of the agent to be marked as critical.
56
+
57
+ Returns:
58
+ json: A JSON response containing the updated agent information.
59
+ """
60
+ service = AgentService()
61
+ result = service.mark_agent_as_critical(agent_id)
62
+ return result
63
+
64
+
65
+@bp.route("/agents/<agent_id>/noncritical", methods=["POST"])
66
+def unmark_agent_critical(agent_id):
67
+ """
68
+ Endpoint to unmark a agent as critical.
69
+
70
+ Args:
71
+ id (str): The id of the agent to be unmarked as critical.
72
+
73
+ Returns:
74
+ json: A JSON response containing the updated agent information.
75
+ """
76
+ service = AgentService()
77
+ result = service.mark_agent_as_non_critical(agent_id)
78
+ return result
79
+
80
+
81
+@bp.route("/agents/sync", methods=["POST"])
82
+def sync_agents():
83
+ """
84
+ Endpoint to sync all agents.
85
+
86
+ Returns:
87
+ json: A JSON response containing the updated agent information.
88
+ """
89
+ service = AgentSyncService()
90
+ result = service.sync_agents()
91
+ return jsonify(result)
92
+
93
+
94
+@bp.route("/agents/<agent_id>/delete", methods=["POST"])
95
+def delete_agent(agent_id):
96
+ """
97
+ Endpoint to delete a agent.
98
+
99
+ Args:
100
+ id (str): The id of the agent to be deleted.
101
+
102
+ Returns:
103
+ json: A JSON response containing the updated agent information.
104
+ """
105
+ service = AgentService()
106
+ result = service.delete_agent_db(agent_id)
107
+
108
+ # Delete from WazuhManager
109
+ # Create instance of UniversalService
110
+ universal_service = UniversalService()
111
+
112
+ # Pass universal_service to WazuhManagerAgentService
113
+ agent_service = WazuhManagerAgentService(universal_service)
114
+ agent_deleted = agent_service.delete_agent(agent_id)
115
+
116
+ return result
117
+
118
+
119
+@bp.route("/agents/<agent_id>/vulnerabilities", methods=["GET"])
120
+def get_agent_vulnerabilities(agent_id):
121
+ """
122
+ Endpoint to get the vulnerabilities of a agent.
123
+
124
+ Args:
125
+ id (str): The id of the agent to be fetched.
126
+
127
+ Returns:
128
+ json: A JSON response containing the vulnerabilities of the agent.
129
+ """
130
+ # Create instance of UniversalService
131
+ universal_service = UniversalService()
132
+
133
+ # Pass universal_service to VulnerabilityService
134
+ vulnerability_service = VulnerabilityService(universal_service)
135
+
136
+ agent_vulnerabilities = vulnerability_service.agent_vulnerabilities(agent_id)
137
+ return agent_vulnerabilities