-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathUtmDataInputStatusService.java
More file actions
500 lines (429 loc) · 21.1 KB
/
UtmDataInputStatusService.java
File metadata and controls
500 lines (429 loc) · 21.1 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package com.park.utmstack.service;
import com.park.utmstack.config.Constants;
import com.park.utmstack.domain.datainput_ingestion.UtmDataInputStatus;
import com.park.utmstack.domain.UtmServerModule;
import com.park.utmstack.domain.application_events.enums.ApplicationEventType;
import com.park.utmstack.domain.chart_builder.types.query.FilterType;
import com.park.utmstack.domain.chart_builder.types.query.OperatorType;
import com.park.utmstack.domain.correlation.config.UtmDataTypes;
import com.park.utmstack.domain.datainput_ingestion.UtmDataInputStatusCheckpoint;
import com.park.utmstack.domain.network_scan.UtmNetworkScan;
import com.park.utmstack.domain.network_scan.enums.AssetStatus;
import com.park.utmstack.domain.network_scan.enums.UpdateLevel;
import com.park.utmstack.domain.shared_types.alert.UtmAlert;
import com.park.utmstack.repository.datainput_ingestion.UtmDataInputStatusCheckpointRepository;
import com.park.utmstack.repository.datainput_ingestion.UtmDataInputStatusRepository;
import com.park.utmstack.repository.correlation.config.UtmDataTypesRepository;
import com.park.utmstack.repository.network_scan.UtmNetworkScanRepository;
import com.park.utmstack.service.application_events.ApplicationEventService;
import com.park.utmstack.service.elasticsearch.ElasticsearchService;
import com.park.utmstack.service.elasticsearch.SearchUtil;
import com.park.utmstack.service.logstash_pipeline.response.statistic.StatisticDocument;
import com.park.utmstack.service.network_scan.DataSourceConstants;
import com.park.utmstack.service.network_scan.UtmNetworkScanService;
import com.park.utmstack.util.enums.AlertSeverityEnum;
import com.park.utmstack.util.enums.AlertStatus;
import lombok.RequiredArgsConstructor;
import org.apache.http.conn.util.InetAddressUtils;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.opensearch._types.SortOrder;
import org.opensearch.client.opensearch.core.SearchRequest;
import org.opensearch.client.opensearch.core.SearchResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Service Implementation for managing UtmDataInputStatus.
*/
@Service
@RequiredArgsConstructor
@Transactional
public class UtmDataInputStatusService {
private static final String CLASSNAME = "UtmDataInputStatusService";
private final Logger log = LoggerFactory.getLogger(UtmDataInputStatusService.class);
private final UtmDataInputStatusRepository dataInputStatusRepository;
private final UtmServerModuleService serverModuleService;
private final ApplicationEventService applicationEventService;
private final UtmNetworkScanService networkScanService;
private final ElasticsearchService elasticsearchService;
private final UtmDataTypesRepository dataTypesRepository;
private final UtmNetworkScanRepository networkScanRepository;
private final UtmDataInputStatusCheckpointRepository checkpointRepository;
/**
* Save a utmDataInputStatus.
*
* @param utmDataInputStatus the entity to save
* @return the persisted entity
*/
public UtmDataInputStatus save(UtmDataInputStatus utmDataInputStatus) {
log.debug("Request to save UtmDataInputStatus : {}", utmDataInputStatus);
return dataInputStatusRepository.save(utmDataInputStatus);
}
/**
* Get all the utmDataInputStatuses.
*
* @param pageable the pagination information
* @return the list of entities
*/
@Transactional(readOnly = true)
public Page<UtmDataInputStatus> findImportantDatasource(Pageable pageable) throws Exception {
final String ctx = CLASSNAME + ".findImportantDatasource";
try {
return dataInputStatusRepository.findImportantDatasource(pageable);
} catch (Exception e) {
throw new Exception(ctx + ": " + e.getMessage());
}
}
/**
* Get one utmDataInputStatus by id.
*
* @param id the id of the entity
* @return the entity
*/
@Transactional(readOnly = true)
public Optional<UtmDataInputStatus> findOne(String id) {
log.debug("Request to get UtmDataInputStatus : {}", id);
return dataInputStatusRepository.findById(id);
}
/**
* Delete the utmDataInputStatus by id.
*
* @param id the id of the entity
*/
public void delete(String id) {
log.debug("Request to delete UtmDataInputStatus : {}", id);
dataInputStatusRepository.deleteById(id);
}
@Scheduled(fixedDelay = 900000)
public void checkDatasource() {
final String ctx = CLASSNAME + ".checkDatasource";
final List<String> types = Arrays.asList("aws", "o365", "hids");
try {
List<UtmDataInputStatus> rows = dataInputStatusRepository.findAllByDataTypeIn(types);
if (CollectionUtils.isEmpty(rows))
return;
List<UtmDataInputStatus> aws = rows.stream().filter(row -> row.getDataType().equalsIgnoreCase("aws")).collect(Collectors.toList());
List<UtmDataInputStatus> o365 = rows.stream().filter(row -> row.getDataType().equalsIgnoreCase("o365")).collect(Collectors.toList());
List<UtmDataInputStatus> hids = rows.stream().filter(row -> row.getDataType().equalsIgnoreCase("hids")).collect(Collectors.toList());
checkDataInputStatus(aws, "aws");
checkDataInputStatus(o365, "office365");
checkDataInputStatus(hids, "transporter");
} catch (Exception e) {
String msg = ctx + ": " + e.getMessage();
log.error(msg);
applicationEventService.createEvent(msg, ApplicationEventType.ERROR);
}
}
/**
* Checks the data input status. If any source stop sending logs it is marked to needed restart
*
* @param inputs Data input sources
* @param serverModule Name of the server module
* @throws Exception In case of any error
*/
private void checkDataInputStatus(List<UtmDataInputStatus> inputs, String serverModule) throws Exception {
final String ctx = CLASSNAME + ".checkDataInputStatus";
try {
if (CollectionUtils.isEmpty(inputs))
return;
long currentTimeInSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
List<UtmDataInputStatus> inTime = inputs.stream().filter(row -> (currentTimeInSeconds - row.getTimestamp()) < 3600)
.toList();
if (!CollectionUtils.isEmpty(inTime))
return;
List<UtmServerModule> modules = serverModuleService.findAllByModuleName(serverModule);
if (CollectionUtils.isEmpty(modules))
return;
modules.forEach(module -> module.setNeedsRestart(true));
serverModuleService.saveAll(modules);
} catch (Exception e) {
throw new Exception(ctx + ": " + e.getMessage());
}
}
@Scheduled(fixedDelay = 15000, initialDelay = 30000)
public void syncDataInputStatus() {
final String ctx = CLASSNAME + ".syncDataInputStatus";
try {
Map<String, StatisticDocument> latestStats = getLatestStatisticsByDataSource();
Map<String, UtmDataInputStatus> existing = dataInputStatusRepository.findAll()
.stream()
.collect(Collectors.toMap(
e -> e.getDataType() + "-" + e.getSource(),
Function.identity()
));
List<UtmDataInputStatus> toSave = new ArrayList<>();
latestStats.forEach((key, stat) -> {
try {
String dataType = stat.getDataType();
String dataSource = stat.getDataSource();
long timestamp = Instant.parse(stat.getTimestamp()).getEpochSecond();
String compositeKey = dataType + "-" + dataSource;
UtmDataInputStatus status = existing.get(compositeKey);
boolean changed = false;
if (status == null) {
status = UtmDataInputStatus.builder()
.id(compositeKey)
.dataType(dataType)
.source(dataSource)
.timestamp(timestamp)
.median(86400L)
.build();
changed = true;
} else if (status.getTimestamp() != timestamp) {
status.setTimestamp(timestamp);
changed = true;
}
if (changed) {
toSave.add(status);
}
} catch (Exception e) {
log.error("{}: Error processing dataType {} - {}", ctx, stat.getDataType(), e.getMessage(), e);
}
});
dataInputStatusRepository.saveAll(toSave);
} catch (Exception e) {
String msg = ctx + ": " + e.getMessage();
log.error(msg, e);
applicationEventService.createEvent(msg, ApplicationEventType.ERROR);
}
}
/**
* Gets the sources from utm_data_input_status that are not registered in utm_network_scan table
* and create new assets with it. This method is a schedule with a delay of 1 hour
*/
@Scheduled(fixedDelay = 30000, initialDelay = 60000)
public void syncSourcesToAssets() {
final String ctx = CLASSNAME + ".syncSourcesToAssets";
try {
synchronizeSourcesToAssets();
} catch (Exception e) {
String msg = ctx + ": " + e.getMessage();
log.error(msg);
applicationEventService.createEvent(msg, ApplicationEventType.ERROR);
}
}
@Transactional
public void synchronizeSourcesToAssets() {
final String ctx = CLASSNAME + ".syncSourcesToAssets";
try {
List<String> excludeDataTypes = dataTypesRepository.findAllByIncludedFalse()
.stream()
.map(UtmDataTypes::getDataType)
.collect(Collectors.toList());
excludeDataTypes.addAll(Arrays.asList("utmstack", "UTMStack", DataSourceConstants.IBM_AS400_TYPE));
List<UtmDataInputStatus> sources = dataInputStatusRepository.extractSourcesToExport(excludeDataTypes);
if (CollectionUtils.isEmpty(sources)) {
return;
}
Map<String, Boolean> sourcesWithStatus = extractSourcesWithUpDownStatus(sources);
List<String> keys = new ArrayList<>(sourcesWithStatus.keySet());
List<UtmNetworkScan> assets = networkScanRepository.findByAssetIpInOrAssetNameIn(keys, keys);
Map<String, UtmNetworkScan> assetsByKey = new HashMap<>();
assets.forEach(a -> {
if (StringUtils.hasText(a.getAssetIp())) assetsByKey.put(a.getAssetIp(), a);
if (StringUtils.hasText(a.getAssetName())) assetsByKey.put(a.getAssetName(), a);
});
for (Map.Entry<String, Boolean> entry : sourcesWithStatus.entrySet()) {
String key = entry.getKey();
Boolean alive = entry.getValue();
UtmNetworkScan asset = assetsByKey.get(key);
if (asset != null) {
if (asset.getUpdateLevel() == null || asset.getUpdateLevel().equals(UpdateLevel.DATASOURCE) || asset.getUpdateLevel().equals(UpdateLevel.AGENT)) {
asset.assetAlive(alive)
.updateLevel(UpdateLevel.DATASOURCE)
.assetStatus(AssetStatus.CHECK)
.modifiedAt(LocalDateTime.now().toInstant(ZoneOffset.UTC));
networkScanService.save(asset);
}
} else {
networkScanService.save(new UtmNetworkScan(key, alive));
}
}
assets.forEach(asset -> {
boolean missing = !sourcesWithStatus.containsKey(asset.getAssetIp())
&& !sourcesWithStatus.containsKey(asset.getAssetName());
if (missing && UpdateLevel.DATASOURCE.equals(asset.getUpdateLevel())) {
asset.assetStatus(AssetStatus.MISSING)
.updateLevel(null)
.modifiedAt(LocalDateTime.now().toInstant(ZoneOffset.UTC));
networkScanService.save(asset);
}
});
networkScanRepository.deleteAllAssetsByDataType(excludeDataTypes);
} catch (Exception e) {
log.error("{}: Error synchronizing sources to assets - {}", ctx, e.getMessage(), e);
throw new RuntimeException(ctx + ": " + e.getLocalizedMessage());
}
}
private Map<String, Boolean> extractSourcesWithUpDownStatus(List<UtmDataInputStatus> sources) {
Map<String, Boolean> upDown = new HashMap<>();
sources.forEach(src -> {
Boolean status = upDown.get(src.getSource());
if (!Objects.isNull(status)) {
if (!status && !src.isDown())
upDown.put(src.getSource(), true);
} else
upDown.put(src.getSource(), !src.isDown());
});
return upDown;
}
/**
* Check datasource from utm_data_input_status table that are not of type WORKSTATION and
* if any of them are down then create a new alert. This method is a schedule with a delay
* of 24 hour
*/
/*@Scheduled(fixedDelay = 24, timeUnit = TimeUnit.HOURS)
public void checkDatasourceDown() {
final String ctx = CLASSNAME + ".checkDatasourceDown";
try {
List<UtmDataInputStatus> sources = dataInputStatusRepository.findDatasourceToCheckIfDown();
if (CollectionUtils.isEmpty(sources))
return;
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy.MM.dd").withZone(ZoneOffset.UTC);
String index = String.format("alert-%1$s", f.format(LocalDateTime.now().toInstant(ZoneOffset.UTC)));
for (UtmDataInputStatus src : sources) {
if (src.isDown())
elasticsearchService.index(index, createAlertForDatasourceDown(src));
}
} catch (Exception e) {
String msg = ctx + ": " + e.getMessage();
log.error(msg);
applicationEventService.createEvent(msg, ApplicationEventType.ERROR);
}
}*/
/**
* Create the alert object for datasource down
*
* @param input: Datasource information
* @return A ${@link UtmAlert} to index
*/
private Map<String, Object> createAlertForDatasourceDown(UtmDataInputStatus input) {
List<String> cloudTypes = Arrays.asList("aws", "o365", "office365", "azure", "gcp", "google", "nids", "netflow");
Map<String, Object> src = new HashMap<>();
src.put("country", "");
src.put("accuracyRadius", 0);
src.put("city", "");
src.put("coordinates", new Float[]{});
src.put("port", 0);
src.put("countryCode", "");
src.put("isAnonymousProxy", false);
src.put("isSatelliteProvider", false);
src.put("aso", "");
src.put("asn", 0);
src.put("user", "");
if (InetAddressUtils.isIPv4Address(input.getSource()) || InetAddressUtils.isIPv6Address(input.getSource()))
src.put("ip", input.getSource());
else
src.put("host", input.getSource());
Map<String, Object> dst = new HashMap<>();
dst.put("country", "");
dst.put("accuracyRadius", 0);
dst.put("city", "");
dst.put("coordinates", new Float[]{});
dst.put("port", 0);
dst.put("countryCode", "");
dst.put("isAnonymousProxy", false);
dst.put("isSatelliteProvider", false);
dst.put("aso", "");
dst.put("asn", 0);
dst.put("user", "");
dst.put("ip", "");
dst.put("host", "");
Map<String, Object> incidentDetails = new HashMap<>();
incidentDetails.put("createdBy", "");
incidentDetails.put("observation", "");
incidentDetails.put("source", "");
incidentDetails.put("creationDate", "");
Map<String, Object> alert = new HashMap<>();
alert.put("id", UUID.randomUUID().toString());
alert.put("@timestamp", Instant.now().toString());
if (cloudTypes.contains(input.getDataType()))
alert.put("name", String.format("The %1$s datasource is taking longer than usual to send logs", input.getDataType()));
else
alert.put("name", String.format("The %1$s datasource installed in %2$s is taking longer than usual to send logs", input.getDataType(), input.getSource()));
alert.put("description", "UTMStack launched this alert because the device exceeded the expected average time in which it can be without sending any log");
alert.put("tactic", "Defense Evasion");
alert.put("reference", Collections.singletonList("https://attack.mitre.org/tactics/TA0005/"));
alert.put("status", AlertStatus.AUTOMATIC_REVIEW.getCode());
alert.put("statusLabel", AlertStatus.AUTOMATIC_REVIEW.getName());
alert.put("severity", AlertSeverityEnum.LOW.getCode());
alert.put("severityLabel", AlertSeverityEnum.LOW.getName());
alert.put("dataType", input.getDataType());
alert.put("dataSource", input.getSource());
alert.put("notes", "");
alert.put("tags", Collections.emptyList());
alert.put("logs", Collections.emptyList());
alert.put("protocol", "");
alert.put("source", src);
alert.put("destination", dst);
alert.put("isIncident", false);
alert.put("incidentDetail", incidentDetails);
alert.put("solution", "Check the data source configuration, error logs, and if it is an agent; verify if it is installed and the service is running");
alert.put("statusObservation", "The system changed the alert status to be analyzed by rule engine");
alert.put("category", "Data sources monitoring");
alert.put("TagRulesApplied", null);
return alert;
}
private Map<String, StatisticDocument> getLatestStatisticsByDataSource() {
UtmDataInputStatusCheckpoint checkpoint = this.checkpointRepository.findById(1L)
.orElseGet(() -> {
UtmDataInputStatusCheckpoint newCheckpoint = new UtmDataInputStatusCheckpoint();
newCheckpoint.setLastProcessedTimestamp(Instant.now().minus(1, ChronoUnit.HOURS));
return newCheckpoint;
});
ArrayList<FilterType> filters = new ArrayList<>();
filters.add(new FilterType("type", OperatorType.IS, "enqueue_success"));
filters.add(new FilterType("@timestamp", OperatorType.IS_GREATER_THAN, checkpoint.getLastProcessedTimestamp().toString()));
SearchRequest sr = SearchRequest.of(s -> s
.query(SearchUtil.toQuery(filters))
.index(Constants.STATISTICS_INDEX_PATTERN)
.collapse(c -> c
.field("dataSource.keyword")
.innerHits(ih -> ih
.name("latest")
.size(1)
.sort(sort -> sort.field(f -> f.field("@timestamp").order(SortOrder.Desc)))
)
)
.sort(sort -> sort.field(f -> f.field("@timestamp").order(SortOrder.Desc)))
.size(10000)
);
SearchResponse<StatisticDocument> response =
elasticsearchService.search(sr, StatisticDocument.class);
Map<String, StatisticDocument> result = new HashMap<>();
response.hits().hits().forEach(hit -> {
if (hit.innerHits() != null && hit.innerHits().containsKey("latest")) {
var inner = hit.innerHits().get("latest").hits().hits();
if (!inner.isEmpty()) {
JsonData json = inner.get(0).source();
if (json != null) {
StatisticDocument doc = json.to(StatisticDocument.class);
result.put(doc.getDataSource(), doc);
}
}
}
});
Optional<Instant> maybeLastTimestamp = result.values().stream()
.map(doc -> Instant.parse(doc.getTimestamp()))
.max(Instant::compareTo);
if (maybeLastTimestamp.isPresent()) {
checkpoint.setLastProcessedTimestamp(maybeLastTimestamp.get());
this.checkpointRepository.save(checkpoint);
}
return result;
}
}