|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + * |
| 9 | + */ |
| 10 | +package org.opensearch.security.systemindex; |
| 11 | + |
| 12 | +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | + |
| 17 | +import org.opensearch.core.rest.RestStatus; |
| 18 | +import org.opensearch.test.framework.cluster.LocalCluster; |
| 19 | +import org.opensearch.test.framework.cluster.TestRestClient; |
| 20 | +import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse; |
| 21 | +import org.opensearch.test.framework.matcher.RestMatchers; |
| 22 | + |
| 23 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 24 | +import static org.hamcrest.Matchers.containsString; |
| 25 | +import static org.hamcrest.Matchers.equalTo; |
| 26 | +import static org.opensearch.security.systemindex.sampleplugin.SystemIndexPlugin1.SYSTEM_INDEX_1; |
| 27 | +import static org.opensearch.security.systemindex.sampleplugin.SystemIndexPlugin2.SYSTEM_INDEX_2; |
| 28 | +import static org.opensearch.test.framework.TestSecurityConfig.User.USER_ADMIN; |
| 29 | + |
| 30 | +@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class) |
| 31 | +@ThreadLeakScope(ThreadLeakScope.Scope.NONE) |
| 32 | +public class AbstractSystemIndexTests { |
| 33 | + |
| 34 | + private final LocalCluster cluster; |
| 35 | + |
| 36 | + protected AbstractSystemIndexTests(LocalCluster cluster) { |
| 37 | + this.cluster = cluster; |
| 38 | + } |
| 39 | + |
| 40 | + @Before |
| 41 | + public void setup() { |
| 42 | + try (TestRestClient client = cluster.getRestClient(cluster.getAdminCertificate())) { |
| 43 | + client.delete(".system-index1"); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void adminShouldNotBeAbleToDeleteSecurityIndex() { |
| 49 | + try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { |
| 50 | + HttpResponse response = client.delete(".opendistro_security"); |
| 51 | + |
| 52 | + assertThat(response.getStatusCode(), equalTo(RestStatus.FORBIDDEN.getStatus())); |
| 53 | + |
| 54 | + // Create regular index |
| 55 | + client.put("test-index"); |
| 56 | + |
| 57 | + // regular user can delete non-system index |
| 58 | + HttpResponse response2 = client.delete("test-index"); |
| 59 | + |
| 60 | + assertThat(response2.getStatusCode(), equalTo(RestStatus.OK.getStatus())); |
| 61 | + |
| 62 | + // regular use can create system index |
| 63 | + HttpResponse response3 = client.put(".system-index1"); |
| 64 | + |
| 65 | + assertThat(response3.getStatusCode(), equalTo(RestStatus.OK.getStatus())); |
| 66 | + |
| 67 | + // regular user cannot delete system index |
| 68 | + HttpResponse response4 = client.delete(".system-index1"); |
| 69 | + |
| 70 | + assertThat(response4.getStatusCode(), equalTo(RestStatus.FORBIDDEN.getStatus())); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testPluginShouldBeAbleToIndexDocumentIntoItsSystemIndex() { |
| 76 | + try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { |
| 77 | + HttpResponse response = client.put("try-create-and-index/" + SYSTEM_INDEX_1); |
| 78 | + |
| 79 | + assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus())); |
| 80 | + assertThat(response.getBody(), containsString("{\"acknowledged\":true}")); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testPluginShouldNotBeAbleToIndexDocumentIntoSystemIndexRegisteredByOtherPlugin() { |
| 86 | + try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { |
| 87 | + HttpResponse response = client.put("try-create-and-index/" + SYSTEM_INDEX_2); |
| 88 | + |
| 89 | + assertThat( |
| 90 | + response, |
| 91 | + RestMatchers.isForbidden( |
| 92 | + "/error/root_cause/0/reason", |
| 93 | + "no permissions for [] and User [name=plugin:org.opensearch.security.systemindex.sampleplugin.SystemIndexPlugin1" |
| 94 | + ) |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testPluginShouldBeAbleToCreateSystemIndexButUserShouldNotBeAbleToIndex() { |
| 101 | + try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { |
| 102 | + HttpResponse response = client.put("try-create-and-index/" + SYSTEM_INDEX_1 + "?runAs=user"); |
| 103 | + |
| 104 | + assertThat(response, RestMatchers.isForbidden("/error/root_cause/0/reason", "no permissions for [] and User [name=admin")); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testPluginShouldNotBeAbleToRunClusterActions() { |
| 110 | + try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { |
| 111 | + HttpResponse response = client.get("try-cluster-health/plugin"); |
| 112 | + |
| 113 | + assertThat( |
| 114 | + response, |
| 115 | + RestMatchers.isForbidden( |
| 116 | + "/error/root_cause/0/reason", |
| 117 | + "no permissions for [cluster:monitor/health] and User [name=plugin:org.opensearch.security.systemindex.sampleplugin.SystemIndexPlugin1" |
| 118 | + ) |
| 119 | + ); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testAdminUserShouldBeAbleToRunClusterActions() { |
| 125 | + try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { |
| 126 | + HttpResponse response = client.get("try-cluster-health/user"); |
| 127 | + |
| 128 | + assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus())); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testAuthenticatedUserShouldBeAbleToRunClusterActions() { |
| 134 | + try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { |
| 135 | + HttpResponse response = client.get("try-cluster-health/default"); |
| 136 | + |
| 137 | + assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus())); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testPluginShouldBeAbleToBulkIndexDocumentIntoItsSystemIndex() { |
| 143 | + try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { |
| 144 | + HttpResponse response = client.put("try-create-and-bulk-index/" + SYSTEM_INDEX_1); |
| 145 | + |
| 146 | + assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus())); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void testPluginShouldNotBeAbleToBulkIndexDocumentIntoMixOfSystemIndexWhereAtLeastOneDoesNotBelongToPlugin() { |
| 152 | + try (TestRestClient client = cluster.getRestClient(cluster.getAdminCertificate())) { |
| 153 | + client.put(".system-index1"); |
| 154 | + client.put(".system-index2"); |
| 155 | + } |
| 156 | + try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { |
| 157 | + HttpResponse response = client.put("try-create-and-bulk-mixed-index"); |
| 158 | + |
| 159 | + assertThat( |
| 160 | + response.getBody(), |
| 161 | + containsString( |
| 162 | + "no permissions for [] and User [name=plugin:org.opensearch.security.systemindex.sampleplugin.SystemIndexPlugin1" |
| 163 | + ) |
| 164 | + ); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void regularUserShouldGetNoResultsWhenSearchingSystemIndex() { |
| 170 | + // Create system index and index a dummy document as the super admin user, data returned to super admin |
| 171 | + try (TestRestClient client = cluster.getRestClient(cluster.getAdminCertificate())) { |
| 172 | + HttpResponse response1 = client.put(".system-index1"); |
| 173 | + |
| 174 | + assertThat(response1.getStatusCode(), equalTo(RestStatus.OK.getStatus())); |
| 175 | + String doc = "{\"field\":\"value\"}"; |
| 176 | + HttpResponse adminPostResponse = client.postJson(".system-index1/_doc/1?refresh=true", doc); |
| 177 | + assertThat(adminPostResponse.getStatusCode(), equalTo(RestStatus.CREATED.getStatus())); |
| 178 | + HttpResponse response2 = client.get(".system-index1/_search"); |
| 179 | + |
| 180 | + assertThat(response2.getStatusCode(), equalTo(RestStatus.OK.getStatus())); |
| 181 | + assertThat(response2.getBody(), response2.getBody().contains("\"hits\":{\"total\":{\"value\":1,\"relation\":\"eq\"}")); |
| 182 | + } |
| 183 | + |
| 184 | + // Regular users should not be able to read it |
| 185 | + try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { |
| 186 | + // regular user cannot read system index |
| 187 | + HttpResponse response1 = client.get(".system-index1/_search"); |
| 188 | + |
| 189 | + assertThat(response1.getBody(), response1.getBody().contains("\"hits\":{\"total\":{\"value\":0,\"relation\":\"eq\"}")); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments