Skip to content

Commit 1227f04

Browse files
committed
Run the same tests for both new style and legacy authz code paths
Signed-off-by: Craig Perkins <[email protected]>
1 parent 18e96f7 commit 1227f04

File tree

5 files changed

+230
-234
lines changed

5 files changed

+230
-234
lines changed

src/integrationTest/java/org/opensearch/security/legacy/SystemIndexTests.java

Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -13,107 +13,45 @@
1313
import java.util.Map;
1414

1515
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
16-
import org.junit.Before;
1716
import org.junit.ClassRule;
18-
import org.junit.Test;
1917
import org.junit.runner.RunWith;
2018

21-
import org.opensearch.core.rest.RestStatus;
22-
import org.opensearch.security.http.ExampleSystemIndexPlugin;
23-
import org.opensearch.security.privileges.PrivilegesEvaluator;
19+
import org.opensearch.security.systemindex.AbstractSystemIndexTests;
20+
import org.opensearch.security.systemindex.sampleplugin.SystemIndexPlugin1;
21+
import org.opensearch.security.systemindex.sampleplugin.SystemIndexPlugin2;
2422
import org.opensearch.test.framework.TestSecurityConfig.AuthcDomain;
2523
import org.opensearch.test.framework.cluster.ClusterManager;
2624
import org.opensearch.test.framework.cluster.LocalCluster;
27-
import org.opensearch.test.framework.cluster.TestRestClient;
28-
import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse;
2925

30-
import static org.hamcrest.MatcherAssert.assertThat;
31-
import static org.hamcrest.Matchers.equalTo;
3226
import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_ROLES_ENABLED;
3327
import static org.opensearch.security.support.ConfigConstants.SECURITY_SYSTEM_INDICES_ENABLED_KEY;
3428
import static org.opensearch.test.framework.TestSecurityConfig.Role.ALL_ACCESS;
3529
import static org.opensearch.test.framework.TestSecurityConfig.User.USER_ADMIN;
3630

3731
@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class)
3832
@ThreadLeakScope(ThreadLeakScope.Scope.NONE)
39-
public class SystemIndexTests {
33+
public class SystemIndexTests extends AbstractSystemIndexTests {
4034

4135
public static final AuthcDomain AUTHC_DOMAIN = new AuthcDomain("basic", 0).httpAuthenticatorWithChallenge("basic").backend("internal");
42-
4336
@ClassRule
4437
public static final LocalCluster cluster = new LocalCluster.Builder().clusterManager(ClusterManager.SINGLENODE)
4538
.anonymousAuth(false)
4639
.authc(AUTHC_DOMAIN)
4740
.users(USER_ADMIN)
48-
.plugin(ExampleSystemIndexPlugin.class)
41+
.plugin(SystemIndexPlugin1.class, SystemIndexPlugin2.class)
4942
.nodeSettings(
5043
Map.of(
5144
SECURITY_RESTAPI_ROLES_ENABLED,
5245
List.of("user_" + USER_ADMIN.getName() + "__" + ALL_ACCESS.getName()),
5346
SECURITY_SYSTEM_INDICES_ENABLED_KEY,
5447
true,
55-
PrivilegesEvaluator.USE_LEGACY_PRIVILEGE_EVALUATOR.getKey(),
48+
"plugins.security.privileges_evaluation.use_legacy_impl",
5649
true
5750
)
5851
)
5952
.build();
6053

61-
@Before
62-
public void setup() {
63-
try (TestRestClient client = cluster.getRestClient(cluster.getAdminCertificate())) {
64-
client.delete(".system-index1");
65-
}
66-
}
67-
68-
@Test
69-
public void adminShouldNotBeAbleToDeleteSecurityIndex() {
70-
try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) {
71-
HttpResponse response = client.delete(".opendistro_security");
72-
73-
assertThat(response.getStatusCode(), equalTo(RestStatus.FORBIDDEN.getStatus()));
74-
75-
// Create regular index
76-
client.put("test-index");
77-
78-
// regular user can delete non-system index
79-
HttpResponse response2 = client.delete("test-index");
80-
81-
assertThat(response2.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
82-
83-
// regular use can create system index
84-
HttpResponse response3 = client.put(".system-index1");
85-
86-
assertThat(response3.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
87-
88-
// regular user cannot delete system index
89-
HttpResponse response4 = client.delete(".system-index1");
90-
91-
assertThat(response4.getStatusCode(), equalTo(RestStatus.FORBIDDEN.getStatus()));
92-
}
93-
}
94-
95-
@Test
96-
public void regularUserShouldGetNoResultsWhenSearchingSystemIndex() {
97-
// Create system index and index a dummy document as the super admin user, data returned to super admin
98-
try (TestRestClient client = cluster.getRestClient(cluster.getAdminCertificate())) {
99-
HttpResponse response1 = client.put(".system-index1");
100-
101-
assertThat(response1.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
102-
String doc = "{\"field\":\"value\"}";
103-
HttpResponse adminPostResponse = client.postJson(".system-index1/_doc/1?refresh=true", doc);
104-
assertThat(adminPostResponse.getStatusCode(), equalTo(RestStatus.CREATED.getStatus()));
105-
HttpResponse response2 = client.get(".system-index1/_search");
106-
107-
assertThat(response2.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
108-
assertThat(response2.getBody(), response2.getBody().contains("\"hits\":{\"total\":{\"value\":1,\"relation\":\"eq\"}"));
109-
}
110-
111-
// Regular users should not be able to read it
112-
try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) {
113-
// regular user cannot read system index
114-
HttpResponse response1 = client.get(".system-index1/_search");
115-
116-
assertThat(response1.getBody(), response1.getBody().contains("\"hits\":{\"total\":{\"value\":0,\"relation\":\"eq\"}"));
117-
}
54+
public SystemIndexTests() {
55+
super(cluster);
11856
}
11957
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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

Comments
 (0)