diff --git a/checkstyle/checkstyle.xml b/checkstyle/checkstyle.xml index 7571101556..7fe4a703de 100644 --- a/checkstyle/checkstyle.xml +++ b/checkstyle/checkstyle.xml @@ -228,12 +228,12 @@ - - - - - - + + + + + + diff --git a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/SampleExtensionPlugin.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/SampleExtensionPlugin.java index aa48c54d7f..7925377f5c 100644 --- a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/SampleExtensionPlugin.java +++ b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/SampleExtensionPlugin.java @@ -47,9 +47,6 @@ import org.opensearch.security.sampleextension.actions.list.ListSampleResourceAction; import org.opensearch.security.sampleextension.actions.list.ListSampleResourceRestAction; import org.opensearch.security.sampleextension.actions.list.ListSampleResourceTransportAction; -import org.opensearch.security.sampleextension.actions.sharing.UpdateSampleResourceSharingAction; -import org.opensearch.security.sampleextension.actions.sharing.UpdateSampleResourceSharingRestAction; -import org.opensearch.security.sampleextension.actions.sharing.UpdateSampleResourceSharingTransportAction; import org.opensearch.security.sampleextension.actions.update.UpdateSampleResourceAction; import org.opensearch.security.sampleextension.actions.update.UpdateSampleResourceRestAction; import org.opensearch.security.sampleextension.actions.update.UpdateSampleResourceTransportAction; @@ -117,8 +114,7 @@ public List getRestHandlers( new CreateSampleResourceRestAction(), new GetSampleResourceRestAction(), new ListSampleResourceRestAction(), - new UpdateSampleResourceRestAction(), - new UpdateSampleResourceSharingRestAction() + new UpdateSampleResourceRestAction() ); } @@ -128,7 +124,6 @@ public List getRestHandlers( new ActionHandler<>(CreateSampleResourceAction.INSTANCE, CreateSampleResourceTransportAction.class), new ActionHandler<>(GetSampleResourceAction.INSTANCE, GetSampleResourceTransportAction.class), new ActionHandler<>(ListSampleResourceAction.INSTANCE, ListSampleResourceTransportAction.class), - new ActionHandler<>(UpdateSampleResourceSharingAction.INSTANCE, UpdateSampleResourceSharingTransportAction.class), new ActionHandler<>(UpdateSampleResourceAction.INSTANCE, UpdateSampleResourceTransportAction.class) ); } diff --git a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/sharing/UpdateSampleResourceSharingAction.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/sharing/UpdateSampleResourceSharingAction.java deleted file mode 100644 index 439c0db520..0000000000 --- a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/sharing/UpdateSampleResourceSharingAction.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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.sampleextension.actions.sharing; - -import org.opensearch.action.ActionType; -import org.opensearch.security.spi.actions.sharing.update.UpdateResourceSharingResponse; - -/** - * Action to update sharing configuration for a sample resource - */ -public class UpdateSampleResourceSharingAction extends ActionType { - /** - * Update sharing configuratino for sample resource action instance - */ - public static final UpdateSampleResourceSharingAction INSTANCE = new UpdateSampleResourceSharingAction(); - /** - * Update sharing configuration for sample resource action name - */ - public static final String NAME = "cluster:admin/sampleresource/updatesharing"; - - private UpdateSampleResourceSharingAction() { - super(NAME, UpdateResourceSharingResponse::new); - } -} diff --git a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/sharing/UpdateSampleResourceSharingRestAction.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/sharing/UpdateSampleResourceSharingRestAction.java deleted file mode 100644 index 42915299b9..0000000000 --- a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/sharing/UpdateSampleResourceSharingRestAction.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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.sampleextension.actions.sharing; - -import java.io.IOException; -import java.util.List; -import java.util.Map; - -import org.opensearch.client.node.NodeClient; -import org.opensearch.core.xcontent.XContentParser; -import org.opensearch.rest.BaseRestHandler; -import org.opensearch.rest.RestRequest; -import org.opensearch.rest.action.RestToXContentListener; -import org.opensearch.security.sampleextension.resource.SampleResource; -import org.opensearch.security.spi.ShareWith; -import org.opensearch.security.spi.actions.sharing.update.UpdateResourceSharingRequest; - -import static java.util.Collections.singletonList; -import static org.opensearch.rest.RestRequest.Method.PUT; - -public class UpdateSampleResourceSharingRestAction extends BaseRestHandler { - - public UpdateSampleResourceSharingRestAction() {} - - @Override - public List routes() { - return singletonList(new Route(PUT, "/_plugins/resource_sharing_example/resource/update_sharing/{id}")); - } - - @Override - public String getName() { - return "update_sample_resource_sharing"; - } - - @SuppressWarnings("unchecked") - @Override - public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { - String resourceId = request.param("id"); - - System.out.println("update sharing endpoint"); - System.out.println("resourceId: " + resourceId); - Map source; - try (XContentParser parser = request.contentParser()) { - source = parser.map(); - } - - Map shareWithMap = (Map) source.get("share_with"); - ShareWith shareWith = new ShareWith( - (List) shareWithMap.get("users"), - (List) shareWithMap.get("backend_roles"), - (List) shareWithMap.get("allowed_actions") - ); - - final UpdateResourceSharingRequest updateSampleResourceSharingRequest = new UpdateResourceSharingRequest<>( - resourceId, - shareWith - ); - return channel -> client.executeLocally( - UpdateSampleResourceSharingAction.INSTANCE, - updateSampleResourceSharingRequest, - new RestToXContentListener<>(channel) - ); - } -} diff --git a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/sharing/UpdateSampleResourceSharingTransportAction.java b/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/sharing/UpdateSampleResourceSharingTransportAction.java deleted file mode 100644 index d21cd43f28..0000000000 --- a/sample-extension-plugin/src/main/java/org/opensearch/security/sampleextension/actions/sharing/UpdateSampleResourceSharingTransportAction.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.sampleextension.actions.sharing; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import org.opensearch.action.support.ActionFilters; -import org.opensearch.client.Client; -import org.opensearch.common.inject.Inject; -import org.opensearch.security.sampleextension.resource.SampleResource; -import org.opensearch.security.spi.ShareWith; -import org.opensearch.security.spi.actions.sharing.update.UpdateResourceSharingTransportAction; -import org.opensearch.transport.TransportService; - -import static org.opensearch.security.sampleextension.SampleExtensionPlugin.RESOURCE_INDEX_NAME; - -/** - * Transport action for UpdateSampleResourceSharing. - */ -public class UpdateSampleResourceSharingTransportAction extends UpdateResourceSharingTransportAction { - private static final Logger log = LogManager.getLogger(UpdateSampleResourceSharingTransportAction.class); - - @Inject - public UpdateSampleResourceSharingTransportAction(TransportService transportService, ActionFilters actionFilters, Client nodeClient) { - super(transportService, actionFilters, nodeClient, UpdateSampleResourceSharingAction.NAME, RESOURCE_INDEX_NAME, ShareWith::new); - } -} diff --git a/sample-extension-plugin/src/test/java/org/opensearch/security/sampleextension/SampleExtensionPluginIT.java b/sample-extension-plugin/src/test/java/org/opensearch/security/sampleextension/SampleExtensionPluginIT.java index 3fec88b4bd..6bb3a88e56 100644 --- a/sample-extension-plugin/src/test/java/org/opensearch/security/sampleextension/SampleExtensionPluginIT.java +++ b/sample-extension-plugin/src/test/java/org/opensearch/security/sampleextension/SampleExtensionPluginIT.java @@ -120,7 +120,7 @@ public void testCreateSampleResource() throws IOException, InterruptedException ).map(); System.out.println("resourceSharingResponse: " + resourceSharingResponseMap); - Request updateSharingRequest = new Request("PUT", "/_plugins/resource_sharing_example/resource/update_sharing/" + resourceId); + Request updateSharingRequest = new Request("PUT", "/_plugins/_security/resource/sample_resource/" + resourceId + "/share_with"); updateSharingRequest.setEntity( new StringEntity("{\"share_with\":{\"users\": [\"admin\"], \"backend_roles\": [], \"allowed_actions\": [\"*\"]}}") ); diff --git a/spi/src/main/java/org/opensearch/security/spi/actions/sharing/update/UpdateResourceSharingRequest.java b/spi/src/main/java/org/opensearch/security/spi/actions/sharing/update/UpdateResourceSharingRequest.java deleted file mode 100644 index af201a6e45..0000000000 --- a/spi/src/main/java/org/opensearch/security/spi/actions/sharing/update/UpdateResourceSharingRequest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.spi.actions.sharing.update; - -import java.io.IOException; - -import org.opensearch.action.ActionRequest; -import org.opensearch.action.ActionRequestValidationException; -import org.opensearch.core.common.io.stream.StreamInput; -import org.opensearch.core.common.io.stream.StreamOutput; -import org.opensearch.security.spi.Resource; -import org.opensearch.security.spi.ShareWith; - -/** - * Request object for UpdateResourceSharing transport action - */ -public class UpdateResourceSharingRequest extends ActionRequest { - - private final String resourceId; - private final ShareWith shareWith; - - /** - * Default constructor - */ - public UpdateResourceSharingRequest(String resourceId, ShareWith shareWith) { - this.resourceId = resourceId; - this.shareWith = shareWith; - } - - public UpdateResourceSharingRequest(StreamInput in, Reader shareWithReader) throws IOException { - this.resourceId = in.readString(); - this.shareWith = shareWithReader.read(in); - } - - @Override - public void writeTo(final StreamOutput out) throws IOException { - out.writeString(resourceId); - shareWith.writeTo(out); - } - - @Override - public ActionRequestValidationException validate() { - return null; - } - - public String getResourceId() { - return this.resourceId; - } - - public ShareWith getShareWith() { - return this.shareWith; - } -} diff --git a/spi/src/main/java/org/opensearch/security/spi/actions/sharing/update/UpdateResourceSharingResponse.java b/spi/src/main/java/org/opensearch/security/spi/actions/sharing/update/UpdateResourceSharingResponse.java deleted file mode 100644 index 2e04572543..0000000000 --- a/spi/src/main/java/org/opensearch/security/spi/actions/sharing/update/UpdateResourceSharingResponse.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.spi.actions.sharing.update; - -import java.io.IOException; - -import org.opensearch.core.action.ActionResponse; -import org.opensearch.core.common.io.stream.StreamInput; -import org.opensearch.core.common.io.stream.StreamOutput; -import org.opensearch.core.xcontent.ToXContentObject; -import org.opensearch.core.xcontent.XContentBuilder; - -/** - * Response to a UpdateResourceSharingRequest - */ -public class UpdateResourceSharingResponse extends ActionResponse implements ToXContentObject { - private final String message; - - /** - * Default constructor - * - * @param message The message - */ - public UpdateResourceSharingResponse(String message) { - this.message = message; - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeString(message); - } - - /** - * Constructor with StreamInput - * - * @param in the stream input - */ - public UpdateResourceSharingResponse(final StreamInput in) throws IOException { - message = in.readString(); - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - builder.field("message", message); - builder.endObject(); - return builder; - } -} diff --git a/spi/src/main/java/org/opensearch/security/spi/actions/sharing/update/UpdateResourceSharingTransportAction.java b/spi/src/main/java/org/opensearch/security/spi/actions/sharing/update/UpdateResourceSharingTransportAction.java deleted file mode 100644 index 07892dd0e0..0000000000 --- a/spi/src/main/java/org/opensearch/security/spi/actions/sharing/update/UpdateResourceSharingTransportAction.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * 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.spi.actions.sharing.update; - -import java.io.IOException; -import java.util.Objects; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import org.opensearch.action.search.SearchRequest; -import org.opensearch.action.search.SearchResponse; -import org.opensearch.action.support.ActionFilters; -import org.opensearch.action.support.HandledTransportAction; -import org.opensearch.action.update.UpdateRequest; -import org.opensearch.action.update.UpdateResponse; -import org.opensearch.client.Client; -import org.opensearch.common.util.concurrent.ThreadContext; -import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.core.action.ActionListener; -import org.opensearch.core.common.io.stream.Writeable; -import org.opensearch.core.xcontent.XContentBuilder; -import org.opensearch.index.query.BoolQueryBuilder; -import org.opensearch.index.query.QueryBuilders; -import org.opensearch.search.SearchHit; -import org.opensearch.search.builder.SearchSourceBuilder; -import org.opensearch.security.spi.Resource; -import org.opensearch.security.spi.ShareWith; -import org.opensearch.tasks.Task; -import org.opensearch.transport.TransportService; - -/** - * Transport action for UpdateResourceSharing. - */ -public class UpdateResourceSharingTransportAction extends HandledTransportAction< - UpdateResourceSharingRequest, - UpdateResourceSharingResponse> { - private static final Logger log = LogManager.getLogger(UpdateResourceSharingTransportAction.class); - - public static final String RESOURCE_SHARING_INDEX = ".resource-sharing"; - - private final TransportService transportService; - private final Client nodeClient; - private final String resourceIndex; - - public UpdateResourceSharingTransportAction( - TransportService transportService, - ActionFilters actionFilters, - Client nodeClient, - String actionName, - String resourceIndex, - Writeable.Reader shareWithReader - ) { - super(actionName, transportService, actionFilters, (in) -> new UpdateResourceSharingRequest(in, shareWithReader)); - this.transportService = transportService; - this.nodeClient = nodeClient; - this.resourceIndex = resourceIndex; - } - - @Override - protected void doExecute(Task task, UpdateResourceSharingRequest request, ActionListener listener) { - try (ThreadContext.StoredContext ignore = transportService.getThreadPool().getThreadContext().stashContext()) { - SearchRequest searchRequest = new SearchRequest(RESOURCE_SHARING_INDEX); - SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); - BoolQueryBuilder boolQuery = QueryBuilders.boolQuery() - .must(QueryBuilders.matchQuery("resource_id", request.getResourceId())) - .must(QueryBuilders.matchQuery("resource_index", resourceIndex)); - searchSourceBuilder.query(boolQuery); - searchRequest.source(searchSourceBuilder); - - // Execute the search request - nodeClient.search(searchRequest, new ActionListener() { - @Override - public void onResponse(SearchResponse searchResponse) { - if (Objects.requireNonNull(searchResponse.getHits().getTotalHits()).value == 1) { - // Record found, update it - SearchHit hit = searchResponse.getHits().getAt(0); - UpdateRequest updateRequest = new UpdateRequest(RESOURCE_SHARING_INDEX, hit.getId()); - try { - XContentBuilder builder = XContentFactory.jsonBuilder(); - builder.startObject(); - { - builder.startObject("share_with"); - { - builder.field("users", request.getShareWith().getUsers()); - builder.field("backend_roles", request.getShareWith().getBackendRoles()); - builder.field("allowed_actions", request.getShareWith().getAllowedActions()); - } - builder.endObject(); - } - builder.endObject(); - updateRequest.doc(builder); - - nodeClient.update(updateRequest, new ActionListener() { - @Override - public void onResponse(UpdateResponse updateResponse) { - listener.onResponse(new UpdateResourceSharingResponse("success")); - } - - @Override - public void onFailure(Exception e) { - listener.onFailure(e); - } - }); - } catch (IOException e) { - listener.onFailure(e); - } - } else { - // Record not found, create a new one - // createNewRecord(request, listener); - listener.onFailure(new IllegalStateException(".resource-sharing entry not found")); - } - } - - @Override - public void onFailure(Exception e) { - listener.onFailure(e); - } - }); - } - } -}