-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-Off-By: Nils Bandener <[email protected]>
- Loading branch information
Showing
23 changed files
with
4,116 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2015-2018 _floragunn_ GmbH | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* 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.node; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.plugins.Plugin; | ||
|
||
public class PluginAwareNode extends Node { | ||
|
||
private final boolean clusterManagerEligible; | ||
|
||
@SafeVarargs | ||
public PluginAwareNode(boolean clusterManagerEligible, final Settings preparedSettings, final Class<? extends Plugin>... plugins) { | ||
super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, Collections.emptyMap(), null, () -> System.getenv("HOSTNAME")), Arrays.asList(plugins), true); | ||
this.clusterManagerEligible = clusterManagerEligible; | ||
} | ||
|
||
|
||
public boolean isClusterManagerEligible() { | ||
return clusterManagerEligible; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/newTest/java/org/opensearch/test/AbstractIntegrationTest.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,36 @@ | ||
/* | ||
* 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.test; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.opensearch.test.framework.TestSecurityConfig; | ||
import org.opensearch.test.framework.TestSecurityConfig.Role; | ||
|
||
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; | ||
|
||
@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class) | ||
@ThreadLeakScope(ThreadLeakScope.Scope.NONE) | ||
public class AbstractIntegrationTest { | ||
|
||
/** | ||
* Auth domain with HTTPS Basic and the internal user backend | ||
*/ | ||
protected final static TestSecurityConfig.AuthcDomain AUTHC_HTTPBASIC_INTERNAL = new TestSecurityConfig.AuthcDomain("basic", 0) | ||
.httpAuthenticator("basic").backend("internal"); | ||
|
||
/** | ||
* Admin user with full access to all indices | ||
*/ | ||
protected final static TestSecurityConfig.User USER_ADMIN = new TestSecurityConfig.User("admin") | ||
.roles(new Role("allaccess").indexPermissions("*").on("*").clusterPermissions("*")); | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
src/newTest/java/org/opensearch/test/GenericIntegrationTest.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,79 @@ | ||
/* | ||
* 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.test; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.junit.AfterClass; | ||
import org.junit.Assert; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.opensearch.test.framework.TestIndex; | ||
import org.opensearch.test.framework.TestSecurityConfig; | ||
import org.opensearch.test.framework.TestSecurityConfig.Role; | ||
import org.opensearch.test.framework.cluster.ClusterConfiguration; | ||
import org.opensearch.test.framework.cluster.LocalCluster; | ||
import org.opensearch.test.framework.cluster.TestRestClient; | ||
import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse; | ||
|
||
import com.fasterxml.jackson.core.JsonPointer; | ||
|
||
/** | ||
* WIP | ||
* Generic test class that demonstrates how to use the test framework to | ||
* set up a test cluster with users, roles, indices and data, and how to | ||
* implement tests. One main goal here is to make tests self-contained. | ||
*/ | ||
public class GenericIntegrationTest extends AbstractIntegrationTest { | ||
|
||
// define indices used in this test | ||
private final static TestIndex INDEX_A = TestIndex.name("index-a").build(); | ||
private final static TestIndex INDEX_B = TestIndex.name("index-b").build(); | ||
|
||
private final static TestSecurityConfig.User INDEX_A_USER = new TestSecurityConfig.User("index_a_user") | ||
.roles(new Role("index_a_role").indexPermissions("*").on(INDEX_A).clusterPermissions("*")); | ||
|
||
|
||
// build our test cluster as a ClassRule | ||
@ClassRule | ||
public static LocalCluster cluster = new LocalCluster.Builder().clusterConfiguration(ClusterConfiguration.THREE_MASTERS) | ||
.authc(AUTHC_HTTPBASIC_INTERNAL) | ||
.users(USER_ADMIN, INDEX_A_USER) | ||
.indices(INDEX_A, INDEX_B).build(); | ||
|
||
@Test | ||
public void testAdminUserHasAccessToAllIndices() throws Exception { | ||
try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { | ||
HttpResponse response = client.get("*/_search?pretty"); | ||
Assert.assertEquals(response.getStatusCode(), HttpStatus.SC_OK); | ||
} | ||
} | ||
|
||
@Test | ||
public void testIndexAUserHasOnlyAccessToIndexA() throws Exception { | ||
try (TestRestClient client = cluster.getRestClient(INDEX_A_USER)) { | ||
HttpResponse response = client.get("index-a/_search?pretty"); | ||
Assert.assertEquals(response.getStatusCode(), HttpStatus.SC_OK); | ||
|
||
// demo: work with JSON response body and check values | ||
JsonPointer jsonPointer = JsonPointer.compile("/_source/hits/value"); | ||
int hits = response.toJsonNode().at(jsonPointer).asInt(); | ||
|
||
response = client.get("index-b/_search?pretty"); | ||
Assert.assertEquals(response.getStatusCode(), HttpStatus.SC_FORBIDDEN); | ||
} | ||
} | ||
|
||
@AfterClass | ||
public static void close() { | ||
cluster.close(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/newTest/java/org/opensearch/test/PrivilegesEvaluatorTest.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,71 @@ | ||
/* | ||
* 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.test; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.junit.Assert; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.opensearch.test.framework.TestSecurityConfig; | ||
import org.opensearch.test.framework.TestSecurityConfig.Role; | ||
import org.opensearch.test.framework.cluster.ClusterConfiguration; | ||
import org.opensearch.test.framework.cluster.LocalCluster; | ||
import org.opensearch.test.framework.cluster.TestRestClient; | ||
import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse; | ||
|
||
/** | ||
* This is a port for the test | ||
* org.opensearch.security.privileges.PrivilegesEvaluatorTest to the new test | ||
* framework for direct comparison | ||
* | ||
*/ | ||
public class PrivilegesEvaluatorTest extends AbstractIntegrationTest { | ||
|
||
protected final static TestSecurityConfig.User NEGATIVE_LOOKAHEAD = new TestSecurityConfig.User( | ||
"negative_lookahead_user") | ||
.roles(new Role("negative_lookahead_role").indexPermissions("read").on("/^(?!t.*).*/") | ||
.clusterPermissions("cluster_composite_ops")); | ||
|
||
protected final static TestSecurityConfig.User NEGATED_REGEX = new TestSecurityConfig.User("negated_regex_user") | ||
.roles(new Role("negated_regex_role").indexPermissions("read").on("/^[a-z].*/") | ||
.clusterPermissions("cluster_composite_ops")); | ||
|
||
@ClassRule | ||
public static LocalCluster cluster = new LocalCluster.Builder() | ||
.clusterConfiguration(ClusterConfiguration.THREE_MASTERS).authc(AUTHC_HTTPBASIC_INTERNAL) | ||
.users(NEGATIVE_LOOKAHEAD, NEGATED_REGEX).build(); | ||
|
||
@Test | ||
public void testNegativeLookaheadPattern() throws Exception { | ||
|
||
try (TestRestClient client = cluster.getRestClient(NEGATIVE_LOOKAHEAD)) { | ||
HttpResponse response = client.get("*/_search"); | ||
Assert.assertEquals(response.getStatusCode(), HttpStatus.SC_FORBIDDEN); | ||
|
||
response = client.get("r*/_search"); | ||
Assert.assertEquals(response.getStatusCode(), HttpStatus.SC_OK); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRegexPattern() throws Exception { | ||
|
||
try (TestRestClient client = cluster.getRestClient(NEGATED_REGEX)) { | ||
HttpResponse response = client.get("*/_search"); | ||
Assert.assertEquals(response.getStatusCode(), HttpStatus.SC_FORBIDDEN); | ||
|
||
response = client.get("r*/_search"); | ||
Assert.assertEquals(response.getStatusCode(), HttpStatus.SC_OK); | ||
} | ||
|
||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/newTest/java/org/opensearch/test/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,77 @@ | ||
/* | ||
* Copyright 2015-2018 _floragunn_ GmbH | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* 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.test; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.junit.Assert; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.opensearch.test.framework.TestSecurityConfig; | ||
import org.opensearch.test.framework.TestSecurityConfig.Role; | ||
import org.opensearch.test.framework.cluster.ClusterConfiguration; | ||
import org.opensearch.test.framework.cluster.LocalCluster; | ||
import org.opensearch.test.framework.cluster.TestRestClient; | ||
import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse; | ||
|
||
import com.fasterxml.jackson.core.JsonPointer; | ||
|
||
public class SecurityRolesTests extends AbstractIntegrationTest { | ||
|
||
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() | ||
.clusterConfiguration(ClusterConfiguration.THREE_MASTERS).anonymousAuth(true) | ||
.authc(AUTHC_HTTPBASIC_INTERNAL).users(USER_SR).build(); | ||
|
||
@Test | ||
public void testSecurityRolesAnon() throws Exception { | ||
|
||
try (TestRestClient client = cluster.getRestClient(USER_SR)) { | ||
HttpResponse response = client.getAuthInfo(); | ||
Assert.assertEquals(response.getStatusCode(), HttpStatus.SC_OK); | ||
|
||
// Check username | ||
JsonPointer jsonPointer = JsonPointer.compile("/user_name"); | ||
String username = response.toJsonNode().at(jsonPointer).asText(); | ||
Assert.assertEquals("sr_user", username); | ||
|
||
// Check security roles | ||
jsonPointer = JsonPointer.compile("/roles/0"); | ||
String securityRole = response.toJsonNode().at(jsonPointer).asText(); | ||
Assert.assertEquals("user_sr_user__abc_ber", securityRole); | ||
|
||
jsonPointer = JsonPointer.compile("/roles/1"); | ||
securityRole = response.toJsonNode().at(jsonPointer).asText(); | ||
Assert.assertEquals("user_sr_user__def_efg", securityRole); | ||
|
||
} | ||
} | ||
|
||
} |
Oops, something went wrong.