-
Notifications
You must be signed in to change notification settings - Fork 180
/
get-ip.js
118 lines (105 loc) · 3.68 KB
/
get-ip.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Wazuh app - Module to fetch index patterns
* 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 { healthCheck } from './health-check';
import { AppState } from '../../react-services/app-state';
import { getDataPlugin, getSavedObjects } from '../../kibana-services';
import { WazuhConfig } from '../../react-services/wazuh-config';
import { GenericRequest } from '../../react-services/generic-request';
import { getWzConfig } from './get-config';
import { UI_LOGGER_LEVELS } from '../../../common/constants';
import { UI_ERROR_SEVERITIES } from '../../react-services/error-orchestrator/types';
import { getErrorOrchestrator } from '../../react-services/common-services';
export function getIp(
$q,
$window,
$location,
wzMisc
) {
const deferred = $q.defer();
const checkWazuhPatterns = async (indexPatterns) => {
const wazuhConfig = new WazuhConfig();
const configuration = await getWzConfig($q, GenericRequest, wazuhConfig);
const wazuhPatterns = [
`${configuration['wazuh.monitoring.pattern']}`,
`${configuration['cron.prefix']}-${configuration['cron.statistics.index.name']}-*`
];
return wazuhPatterns.every(pattern => {
return indexPatterns.find(
element => element.id === pattern
);
});
}
const buildSavedObjectsClient = async () => {
try {
const savedObjectsClient = getSavedObjects().client;
const savedObjectsData = await savedObjectsClient.find({
type: 'index-pattern',
fields: ['title'],
perPage: 10000
});
const { savedObjects } = savedObjectsData;
const currentPattern = AppState.getCurrentPattern() || '';
if (
!currentPattern ||
!savedObjects.find(
element => element.id === currentPattern
) ||
!(await checkWazuhPatterns(savedObjects))
) {
if (!$location.path().includes('/health-check')) {
$location.search('tab', null);
$location.path('/health-check');
}
}
const onlyWazuhAlerts = savedObjects.filter(
element => element.id === currentPattern
);
if (!onlyWazuhAlerts || !onlyWazuhAlerts.length) {
// There's now selected ip
AppState.removeCurrentPattern();
deferred.resolve('No ip');
return;
}
const courierData = await getDataPlugin().indexPatterns.get(currentPattern);
deferred.resolve({
list: onlyWazuhAlerts,
loaded: courierData,
stateVal: null,
stateValFound: false
});
} catch (error) {
deferred.reject(error);
const options = {
context: `${getIp.name}.checkWazuhPatterns`,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.CRITICAL,
store: true,
error: {
error: error,
message: (error?.body?.message?.includes('no permissions for') && `You have no permissions. Contact to an administrator:\n${error?.body?.message}`) || error.message || error,
title: error.name || error,
},
};
getErrorOrchestrator().handleError(options);
}
};
const currentParams = $location.search();
const targetedRule =
currentParams && currentParams.tab === 'ruleset' && currentParams.ruleid;
if (!targetedRule && healthCheck($window)) {
deferred.reject();
$location.path('/health-check');
} else {
buildSavedObjectsClient();
}
return deferred.promise;
}