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

Ensure that plugin can update on system index when utilizing pluginSubject.runAs #5055

Merged
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 @@ -238,6 +238,40 @@ public void testPluginShouldBeAbleGetOnItsSystemIndex() {
assertThat(getResponse1.toPrettyString(), equalTo(getResponse2.toPrettyString()));
}

@Test
public void testPluginShouldBeAbleUpdateOnItsSystemIndex() {
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 updateResponse = client.put("update-on-system-index/" + SYSTEM_INDEX_1 + "/" + docId);

updateResponse.assertStatusCode(RestStatus.OK.getStatus());
}

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

searchResponse.assertStatusCode(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);

assertThat("{\"content\":3}", equalTo(getResponse.bodyAsJsonNode().get("_source").toString()));
}
}

@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,64 @@
/*
* 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.update.UpdateRequest;
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.PUT;

public class RestUpdateOnSystemIndexAction extends BaseRestHandler {

private final RunAsSubjectClient pluginClient;

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

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

@Override
public String getName() {
return "test_update_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 {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(indexName);
updateRequest.id(docId);
updateRequest.doc("content", 3);
pluginClient.update(updateRequest, 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 @@ -90,7 +90,8 @@ public List<RestHandler> getRestHandlers(
new RestBulkIndexDocumentIntoSystemIndexAction(client, pluginClient),
new RestBulkIndexDocumentIntoMixOfSystemIndexAction(client, pluginClient),
new RestSearchOnSystemIndexAction(pluginClient),
new RestGetOnSystemIndexAction(pluginClient)
new RestGetOnSystemIndexAction(pluginClient),
new RestUpdateOnSystemIndexAction(pluginClient)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.opensearch.security.securityconf.impl.SecurityDynamicConfiguration;
import org.opensearch.security.securityconf.impl.v7.RoleV7;
import org.opensearch.security.support.ConfigConstants;
import org.opensearch.security.support.HeaderHelper;
import org.opensearch.threadpool.ThreadPool;

public class DlsFlsValveImpl implements DlsFlsRequestValve {
Expand Down Expand Up @@ -135,6 +136,9 @@ public DlsFlsValveImpl(
*/
@Override
public boolean invoke(PrivilegesEvaluationContext context, final ActionListener<?> listener) {
if (HeaderHelper.isInternalOrPluginRequest(threadContext)) {
return true;
}
cwperks marked this conversation as resolved.
Show resolved Hide resolved
DlsFlsProcessedConfig config = this.dlsFlsProcessedConfig.get();
ActionRequest request = context.getRequest();
IndexResolverReplacer.Resolved resolved = context.getResolvedRequest();
Expand Down
Loading