Skip to content

Commit 5179b5d

Browse files
Add error checking for HTTP error codes to all API calls
1 parent 55e77bf commit 5179b5d

4 files changed

Lines changed: 117 additions & 56 deletions

File tree

chain/create.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,20 @@ def create_scans(policy_uuid, scan_name, description, policy_id, folder_id,
5757
payload = re.sub('\s+', ' ', payload)
5858
logger.info(f'Payload is: {payload}')
5959

60-
url = "https://cloud.tenable.com/scans"
61-
response = requests.request("POST", url, data=payload, headers=headers)
62-
63-
logger.info(f'The scan \"{scan_name}\" has been created')
64-
# A delay is not required, but is good to tread lightly on the API
65-
# and avoice rate limiting isues. See the following URL:
66-
# https://developer.tenable.com/docs/rate-limiting
67-
logger.info('Pausing for 1 second before the next API call....')
68-
time.sleep(1)
60+
try:
61+
url = "https://cloud.tenable.com/scans"
62+
response = requests.request("POST", url, data=payload, headers=headers)
63+
response.raise_for_status()
64+
logger.info(f'The scan \"{scan_name}\" has been created')
65+
# A delay is not required, but is good to tread lightly on the API
66+
# and avoice rate limiting isues. See the following URL:
67+
# https://developer.tenable.com/docs/rate-limiting
68+
logger.info('Pausing for 1 second before the next API call....')
69+
time.sleep(1)
70+
71+
except requests.HTTPError as e:
72+
logger.info(f'ERROR - {e}')
73+
sys.exit()
6974
# End function
7075

7176
# main function

chain/delete.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ def get_scan_names(headers):
4646
# Function to get scan id's
4747
def get_scan_ids(headers):
4848
"""Get scanner ID's from IO"""
49-
url = "https://cloud.tenable.com/scans"
50-
response = requests.request("GET", url, headers=headers)
51-
pretty_json = json.loads(response.text)
52-
data = (json.dumps(pretty_json, indent=2))
53-
data_dict = json.loads(data)
49+
try:
50+
url = "https://cloud.tenable.com/scans"
51+
response = requests.request("GET", url, headers=headers)
52+
pretty_json = json.loads(response.text)
53+
data = (json.dumps(pretty_json, indent=2))
54+
data_dict = json.loads(data)
55+
56+
except requests.HTTPError as e:
57+
logger.info(f'ERROR - {e}')
58+
sys.exit()
5459

5560
cntr = 0
5661
for scan in data_dict['scans']:
@@ -67,9 +72,15 @@ def delete_scans(headers):
6772
if scan_name in scan_id_dict:
6873
scan_id = scan_id_dict[scan_name]
6974
scan_name = '"'.join([scan_name])
70-
url = f"https://cloud.tenable.com/scans/{scan_id}"
71-
response = requests.request("DELETE", url, headers=headers)
72-
logger.info(f'Deleting scan "{scan_name}" with scan_id {scan_id}')
75+
try:
76+
url = f"https://cloud.tenable.com/scans/{scan_id}"
77+
response = requests.request("DELETE", url, headers=headers)
78+
response.raise_for_status()
79+
logger.info(f'Deleting scan "{scan_name}" with scan_id {scan_id}')
80+
81+
except requests.HTTPError as e:
82+
logger.info(f'ERROR - {e}')
83+
sys.exit()
7384
else:
7485
logger.info('This scan does not exist in IO. Skipping ' \
7586
f'DELETE API call for: "{scan_name}"')

chain/info.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# Import libraries
55
import json
6+
import sys
67
import logging
78
from logging.config import fileConfig
89
import requests
@@ -25,10 +26,14 @@
2526
def get_folders(headers):
2627
"""Get folder ID's from IO"""
2728
url = "https://cloud.tenable.com/folders"
28-
response = requests.request("GET", url, headers=headers)
29-
pretty_json = json.loads(response.text)
30-
data = (json.dumps(pretty_json, indent=2))
31-
data_dict = json.loads(data)
29+
try:
30+
response = requests.request("GET", url, headers=headers)
31+
pretty_json = json.loads(response.text)
32+
data = (json.dumps(pretty_json, indent=2))
33+
data_dict = json.loads(data)
34+
except requests.HTTPError as e:
35+
logger.info(f'ERROR - {e}')
36+
sys.exit()
3237

3338
for policies in data_dict:
3439
length = len(data_dict['folders'])
@@ -51,10 +56,14 @@ def get_folders(headers):
5156
def get_scanners(headers):
5257
"""Get scanner ID's from IO"""
5358
url = "https://cloud.tenable.com/scanners"
54-
response = requests.request("GET", url, headers=headers)
55-
pretty_json = json.loads(response.text)
56-
data = (json.dumps(pretty_json, indent=2))
57-
data_dict = json.loads(data)
59+
try:
60+
response = requests.request("GET", url, headers=headers)
61+
pretty_json = json.loads(response.text)
62+
data = (json.dumps(pretty_json, indent=2))
63+
data_dict = json.loads(data)
64+
except requests.HTTPError as e:
65+
logger.info(f'ERROR - {e}')
66+
sys.exit()
5867

5968
for scanners in data_dict:
6069
length = len(data_dict['scanners'])
@@ -72,10 +81,14 @@ def get_scanners(headers):
7281
def get_policies(headers):
7382
"""Get policy ID's from IO"""
7483
url = "https://cloud.tenable.com/policies"
75-
response = requests.request("GET", url, headers=headers)
76-
pretty_json = json.loads(response.text)
77-
data = (json.dumps(pretty_json, indent=2))
78-
data_dict = json.loads(data)
84+
try:
85+
response = requests.request("GET", url, headers=headers)
86+
pretty_json = json.loads(response.text)
87+
data = (json.dumps(pretty_json, indent=2))
88+
data_dict = json.loads(data)
89+
except requests.HTTPError as e:
90+
logger.info(f'ERROR - {e}')
91+
sys.exit()
7992

8093
for policies in data_dict:
8194
length = len(data_dict['policies'])
@@ -97,10 +110,14 @@ def get_policies(headers):
97110
def get_tags(headers):
98111
"""Get tag data from IO for using in scans.ini"""
99112
url = "https://cloud.tenable.com/tags/values"
100-
response = requests.request("GET", url, headers=headers)
101-
pretty_json = json.loads(response.text)
102-
data = (json.dumps(pretty_json, indent=2))
103-
data_dict = json.loads(data)
113+
try:
114+
response = requests.request("GET", url, headers=headers)
115+
pretty_json = json.loads(response.text)
116+
data = (json.dumps(pretty_json, indent=2))
117+
data_dict = json.loads(data)
118+
except requests.HTTPError as e:
119+
logger.info(f'ERROR - {e}')
120+
sys.exit()
104121

105122
for values in data_dict:
106123
length = len(data_dict['values'])

chain/run.py

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ def get_scan_names():
5252
# Function to run scan
5353
def 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

Comments
 (0)