@@ -99,16 +99,21 @@ def get_ip_info_from_slips(ip_address: str, db) -> (float, float):
9999 """
100100
101101 # poll new info from redis
102- ip_info = db .get_ip_info (ip_address )
103-
104- # There is a bug in the database where sometimes False is returned when key is not found. Correctly, dictionary
105- # should be always returned, even if it is empty. This check cannot be simplified to `if not ip_info`, because I
106- # want the empty dictionary to be handled by the read data function.
107- # TODO: when database is fixed and doesn't return booleans, remove this IF statement
108- if ip_info is False :
102+ # convert it to float
103+ # the higher the score, the more malicious this ip
104+ try :
105+ if threat_level := db .get_ip_info (ip_address , "threat_level" ):
106+ slips_score : float = threat_levels [threat_level ]
107+ else :
108+ slips_score = float (db .get_ip_info (ip_address , "score" ))
109+ except (KeyError , ValueError ):
109110 return None , None
110111
111- slips_score , slips_confidence = read_data_from_ip_info (ip_info )
112+ try :
113+ slips_confidence = float (db .get_ip_info (ip_address , "confidence" ))
114+ except ValueError :
115+ pass
116+
112117 # check that both values were provided
113118 # TODO by Martin: Dita does not handle scenario when only confidence is None, is it intentional?
114119 return (
@@ -118,36 +123,6 @@ def get_ip_info_from_slips(ip_address: str, db) -> (float, float):
118123 )
119124
120125
121- # parse data from redis
122- def read_data_from_ip_info (ip_info : dict ) -> (float , float ):
123- """
124- Get score and confidence from the data that is saved in Redis.
125-
126- :param ip_info: The redis data for one IP address
127- :return: Tuple with score and confidence. If data is not there,
128- (None, None) is returned instead.
129- """
130- # the higher the score, the more malicious this ip
131- try :
132- if "threat_level" in ip_info :
133- score = threat_levels [ip_info ["threat_level" ]]
134- else :
135- score = ip_info ["score" ]
136-
137- confidence = ip_info ["confidence" ]
138- try :
139- confidence = float (confidence )
140- except ValueError :
141- # sometimes the confidence is stored as a float,
142- # and sometimes it's stored like this 'confidence: 0.6'
143- # #TODO see what stores it in the second format instead of this try except
144- confidence = float (confidence .split ()[- 1 ])
145-
146- return float (score ), confidence
147- except (KeyError , TypeError ):
148- return None , None
149-
150-
151126#
152127# SEND COMMUNICATION TO GO
153128#
0 commit comments