/
modules-helper.js
242 lines (222 loc) · 7.91 KB
/
modules-helper.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { getAngularModule, getDataPlugin } from '../../../kibana-services';
import { AppState } from '../../../react-services/app-state';
import { FilterHandler } from '../../../utils/filter-handler';
import { VULNERABILITY_IMPLICIT_CLUSTER_MODE_FILTER } from '../../../../common/constants';
import { useFilterManager } from '../hooks';
import { FilterStateStore } from '../../../../../../src/plugins/data/common';
export class ModulesHelper {
static async getDiscoverScope() {
const $injector = getAngularModule().$injector;
const location = $injector.get('$location');
const initialTab = location.search().tab;
return new Promise(resolve => {
const checkExist = setInterval(() => {
const app = getAngularModule();
if (app.discoverScope) {
clearInterval(checkExist);
resolve(app.discoverScope);
}
const currentTab = location.search().tab;
if (initialTab !== currentTab) {
clearInterval(checkExist);
}
}, 250);
});
}
static async cleanAvailableFields() {
const fields = document.querySelectorAll(
`.dscFieldChooser .dscFieldList--unpopular li`,
);
if (fields.length) {
fields.forEach(field => {
const attr = field.getAttribute('data-attr-field');
if (attr.startsWith('_')) {
field.style.display = 'none';
}
});
}
}
static hideCloseButtons = () => {
this.activeNoImplicitsFilters();
};
static async activeNoImplicitsFilters(generatedImplicitFilters) {
const { filterManager } = getDataPlugin().query;
const filters = filterManager.getFilters();
const implicitFilters = generatedImplicitFilters
? generatedImplicitFilters
: filters.filter(filter => filter.$state.isImplicit);
/*
Since the OSD filter definition does not include the "isImplicit" attribute that Wazuh adds, there may be cases where the "isImplicit" attribute is lost, since any action regarding filters that is done with the filterManager ( addFilters, setFilters, setGlobalFilters, setAppFilters) does a mapAndFlattenFilters mapping to the filters that removes any attributes that are not part of the filter definition.
If this case happens, the implicit filters are regenerated and the function is called again with the generated implicit filters.
*/
if (implicitFilters.length === 0) {
const generatedImplicitFilters = ModulesHelper.getImplicitFilter();
setTimeout(() => {
this.activeNoImplicitsFilters(generatedImplicitFilters);
}, 100);
return;
}
this.processFilters(implicitFilters);
}
static processFilters(filters) {
const allFilters = $(`.globalFilterItem .euiBadge__childButton`);
allFilters.each((_index, filter) => {
const data = filter.attributes['data-test-subj'];
/* With the filter classes decide if they are from the module view or not */
const isImplicitFilter = this.checkFilterValues(data, filters);
this.updateFilterState(isImplicitFilter, filter);
});
}
static checkFilterValues(data, filters) {
/*
Checks if the filter is already in use
Check which of the filters are from the module view and which are not pinned filters
*/
for (const moduleFilter of filters) {
if (!moduleFilter.used) {
const { objKey, objValue } = this.extractKeyAndValue(moduleFilter);
const noExcludedValues =
!data.value.includes('filter-pinned') &&
!data.value.includes('filter-negated');
const acceptedValues =
data.value.includes(`filter-key-${objKey}`) &&
data.value.includes(`filter-value-${objValue}`);
if (acceptedValues && noExcludedValues) {
moduleFilter.used = true;
return true;
}
}
}
return false;
}
static extractKeyAndValue(moduleFilter) {
const objKey = moduleFilter.query?.match
? Object.keys(moduleFilter.query.match)[0]
: moduleFilter.meta.key;
const objValue = moduleFilter.query?.match
? moduleFilter.query.match[objKey].query
: moduleFilter.meta.value;
return { objKey, objValue };
}
static updateFilterState(isImplicitFilter, filter) {
const closeButton = $(filter).siblings('.euiBadge__iconButton');
if (!isImplicitFilter) {
closeButton.removeClass('hide-close-button');
$(filter).off('click');
} else {
closeButton.addClass('hide-close-button');
$(filter).on('click', ev => ev.stopPropagation());
}
}
static getImplicitFilter() {
const tabFilters = {
general: { group: '' },
welcome: { group: '' },
fim: { group: 'syscheck' },
pm: { group: 'rootcheck' },
vuls: { group: 'vulnerability-detector' },
oscap: { group: 'oscap' },
ciscat: { group: 'ciscat' },
audit: { group: 'audit' },
pci: { group: 'pci_dss' },
gdpr: { group: 'gdpr' },
hipaa: { group: 'hipaa' },
nist: { group: 'nist' },
tsc: { group: 'tsc' },
aws: { group: 'amazon' },
gcp: { group: 'gcp' },
office: { group: 'office365' },
virustotal: { group: 'virustotal' },
osquery: { group: 'osquery' },
sca: { group: 'sca' },
docker: { group: 'docker' },
github: { group: 'github' },
};
const filterHandler = new FilterHandler(AppState.getCurrentPattern());
const filters = [];
const isCluster = AppState.getClusterInfo().status == 'enabled';
filters.push(
filterHandler.managerQuery(
isCluster
? AppState.getClusterInfo().cluster
: AppState.getClusterInfo().manager,
isCluster,
),
);
/* Add vulnerability cluster implicit filter*/
filters.push(
filterHandler.managerQuery(
isCluster
? AppState.getClusterInfo().cluster
: AppState.getClusterInfo().manager,
isCluster,
VULNERABILITY_IMPLICIT_CLUSTER_MODE_FILTER[
AppState.getClusterInfo().status
],
),
);
filters.push(filterHandler.pciQuery());
filters.push(filterHandler.gdprQuery());
filters.push(filterHandler.hipaaQuery());
filters.push(filterHandler.nistQuery());
filters.push(filterHandler.tscQuery());
filters.push(filterHandler.mitreQuery());
Object.keys(tabFilters).forEach(tab => {
filters.push(filterHandler.ruleGroupQuery(tabFilters[tab].group));
});
return filters;
}
/**
*
* @param {string} indexPattern - Index pattern id being used
* @param {Filter[] | undefined} filters - (Optional) Filter Array in which to search if a pinned agent exists
* @returns
*/
static getImplicitPinnedAgent(indexPattern, filters = []) {
const filterManager = useFilterManager().filterManager;
const initialFilters =
filters?.length > 0 ? filters : filterManager.getFilters();
const pinnedAgentByFilterManager = initialFilters.find(
filter =>
filter?.meta?.key === 'agent.id' && !!filter?.$state?.isImplicit,
);
const url = window.location.href;
const regex = new RegExp('agentId=' + '[^&]*');
const match = url.match(regex);
const isPinnedAgentByUrl = match && match[0];
if (pinnedAgentByFilterManager && isPinnedAgentByUrl) {
return {
...pinnedAgentByFilterManager,
meta: {
...pinnedAgentByFilterManager.meta,
index: indexPattern,
},
$state: { store: FilterStateStore.APP_STATE, isImplicit: true },
};
}
if (isPinnedAgentByUrl) {
const agentId = match[0].split('=')[1];
return {
meta: {
alias: null,
disabled: false,
key: 'agent.id',
negate: false,
params: { query: agentId },
type: 'phrase',
index: indexPattern,
},
query: {
match: {
'agent.id': {
query: agentId,
type: 'phrase',
},
},
},
$state: { store: 'appState', isImplicit: true },
};
}
return undefined;
}
}