8
8
from tests .monkey_island import (
9
9
InMemoryAgentConfigurationRepository ,
10
10
InMemoryAgentEventRepository ,
11
- InMemoryAgentPluginRepository ,
12
11
InMemoryAgentRepository ,
13
12
)
14
13
14
+ from common import OperatingSystem
15
15
from common .agent_events import (
16
16
AgentShutdownEvent ,
17
17
ExploitationEvent ,
18
18
PasswordRestorationEvent ,
19
19
PropagationEvent ,
20
20
)
21
+ from common .agent_plugins import AgentPluginManifest , AgentPluginType
22
+ from common .hard_coded_exploiter_manifests import HARD_CODED_EXPLOITER_MANIFESTS
21
23
from common .types import SocketAddress
22
24
from monkey_island .cc .models import Agent , CommunicationType , Machine , Node
23
- from monkey_island .cc .repositories import IAgentEventRepository , IAgentRepository
25
+ from monkey_island .cc .repositories import (
26
+ IAgentEventRepository ,
27
+ IAgentPluginRepository ,
28
+ IAgentRepository ,
29
+ )
24
30
from monkey_island .cc .services .reporting .report import ReportService
25
31
26
32
EVENT_1 = AgentShutdownEvent (source = UUID ("2d56f972-78a8-4026-9f47-2dfd550ee207" ), timestamp = 10 )
159
165
},
160
166
]
161
167
168
+ REMEDIATION_SUGGESTION = "Fix it like this!"
169
+
170
+ MOCK_EXPLOITER_NAME = "MockExploiter"
171
+
172
+ PLUGIN_MANIFESTS = {
173
+ AgentPluginType .EXPLOITER : {
174
+ MOCK_EXPLOITER_NAME : AgentPluginManifest (
175
+ name = MOCK_EXPLOITER_NAME ,
176
+ plugin_type = AgentPluginType .EXPLOITER ,
177
+ supported_operating_systems = (OperatingSystem .WINDOWS ,),
178
+ target_operating_systems = (OperatingSystem .WINDOWS ,),
179
+ title = "Mock Exploiter" ,
180
+ description = "Description for mock exploiter" ,
181
+ remediation_suggestion = REMEDIATION_SUGGESTION ,
182
+ link_to_documentation = "htp:/no_link" ,
183
+ safe = True ,
184
+ )
185
+ }
186
+ }
187
+
162
188
163
189
def get_machine_by_id (machine_id ):
164
190
return [machine for machine in MACHINES if machine_id == machine .id ][0 ]
@@ -182,9 +208,16 @@ def agent_event_repository() -> IAgentEventRepository:
182
208
return repo
183
209
184
210
211
+ @pytest .fixture
212
+ def mock_agent_plugin_repository () -> IAgentPluginRepository :
213
+ return MagicMock (spec = IAgentPluginRepository )
214
+
215
+
185
216
@pytest .fixture (autouse = True )
186
217
def report_service (
187
- agent_repository : IAgentRepository , agent_event_repository : IAgentEventRepository
218
+ agent_repository : IAgentRepository ,
219
+ agent_event_repository : IAgentEventRepository ,
220
+ mock_agent_plugin_repository : IAgentPluginRepository ,
188
221
):
189
222
ReportService ._machine_repository = MagicMock ()
190
223
ReportService ._machine_repository .get_machines .return_value = MACHINES
@@ -194,7 +227,7 @@ def report_service(
194
227
ReportService ._node_repository .get_nodes .return_value = NODES
195
228
ReportService ._agent_event_repository = agent_event_repository
196
229
ReportService ._agent_configuration_repository = InMemoryAgentConfigurationRepository ()
197
- ReportService ._agent_plugin_repository = InMemoryAgentPluginRepository ()
230
+ ReportService ._agent_plugin_repository = mock_agent_plugin_repository
198
231
199
232
200
233
def test_get_scanned ():
@@ -238,6 +271,7 @@ def test_report_service_get_latest_event_timestamp():
238
271
def test_report_generation (monkeypatch , agent_event_repository ):
239
272
monkeypatch .setattr (ReportService , "get_issues" , lambda : [])
240
273
monkeypatch .setattr (ReportService , "get_cross_segment_issues" , lambda : [])
274
+ monkeypatch .setattr (ReportService , "get_remediation_suggestions" , lambda _ : {})
241
275
242
276
ReportService .update_report ()
243
277
actual_report = ReportService .get_report ()
@@ -260,3 +294,35 @@ def test_report_generation__no_agent_repository():
260
294
261
295
with pytest .raises (RuntimeError ):
262
296
ReportService .update_report ()
297
+
298
+
299
+ @pytest .mark .parametrize (
300
+ "issues_set, expected_remediation_suggestions" ,
301
+ [
302
+ ({MOCK_EXPLOITER_NAME }, {MOCK_EXPLOITER_NAME : REMEDIATION_SUGGESTION }),
303
+ ({"NonExistingExploiter" }, {}),
304
+ ({}, {}),
305
+ (
306
+ {"SSHExploiter" },
307
+ {"SSHExploiter" : HARD_CODED_EXPLOITER_MANIFESTS ["SSHExploiter" ].remediation_suggestion },
308
+ ),
309
+ (
310
+ {MOCK_EXPLOITER_NAME , "SSHExploiter" },
311
+ {
312
+ "SSHExploiter" : HARD_CODED_EXPLOITER_MANIFESTS [
313
+ "SSHExploiter"
314
+ ].remediation_suggestion ,
315
+ MOCK_EXPLOITER_NAME : REMEDIATION_SUGGESTION ,
316
+ },
317
+ ),
318
+ ],
319
+ )
320
+ def test_report__get_remediation_suggestions (
321
+ issues_set ,
322
+ expected_remediation_suggestions ,
323
+ mock_agent_plugin_repository ,
324
+ ):
325
+ mock_agent_plugin_repository .get_all_plugin_manifests .return_value = PLUGIN_MANIFESTS
326
+ actual_remediation_suggestion = ReportService .get_remediation_suggestions (issues_set )
327
+
328
+ assert actual_remediation_suggestion == expected_remediation_suggestions
0 commit comments