@@ -192,7 +192,12 @@ def get_apis(self, **kwargs):
192
192
"""
193
193
try :
194
194
apis = self .db .all ()
195
- result = apis
195
+ parsed_apis = jsonbak .loads (apis )
196
+ # Remove the password from the list of apis
197
+ for api in parsed_apis :
198
+ if "passapi" in api :
199
+ del api ["passapi" ]
200
+ result = jsonbak .dumps (parsed_apis )
196
201
except Exception as e :
197
202
self .logger .error (jsonbak .dumps ({"error" : str (e )}))
198
203
return jsonbak .dumps ({"error" : str (e )})
@@ -349,6 +354,60 @@ def check_connection(self, **kwargs):
349
354
return jsonbak .dumps ({"status" : 400 , "error" : "Cannot connect to the API" })
350
355
return result
351
356
357
+ @expose_page (must_login = False , methods = ['GET' ])
358
+ def check_connection_by_id (self , ** kwargs ):
359
+ """Given an API id we check the connection.
360
+
361
+ Parameters
362
+ ----------
363
+ kwargs : dict
364
+ The request's parameters
365
+ """
366
+ try :
367
+ opt_id = kwargs ["apiId" ]
368
+ current_api = self .get_api (apiId = opt_id )
369
+ current_api_json = jsonbak .loads (jsonbak .loads (current_api ))
370
+ opt_username = str (current_api_json ["data" ]["userapi" ])
371
+ opt_password = str (current_api_json ["data" ]["passapi" ])
372
+ opt_base_url = str (current_api_json ["data" ]["url" ])
373
+ opt_base_port = str (current_api_json ["data" ]["portapi" ])
374
+ opt_cluster = False
375
+ if "cluster" in current_api_json ["data" ]:
376
+ opt_cluster = current_api_json ["data" ]["cluster" ] == "true"
377
+ url = opt_base_url + ":" + opt_base_port
378
+ auth = requestsbak .auth .HTTPBasicAuth (opt_username , opt_password )
379
+ verify = False
380
+ try :
381
+ # Checks in the first request if the credentials are ok
382
+ request_manager = self .session .get (
383
+ url + '/agents/000?select=name' , auth = auth , timeout = 20 , verify = verify )
384
+ if request_manager .status_code == 401 :
385
+ self .logger .error ("Cannot connect to API; Invalid credentials." )
386
+ return jsonbak .dumps ({"status" : "400" , "error" : "Invalid credentials, please check the username and password." })
387
+ request_manager = request_manager .json ()
388
+ request_cluster = self .session .get (
389
+ url + '/cluster/status' , auth = auth , timeout = 20 , verify = verify ).json ()
390
+ request_cluster_name = self .session .get (
391
+ url + '/cluster/node' , auth = auth , timeout = 20 , verify = verify ).json ()
392
+ except ConnectionError as e :
393
+ self .logger .error ("manager: Cannot connect to API : %s" % (e ))
394
+ return jsonbak .dumps ({"status" : "400" , "error" : "Unreachable API, please check the URL and port." })
395
+ output = {}
396
+ daemons_ready = self .check_daemons (url , auth , verify , opt_cluster )
397
+ # Pass the cluster status instead of always False
398
+ if not daemons_ready :
399
+ raise Exception ("Daemons are not ready yet." )
400
+ output ['managerName' ] = request_manager ['data' ]
401
+ output ['clusterMode' ] = request_cluster ['data' ]
402
+ output ['clusterName' ] = request_cluster_name ['data' ]
403
+ del current_api_json ["data" ]["passapi" ]
404
+ output ['api' ] = current_api_json
405
+ result = jsonbak .dumps (output )
406
+ except Exception as e :
407
+ self .logger .error ("Error when checking API connection: %s" % (e ))
408
+ raise e
409
+ return result
410
+
352
411
def check_wazuh_version (self , kwargs ):
353
412
"""Check Wazuh version
354
413
0 commit comments