@@ -57,25 +57,21 @@ def http_request(url, headers, method, body=None):
5757
5858 log (f"HTTP request: { method } { url } " )
5959
60- try :
61- conn = http .client .HTTPSConnection (hostname , context = ssl ._create_unverified_context ())
62- conn .request (method , path , body = body , headers = headers )
60+ conn = http .client .HTTPSConnection (hostname , context = ssl ._create_unverified_context ())
61+ conn .request (method , path , body = body , headers = headers )
6362
64- response = conn .getresponse ()
65- response_data = response .read ().decode ("utf-8" )
63+ response = conn .getresponse ()
64+ response_data = response .read ().decode ("utf-8" )
6665
67- conn .close ()
66+ conn .close ()
6867
68+ # Don't log response body for /v2/tokens endpoint to avoid exposing bearer tokens
69+ if '/v2/tokens' in url and response .status == 200 :
70+ log (f"HTTP response: { response .status } { response .reason } " )
71+ else :
6972 log (f"HTTP response: { response .status } { response .reason } - { response_data } " )
7073
71- return {
72- "status" : response .status ,
73- "reason" : response .reason ,
74- "data" : response_data
75- }
76- except Exception as e :
77- log (f"Failed to send HTTP request: { e } " )
78- return None
74+ return response .status , response_data
7975
8076
8177def get_bearer_token (cspm_base_url , api_key , aqua_secret , tstmp ):
@@ -95,51 +91,42 @@ def get_bearer_token(cspm_base_url, api_key, aqua_secret, tstmp):
9591 "Content-Type" : "application/json"
9692 }
9793
98- response = http_request (tokens_url , headers , method , body )
99- if response is None :
100- raise Exception ("Failed to get Bearer token: HTTP request failed" )
101-
102- if response ["status" ] not in [200 , 201 ]:
103- raise Exception (f"Failed to get Bearer token: { response ['data' ]} " )
104-
105- json_object = json .loads (response ["data" ].strip ())
106- if json_object .get ('status' ) != 200 :
107- error_msg = json_object .get ('message' , 'Unknown error' )
108- raise Exception (f"Tokens API failed: { error_msg } " )
94+ status , data = http_request (tokens_url , headers , method , body )
95+ if status not in [200 , 201 ]:
96+ raise Exception (f"Failed to get Bearer token: { data } " )
10997
98+ json_object = json .loads (data .strip ())
11099 return json_object ['data' ]
111100
112101
113102def cspm_request_with_fallback (cspm_base_url , path , headers , method , body , api_key , aqua_secret , tstmp ):
114103 """Make CSPM request with automatic token authentication fallback"""
115104 url = cspm_base_url + path
116- response = http_request (url , headers , method , body )
117-
118- if response is None :
119- raise ValueError ("HTTP request failed" )
105+ original_status , original_data = http_request (url , headers , method , body )
120106
121107 # Attempt fallback for 401/403 errors
122- if response [ "status" ] in [401 , 403 ]:
123- log (f"Token fallback: API key authentication failed with status { response [ 'status' ] } , attempting Bearer token fallback" )
108+ if original_status in [401 , 403 ]:
109+ log (f"Token fallback: API key authentication failed with status { original_status } , attempting Bearer token fallback" )
124110 try :
125111 bearer_token = get_bearer_token (cspm_base_url , api_key , aqua_secret , tstmp )
126-
112+
127113 fallback_headers = {
128114 "Authorization" : f"Bearer { bearer_token } " ,
129115 "X-Timestamp" : tstmp ,
130116 "Content-Type" : "application/json"
131117 }
132-
133- response = http_request (url , fallback_headers , method , body )
134- if response [ "status" ] in [200 , 201 ]:
118+
119+ fallback_status , fallback_data = http_request (url , fallback_headers , method , body )
120+ if fallback_status in [200 , 201 ]:
135121 log ("Token fallback: Bearer token authentication succeeded" )
122+ return fallback_status , fallback_data
136123 else :
137- log (f"Token fallback: Bearer token authentication failed with status { response [ 'status' ] } " )
124+ log (f"Token fallback: Bearer token authentication failed with status { fallback_status } " )
138125 except Exception as e :
139126 log (f"Token fallback failed: { e } " )
140127 # Return original response if fallback fails
141128
142- return response
129+ return original_status , original_data
143130
144131
145132def get_cspm_key_id (aqua_api_key , aqua_secret , cspm_url , role_arn ):
@@ -152,15 +139,12 @@ def get_cspm_key_id(aqua_api_key, aqua_secret, cspm_url, role_arn):
152139 "X-Timestamp" : tstmp
153140 }
154141
155- response = cspm_request_with_fallback (cspm_url , "/v2/keys" , headers , "GET" , '' , aqua_api_key , aqua_secret , tstmp )
142+ status , data = cspm_request_with_fallback (cspm_url , "/v2/keys" , headers , "GET" , '' , aqua_api_key , aqua_secret , tstmp )
156143
157- if response is None :
158- raise ValueError (f"HTTP request failed while getting CSPM key ID for { role_arn } " )
144+ if status not in [ 200 , 201 ] :
145+ raise ValueError (f"Failed to get CSPM key ID: { data } " )
159146
160- if response ["status" ] not in [200 , 201 ]:
161- raise ValueError (f"Failed to get CSPM key ID: { response ['data' ]} " )
162-
163- json_object = json .loads (response ["data" ].strip ())
147+ json_object = json .loads (data .strip ())
164148 for key in json_object ['data' ]:
165149 if key ['role_arn' ] == role_arn :
166150 return key ['id' ]
@@ -213,12 +197,8 @@ def trigger_discovery():
213197 "X-Timestamp" : tstmp
214198 }
215199
216- response = http_request (url = f"{ ac_url } /discover/{ cloud } " , headers = headers , method = "POST" , body = body )
217-
218- if response is None :
219- raise ValueError ("Discovery request failed" )
220-
221- return response
200+ status , data = http_request (url = f"{ ac_url } /discover/{ cloud } " , headers = headers , method = "POST" , body = body )
201+ return {"status" : status , "data" : data }
222202
223203
224204def update_credentials ():
@@ -233,7 +213,7 @@ def update_credentials():
233213
234214 cspm_headers = {"X-API-Key" : aqua_api_key , "X-Signature" : cspm_sig , "X-Timestamp" : tstmp }
235215
236- cspm_response = cspm_request_with_fallback (cspm_url , f"/v2/keys/{ cspm_key_id } " , cspm_headers , "PUT" , cspm_body , aqua_api_key , aqua_secret , tstmp )
216+ cspm_status , cspm_data = cspm_request_with_fallback (cspm_url , f"/v2/keys/{ cspm_key_id } " , cspm_headers , "PUT" , cspm_body , aqua_api_key , aqua_secret , tstmp )
237217
238218 ac_body = json .dumps ({
239219 "cloud_account_id" : aws_account_id ,
@@ -250,12 +230,8 @@ def update_credentials():
250230
251231 ac_headers = {"X-API-Key" : aqua_api_key , "X-Authenticate-Api-Key-Signature" : ac_sig , "X-Tokens-Signature" : tokens_signature , "X-Timestamp" : tstmp }
252232
253- ac_response = http_request (ac_url + f"/discover/update-credentials/{ cloud } " , ac_headers , "PUT" , ac_body )
254-
255- if ac_response is None :
256- raise ValueError ("Update credentials request failed" )
257-
258- return cspm_response
233+ ac_status , ac_data = http_request (ac_url + f"/discover/update-credentials/{ cloud } " , ac_headers , "PUT" , ac_body )
234+ return {"status" : cspm_status , "data" : cspm_data }
259235
260236
261237def main ():
0 commit comments