9
9
10
10
from api_app .exceptions import AnalyzerRunException
11
11
from api_app .script_analyzers import general
12
- from intel_owl import settings
12
+ from intel_owl import settings , secrets
13
13
14
14
logger = get_task_logger (__name__ )
15
15
@@ -22,10 +22,9 @@ def run(analyzer_name, job_id, observable_name, observable_classification, addit
22
22
"" .format (analyzer_name , job_id , observable_name ))
23
23
report = general .get_basic_report_template (analyzer_name )
24
24
try :
25
-
26
25
try :
27
26
if not os .path .isfile (database_location ):
28
- updater ()
27
+ updater (additional_config_params )
29
28
reader = maxminddb .open_database (database_location )
30
29
maxmind_result = reader .get (observable_name )
31
30
reader .close ()
@@ -62,11 +61,19 @@ def run(analyzer_name, job_id, observable_name, observable_classification, addit
62
61
return report
63
62
64
63
65
- def updater ():
64
+ def updater (additional_config_params ):
66
65
67
66
try :
67
+ api_key_name = additional_config_params .get ('api_key_name' , '' )
68
+ if not api_key_name :
69
+ api_key_name = "MAXMIND_KEY"
70
+ api_key = secrets .get_secret (api_key_name )
71
+ if not api_key :
72
+ raise AnalyzerRunException ("no api key retrieved" )
73
+
68
74
logger .info ("starting download of db from maxmind" )
69
- url = "http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz"
75
+ url = "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key={}" \
76
+ "&suffix=tar.gz" .format (api_key )
70
77
r = requests .get (url )
71
78
if r .status_code >= 300 :
72
79
raise AnalyzerRunException ("failed request for new maxmind db. Status code: {}" .format (r .status_code ))
@@ -82,6 +89,7 @@ def updater():
82
89
today = datetime .datetime .now ().date ()
83
90
counter = 0
84
91
directory_found = False
92
+ downloaded_db_path = ""
85
93
# this is because we do not know the exact date of the db we downloaded
86
94
while counter < 10 or not directory_found :
87
95
date_to_check = today - datetime .timedelta (days = counter )
@@ -96,14 +104,16 @@ def updater():
96
104
else :
97
105
directory_found = True
98
106
99
- if not directory_found :
107
+ if directory_found :
108
+ logger .info ("maxmind directory found {}" .format (downloaded_db_path ))
109
+ else :
100
110
raise AnalyzerRunException ("failed extraction of maxmind db, reached max number of attempts" )
101
111
102
112
logger .info ("ended download of db from maxmind" )
103
113
104
114
except Exception as e :
105
115
traceback .print_exc ()
106
- logger .exception (e )
116
+ logger .exception (str ( e ) )
107
117
108
118
return database_location
109
119
0 commit comments