Skip to content

Commit c68f688

Browse files
pablotr9Jesús Ángel
authored andcommitted
Backport #792 for 3.9.4. Hide API password on requests (#796)
1 parent bdf155d commit c68f688

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ All notable changes to the Wazuh app for Splunk project will be documented in th
1919
- Fixed error when adding a filter with spaces. [#793](https://github.com/wazuh/wazuh-splunk/issues/793)
2020
- Fixed downloading tables as CSV. [#788](https://github.com/wazuh/wazuh-splunk/issues/788)
2121
- Fixed flick in CDB lists table when deleting a list. [#788](https://github.com/wazuh/wazuh-splunk/issues/788)
22+
- Hide API password from check-connection requests [#792](https://github.com/wazuh/wazuh-splunk/issues/792)
2223

2324
## Wazuh v3.9.3 - Splunk Enterprise v7.3.0 - Revision 34
2425

SplunkAppForWazuh/appserver/controllers/manager.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,12 @@ def get_apis(self, **kwargs):
192192
"""
193193
try:
194194
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)
196201
except Exception as e:
197202
self.logger.error(jsonbak.dumps({"error": str(e)}))
198203
return jsonbak.dumps({"error": str(e)})
@@ -349,6 +354,60 @@ def check_connection(self, **kwargs):
349354
return jsonbak.dumps({"status": 400, "error": "Cannot connect to the API"})
350355
return result
351356

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+
352411
def check_wazuh_version(self, kwargs):
353412
"""Check Wazuh version
354413

SplunkAppForWazuh/appserver/static/js/run/run.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ define(['./module'], function (module) {
4949
if (state != 'settings.api'){
5050
$rootScope.$broadcast('stateChanged', 'settings')
5151
}
52-
if (typeof err === 'string' && err.startsWith('Unexpected Wazuh version.')) {
52+
if (typeof err === 'string') {
5353
$notificationService.showErrorToast(err)
5454
}
5555
$state.go('settings.api')

SplunkAppForWazuh/appserver/static/js/services/api-manager/apiMgrService.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,37 @@ define(['../module'], function (module) {
250250
}
251251
}
252252

253+
254+
/**
255+
* Checks a connection given its ID
256+
* @param {Object} api
257+
*/
258+
const checkRawConnectionById = async id => {
259+
try {
260+
const checkConnectionEndpoint = `/manager/check_connection_by_id?apiId=${id}`
261+
const result = await $requestService.httpReq(
262+
'GET',
263+
checkConnectionEndpoint
264+
)
265+
266+
if (result.data.status === 400 || result.data.error) {
267+
if (result.data.error === 3099) {
268+
throw 'ERROR3099 - Wazuh not ready yet.'
269+
} else {
270+
throw result.data.error || 'Unreachable API.'
271+
}
272+
}
273+
return result
274+
} catch (err) {
275+
if (err.status === 500) {
276+
throw new Error(
277+
'There was an error connecting to the api. Please check your api configuration.'
278+
)
279+
}
280+
return Promise.reject(err)
281+
}
282+
}
283+
253284
/**
254285
* Checks if the API has to change its filters
255286
* @param {Object} api
@@ -297,8 +328,8 @@ define(['../module'], function (module) {
297328
*/
298329
const checkApiConnection = async id => {
299330
try {
300-
const api = await select(id) //Before update cluster or not cluster
301-
await checkRawConnection(api)
331+
const connectionData = await checkRawConnectionById(id)
332+
const api = connectionData.data.api.data
302333
const apiSaved = { ...api } //eslint-disable-line
303334
const updatedApi = await updateApiFilter(api)
304335
let equal = true
@@ -353,6 +384,7 @@ define(['../module'], function (module) {
353384

354385
return {
355386
checkApiConnection: checkApiConnection,
387+
checkRawConnectionById: checkRawConnectionById,
356388
checkPollingState: checkPollingState,
357389
checkSelectedApiConnection: checkSelectedApiConnection,
358390
getApiList: getApiList,

0 commit comments

Comments
 (0)