Skip to content

Commit ec99e7e

Browse files
authored
Ensure that plugin can update on system index when utilizing pluginSubject.runAs (opensearch-project#5055)
Signed-off-by: Craig Perkins <cwperx@amazon.com>
1 parent 772eef6 commit ec99e7e

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

src/integrationTest/java/org/opensearch/security/systemindex/SystemIndexTests.java

+34
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,40 @@ public void testPluginShouldBeAbleGetOnItsSystemIndex() {
238238
assertThat(getResponse1.toPrettyString(), equalTo(getResponse2.toPrettyString()));
239239
}
240240

241+
@Test
242+
public void testPluginShouldBeAbleUpdateOnItsSystemIndex() {
243+
try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) {
244+
HttpResponse response = client.put("try-create-and-bulk-index/" + SYSTEM_INDEX_1);
245+
246+
assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
247+
248+
HttpResponse searchResponse = client.get("search-on-system-index/" + SYSTEM_INDEX_1);
249+
250+
assertThat(searchResponse.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
251+
assertThat(searchResponse.getIntFromJsonBody("/hits/total/value"), equalTo(2));
252+
253+
String docId = searchResponse.getTextFromJsonBody("/hits/hits/0/_id");
254+
255+
HttpResponse updateResponse = client.put("update-on-system-index/" + SYSTEM_INDEX_1 + "/" + docId);
256+
257+
updateResponse.assertStatusCode(RestStatus.OK.getStatus());
258+
}
259+
260+
try (TestRestClient client = cluster.getRestClient(cluster.getAdminCertificate())) {
261+
HttpResponse searchResponse = client.get(SYSTEM_INDEX_1 + "/_search");
262+
263+
searchResponse.assertStatusCode(RestStatus.OK.getStatus());
264+
265+
assertThat(searchResponse.getIntFromJsonBody("/hits/total/value"), equalTo(2));
266+
267+
String docId = searchResponse.getTextFromJsonBody("/hits/hits/0/_id");
268+
269+
HttpResponse getResponse = client.get(SYSTEM_INDEX_1 + "/_doc/" + docId);
270+
271+
assertThat("{\"content\":3}", equalTo(getResponse.bodyAsJsonNode().get("_source").toString()));
272+
}
273+
}
274+
241275
@Test
242276
public void testPluginShouldNotBeAbleToBulkIndexDocumentIntoMixOfSystemIndexWhereAtLeastOneDoesNotBelongToPlugin() {
243277
try (TestRestClient client = cluster.getRestClient(cluster.getAdminCertificate())) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* The OpenSearch Contributors require contributions made to
6+
* this file be licensed under the Apache-2.0 license or a
7+
* compatible open source license.
8+
*
9+
*/
10+
11+
package org.opensearch.security.systemindex.sampleplugin;
12+
13+
import java.util.List;
14+
15+
import org.opensearch.action.update.UpdateRequest;
16+
import org.opensearch.client.node.NodeClient;
17+
import org.opensearch.core.action.ActionListener;
18+
import org.opensearch.core.rest.RestStatus;
19+
import org.opensearch.core.xcontent.ToXContent;
20+
import org.opensearch.rest.BaseRestHandler;
21+
import org.opensearch.rest.BytesRestResponse;
22+
import org.opensearch.rest.RestChannel;
23+
import org.opensearch.rest.RestRequest;
24+
25+
import static java.util.Collections.singletonList;
26+
import static org.opensearch.rest.RestRequest.Method.PUT;
27+
28+
public class RestUpdateOnSystemIndexAction extends BaseRestHandler {
29+
30+
private final RunAsSubjectClient pluginClient;
31+
32+
public RestUpdateOnSystemIndexAction(RunAsSubjectClient pluginClient) {
33+
this.pluginClient = pluginClient;
34+
}
35+
36+
@Override
37+
public List<Route> routes() {
38+
return singletonList(new Route(PUT, "/update-on-system-index/{index}/{docId}"));
39+
}
40+
41+
@Override
42+
public String getName() {
43+
return "test_update_on_system_index_action";
44+
}
45+
46+
@Override
47+
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
48+
String indexName = request.param("index");
49+
String docId = request.param("docId");
50+
return new RestChannelConsumer() {
51+
52+
@Override
53+
public void accept(RestChannel channel) throws Exception {
54+
UpdateRequest updateRequest = new UpdateRequest();
55+
updateRequest.index(indexName);
56+
updateRequest.id(docId);
57+
updateRequest.doc("content", 3);
58+
pluginClient.update(updateRequest, ActionListener.wrap(r -> {
59+
channel.sendResponse(new BytesRestResponse(RestStatus.OK, r.toXContent(channel.newBuilder(), ToXContent.EMPTY_PARAMS)));
60+
}, fr -> { channel.sendResponse(new BytesRestResponse(RestStatus.FORBIDDEN, String.valueOf(fr))); }));
61+
}
62+
};
63+
}
64+
}

src/integrationTest/java/org/opensearch/security/systemindex/sampleplugin/SystemIndexPlugin1.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public List<RestHandler> getRestHandlers(
9090
new RestBulkIndexDocumentIntoSystemIndexAction(client, pluginClient),
9191
new RestBulkIndexDocumentIntoMixOfSystemIndexAction(client, pluginClient),
9292
new RestSearchOnSystemIndexAction(pluginClient),
93-
new RestGetOnSystemIndexAction(pluginClient)
93+
new RestGetOnSystemIndexAction(pluginClient),
94+
new RestUpdateOnSystemIndexAction(pluginClient)
9495
);
9596
}
9697

src/main/java/org/opensearch/security/configuration/DlsFlsValveImpl.java

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import org.opensearch.security.securityconf.impl.SecurityDynamicConfiguration;
8282
import org.opensearch.security.securityconf.impl.v7.RoleV7;
8383
import org.opensearch.security.support.ConfigConstants;
84+
import org.opensearch.security.support.HeaderHelper;
8485
import org.opensearch.threadpool.ThreadPool;
8586

8687
public class DlsFlsValveImpl implements DlsFlsRequestValve {
@@ -135,6 +136,9 @@ public DlsFlsValveImpl(
135136
*/
136137
@Override
137138
public boolean invoke(PrivilegesEvaluationContext context, final ActionListener<?> listener) {
139+
if (HeaderHelper.isInternalOrPluginRequest(threadContext)) {
140+
return true;
141+
}
138142
DlsFlsProcessedConfig config = this.dlsFlsProcessedConfig.get();
139143
ActionRequest request = context.getRequest();
140144
IndexResolverReplacer.Resolved resolved = context.getResolvedRequest();

0 commit comments

Comments
 (0)