Skip to content

Commit

Permalink
changes to remove dummy detector and graceful implementation of searc…
Browse files Browse the repository at this point in the history
…h on name in TransportIndexDetectorAction

Signed-off-by: Raj Chakravarthi <[email protected]>
  • Loading branch information
raj-chak committed Jan 25, 2023
1 parent 07a134e commit cb1265f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,8 @@ public void onResponse(SearchResponse searchResponse) {
)
)
);
return;
} else {
continueCreatingDetector(detector);
}


Expand All @@ -790,7 +791,10 @@ public void onFailure(Exception e) {
}
});
}
}


void continueCreatingDetector(Detector detector){
String ruleTopic = detector.getDetectorType();

request.getDetector().setAlertsIndex(DetectorMonitorConfig.getAlertsIndex(ruleTopic));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1197,16 +1197,8 @@ boolean preserveODFEIndicesAfterTest() {
return false;
}

protected boolean keepDetectorConfigIndex() {
return false;
}

@After
protected void wipeAllODFEIndices() throws IOException {
wipeAllODFEIndices(keepDetectorConfigIndex());
}

protected void wipeAllODFEIndices(boolean keepDetectorConfigIndex) throws IOException {
if (preserveODFEIndicesAfterTest()) return;

Response response = client().performRequest(new Request("GET", "/_cat/indices?format=json&expand_wildcards=all"));
Expand All @@ -1224,15 +1216,12 @@ protected void wipeAllODFEIndices(boolean keepDetectorConfigIndex) throws IOExc
String indexName = jsonObject.get("index").toString();
// .opendistro_security isn't allowed to delete from cluster
if (!".opendistro_security".equals(indexName)) {
String detectorConfigIndex = keepDetectorConfigIndex?".opensearch-sap-detectors-config":"";
if (!detectorConfigIndex.equals(indexName)) {
Request request = new Request("DELETE", String.format(Locale.getDefault(), "/%s", indexName));
// TODO: remove PERMISSIVE option after moving system index access to REST API call
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.setWarningsHandler(WarningsHandler.PERMISSIVE);
request.setOptions(options.build());
adminClient().performRequest(request);
}
}
}
}
Expand Down Expand Up @@ -1522,32 +1511,4 @@ protected void createSampleDatastream(String datastreamName, String mappings) th

createDatastreamAPI(datastreamName);
}


// private String dummyIndex="";
//
// @Before
// void createDummyDetector() throws IOException {
// if ("".equals(dummyIndex)) {
// dummyIndex = createTestIndex(randomIndexDns(), dnsIndexMapping());
// //indexDoc(index, "1", randomDoc());
// // Execute CreateMappingsAction to add alias mapping for index
// Request createMappingRequest = new Request("POST", SecurityAnalyticsPlugin.MAPPER_BASE_URI);
// // both req params and req body are supported
// createMappingRequest.setJsonEntity(
// "{ \"index_name\":\"" + dummyIndex + "\"," +
// " \"rule_topic\":\"" + randomDetectorTypeDns() + "\", " +
// " \"partial\":true" +
// "}"
// );
//
// Response response = client().performRequest(createMappingRequest);
// assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
//
//
// Detector detector = randomDetectorDns(List.of(new String("8ae51330-899c-4641-8125-e39f2e07da72")));
//
// Response createResponse = makeRequest(client(), "POST", SecurityAnalyticsPlugin.DETECTOR_BASE_URI, Collections.emptyMap(), toHttpEntity(detector));
// }
// }
}
37 changes: 0 additions & 37 deletions src/test/java/org/opensearch/securityanalytics/TestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -1258,41 +1258,4 @@ public static NamedXContentRegistry xContentRegistry() {
public static XContentBuilder builder() throws IOException {
return XContentBuilder.builder(XContentType.JSON.xContent());
}

public static String dnsIndexMapping() {
return "\"properties\": {\n" +
" \"parent_domain\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"type\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"record_type\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"query\": {\n" +
" \"type\": \"text\",\n" +
" \"fields\": {\n" +
" \"keyword\": {\n" +
" \"type\": \"keyword\",\n" +
" \"ignore_above\": 256\n" +
" }\n" +
" }\n" +
" }\n" +
" }";
}

public static String randomDetectorTypeDns() {
return "dns";
}

public static String randomIndexDns() {
return "dns";
}

public static Detector randomDetectorDns(List<String> rules) {
DetectorInput input = new DetectorInput("dns detector for security analytics", List.of("dns"), Collections.emptyList(),
rules.stream().map(DetectorRule::new).collect(Collectors.toList()));
return randomDetector(null, Detector.DetectorType.valueOf(randomDetectorTypeDns().toUpperCase(Locale.ROOT)), null, List.of(input), List.of(), null, null, null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,40 +45,6 @@

public class DetectorRestApiIT extends SecurityAnalyticsRestTestCase {

private String dummyIndex="";

@Before
void createDummyDetector() throws IOException {
if ("".equals(dummyIndex)) {
dummyIndex = createTestIndex(randomIndexDns(), dnsIndexMapping());
//indexDoc(index, "1", randomDoc());
// Execute CreateMappingsAction to add alias mapping for index
Request createMappingRequest = new Request("POST", SecurityAnalyticsPlugin.MAPPER_BASE_URI);
// both req params and req body are supported
createMappingRequest.setJsonEntity(
"{ \"index_name\":\"" + dummyIndex + "\"," +
" \"rule_topic\":\"" + randomDetectorTypeDns() + "\", " +
" \"partial\":true" +
"}"
);

Response response = client().performRequest(createMappingRequest);
assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());


Detector detector = randomDetectorDns(List.of(new String("8ae51330-899c-4641-8125-e39f2e07da72")));

Response createResponse = makeRequest(client(), "POST", SecurityAnalyticsPlugin.DETECTOR_BASE_URI, Collections.emptyMap(), toHttpEntity(detector));

}
}

@Override
protected boolean keepDetectorConfigIndex() {
return true;
}


@SuppressWarnings("unchecked")
public void testCreatingADetector() throws IOException {
String index = createTestIndex(randomIndex(), windowsIndexMapping());
Expand Down

0 comments on commit cb1265f

Please sign in to comment.