From 6dfa34a13379dcbf59fb0cf7bf379fcc945a301f Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 7 Dec 2021 12:26:14 +0200 Subject: [PATCH] Island: add the ability to check if monkey document has parent and retrieve it from the model --- monkey/monkey_island/cc/models/monkey.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/monkey/monkey_island/cc/models/monkey.py b/monkey/monkey_island/cc/models/monkey.py index 9fbf15eb2..778606151 100644 --- a/monkey/monkey_island/cc/models/monkey.py +++ b/monkey/monkey_island/cc/models/monkey.py @@ -21,6 +21,10 @@ from monkey_island.cc.server_utils.consts import DEFAULT_MONKEY_TTL_EXPIRY_DURAT from monkey_island.cc.services.utils.network_utils import local_ip_addresses +class ParentNotFoundError(Exception): + """Raise when trying to get a parent of monkey that doesn't have one""" + + class Monkey(Document): """ This class has 2 main section: @@ -96,6 +100,18 @@ class Monkey(Document): monkey_is_dead = True return monkey_is_dead + def has_parent(self): + for p in self.parent: + if p[0] != self.guid: + return True + return False + + def get_parent(self): + if self.has_parent(): + Monkey.objects(guid=self.parent[0][0]).first() + else: + raise ParentNotFoundError + def get_os(self): os = "unknown" if self.description.lower().find("linux") != -1: