2
2
Define a Document Schema for the Monkey document.
3
3
4
4
"""
5
- import ring
6
5
from mongoengine import (
7
- BooleanField ,
8
6
DateTimeField ,
9
7
Document ,
10
- DoesNotExist ,
11
8
DynamicField ,
12
9
FloatField ,
13
10
ListField ,
14
11
ReferenceField ,
15
12
StringField ,
16
13
)
17
14
18
- from common .network .network_utils import get_my_ip_addresses_legacy
19
- from monkey_island .cc .models .monkey_ttl import MonkeyTtl , create_monkey_ttl_document
20
- from monkey_island .cc .server_utils .consts import DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS
21
-
22
15
23
16
class Monkey (Document ):
24
17
"""
@@ -31,8 +24,6 @@ class Monkey(Document):
31
24
32
25
# SCHEMA
33
26
guid = StringField (required = True )
34
- should_stop = BooleanField ()
35
- dead = BooleanField ()
36
27
hostname = StringField ()
37
28
ip_addresses = ListField (StringField ())
38
29
launch_time = FloatField ()
@@ -43,70 +34,15 @@ class Monkey(Document):
43
34
# (even with required=False of null=True).
44
35
# See relevant issue: https://github.com/MongoEngine/mongoengine/issues/1904
45
36
parent = ListField (ListField (DynamicField ()))
46
- ttl_ref = ReferenceField (MonkeyTtl )
47
37
tunnel = ReferenceField ("self" )
48
38
49
39
# This field only exists when the monkey is running on an AWS
50
40
aws_instance_id = StringField (required = False )
51
41
52
42
# instance. See https://github.com/guardicore/monkey/issues/426.
53
43
54
- # LOGIC
55
- @staticmethod
56
- def get_single_monkey_by_id (db_id ):
57
- try :
58
- return Monkey .objects .get (id = db_id )
59
- except DoesNotExist as ex :
60
- raise MonkeyNotFoundError ("info: {0} | id: {1}" .format (ex , str (db_id )))
61
-
62
- @staticmethod
63
- # See https://www.python.org/dev/peps/pep-0484/#forward-references
64
- def get_single_monkey_by_guid (monkey_guid ) -> "Monkey" :
65
- try :
66
- return Monkey .objects .get (guid = monkey_guid )
67
- except DoesNotExist as ex :
68
- raise MonkeyNotFoundError ("info: {0} | guid: {1}" .format (ex , str (monkey_guid )))
69
-
70
44
@staticmethod
71
45
def get_latest_modifytime ():
72
46
if Monkey .objects .count () > 0 :
73
47
return Monkey .objects .order_by ("-modifytime" ).first ().modifytime
74
48
return None
75
-
76
- @ring .lru ()
77
- @staticmethod
78
- def get_label_by_id (object_id ):
79
- current_monkey = Monkey .get_single_monkey_by_id (object_id )
80
- label = Monkey .get_hostname_by_id (object_id ) + " : " + current_monkey .ip_addresses [0 ]
81
- local_ips = map (str , get_my_ip_addresses_legacy ())
82
- if len (set (current_monkey .ip_addresses ).intersection (local_ips )) > 0 :
83
- label = "MonkeyIsland - " + label
84
- return label
85
-
86
- @ring .lru ()
87
- @staticmethod
88
- def get_hostname_by_id (object_id ):
89
- """
90
- :param object_id: the object ID of a Monkey in the database.
91
- :return: The hostname of that machine.
92
- :note: Use this and not monkey.hostname for performance - this is lru-cached.
93
- """
94
- return Monkey .get_single_monkey_by_id (object_id ).hostname
95
-
96
- # data has TTL of 1 second. This is useful for rapid calls for report generation.
97
- @ring .lru (expire = 1 )
98
- @staticmethod
99
- def is_monkey (object_id ):
100
- try :
101
- _ = Monkey .get_single_monkey_by_id (object_id )
102
- return True
103
- except : # noqa: E722
104
- return False
105
-
106
- def renew_ttl (self , duration = DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS ):
107
- self .ttl_ref = create_monkey_ttl_document (duration )
108
- self .save ()
109
-
110
-
111
- class MonkeyNotFoundError (Exception ):
112
- pass
0 commit comments