@@ -52,8 +52,14 @@ def get_scan_names():
5252# Function to run scan
5353def run_scan (scan_id , headers ):
5454 """Run scans defined in scans.ini config file"""
55- url = f"https://cloud.tenable.com/scans/{ scan_id } /launch"
56- response = requests .request ("POST" , url , headers = headers )
55+ try :
56+ url = f"https://cloud.tenable.com/scans/{ scan_id } /launch"
57+ response = requests .request ("POST" , url , headers = headers )
58+ response .raise_for_status ()
59+
60+ except requests .HTTPError as e :
61+ logger .info (f'ERROR - { e } ' )
62+ sys .exit ()
5763# End function
5864
5965# Funtion to check scan status
@@ -63,10 +69,15 @@ def check_scan_status(scan_id, scan_status, headers):
6369 while scan_status == 'running' or scan_status == 'pending' or \
6470 scan_status == 'None' :
6571 url = f"https://cloud.tenable.com/scans/{ scan_id } "
66- response = requests .request ("GET" , url , headers = headers )
67- pretty_json = json .loads (response .text )
68- data3 = (json .dumps (pretty_json , indent = 2 ))
69- data_dict3 = json .loads (data3 )
72+ try :
73+ response = requests .request ("GET" , url , headers = headers )
74+ pretty_json = json .loads (response .text )
75+ data3 = (json .dumps (pretty_json , indent = 2 ))
76+ data_dict3 = json .loads (data3 )
77+
78+ except requests .HTTPError as e :
79+ logger .info (f'ERROR - { e } ' )
80+ sys .exit ()
7081
7182 # Don't need a condition for 'None' as scan status is now either
7283 # pending, running, or completed
@@ -102,13 +113,19 @@ def main(access_key, secret_key):
102113 # Call function to get scan names from scans.ini file
103114 get_scan_names ()
104115 # Get list of scans
105- url = "https://cloud.tenable.com/scans"
106- response = requests .request ("GET" , url , headers = headers )
107- pretty_json = json .loads (response .text )
108- data = (json .dumps (pretty_json , indent = 2 ))
109- data_dict = json .loads (data )
110- length = len (data_dict ['scans' ])
111- logger .info (f'Number of total scans in IO instance is: { length } ' )
116+ try :
117+ url = "https://cloud.tenable.com/scans"
118+ response = requests .request ("GET" , url , headers = headers )
119+ pretty_json = json .loads (response .text )
120+ data = (json .dumps (pretty_json , indent = 2 ))
121+ data_dict = json .loads (data )
122+ length = len (data_dict ['scans' ])
123+ response .raise_for_status ()
124+ logger .info (f'Number of total scans in IO instance is: { length } ' )
125+
126+ except requests .HTTPError as e :
127+ logger .info (f'ERROR - { e } ' )
128+ sys .exit ()
112129
113130 # Beginning of the section that runs scans
114131 logger .info ('Running the following scans in a chained manner,' \
@@ -161,23 +178,34 @@ def main(access_key, secret_key):
161178 # Populate previous_scan_history_list so we can check later if a second
162179 # instantiation of the scripts still has any scans running
163180 for scan_id in chained_scan_id_list :
164- url = f"https://cloud.tenable.com/scans/{ scan_id } "
165- response = requests .request ("GET" , url , headers = headers )
166- pretty_json = json .loads (response .text )
167- data2 = (json .dumps (pretty_json , indent = 2 ))
168- data_dict2 = json .loads (data2 )
181+ try :
182+ url = f"https://cloud.tenable.com/scans/{ scan_id } "
183+ response = requests .request ("GET" , url , headers = headers )
184+ pretty_json = json .loads (response .text )
185+ data2 = (json .dumps (pretty_json , indent = 2 ))
186+ data_dict2 = json .loads (data2 )
187+
188+ except requests .HTTPError as e :
189+ logger .info (f'ERROR - { e } ' )
190+ sys .exit ()
191+
169192 if data_dict2 ['history' ] != []:
170193 previous_scan_history_list .append ( \
171194 data_dict2 ['history' ][0 ]['status' ])
172195
173196 # Iterate over the chained_scan_id_list again, this time using
174197 # the list previous_scan_history_list built above
175198 for scan_id in chained_scan_id_list :
176- url = f"https://cloud.tenable.com/scans/{ scan_id } "
177- response = requests .request ("GET" , url , headers = headers )
178- pretty_json = json .loads (response .text )
179- data4 = (json .dumps (pretty_json , indent = 2 ))
180- data_dict4 = json .loads (data4 )
199+ try :
200+ url = f"https://cloud.tenable.com/scans/{ scan_id } "
201+ response = requests .request ("GET" , url , headers = headers )
202+ pretty_json = json .loads (response .text )
203+ data4 = (json .dumps (pretty_json , indent = 2 ))
204+ data_dict4 = json .loads (data4 )
205+
206+ except requests .HTTPError as e :
207+ logger .info (f'ERROR - { e } ' )
208+ sys .exit ()
181209
182210 if data_dict4 ['history' ] != []:
183211 dict_val_as_str = str (data_dict4 ['history' ][0 ]['status' ])
0 commit comments