-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathwz-request.ts
180 lines (170 loc) · 6.26 KB
/
wz-request.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Wazuh app - API request service
* Copyright (C) 2015-2022 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
import axios from 'axios';
import { AppState } from './app-state';
import { ApiCheck } from './wz-api-check';
import { WzAuthentication } from './wz-authentication';
import { WzMisc } from '../factories/misc';
import { WazuhConfig } from './wazuh-config';
import { OdfeUtils } from '../utils';
import IApiResponse from './interfaces/api-response.interface';
import { getHttp } from '../kibana-services';
import { PLUGIN_PLATFORM_REQUEST_HEADERS } from '../../common/constants';
export class WzRequest {
static wazuhConfig: any;
/**
* Permorn a generic request
* @param {String} method
* @param {String} path
* @param {Object} payload
*/
static async genericReq(
method,
path,
payload: any = null,
extraOptions: { shouldRetry?: boolean, checkCurrentApiIsUp?: boolean } = {
shouldRetry: true,
checkCurrentApiIsUp: true
}
) {
const shouldRetry = typeof extraOptions.shouldRetry === 'boolean' ? extraOptions.shouldRetry : true;
const checkCurrentApiIsUp = typeof extraOptions.checkCurrentApiIsUp === 'boolean' ? extraOptions.checkCurrentApiIsUp : true;
try {
if (!method || !path) {
throw new Error('Missing parameters');
}
this.wazuhConfig = new WazuhConfig();
const configuration = this.wazuhConfig.getConfig();
const timeout = configuration ? configuration.timeout : 20000;
const url = getHttp().basePath.prepend(path);
const options = {
method: method,
headers: { ...PLUGIN_PLATFORM_REQUEST_HEADERS, 'content-type': 'application/json' },
url: url,
data: payload,
timeout: timeout,
};
const data = await axios(options);
if (data['error']) {
throw new Error(data['error']);
}
return Promise.resolve(data);
} catch (error) {
OdfeUtils.checkOdfeSessionExpired(error);
//if the requests fails, we need to check if the API is down
if(checkCurrentApiIsUp){
const currentApi = JSON.parse(AppState.getCurrentAPI() || '{}');
if (currentApi && currentApi.id) {
try {
await ApiCheck.checkStored(currentApi.id);
} catch (error) {
const wzMisc = new WzMisc();
wzMisc.setApiIsDown(true);
if (!window.location.hash.includes('#/settings')) {
window.location.href = getHttp().basePath.prepend('/app/wazuh#/health-check');
}
throw new Error(error);
}
}
}
const errorMessage =
(error && error.response && error.response.data && error.response.data.message) ||
(error || {}).message;
if (
typeof errorMessage === 'string' &&
errorMessage.includes('status code 401') &&
shouldRetry
) {
try {
await WzAuthentication.refresh(true);
return this.genericReq(method, path, payload, { shouldRetry: false });
} catch (error) {
return ((error || {}).data || {}).message || false
? Promise.reject(this.returnErrorInstance(error, error.data.message))
: Promise.reject(this.returnErrorInstance(error, error.message));
}
}
return errorMessage
? Promise.reject(this.returnErrorInstance(error, errorMessage))
: Promise.reject(this.returnErrorInstance(error,'Server did not respond'));
}
}
/**
* Perform a request to the Wazuh API
* @param {String} method Eg. GET, PUT, POST, DELETE
* @param {String} path API route
* @param {Object} body Request body
*/
static async apiReq(
method,
path,
body,
options: { checkCurrentApiIsUp?: boolean } = { checkCurrentApiIsUp: true }
): Promise<IApiResponse<any>> {
try {
if (!method || !path || !body) {
throw new Error('Missing parameters');
}
const id = JSON.parse(AppState.getCurrentAPI()).id;
const requestData = { method, path, body, id };
const response = await this.genericReq('POST', '/api/request', requestData, options);
const hasFailed = (((response || {}).data || {}).data || {}).total_failed_items || 0;
if (hasFailed) {
const error =
((((response.data || {}).data || {}).failed_items || [])[0] || {}).error || {};
const failed_ids =
((((response.data || {}).data || {}).failed_items || [])[0] || {}).id || {};
const message = (response.data || {}).message || 'Unexpected error';
const errorMessage = `${message} (${error.code}) - ${error.message} ${failed_ids && failed_ids.length > 1 ? ` Affected ids: ${failed_ids} ` : ''}`
return Promise.reject(this.returnErrorInstance(null, errorMessage));
}
return Promise.resolve(response);
} catch (error) {
return ((error || {}).data || {}).message || false
? Promise.reject(this.returnErrorInstance(error, error.data.message))
: Promise.reject(this.returnErrorInstance(error, error.message));
}
}
/**
* Perform a request to generate a CSV
* @param {String} path
* @param {Object} filters
*/
static async csvReq(path, filters) {
try {
if (!path || !filters) {
throw new Error('Missing parameters');
}
const id = JSON.parse(AppState.getCurrentAPI()).id;
const requestData = { path, id, filters };
const data = await this.genericReq('POST', '/api/csv', requestData);
return Promise.resolve(data);
} catch (error) {
return ((error || {}).data || {}).message || false
? Promise.reject(this.returnErrorInstance(error, error.data.message))
: Promise.reject(this.returnErrorInstance(error, error.message));
}
}
/**
* Customize message and return an error object
* @param error
* @param message
* @returns error
*/
static returnErrorInstance(error, message){
if(!error || typeof error === 'string'){
return new Error(message || error);
}
error.message = message
return error
}
}