|
18 | 18 | import org.junit.runner.RunWith;
|
19 | 19 |
|
20 | 20 | import org.opensearch.script.mustache.MustachePlugin;
|
| 21 | +import org.opensearch.script.mustache.RenderSearchTemplateAction; |
21 | 22 | import org.opensearch.test.framework.TestSecurityConfig;
|
22 | 23 | import org.opensearch.test.framework.TestSecurityConfig.Role;
|
23 | 24 | import org.opensearch.test.framework.cluster.ClusterManager;
|
@@ -49,15 +50,25 @@ public class PrivilegesEvaluatorTest {
|
49 | 50 | new Role("search_template_role").indexPermissions("read").on("services").clusterPermissions("cluster_composite_ops")
|
50 | 51 | );
|
51 | 52 |
|
| 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 | + |
52 | 60 | private String TEST_QUERY =
|
53 | 61 | "{\"source\":{\"query\":{\"match\":{\"service\":\"{{service_name}}\"}}},\"params\":{\"service_name\":\"Oracle\"}}";
|
54 | 62 |
|
55 | 63 | private String TEST_DOC = "{\"source\": {\"title\": \"Spirited Away\"}}";
|
56 | 64 |
|
| 65 | + private String TEST_RENDER_SEARCH_TEMPLATE_QUERY = |
| 66 | + "{\"params\":{\"status\":[\"pending\",\"published\"]},\"source\":\"{\\\"query\\\": {\\\"terms\\\": {\\\"status\\\": [\\\"{{#status}}\\\",\\\"{{.}}\\\",\\\"{{/status}}\\\"]}}}\"}"; |
| 67 | + |
57 | 68 | @ClassRule
|
58 | 69 | public static LocalCluster cluster = new LocalCluster.Builder().clusterManager(ClusterManager.THREE_CLUSTER_MANAGERS)
|
59 | 70 | .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) |
61 | 72 | .plugin(MustachePlugin.class)
|
62 | 73 | .build();
|
63 | 74 |
|
@@ -118,4 +129,28 @@ public void testSearchTemplateRequestUnauthorizedAllIndices() {
|
118 | 129 | assertThat(searchOnAllIndicesResponse.getStatusCode(), equalTo(HttpStatus.SC_FORBIDDEN));
|
119 | 130 | }
|
120 | 131 | }
|
| 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 | + } |
121 | 156 | }
|
0 commit comments