-
Notifications
You must be signed in to change notification settings - Fork 357
Create a mechanism for plugins to explicitly declare actions they need to perform with their assigned PluginSubject #5341
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
Changes from all commits
8a91e41
7dbf73f
8ff1525
7f35d02
06e9340
031cfac
d5f5b23
9e7ed97
12bc531
13f54f9
182e937
a5f90fe
46b4fb5
562e9be
2857354
3d52fcc
1bbc0e3
20437f8
094b36b
3edcdd8
c677d0b
5127e5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * 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.sample.secure; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; | ||
| import org.junit.ClassRule; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| import org.opensearch.Version; | ||
| import org.opensearch.core.rest.RestStatus; | ||
| import org.opensearch.painless.PainlessModulePlugin; | ||
| import org.opensearch.plugins.PluginInfo; | ||
| import org.opensearch.sample.SampleResourcePlugin; | ||
| import org.opensearch.security.OpenSearchSecurityPlugin; | ||
| import org.opensearch.test.framework.TestSecurityConfig.AuthcDomain; | ||
| import org.opensearch.test.framework.cluster.ClusterManager; | ||
| import org.opensearch.test.framework.cluster.LocalCluster; | ||
| import org.opensearch.test.framework.cluster.TestRestClient; | ||
| import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.opensearch.sample.utils.Constants.SAMPLE_RESOURCE_PLUGIN_PREFIX; | ||
| import static org.opensearch.security.support.ConfigConstants.SECURITY_SYSTEM_INDICES_ENABLED_KEY; | ||
| import static org.opensearch.test.framework.TestSecurityConfig.User.USER_ADMIN; | ||
|
|
||
| @RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class) | ||
| @ThreadLeakScope(ThreadLeakScope.Scope.NONE) | ||
| public class SecurePluginTests { | ||
|
|
||
| public static final AuthcDomain AUTHC_DOMAIN = new AuthcDomain("basic", 0).httpAuthenticatorWithChallenge("basic").backend("internal"); | ||
|
|
||
| @ClassRule | ||
| public static final LocalCluster cluster = new LocalCluster.Builder().clusterManager(ClusterManager.SINGLENODE) | ||
| .anonymousAuth(false) | ||
| .authc(AUTHC_DOMAIN) | ||
| .users(USER_ADMIN) | ||
| .plugin(PainlessModulePlugin.class) | ||
| .plugin( | ||
| new PluginInfo( | ||
| SampleResourcePlugin.class.getName(), | ||
| "classpath plugin", | ||
| "NA", | ||
| Version.CURRENT, | ||
| "1.8", | ||
| SampleResourcePlugin.class.getName(), | ||
| null, | ||
| List.of(OpenSearchSecurityPlugin.class.getName()), | ||
| false | ||
| ) | ||
| ) | ||
| .nodeSettings(Map.of(SECURITY_SYSTEM_INDICES_ENABLED_KEY, true)) | ||
| .build(); | ||
|
|
||
| @Test | ||
| public void testRunClusterHealthWithPluginSubject() { | ||
| try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { | ||
| HttpResponse response = client.postJson(SAMPLE_RESOURCE_PLUGIN_PREFIX + "/run_action", """ | ||
| { | ||
| "action": "cluster:monitor/health" | ||
| } | ||
| """); | ||
|
|
||
| assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus())); | ||
| assertThat(response.getBody(), containsString("number_of_nodes")); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testRunCreateIndexWithPluginSubject() { | ||
| try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { | ||
| HttpResponse response = client.postJson(SAMPLE_RESOURCE_PLUGIN_PREFIX + "/run_action", """ | ||
| { | ||
| "action": "indices:admin/create", | ||
| "index": "test-index" | ||
| } | ||
| """); | ||
|
|
||
| assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus())); | ||
| HttpResponse catIndicesResponse = client.get("_cat/indices"); | ||
| assertThat(catIndicesResponse.getBody(), containsString("test-index")); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * 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.sample.secure.actions.rest.create; | ||
|
|
||
| import org.opensearch.action.ActionType; | ||
|
|
||
| /** | ||
| * Action for testing running actions with PluginSubject | ||
| */ | ||
| public class SecurePluginAction extends ActionType<SecurePluginResponse> { | ||
| /** | ||
| * Secure plugin action instance | ||
| */ | ||
| public static final SecurePluginAction INSTANCE = new SecurePluginAction(); | ||
| /** | ||
| * Secure plugin action name | ||
| */ | ||
| public static final String NAME = "cluster:admin/sample-resource-plugin/run-actions"; | ||
|
|
||
| private SecurePluginAction() { | ||
| super(NAME, SecurePluginResponse::new); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * 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.sample.secure.actions.rest.create; | ||
|
|
||
| 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; | ||
|
|
||
| /** | ||
| * Request object for SecurePluginAction transport action | ||
| */ | ||
| public class SecurePluginRequest extends ActionRequest { | ||
|
|
||
| private final String action; | ||
| private final String index; | ||
|
|
||
| /** | ||
| * Default constructor | ||
| */ | ||
| public SecurePluginRequest(String action, String index) { | ||
| this.action = action; | ||
| this.index = index; | ||
| } | ||
|
|
||
| public SecurePluginRequest(StreamInput in) throws IOException { | ||
| this.action = in.readString(); | ||
| this.index = in.readString(); | ||
| } | ||
|
Check warning on line 37 in sample-resource-plugin/src/main/java/org/opensearch/sample/secure/actions/rest/create/SecurePluginRequest.java
|
||
|
|
||
| @Override | ||
| public void writeTo(final StreamOutput out) throws IOException { | ||
| out.writeString(action); | ||
| out.writeString(index); | ||
| } | ||
|
Check warning on line 43 in sample-resource-plugin/src/main/java/org/opensearch/sample/secure/actions/rest/create/SecurePluginRequest.java
|
||
|
|
||
| @Override | ||
| public ActionRequestValidationException validate() { | ||
| return null; | ||
| } | ||
|
|
||
| public String getAction() { | ||
| return this.action; | ||
| } | ||
|
|
||
| public String getIndex() { | ||
| return this.index; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * 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.sample.secure.actions.rest.create; | ||
|
|
||
| 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 CreateSampleResourceRequest | ||
| */ | ||
| public class SecurePluginResponse extends ActionResponse implements ToXContentObject { | ||
| private final String message; | ||
|
|
||
| /** | ||
| * Default constructor | ||
| * | ||
| * @param message The message | ||
| */ | ||
| public SecurePluginResponse(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| out.writeString(message); | ||
| } | ||
|
Check warning on line 37 in sample-resource-plugin/src/main/java/org/opensearch/sample/secure/actions/rest/create/SecurePluginResponse.java
|
||
|
|
||
| /** | ||
| * Constructor with StreamInput | ||
| * | ||
| * @param in the stream input | ||
| */ | ||
| public SecurePluginResponse(final StreamInput in) throws IOException { | ||
| message = in.readString(); | ||
| } | ||
|
Check warning on line 46 in sample-resource-plugin/src/main/java/org/opensearch/sample/secure/actions/rest/create/SecurePluginResponse.java
|
||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
| builder.startObject(); | ||
| builder.field("message", message); | ||
| builder.endObject(); | ||
| return builder; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.