From de3ecebef6723ef7a0d405e363fdf6373377b8b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel?= Date: Mon, 28 Jan 2019 20:52:14 +0100 Subject: [PATCH] Fix all backend responses --- server/controllers/error-response.js | 4 +- server/controllers/wazuh-api-elastic.js | 10 ++--- server/controllers/wazuh-api.js | 56 +++++++++++++------------ server/controllers/wazuh-elastic.js | 28 ++++++------- server/controllers/wazuh-reporting.js | 6 +-- server/controllers/wazuh-utils.js | 10 ++--- 6 files changed, 58 insertions(+), 56 deletions(-) diff --git a/server/controllers/error-response.js b/server/controllers/error-response.js index 9efcc6aed..8c1d2e046 100644 --- a/server/controllers/error-response.js +++ b/server/controllers/error-response.js @@ -66,7 +66,7 @@ export function ErrorResponse( } } - return reply({ + return { message: filteredMessage ? `${code ? code : 1000} - ${filteredMessage}` : typeof message === 'string' @@ -74,5 +74,5 @@ export function ErrorResponse( : `${code ? code : 1000} - Unexpected error`, code: code ? code : 1000, statusCode: statusCode ? statusCode : 500 - }).code(statusCode ? statusCode : 500); + }; } diff --git a/server/controllers/wazuh-api-elastic.js b/server/controllers/wazuh-api-elastic.js index 5942e2385..4a1a0606f 100644 --- a/server/controllers/wazuh-api-elastic.js +++ b/server/controllers/wazuh-api-elastic.js @@ -52,7 +52,7 @@ export class WazuhApiElasticCtrl { } } - return reply(result); + return (result); } catch (error) { log('GET /elastic/apis', error.message || error); return ErrorResponse(error.message || error, 2001, 500, reply); @@ -69,7 +69,7 @@ export class WazuhApiElasticCtrl { try { const data = await this.wzWrapper.deleteWazuhAPIEntriesWithRequest(req); - return reply(data); + return (data); } catch (error) { log('DELETE /elastic/apis/{id}', error.message || error); return ErrorResponse(error.message || error, 2002, 500, reply); @@ -154,7 +154,7 @@ export class WazuhApiElasticCtrl { settings ); - return reply({ statusCode: 200, message: 'ok', response }); + return ({ statusCode: 200, message: 'ok', response }); } catch (error) { log('PUT /elastic/api', error.message || error); return ErrorResponse( @@ -178,7 +178,7 @@ export class WazuhApiElasticCtrl { doc: { cluster_info: req.payload.cluster_info } }); - return reply({ statusCode: 200, message: 'ok' }); + return ({ statusCode: 200, message: 'ok' }); } catch (error) { log('PUT /elastic/api-hostname/{id}', error.message || error); return ErrorResponse( @@ -216,7 +216,7 @@ export class WazuhApiElasticCtrl { doc: settings }); - return reply({ statusCode: 200, message: 'ok' }); + return ({ statusCode: 200, message: 'ok' }); } catch (error) { log('PUT /elastic/api-settings', error.message || error); return ErrorResponse( diff --git a/server/controllers/wazuh-api.js b/server/controllers/wazuh-api.js index 361f873a8..78272be8c 100644 --- a/server/controllers/wazuh-api.js +++ b/server/controllers/wazuh-api.js @@ -110,7 +110,7 @@ export class WazuhApiCtrl { api.cluster_info.node = response.body.data.node; api.cluster_info.cluster = response.body.data.cluster; api.password = '****'; - return reply({ + return ({ statusCode: 200, data: api, idChanged: req.idChanged || null @@ -131,7 +131,7 @@ export class WazuhApiCtrl { api.cluster_info.manager = managerName; api.password = '****'; - return reply({ + return ({ statusCode: 200, data: api, idChanged: req.idChanged || null @@ -154,7 +154,7 @@ export class WazuhApiCtrl { } catch (error) { if (error.code === 'ECONNREFUSED') { log('POST /api/check-stored-api', error.message || error); - return reply({ + return ({ statusCode: 200, data: { password: '****', apiIsDown: true } }); @@ -309,7 +309,7 @@ export class WazuhApiCtrl { ); if (!response.body.error) { - return reply({ + return ({ manager: managerName, node: response.body.data.node, cluster: response.body.data.cluster, @@ -318,7 +318,7 @@ export class WazuhApiCtrl { } } else { // Cluster mode is not active - return reply({ + return ({ manager: managerName, cluster: 'Disabled', status: 'disabled' @@ -351,7 +351,7 @@ export class WazuhApiCtrl { if (req.params.requirement === 'all') { if (!req.headers.id) { - return reply(pciRequirementsFile); + return (pciRequirementsFile); } let api = await this.wzWrapper.getWazuhConfigurationById( req.headers.id @@ -383,7 +383,7 @@ export class WazuhApiCtrl { if (typeof pciRequirementsFile[item] !== 'undefined') PCIobject[item] = pciRequirementsFile[item]; } - return reply(PCIobject); + return (PCIobject); } else { return ErrorResponse( 'An error occurred trying to parse PCI DSS requirements', @@ -399,7 +399,7 @@ export class WazuhApiCtrl { pci_description = pciRequirementsFile[req.params.requirement]; } - return reply({ + return ({ pci: { requirement: req.params.requirement, description: pci_description @@ -423,7 +423,7 @@ export class WazuhApiCtrl { if (req.params.requirement === 'all') { if (!req.headers.id) { - return reply(gdprRequirementsFile); + return (gdprRequirementsFile); } const api = await this.wzWrapper.getWazuhConfigurationById( req.headers.id @@ -453,7 +453,7 @@ export class WazuhApiCtrl { (major >= 3 && minor < 2) || (major >= 3 && minor >= 2 && patch < 3) ) { - return reply({}); + return ({}); } if (api.error_code > 1) { @@ -482,7 +482,7 @@ export class WazuhApiCtrl { if (typeof gdprRequirementsFile[item] !== 'undefined') GDPRobject[item] = gdprRequirementsFile[item]; } - return reply(GDPRobject); + return (GDPRobject); } else { return ErrorResponse( 'An error occurred trying to parse GDPR requirements', @@ -498,7 +498,7 @@ export class WazuhApiCtrl { gdpr_description = gdprRequirementsFile[req.params.requirement]; } - return reply({ + return ({ gdpr: { requirement: req.params.requirement, description: gdpr_description @@ -564,11 +564,11 @@ export class WazuhApiCtrl { ((response || {}).body || {}).data ) { cleanKeys(response); - return reply(response.body); + return (response.body); } if (((response || {}).body || {}).error && devTools) { - return reply(response.body); + return (response.body); } throw ((response || {}).body || {}).error && @@ -576,14 +576,16 @@ export class WazuhApiCtrl { ? { message: response.body.message, code: response.body.error } : new Error('Unexpected error fetching data from the Wazuh API'); } catch (error) { - return devTools - ? reply({ error: '3013', message: error.message || error }) - : ErrorResponse( - error.message || error, - `Wazuh API error: ${error.code}` || 3013, - 500, - reply - ); + if (devTools) { + return ({ error: '3013', message: error.message || error }); + } else { + return ErrorResponse( + error.message || error, + `Wazuh API error: ${error.code}` || 3013, + 500, + reply + ); + } } } @@ -698,7 +700,7 @@ export class WazuhApiCtrl { async fetchAgents(req, reply) { try { const output = await this.monitoringInstance.fetchAgentsExternal(); - return reply({ + return ({ statusCode: 200, error: '0', data: '', @@ -798,7 +800,7 @@ export class WazuhApiCtrl { } } - return reply(csv).type('text/csv'); + return (csv).type('text/csv'); } else if ( output && output.body && @@ -947,15 +949,15 @@ export class WazuhApiCtrl { Object.assign(result.lastAgent, lastAgent.items[0]); } - return reply({ error: 0, result }); + return ({ error: 0, result }); } catch (error) { return ErrorResponse(error.message || error, 3035, 500, reply); } } // Get de list of available requests in the API - getRequestList(req, reply) { + getRequestList() { //Read a static JSON until the api call has implemented - return reply(apiRequestList); + return (apiRequestList); } } diff --git a/server/controllers/wazuh-elastic.js b/server/controllers/wazuh-elastic.js index 4abfa9cbe..110b99274 100644 --- a/server/controllers/wazuh-elastic.js +++ b/server/controllers/wazuh-elastic.js @@ -43,7 +43,7 @@ export class WazuhElasticCtrl { ((((data || {}).hits || {}).hits || [])[0] || {})._source || {}; if (source.installationDate && source.lastRestart) { - return reply({ + return ({ installationDate: data.hits.hits[0]._source.installationDate, lastRestart: data.hits.hits[0]._source.lastRestart }); @@ -115,12 +115,12 @@ export class WazuhElasticCtrl { }); return isIncluded && Array.isArray(isIncluded) && isIncluded.length - ? reply({ + ? ({ statusCode: 200, status: true, data: `Template found for ${req.params.pattern}` }) - : reply({ + : ({ statusCode: 200, status: false, data: `No template found for ${req.params.pattern}` @@ -152,8 +152,8 @@ export class WazuhElasticCtrl { ); return filtered.length >= 1 - ? reply({ statusCode: 200, status: true, data: 'Index pattern found' }) - : reply({ + ? ({ statusCode: 200, status: true, data: 'Index pattern found' }) + : ({ statusCode: 500, status: false, error: 10020, @@ -218,8 +218,8 @@ export class WazuhElasticCtrl { return data.hits.total === 0 || typeof data.aggregations['2'].buckets[0] === 'undefined' - ? reply({ statusCode: 200, data: '' }) - : reply({ + ? ({ statusCode: 200, data: '' }) + : ({ statusCode: 200, data: data.aggregations['2'].buckets[0].key }); @@ -239,8 +239,8 @@ export class WazuhElasticCtrl { const data = await this.wzWrapper.getWazuhVersionIndexAsSearch(); return data.hits.total === 0 - ? reply({ statusCode: 200, data: '' }) - : reply({ statusCode: 200, data: data.hits.hits[0]._source }); + ? ({ statusCode: 200, data: '' }) + : ({ statusCode: 200, data: data.hits.hits[0]._source }); } catch (error) { log('GET /elastic/setup', error.message || error); return ErrorResponse( @@ -351,7 +351,7 @@ export class WazuhElasticCtrl { item && item.title && !config['ip.ignore'].includes(item.title) ); } - return reply({ + return ({ data: isXpackEnabled && !isSuperUser ? await this.filterAllowedIndexPatternList(list, req) @@ -528,7 +528,7 @@ export class WazuhElasticCtrl { : AgentsVisualizations[tabSufix]; const raw = await this.buildVisualizationsRaw(file, req.params.pattern); - return reply({ acknowledge: true, raw: raw }); + return ({ acknowledge: true, raw: raw }); } catch (error) { return ErrorResponse(error.message || error, 4007, 500, reply); } @@ -573,7 +573,7 @@ export class WazuhElasticCtrl { pattern_name ); - return reply({ acknowledge: true, raw: raw }); + return ({ acknowledge: true, raw: raw }); } catch (error) { return ErrorResponse(error.message || error, 4009, 500, reply); } @@ -596,7 +596,7 @@ export class WazuhElasticCtrl { req.params.pattern ); - return reply({ acknowledge: true, output: output }); + return ({ acknowledge: true, output: output }); } catch (error) { log('GET /elastic/known-fields/{pattern}', error.message || error); return ErrorResponse(error.message || error, 4008, 500, reply); @@ -661,7 +661,7 @@ export class WazuhElasticCtrl { 'rule.description' ]; const data = await this.wzWrapper.searchWazuhAlertsWithPayload(payload); - return reply({ alerts: data.hits.hits }); + return ({ alerts: data.hits.hits }); } catch (error) { log('POST /elastic/alerts', error.message || error); return ErrorResponse(error.message || error, 4010, 500, reply); diff --git a/server/controllers/wazuh-reporting.js b/server/controllers/wazuh-reporting.js index 8dd2c4f70..71a53bd38 100644 --- a/server/controllers/wazuh-reporting.js +++ b/server/controllers/wazuh-reporting.js @@ -1617,7 +1617,7 @@ export class WazuhReportingCtrl { ); pdfDoc.end(); } - return reply({ error: 0, data: null }); + return ({ error: 0, data: null }); } catch (error) { // Delete generated file if an error occurred if ( @@ -1659,7 +1659,7 @@ export class WazuhReportingCtrl { list.push(file); }); TimSort.sort(list, sortFunction); - return reply({ list: list }); + return ({ list: list }); } catch (error) { return ErrorResponse(error.message || error, 5031, 500, reply); } @@ -1694,7 +1694,7 @@ export class WazuhReportingCtrl { fs.unlinkSync( path.join(__dirname, REPORTING_PATH + '/' + req.params.name) ); - return reply({ error: 0 }); + return ({ error: 0 }); } catch (error) { return ErrorResponse(error.message || error, 5032, 500, reply); } diff --git a/server/controllers/wazuh-utils.js b/server/controllers/wazuh-utils.js index 7814092a4..af4f4dc74 100644 --- a/server/controllers/wazuh-utils.js +++ b/server/controllers/wazuh-utils.js @@ -35,7 +35,7 @@ export class WazuhUtilsCtrl { try { const configFile = getConfiguration(); - return reply({ + return ({ statusCode: 200, error: 0, data: configFile || {} @@ -54,7 +54,7 @@ export class WazuhUtilsCtrl { async updateConfigurationFile(req, reply) { try { const result = updateConfigurationFile.updateConfiguration(req); - return reply({ + return ({ statusCode: 200, error: 0, data: result.needRestart @@ -74,7 +74,7 @@ export class WazuhUtilsCtrl { try { // RAM in MB const ram = Math.ceil(totalmem() / 1024 / 1024); - return reply({ statusCode: 200, error: 0, ram }); + return ({ statusCode: 200, error: 0, ram }); } catch (error) { return ErrorResponse(error.message || error, 3033, 500, reply); } @@ -93,13 +93,13 @@ export class WazuhUtilsCtrl { 20 ); return lastLogs && Array.isArray(lastLogs) - ? reply({ + ? ({ error: 0, lastLogs: lastLogs.filter( item => typeof item === 'string' && item.length ) }) - : reply({ error: 0, lastLogs: [] }); + : ({ error: 0, lastLogs: [] }); } catch (error) { return ErrorResponse(error.message || error, 3036, 500, reply); }