Add full error output from wazuh api

This commit is contained in:
Pablo 2019-01-14 11:45:25 +01:00
parent 382409da67
commit 67be7f3991

View File

@ -523,122 +523,72 @@ export class WazuhApiCtrl {
try {
const api = await this.wzWrapper.getWazuhConfigurationById(id);
if (api.error_code > 1) {
//Can not connect to elasticsearch
return ErrorResponse(
'Could not connect with elasticsearch',
3011,
404,
reply
);
} else if (api.error_code > 0) {
//Credentials not found
return ErrorResponse('Credentials does not exists', 3012, 404, reply);
}
if (!data) {
data = {};
}
const options = ApiHelper.buildOptionsObject(api);
// Set content type application/xml if needed
if (
typeof (data || {}).content === 'string' &&
(data || {}).origin === 'xmleditor'
) {
options.content_type = 'application/xml';
data = data.content.replace(new RegExp('\\n', 'g'), '');
}
const fullUrl = getPath(api) + path;
const response = await needle(method, fullUrl, data, options);
if (
response &&
response.body &&
!response.body.error &&
response.body.data
) {
cleanKeys(response);
return reply(response.body);
}
throw ((response || {}).body || {}).error &&
((response || {}).body || {}).message
? { message: response.body.message, code: response.body.error }
: new Error('Unexpected error fetching data from the Wazuh API');
} catch (error) {
return ErrorResponse(
error,
`Wazuh API error: ${error.code}` || 3013,
500,
reply
);
}
}
async makeDevToolsRequest(method, path, data, id, reply) {
try {
const api = await this.wzWrapper.getWazuhConfigurationById(id);
if (api.error_code > 1) {
//Can not connect to elasticsearch
return ErrorResponse(
'Could not connect with elasticsearch',
3011,
404,
reply
);
} else if (api.error_code > 0) {
//Credentials not found
return ErrorResponse('Credentials does not exists', 3012, 404, reply);
}
if (!data) {
data = {};
}
const options = ApiHelper.buildOptionsObject(api);
// Set content type application/xml if needed
if (
typeof (data || {}).content === 'string' &&
(data || {}).origin === 'xmleditor'
) {
options.content_type = 'application/xml';
data = data.content.replace(new RegExp('\\n', 'g'), '');
}
const fullUrl = getPath(api) + path;
const response = await needle(method, fullUrl, data, options);
if (
response &&
response.body &&
!response.body.error &&
response.body.data
) {
cleanKeys(response);
return reply(response.body);
let devtools = false;
if(data.devTools){
delete data.devTools;
devtools = true;
}
if (api.error_code > 1) {
//Can not connect to elasticsearch
return ErrorResponse(
'Could not connect with elasticsearch',
3011,
404,
reply
);
} else if (api.error_code > 0) {
//Credentials not found
return ErrorResponse('Credentials does not exists', 3012, 404, reply);
}
if (!data) {
data = {};
}
const options = ApiHelper.buildOptionsObject(api);
// Set content type application/xml if needed
if (
typeof (data || {}).content === 'string' &&
(data || {}).origin === 'xmleditor'
) {
options.content_type = 'application/xml';
data = data.content.replace(new RegExp('\\n', 'g'), '');
}
const fullUrl = getPath(api) + path;
const response = await needle(method, fullUrl, data, options);
if (
response &&
response.body &&
response.body.error
!response.body.error &&
response.body.data
) {
cleanKeys(response);
return reply(response.body);
}
if (
response &&
response.body &&
response.body.error &&
devtools
) {
return reply(response.body);
}
throw ((response || {}).body || {}).error &&
((response || {}).body || {}).message
? { message: response.body.message, code: response.body.error }
: new Error('Unexpected error fetching data from the Wazuh API');
} catch (error) {
return ErrorResponse(
error,
error.message || error,
`Wazuh API error: ${error.code}` || 3013,
500,
reply
@ -646,6 +596,8 @@ export class WazuhApiCtrl {
}
}
/**
* This performs a generic request and returs its response
* @param {String} method Method: GET, PUT, POST, DELETE
@ -723,7 +675,7 @@ export class WazuhApiCtrl {
);
}
if (req.payload.body.devTools) {
delete req.payload.body.devTools;
//delete req.payload.body.devTools;
const keyRegex = new RegExp(/.*agents\/\d*\/key.*/);
if (
typeof req.payload.path === 'string' &&
@ -737,13 +689,7 @@ export class WazuhApiCtrl {
reply
);
}
return this.makeDevToolsRequest(
req.payload.method,
req.payload.path,
req.payload.body,
req.payload.id,
reply
);
}
return this.makeRequest(
req.payload.method,