Adapt API requests to API4.0 format

This commit is contained in:
Pablo 2020-01-30 15:54:17 +01:00
parent 6cc0eac6a5
commit 548f3fcb74
No known key found for this signature in database
GPG Key ID: 7CD2CEF2014ABE04
2 changed files with 30 additions and 34 deletions

View File

@ -490,7 +490,7 @@ export class WazuhApiCtrl {
// Avoid "Error communicating with socket" like errors
const socketErrorCodes = [1013, 1014, 1017, 1018, 1019];
const isDown = socketErrorCodes.includes(response.data.code || 1);
const isDown = socketErrorCodes.includes(response.data.status || 1);
isDown && log('wazuh-api:makeRequest', 'Wazuh API is online but Wazuh is not ready yet');
@ -642,14 +642,14 @@ export class WazuhApiCtrl {
*/
async checkDaemons(api, path) {
try {
const response = await needle(
const response = await this.apiInterceptor.request(
'GET',
getPath(api) + '/manager/status',
{},
ApiHelper.buildOptionsObject(api)
{ idHost: api.id}
);
const daemons = ((response || {}).body || {}).data || {};
const daemons = ((response || {}).data || {}).data || {};
const isCluster =
((api || {}).cluster_info || {}).status === 'enabled' &&
@ -717,7 +717,6 @@ export class WazuhApiCtrl {
const devTools = !!(data || {}).devTools;
try {
const api = await this.manageHosts.getHostById(id);
if (devTools) {
delete data.devTools;
}
@ -766,7 +765,7 @@ export class WazuhApiCtrl {
}
if (path === '/ping') {
try {
try { // TODO
const check = await this.checkDaemons(api, path);
return check;
} catch (error) {
@ -806,7 +805,7 @@ export class WazuhApiCtrl {
data = {};
}
const response = await needle(method, fullUrl, data, options);
const response = await this.apiInterceptor.request(method, fullUrl, data, options);
const responseIsDown = this.checkResponseIsDown(response);
if (responseIsDown) {
@ -817,29 +816,26 @@ export class WazuhApiCtrl {
reply
);
}
const responseBody = (response || {}).body || {};
let responseData = responseBody.data;
if (!responseData) {
responseData =
typeof responseData === 'string' && path.includes('/files') && method === 'GET'
const responseBody = (response || {}).data || {};
if (!responseBody) {
responseBody =
typeof responseBody === 'string' && path.includes('/files') && method === 'GET'
? ' '
: false;
response.body.data = responseData;
response.data = responseBody;
}
const responseError = responseBody.error || false;
const responseError = response.status !== 200;
if (!responseError && responseData) {
cleanKeys(response);
return response.body;
if (!responseError && responseBody) {
//cleanKeys(response);
return response.data;
}
if (responseError && devTools) {
return response.body;
return response.data;
}
throw responseError && responseBody.message
? { message: responseBody.message, code: responseError }
throw responseError && responseBody.detail
? { message: responseBody.detail, code: responseError }
: new Error('Unexpected error fetching data from the Wazuh API');
} catch (error) {
log('wazuh-api:makeRequest', error.message || error);

View File

@ -11,27 +11,27 @@
*/
export function cleanKeys(response) {
// Remove agent key
if (response.body.data.internal_key) {
response.body.data.internal_key = '********';
if (response.data.data.internal_key) {
response.data.data.internal_key = '********';
}
// Remove cluster key (/com/cluster)
if (response.body.data.node_type && response.body.data.key) {
response.body.data.key = '********';
if (response.data.data.node_type && response.data.data.key) {
response.data.data.key = '********';
}
// Remove cluster key (/manager/configuration)
if (
response.body.data.cluster &&
response.body.data.cluster.node_type &&
response.body.data.cluster.key
response.data.data.cluster &&
response.data.data.cluster.node_type &&
response.data.data.cluster.key
) {
response.body.data.cluster.key = '********';
response.data.data.cluster.key = '********';
}
// Remove AWS keys
if (response.body.data.wmodules) {
response.body.data.wmodules.map(item => {
if (response.data.data.wmodules) {
response.data.data.wmodules.map(item => {
if (item['aws-s3']) {
if (item['aws-s3'].buckets) {
item['aws-s3'].buckets.map(item => {
@ -50,7 +50,7 @@ export function cleanKeys(response) {
}
// Remove integrations keys
if (response.body.data.integration) {
response.body.data.integration.map(item => (item.api_key = '********'));
if (response.data.data.integration) {
response.data.data.integration.map(item => (item.api_key = '********'));
}
}