Skip to content

Commit 5acefc9

Browse files
committed
Add render search template as a cluster permission (opensearch-project#3689)
Companion PRs in core: - opensearch-project/OpenSearch#11170 - opensearch-project/OpenSearch#11591 This PR adds render search template as a cluster perm so that its separately permissioned from a SearchTemplateRequest which needs a set of indices to authorize the request. The companion PR in core separates the transport actions that handle search template request and render search template request so that they can be authorized separately. I am opening this in Draft until the core PR is merged because this PR depends on the core PR. * Category (Enhancement, New feature, Bug fix, Test fix, Refactoring, Maintenance, Documentation) Bug fix - opensearch-project#3672 - [ ] New functionality includes testing - [ ] New functionality has been documented - [ ] Commits are signed per the DCO using --signoff By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. For more information on following Developer Certificate of Origin and signing off your commits, please check [here](https://github.com/opensearch-project/OpenSearch/blob/main/CONTRIBUTING.md#developer-certificate-of-origin). --------- Signed-off-by: Craig Perkins <[email protected]> (cherry picked from commit cc57710)
1 parent 45917e9 commit 5acefc9

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/integrationTest/java/org/opensearch/security/privileges/PrivilegesEvaluatorTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.junit.runner.RunWith;
1919

2020
import org.opensearch.script.mustache.MustachePlugin;
21+
import org.opensearch.script.mustache.RenderSearchTemplateAction;
2122
import org.opensearch.test.framework.TestSecurityConfig;
2223
import org.opensearch.test.framework.TestSecurityConfig.Role;
2324
import org.opensearch.test.framework.cluster.ClusterManager;
@@ -49,15 +50,25 @@ public class PrivilegesEvaluatorTest {
4950
new Role("search_template_role").indexPermissions("read").on("services").clusterPermissions("cluster_composite_ops")
5051
);
5152

53+
protected final static TestSecurityConfig.User RENDER_SEARCH_TEMPLATE = new TestSecurityConfig.User("render_search_template_user")
54+
.roles(
55+
new Role("render_search_template_role").indexPermissions("read")
56+
.on("services")
57+
.clusterPermissions(RenderSearchTemplateAction.NAME)
58+
);
59+
5260
private String TEST_QUERY =
5361
"{\"source\":{\"query\":{\"match\":{\"service\":\"{{service_name}}\"}}},\"params\":{\"service_name\":\"Oracle\"}}";
5462

5563
private String TEST_DOC = "{\"source\": {\"title\": \"Spirited Away\"}}";
5664

65+
private String TEST_RENDER_SEARCH_TEMPLATE_QUERY =
66+
"{\"params\":{\"status\":[\"pending\",\"published\"]},\"source\":\"{\\\"query\\\": {\\\"terms\\\": {\\\"status\\\": [\\\"{{#status}}\\\",\\\"{{.}}\\\",\\\"{{/status}}\\\"]}}}\"}";
67+
5768
@ClassRule
5869
public static LocalCluster cluster = new LocalCluster.Builder().clusterManager(ClusterManager.THREE_CLUSTER_MANAGERS)
5970
.authc(AUTHC_HTTPBASIC_INTERNAL)
60-
.users(NEGATIVE_LOOKAHEAD, NEGATED_REGEX, SEARCH_TEMPLATE, TestSecurityConfig.User.USER_ADMIN)
71+
.users(NEGATIVE_LOOKAHEAD, NEGATED_REGEX, SEARCH_TEMPLATE, RENDER_SEARCH_TEMPLATE, TestSecurityConfig.User.USER_ADMIN)
6172
.plugin(MustachePlugin.class)
6273
.build();
6374

@@ -118,4 +129,28 @@ public void testSearchTemplateRequestUnauthorizedAllIndices() {
118129
assertThat(searchOnAllIndicesResponse.getStatusCode(), equalTo(HttpStatus.SC_FORBIDDEN));
119130
}
120131
}
132+
133+
@Test
134+
public void testRenderSearchTemplateRequestFailure() {
135+
try (TestRestClient client = cluster.getRestClient(SEARCH_TEMPLATE)) {
136+
final String renderSearchTemplate = "_render/template";
137+
final TestRestClient.HttpResponse renderSearchTemplateResponse = client.postJson(
138+
renderSearchTemplate,
139+
TEST_RENDER_SEARCH_TEMPLATE_QUERY
140+
);
141+
assertThat(renderSearchTemplateResponse.getStatusCode(), equalTo(HttpStatus.SC_FORBIDDEN));
142+
}
143+
}
144+
145+
@Test
146+
public void testRenderSearchTemplateRequestSuccess() {
147+
try (TestRestClient client = cluster.getRestClient(RENDER_SEARCH_TEMPLATE)) {
148+
final String renderSearchTemplate = "_render/template";
149+
final TestRestClient.HttpResponse renderSearchTemplateResponse = client.postJson(
150+
renderSearchTemplate,
151+
TEST_RENDER_SEARCH_TEMPLATE_QUERY
152+
);
153+
assertThat(renderSearchTemplateResponse.getStatusCode(), equalTo(HttpStatus.SC_OK));
154+
}
155+
}
121156
}

src/main/java/org/opensearch/security/privileges/PrivilegesEvaluator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import org.opensearch.core.common.transport.TransportAddress;
8282
import org.opensearch.core.xcontent.NamedXContentRegistry;
8383
import org.opensearch.index.reindex.ReindexAction;
84+
import org.opensearch.script.mustache.RenderSearchTemplateAction;
8485
import org.opensearch.security.auditlog.AuditLog;
8586
import org.opensearch.security.configuration.ClusterInfoHolder;
8687
import org.opensearch.security.configuration.ConfigurationRepository;
@@ -697,8 +698,7 @@ public static boolean isClusterPerm(String action0) {
697698
|| (action0.startsWith(MultiSearchAction.NAME))
698699
|| (action0.equals(MultiTermVectorsAction.NAME))
699700
|| (action0.equals(ReindexAction.NAME))
700-
701-
);
701+
|| (action0.equals(RenderSearchTemplateAction.NAME)));
702702
}
703703

704704
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)