-
Notifications
You must be signed in to change notification settings - Fork 186
/
check-api.service.ts
107 lines (101 loc) · 3.83 KB
/
check-api.service.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
/*
* Wazuh app - Check APIs 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 { getToasts } from '../../../kibana-services';
import { ApiCheck, AppState, GenericRequest } from '../../../react-services';
import { CheckLogger } from '../types/check_logger';
const trySetDefault = async (checkLogger: CheckLogger) => {
try {
checkLogger.info(`Getting API hosts...`);
const response = await GenericRequest.request('GET', '/hosts/apis');
checkLogger.info(`API hosts found: ${response.data.length}`);
const hosts = response.data;
const errors = [];
if (hosts.length) {
for (var i = 0; i < hosts.length; i++) {
try {
checkLogger.info(`Checking API host id [${hosts[i].id}]...`);
const API = await ApiCheck.checkApi(hosts[i], true);
if (API && API.data) {
return hosts[i].id;
}
} catch (err) {
checkLogger.info(`Could not connect to API id [${hosts[i].id}]: ${err.message || err}`);
errors.push(`Could not connect to API id [${hosts[i].id}]: ${err.message || err}`);
}
}
if (errors.length) {
return Promise.reject('No API available to connect');
}
}
return Promise.reject('No API configuration found');
} catch (error) {
checkLogger.error(`Error connecting to API: ${error}`);
return Promise.reject(`Error connecting to API: ${error}`);
}
};
export const checkApiService = (appInfo: any) => async (checkLogger: CheckLogger) => {
let apiChanged = false;
try {
let currentApi = JSON.parse(AppState.getCurrentAPI() || '{}');
if(!currentApi.id){
checkLogger.info(`No current API selected`);
currentApi.id = await trySetDefault(checkLogger);
apiChanged = true;
}
checkLogger.info(`Current API id [${currentApi.id}]`);
checkLogger.info(`Checking current API id [${currentApi.id}]...`);
const data = await ApiCheck.checkStored(currentApi.id).catch(async (err) => {
checkLogger.info(`Current API id [${currentApi.id}] has some problem: ${err.message || err}`);
const newApi = await trySetDefault(checkLogger);
if (newApi.error) {
return { error: newApi.error };
}
apiChanged = true;
checkLogger.info(`API host id [${newApi}] available`);
return await ApiCheck.checkStored(newApi, true);
});
if (apiChanged) {
const api = ((data || {}).data || {}).data || {};
const name = (api.cluster_info || {}).manager || false;
AppState.setCurrentAPI(JSON.stringify({ name: name, id: api.id }));
checkLogger.info(`Set current API in cookie: id [${api.id}], name [${name}]`);
getToasts().add({
color: 'warning',
title: 'Selected Wazuh API has been updated',
text: '',
toastLifeTimeMs: 3000,
});
}
//update cluster info
const cluster_info = (((data || {}).data || {}).data || {}).cluster_info;
if (cluster_info) {
AppState.setClusterInfo(cluster_info);
checkLogger.info(`Set cluster info in cookie`);
}
if (data === 3099) {
checkLogger.error('Wazuh not ready yet');
} else if (data.data.error || data.data.data.apiIsDown) {
const errorMessage = data.data.data.apiIsDown
? 'Wazuh API is down'
: `Error connecting to the API: ${
data.data.error && data.data.error.message ? ` ${data.data.error.message}` : ''
}`;
checkLogger.error(errorMessage);
}
} catch (error) {
AppState.removeNavigation();
checkLogger.info('Removed [navigate] cookie');
throw error;
}
};