forked from opensearch-project/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional tests for old privilege evaluation code
Signed-off-by: Nils Bandener <nils.bandener@eliatra.com>
- Loading branch information
Showing
26 changed files
with
4,526 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
546 changes: 546 additions & 0 deletions
546
src/integrationTest/java/org/opensearch/security/legacy/DoNotFailOnForbiddenTests.java
Large diffs are not rendered by default.
Oops, something went wrong.
417 changes: 417 additions & 0 deletions
417
src/integrationTest/java/org/opensearch/security/legacy/PointInTimeOperationTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
2,761 changes: 2,761 additions & 0 deletions
2,761
src/integrationTest/java/org/opensearch/security/legacy/SearchOperationTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
src/integrationTest/java/org/opensearch/security/legacy/SecurityRolesTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.legacy; | ||
|
||
import java.util.Map; | ||
|
||
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; | ||
import org.apache.http.HttpStatus; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import org.opensearch.security.privileges.PrivilegesEvaluator; | ||
import org.opensearch.test.framework.TestSecurityConfig; | ||
import org.opensearch.test.framework.TestSecurityConfig.Role; | ||
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.equalTo; | ||
import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AUTHC_HTTPBASIC_INTERNAL; | ||
|
||
@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class) | ||
@ThreadLeakScope(ThreadLeakScope.Scope.NONE) | ||
public class SecurityRolesTests { | ||
|
||
protected final static TestSecurityConfig.User USER_SR = new TestSecurityConfig.User("sr_user").roles( | ||
new Role("abc_ber").indexPermissions("*").on("*").clusterPermissions("*"), | ||
new Role("def_efg").indexPermissions("*").on("*").clusterPermissions("*") | ||
); | ||
|
||
@ClassRule | ||
public static LocalCluster cluster = new LocalCluster.Builder().clusterManager(ClusterManager.THREE_CLUSTER_MANAGERS) | ||
.anonymousAuth(true) | ||
.authc(AUTHC_HTTPBASIC_INTERNAL) | ||
.users(USER_SR) | ||
.nodeSettings(Map.of(PrivilegesEvaluator.USE_LEGACY_PRIVILEGE_EVALUATOR.getKey(), true)) | ||
.build(); | ||
|
||
@Test | ||
public void testSecurityRoles() throws Exception { | ||
try (TestRestClient client = cluster.getRestClient(USER_SR)) { | ||
HttpResponse response = client.getAuthInfo(); | ||
response.assertStatusCode(HttpStatus.SC_OK); | ||
|
||
// Check username | ||
assertThat(response.getTextFromJsonBody("/user_name"), equalTo("sr_user")); | ||
|
||
// Check security roles | ||
assertThat(response.getTextFromJsonBody("/roles/0"), equalTo("user_sr_user__abc_ber")); | ||
assertThat(response.getTextFromJsonBody("/roles/1"), equalTo("user_sr_user__def_efg")); | ||
|
||
} | ||
} | ||
|
||
} |
119 changes: 119 additions & 0 deletions
119
src/integrationTest/java/org/opensearch/security/legacy/SystemIndexTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* 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.security.legacy; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; | ||
import org.junit.Before; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.security.http.ExampleSystemIndexPlugin; | ||
import org.opensearch.security.privileges.PrivilegesEvaluator; | ||
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.equalTo; | ||
import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_ROLES_ENABLED; | ||
import static org.opensearch.security.support.ConfigConstants.SECURITY_SYSTEM_INDICES_ENABLED_KEY; | ||
import static org.opensearch.test.framework.TestSecurityConfig.Role.ALL_ACCESS; | ||
import static org.opensearch.test.framework.TestSecurityConfig.User.USER_ADMIN; | ||
|
||
@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class) | ||
@ThreadLeakScope(ThreadLeakScope.Scope.NONE) | ||
public class SystemIndexTests { | ||
|
||
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(ExampleSystemIndexPlugin.class) | ||
.nodeSettings( | ||
Map.of( | ||
SECURITY_RESTAPI_ROLES_ENABLED, | ||
List.of("user_" + USER_ADMIN.getName() + "__" + ALL_ACCESS.getName()), | ||
SECURITY_SYSTEM_INDICES_ENABLED_KEY, | ||
true, | ||
PrivilegesEvaluator.USE_LEGACY_PRIVILEGE_EVALUATOR.getKey(), | ||
true | ||
) | ||
) | ||
.build(); | ||
|
||
@Before | ||
public void setup() { | ||
try (TestRestClient client = cluster.getRestClient(cluster.getAdminCertificate())) { | ||
client.delete(".system-index1"); | ||
} | ||
} | ||
|
||
@Test | ||
public void adminShouldNotBeAbleToDeleteSecurityIndex() { | ||
try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { | ||
HttpResponse response = client.delete(".opendistro_security"); | ||
|
||
assertThat(response.getStatusCode(), equalTo(RestStatus.FORBIDDEN.getStatus())); | ||
|
||
// Create regular index | ||
client.put("test-index"); | ||
|
||
// regular user can delete non-system index | ||
HttpResponse response2 = client.delete("test-index"); | ||
|
||
assertThat(response2.getStatusCode(), equalTo(RestStatus.OK.getStatus())); | ||
|
||
// regular use can create system index | ||
HttpResponse response3 = client.put(".system-index1"); | ||
|
||
assertThat(response3.getStatusCode(), equalTo(RestStatus.OK.getStatus())); | ||
|
||
// regular user cannot delete system index | ||
HttpResponse response4 = client.delete(".system-index1"); | ||
|
||
assertThat(response4.getStatusCode(), equalTo(RestStatus.FORBIDDEN.getStatus())); | ||
} | ||
} | ||
|
||
@Test | ||
public void regularUserShouldGetNoResultsWhenSearchingSystemIndex() { | ||
// Create system index and index a dummy document as the super admin user, data returned to super admin | ||
try (TestRestClient client = cluster.getRestClient(cluster.getAdminCertificate())) { | ||
HttpResponse response1 = client.put(".system-index1"); | ||
|
||
assertThat(response1.getStatusCode(), equalTo(RestStatus.OK.getStatus())); | ||
String doc = "{\"field\":\"value\"}"; | ||
HttpResponse adminPostResponse = client.postJson(".system-index1/_doc/1?refresh=true", doc); | ||
assertThat(adminPostResponse.getStatusCode(), equalTo(RestStatus.CREATED.getStatus())); | ||
HttpResponse response2 = client.get(".system-index1/_search"); | ||
|
||
assertThat(response2.getStatusCode(), equalTo(RestStatus.OK.getStatus())); | ||
assertThat(response2.getBody(), response2.getBody().contains("\"hits\":{\"total\":{\"value\":1,\"relation\":\"eq\"}")); | ||
} | ||
|
||
// Regular users should not be able to read it | ||
try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { | ||
// regular user cannot read system index | ||
HttpResponse response1 = client.get(".system-index1/_search"); | ||
|
||
assertThat(response1.getBody(), response1.getBody().contains("\"hits\":{\"total\":{\"value\":0,\"relation\":\"eq\"}")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.