-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 2.x] Ensure that plugin can search on system index when uti…
- Loading branch information
Showing
7 changed files
with
223 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/integrationTest/java/org/opensearch/security/systemindex/SystemIndexDisabledTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...est/java/org/opensearch/security/systemindex/sampleplugin/RestGetOnSystemIndexAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); })); | ||
} | ||
}; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
.../java/org/opensearch/security/systemindex/sampleplugin/RestSearchOnSystemIndexAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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.search.SearchRequest; | ||
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.index.query.QueryBuilders; | ||
import org.opensearch.rest.BaseRestHandler; | ||
import org.opensearch.rest.BytesRestResponse; | ||
import org.opensearch.rest.RestChannel; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.search.builder.SearchSourceBuilder; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.opensearch.rest.RestRequest.Method.GET; | ||
|
||
public class RestSearchOnSystemIndexAction extends BaseRestHandler { | ||
|
||
private final RunAsSubjectClient pluginClient; | ||
|
||
public RestSearchOnSystemIndexAction(RunAsSubjectClient pluginClient) { | ||
this.pluginClient = pluginClient; | ||
} | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return singletonList(new Route(GET, "/search-on-system-index/{index}")); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "test_search_on_system_index_action"; | ||
} | ||
|
||
@Override | ||
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) { | ||
String indexName = request.param("index"); | ||
return new RestChannelConsumer() { | ||
|
||
@Override | ||
public void accept(RestChannel channel) throws Exception { | ||
SearchRequest searchRequest = new SearchRequest(indexName); | ||
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); | ||
sourceBuilder.query(QueryBuilders.matchAllQuery()); | ||
searchRequest.source(sourceBuilder); | ||
pluginClient.search(searchRequest, 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))); })); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters