-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Riya Saxena <[email protected]>
- Loading branch information
1 parent
e3362f6
commit d0c9e3d
Showing
5 changed files
with
253 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/org/opensearch/securityanalytics/util/ErrorsHistoryIndex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.util; | ||
|
||
import kotlin.time.TimedValue; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.OpenSearchStatusException; | ||
import org.opensearch.action.admin.indices.create.CreateIndexRequest; | ||
import org.opensearch.action.admin.indices.create.CreateIndexResponse; | ||
import org.opensearch.action.index.IndexRequest; | ||
import org.opensearch.action.index.IndexResponse; | ||
import org.opensearch.action.support.WriteRequest; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.cluster.ClusterState; | ||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.common.UUIDs; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.common.unit.TimeValue; | ||
import org.opensearch.common.xcontent.XContentFactory; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.securityanalytics.action.IndexDetectorRequest; | ||
import org.opensearch.securityanalytics.config.monitors.DetectorMonitorConfig; | ||
import org.opensearch.securityanalytics.logtype.LogTypeService; | ||
import org.opensearch.securityanalytics.model.Detector; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.time.Instant; | ||
import java.util.*; | ||
|
||
public class ErrorsHistoryIndex { | ||
|
||
private static final Logger log = LogManager.getLogger(ErrorsHistoryIndex.class); | ||
|
||
private final Client client; | ||
|
||
private final ClusterService clusterService; | ||
|
||
private final ThreadPool threadPool; | ||
|
||
private final LogTypeService logTypeService; | ||
|
||
public ErrorsHistoryIndex(LogTypeService logTypeService, Client client, ClusterService clusterService, ThreadPool threadPool) { | ||
this.client = client; | ||
this.clusterService = clusterService; | ||
this.threadPool = threadPool; | ||
this.logTypeService = logTypeService; | ||
} | ||
|
||
public static String errorsHistoryIndexMappings() throws IOException { | ||
return new String(Objects.requireNonNull(RuleIndices.class.getClassLoader().getResourceAsStream("mappings/errorsHistory.json")).readAllBytes(), Charset.defaultCharset()); | ||
} | ||
public void initErrorsHistoryIndex(ActionListener<CreateIndexResponse> actionListener) throws IOException { | ||
Settings errorHistoryIndexSettings = Settings.builder() | ||
.put("index.hidden", true) | ||
.build(); | ||
CreateIndexRequest indexRequest = new CreateIndexRequest(DetectorMonitorConfig.OPENSEARCH_SAP_ERROR_INDEX) | ||
.mapping(errorsHistoryIndexMappings()) | ||
.settings(errorHistoryIndexSettings); | ||
client.admin().indices().create(indexRequest, actionListener); | ||
} | ||
|
||
public void addErrorsToSAPHistoryIndex(IndexDetectorRequest request, String exception, TimeValue indexTimeout, ActionListener<IndexResponse> actionListener) throws IOException { | ||
Detector detector = request.getDetector(); | ||
String ruleTopic = detector.getDetectorType(); | ||
String indexName = DetectorMonitorConfig.OPENSEARCH_SAP_ERROR_INDEX; | ||
Instant timestamp = detector.getLastUpdateTime(); | ||
String detectorId = detector.getId(); | ||
String operation = detectorId.isEmpty() ? "CREATE_DETECTOR" : "UPDATE_DETECTOR"; | ||
String user = detector.getUser() == null ? "user" : detector.getUser().getName(); | ||
|
||
XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); | ||
builder.field("detectorId", detectorId); | ||
builder.field("exception", exception); | ||
builder.field("timestamp", timestamp); | ||
builder.field("logType", ruleTopic); | ||
builder.field("operation", operation); | ||
builder.field("user", user); | ||
builder.endObject(); | ||
IndexRequest indexRequest = new IndexRequest(indexName) | ||
.id(UUIDs.base64UUID()) | ||
.source(builder) | ||
.timeout(indexTimeout) | ||
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); | ||
client.index(indexRequest, actionListener); | ||
} | ||
public void onCreateMappingsResponse(CreateIndexResponse response) { | ||
if (response.isAcknowledged()) { | ||
log.info(String.format(Locale.getDefault(), "Created %s with mappings.", DetectorMonitorConfig.OPENSEARCH_SAP_ERROR_INDEX)); | ||
} else { | ||
log.error(String.format(Locale.getDefault(), "Create %s mappings call not acknowledged.", DetectorMonitorConfig.OPENSEARCH_SAP_ERROR_INDEX)); | ||
throw new OpenSearchStatusException(String.format(Locale.getDefault(), "Create %s mappings call not acknowledged", Detector.DETECTORS_INDEX), RestStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
public boolean errorHistoryIndexExists() { | ||
ClusterState clusterState = clusterService.state(); | ||
return clusterState.getRoutingTable().hasIndex(DetectorMonitorConfig.OPENSEARCH_SAP_ERROR_INDEX); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"dynamic": "strict", | ||
"_meta" : { | ||
"schema_version": 1 | ||
}, | ||
"properties": { | ||
"detectorId": { | ||
"type": "keyword" | ||
}, | ||
"exception": { | ||
"type": "text" | ||
}, | ||
"timestamp": { | ||
"type": "text" | ||
}, | ||
"operation": { | ||
"type": "keyword" | ||
}, | ||
"logType": { | ||
"type": "keyword" | ||
}, | ||
"user": { | ||
"type": "text" | ||
} | ||
} | ||
} |
Oops, something went wrong.