Skip to content

Commit

Permalink
changes to return empty serach response for custom rules (#231)
Browse files Browse the repository at this point in the history
Signed-off-by: Raj Chakravarthi <[email protected]>

Signed-off-by: Raj Chakravarthi <[email protected]>
(cherry picked from commit 2fcbdba)
  • Loading branch information
raj-chak authored and github-actions[bot] committed Jan 5, 2023
1 parent ddbcb21 commit 2f2160e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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

0 comments on commit 2f2160e

Please sign in to comment.