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.
[Backport 2.x] Use SystemIndexRegistry from core to determine if requ…
…est contains system indices (opensearch-project#4550) Signed-off-by: Craig Perkins <cwperx@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
3149e76
commit ed67676
Showing
5 changed files
with
126 additions
and
13 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/integrationTest/java/org/opensearch/security/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,83 @@ | ||
/* | ||
* 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; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; | ||
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.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 | ||
) | ||
) | ||
.build(); | ||
|
||
@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())); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/integrationTest/java/org/opensearch/security/http/ExampleSystemIndexPlugin.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,27 @@ | ||
/* | ||
* 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.http; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.indices.SystemIndexDescriptor; | ||
import org.opensearch.plugins.Plugin; | ||
import org.opensearch.plugins.SystemIndexPlugin; | ||
|
||
public class ExampleSystemIndexPlugin extends Plugin implements SystemIndexPlugin { | ||
|
||
@Override | ||
public Collection<SystemIndexDescriptor> getSystemIndexDescriptors(Settings settings) { | ||
final SystemIndexDescriptor systemIndexDescriptor = new SystemIndexDescriptor(".system-index1", "System index 1"); | ||
return Collections.singletonList(systemIndexDescriptor); | ||
} | ||
} |
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
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