Skip to content

Commit

Permalink
BB: Refactor agent communication check
Browse files Browse the repository at this point in the history
Updated CommunicationAnalyzer to use the /api/agents and /api/machines
endpoints to determine whether or not an agent communicated back to the
island.

Resolves PR #2388
  • Loading branch information
cakekoa authored and mssalvatore committed Oct 3, 2022
1 parent 368ddde commit 57b4ec4
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions envs/monkey_zoo/blackbox/analyzers/communication_analyzer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Iterable
from ipaddress import IPv4Address
from typing import Collection, Iterable

from envs.monkey_zoo.blackbox.analyzers.analyzer import Analyzer
from envs.monkey_zoo.blackbox.analyzers.analyzer_log import AnalyzerLog
Expand All @@ -13,15 +14,22 @@ def __init__(self, island_client: MonkeyIslandClient, machine_ips: Iterable[str]

def analyze_test_results(self):
self.log.clear()
all_monkeys_communicated = True
all_agents_communicated = True
agent_ips = self._get_agent_ips()

for machine_ip in self.machine_ips:
if not self.did_monkey_communicate_back(machine_ip):
self.log.add_entry("Monkey from {} didn't communicate back".format(machine_ip))
all_monkeys_communicated = False
if self._agent_communicated_back(machine_ip, agent_ips):
self.log.add_entry("Agent from {} communicated back".format(machine_ip))
else:
self.log.add_entry("Monkey from {} communicated back".format(machine_ip))
return all_monkeys_communicated
self.log.add_entry("Agent from {} didn't communicate back".format(machine_ip))
all_agents_communicated = False

return all_agents_communicated

def _get_agent_ips(self) -> Collection[IPv4Address]:
agents = self.island_client.get_agents()
machines = self.island_client.get_machines()
return {i.ip for a in agents for i in machines[a.machine_id].network_interfaces}

def did_monkey_communicate_back(self, machine_ip: str):
query = {"ip_addresses": {"$elemMatch": {"$eq": machine_ip}}}
return len(self.island_client.find_monkeys_in_db(query)) > 0
def _agent_communicated_back(self, machine_ip: str, agent_ips: Collection[IPv4Address]) -> bool:
return IPv4Address(machine_ip) in agent_ips

0 comments on commit 57b4ec4

Please sign in to comment.