Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correlation rule search, delete and edit api #476

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ public String getName() {
@Override
public List<Route> routes() {
return List.of(
new Route(RestRequest.Method.POST, SecurityAnalyticsPlugin.CORRELATION_RULES_BASE_URI)
new Route(RestRequest.Method.POST, SecurityAnalyticsPlugin.CORRELATION_RULES_BASE_URI),
new Route(RestRequest.Method.PUT, String.format(Locale.getDefault(),
"%s/{%s}",
SecurityAnalyticsPlugin.CORRELATION_RULES_BASE_URI,
"correlation_rule_id"))
);
}

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
log.debug(String.format(Locale.ROOT, "%s %s", request.method(), SecurityAnalyticsPlugin.CORRELATION_RULES_BASE_URI));

String id = request.param("rule_id", CorrelationRule.NO_ID);
String id = request.param("correlation_rule_id", CorrelationRule.NO_ID);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a breaking change from previous version?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this piece of code was not used in previous releases. today while writing integ tests i found this error. so changed this.


XContentParser xcp = request.contentParser();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected void doExecute(Task task, DeleteCorrelationRuleRequest request, Action
new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE)
.source(CorrelationRule.CORRELATION_RULE_INDEX)
.filter(QueryBuilders.matchQuery("_id", correlationRuleId))
.refresh(true)
.execute(new ActionListener<>() {
@Override
public void onResponse(BulkByScrollResponse response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package org.opensearch.securityanalytics.correlation;

import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicHeader;
import org.junit.Assert;
import org.opensearch.client.Response;
import org.opensearch.client.ResponseException;
Expand All @@ -13,6 +15,7 @@

import java.io.IOException;
import java.util.Collections;
import java.util.Map;

import static org.opensearch.securityanalytics.TestHelpers.randomCorrelationRule;

Expand All @@ -33,4 +36,81 @@ public void testCreateCorrelationRuleWithInvalidName() {
String actualMessage = exception.getMessage();
Assert.assertTrue(actualMessage.contains(expectedMessage));
}

@SuppressWarnings("unchecked")
public void testUpdateCorrelationRule() throws IOException {
CorrelationRule rule = randomCorrelationRule("custom-rule");
Response response = makeRequest(client(), "POST", SecurityAnalyticsPlugin.CORRELATION_RULES_BASE_URI, Collections.emptyMap(), toHttpEntity(rule));
Assert.assertEquals(201, response.getStatusLine().getStatusCode());
Map<String, Object> responseMap = responseAsMap(response);
Assert.assertEquals("custom-rule", ((Map<String, Object>) responseMap.get("rule")).get("name"));

String id = responseMap.get("_id").toString();

rule = randomCorrelationRule("custom-updated-rule");
response = makeRequest(client(), "PUT", SecurityAnalyticsPlugin.CORRELATION_RULES_BASE_URI + "/" + id, Collections.emptyMap(), toHttpEntity(rule));
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
responseMap = responseAsMap(response);
Assert.assertEquals("custom-updated-rule", ((Map<String, Object>) responseMap.get("rule")).get("name"));
}

@SuppressWarnings("unchecked")
public void testDeleteCorrelationRule() throws IOException {
CorrelationRule rule = randomCorrelationRule("custom-rule");
Response response = makeRequest(client(), "POST", SecurityAnalyticsPlugin.CORRELATION_RULES_BASE_URI, Collections.emptyMap(), toHttpEntity(rule));
Assert.assertEquals(201, response.getStatusLine().getStatusCode());
Map<String, Object> responseMap = responseAsMap(response);
Assert.assertEquals("custom-rule", ((Map<String, Object>) responseMap.get("rule")).get("name"));
String id = responseMap.get("_id").toString();

String request = "{\n" +
" \"query\" : {\n" +
" \"match_all\":{\n" +
" }\n" +
" }\n" +
"}";
response = makeRequest(client(), "POST", SecurityAnalyticsPlugin.CORRELATION_RULES_BASE_URI + "/_search", Collections.emptyMap(), new StringEntity(request), new BasicHeader("Content-type", "application/json"));
responseMap = responseAsMap(response);
Assert.assertEquals(1, Integer.parseInt(((Map<String, Object>) ((Map<String, Object>) responseMap.get("hits")).get("total")).get("value").toString()));

response = makeRequest(client(), "DELETE", SecurityAnalyticsPlugin.CORRELATION_RULES_BASE_URI + "/" + id, Collections.emptyMap(), null);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());

request = "{\n" +
" \"query\" : {\n" +
" \"match_all\":{\n" +
" }\n" +
" }\n" +
"}";
response = makeRequest(client(), "POST", SecurityAnalyticsPlugin.CORRELATION_RULES_BASE_URI + "/_search", Collections.emptyMap(), new StringEntity(request), new BasicHeader("Content-type", "application/json"));
responseMap = responseAsMap(response);
Assert.assertEquals(0, Integer.parseInt(((Map<String, Object>) ((Map<String, Object>) responseMap.get("hits")).get("total")).get("value").toString()));
}

@SuppressWarnings("unchecked")
public void testSearchCorrelationRule() throws IOException {
CorrelationRule rule = randomCorrelationRule("custom-rule");
Response response = makeRequest(client(), "POST", SecurityAnalyticsPlugin.CORRELATION_RULES_BASE_URI, Collections.emptyMap(), toHttpEntity(rule));
Assert.assertEquals(201, response.getStatusLine().getStatusCode());
Map<String, Object> responseMap = responseAsMap(response);
Assert.assertEquals("custom-rule", ((Map<String, Object>) responseMap.get("rule")).get("name"));

String request = "{\n" +
" \"query\": {\n" +
" \"nested\": {\n" +
" \"path\": \"correlate\",\n" +
" \"query\": {\n" +
" \"bool\": {\n" +
" \"must\": [\n" +
" { \"match\": {\"correlate.category\": \"network\"}}\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
response = makeRequest(client(), "POST", SecurityAnalyticsPlugin.CORRELATION_RULES_BASE_URI + "/_search", Collections.emptyMap(), new StringEntity(request), new BasicHeader("Content-type", "application/json"));
responseMap = responseAsMap(response);
Assert.assertEquals(1, Integer.parseInt(((Map<String, Object>) ((Map<String, Object>) responseMap.get("hits")).get("total")).get("value").toString()));
}
}