Skip to content

Commit

Permalink
Add test for GET as well as search
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Jan 16, 2025
1 parent 2d4daab commit a06fbf4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,42 @@ public void testPluginShouldBeAbleSearchOnItsSystemIndex() {
assertThat(hits1.toPrettyString(), equalTo(hits2.toPrettyString()));
}

@Test
public void testPluginShouldBeAbleGetOnItsSystemIndex() {
JsonNode getResponse1;
JsonNode getResponse2;
try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) {
HttpResponse response = client.put("try-create-and-bulk-index/" + SYSTEM_INDEX_1);

assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus()));

HttpResponse searchResponse = client.get("search-on-system-index/" + SYSTEM_INDEX_1);

assertThat(searchResponse.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
assertThat(searchResponse.getIntFromJsonBody("/hits/total/value"), equalTo(2));

String docId = searchResponse.getTextFromJsonBody("/hits/hits/0/_id");

HttpResponse getResponse = client.get("get-on-system-index/" + SYSTEM_INDEX_1 + "/" + docId);

getResponse1 = getResponse.bodyAsJsonNode();
}

try (TestRestClient client = cluster.getRestClient(cluster.getAdminCertificate())) {
HttpResponse searchResponse = client.get(SYSTEM_INDEX_1 + "/_search");

assertThat(searchResponse.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
assertThat(searchResponse.getIntFromJsonBody("/hits/total/value"), equalTo(2));

String docId = searchResponse.getTextFromJsonBody("/hits/hits/0/_id");

HttpResponse getResponse = client.get(SYSTEM_INDEX_1 + "/_doc/" + docId);

getResponse2 = getResponse.bodyAsJsonNode();
}
assertThat(getResponse1.toPrettyString(), equalTo(getResponse2.toPrettyString()));
}

@Test
public void testPluginShouldNotBeAbleToBulkIndexDocumentIntoMixOfSystemIndexWhereAtLeastOneDoesNotBelongToPlugin() {
try (TestRestClient client = cluster.getRestClient(cluster.getAdminCertificate())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
*/

package org.opensearch.security.systemindex.sampleplugin;

import java.util.List;

import org.opensearch.action.get.GetRequest;
import org.opensearch.client.node.NodeClient;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestChannel;
import org.opensearch.rest.RestRequest;

import static java.util.Collections.singletonList;
import static org.opensearch.rest.RestRequest.Method.GET;

public class RestGetOnSystemIndexAction extends BaseRestHandler {

private final RunAsSubjectClient pluginClient;

public RestGetOnSystemIndexAction(RunAsSubjectClient pluginClient) {
this.pluginClient = pluginClient;
}

@Override
public List<Route> routes() {
return singletonList(new Route(GET, "/get-on-system-index/{index}/{docId}"));
}

@Override
public String getName() {
return "test_get_on_system_index_action";
}

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
String indexName = request.param("index");
String docId = request.param("docId");
return new RestChannelConsumer() {

@Override
public void accept(RestChannel channel) throws Exception {
GetRequest getRequest = new GetRequest(indexName);
getRequest.id(docId);
pluginClient.get(getRequest, ActionListener.wrap(r -> {
channel.sendResponse(new BytesRestResponse(RestStatus.OK, r.toXContent(channel.newBuilder(), ToXContent.EMPTY_PARAMS)));
}, fr -> { channel.sendResponse(new BytesRestResponse(RestStatus.FORBIDDEN, String.valueOf(fr))); }));
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public List<RestHandler> getRestHandlers(
new RestRunClusterHealthAction(client),
new RestBulkIndexDocumentIntoSystemIndexAction(client, pluginClient),
new RestBulkIndexDocumentIntoMixOfSystemIndexAction(client, pluginClient),
new RestSearchOnSystemIndexAction(pluginClient)
new RestSearchOnSystemIndexAction(pluginClient),
new RestGetOnSystemIndexAction(pluginClient)
);
}

Expand Down

0 comments on commit a06fbf4

Please sign in to comment.