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

changes to return empty search response for custom rules #231

Merged
merged 1 commit into from
Jan 5, 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 @@ -13,6 +13,7 @@
import org.opensearch.action.bulk.BulkResponse;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.action.search.ShardSearchFailure;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.action.support.WriteRequest;
Expand All @@ -24,6 +25,7 @@
import org.opensearch.common.unit.TimeValue;
import org.opensearch.index.reindex.BulkByScrollResponse;
import org.opensearch.rest.RestStatus;
import org.opensearch.search.internal.InternalSearchResponse;
import org.opensearch.securityanalytics.action.SearchRuleAction;
import org.opensearch.securityanalytics.action.SearchRuleRequest;
import org.opensearch.securityanalytics.settings.SecurityAnalyticsSettings;
Expand All @@ -47,7 +49,6 @@ public class TransportSearchRuleAction extends HandledTransportAction<SearchRule
private final ThreadPool threadPool;

private final ClusterService clusterService;

private final Settings settings;

private volatile TimeValue indexTimeout;
Expand Down Expand Up @@ -190,7 +191,16 @@ public void onFailure(Exception e) {
if (ruleIndices.ruleIndexExists(false)) {
search(request.getSearchRequest());
} else {
onFailures(new IllegalArgumentException("Custom rule index doesnt exist. Please create custom rules first."));
this.listener.onResponse(new SearchResponse(
InternalSearchResponse.empty(),
null,
1,
1,
0,
1,
ShardSearchFailure.EMPTY_ARRAY,
SearchResponse.Clusters.EMPTY
));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,19 @@ public void testSearchingPrepackagedRulesByAuthor() throws IOException {
Assert.assertEquals(17, ((Map<String, Object>) ((Map<String, Object>) responseBody.get("hits")).get("total")).get("value"));
}

public void testSearchingCustomRulesWhenNoneExist() throws IOException {
String request = "{\n" +
" \"query\": {\n" +
" \"match_all\": {}\n" +
" }\n" +
"}";

Response searchResponse = makeRequest(client(), "POST", String.format(Locale.getDefault(), "%s/_search", SecurityAnalyticsPlugin.RULE_BASE_URI), Collections.singletonMap("pre_packaged", "false"),
new StringEntity(request), new BasicHeader("Content-Type", "application/json"));
Assert.assertEquals("Searching rules failed", RestStatus.OK, restStatus(searchResponse));
Map<String, Object> responseBody = asMap(searchResponse);
Assert.assertEquals(0, ((Map<String, Object>) ((Map<String, Object>) responseBody.get("hits")).get("total")).get("value"));
}
@SuppressWarnings("unchecked")
public void testSearchingCustomRules() throws IOException {
String rule = randomRule();
Expand Down