-
Notifications
You must be signed in to change notification settings - Fork 186
/
wz-api-check.js
100 lines (89 loc) · 2.99 KB
/
wz-api-check.js
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
/*
* Wazuh app - API status check 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 { WazuhConfig } from './wazuh-config';
import axios from 'axios';
import { AppState } from './app-state';
import { WzMisc } from '../factories/misc';
import { getHttp } from '../kibana-services';
import { PLUGIN_PLATFORM_REQUEST_HEADERS } from '../../common/constants';
export class ApiCheck {
static async checkStored(data, idChanged = false) {
try {
const wazuhConfig = new WazuhConfig();
const configuration = wazuhConfig.getConfig();
const timeout = configuration ? configuration.timeout : 20000;
const payload = { id: data };
if (idChanged) {
payload.idChanged = data;
}
const url = getHttp().basePath.prepend('/api/check-stored-api');
const options = {
method: 'POST',
headers: { ...PLUGIN_PLATFORM_REQUEST_HEADERS, 'content-type': 'application/json' },
url: url,
data: payload,
timeout: timeout || 20000
};
if (Object.keys(configuration).length) {
AppState.setPatternSelector(configuration['ip.selector']);
}
const response = await axios(options);
if (response.error) {
return Promise.reject(response);
}
return response;
} catch (err) {
if (err.response) {
const wzMisc = new WzMisc();
wzMisc.setApiIsDown(true);
const response = (err.response.data || {}).message || err.message;
return Promise.reject(response);
} else {
return (err || {}).message || false
? Promise.reject(err.message)
: Promise.reject(err || 'Server did not respond');
}
}
}
/**
* Check the status of an API entry
* @param {String} apiObject
*/
static async checkApi(apiEntry, forceRefresh=false) {
try {
const wazuhConfig = new WazuhConfig();
const { timeout } = wazuhConfig.getConfig();
const url = getHttp().basePath.prepend('/api/check-api');
const options = {
method: 'POST',
headers: { ...PLUGIN_PLATFORM_REQUEST_HEADERS, 'content-type': 'application/json' },
url: url,
data: {...apiEntry, forceRefresh},
timeout: timeout || 20000
};
const response = await axios(options);
if (response.error) {
return Promise.reject(response);
}
return response;
} catch (err) {
if (err.response) {
const response = (err.response.data || {}).message || err.message;
return Promise.reject(response);
} else {
return (err || {}).message || false
? Promise.reject(err.message)
: Promise.reject(err || 'Server did not respond');
}
}
}
}