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.
Signed-off-by: Stephen Crawford <[email protected]>
- Loading branch information
1 parent
fdaa37b
commit e19ffa0
Showing
6 changed files
with
253 additions
and
5 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/integrationTest/java/org/opensearch/security/IndexOperationsHelper.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,65 @@ | ||
/* | ||
* 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.Map; | ||
import org.opensearch.action.admin.indices.close.CloseIndexRequest; | ||
import org.opensearch.action.admin.indices.close.CloseIndexResponse; | ||
import org.opensearch.action.admin.indices.create.CreateIndexRequest; | ||
import org.opensearch.action.admin.indices.create.CreateIndexResponse; | ||
import org.opensearch.action.admin.indices.mapping.put.PutMappingRequest; | ||
import org.opensearch.action.support.master.AcknowledgedResponse; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.cluster.metadata.IndexMetadata; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.test.framework.cluster.LocalCluster; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.opensearch.test.framework.matcher.ClusterMatchers.indexExists; | ||
import static org.opensearch.test.framework.matcher.ClusterMatchers.indexStateIsEqualTo; | ||
|
||
public class IndexOperationsHelper { | ||
|
||
public static void createIndex(LocalCluster cluster, String indexName) { | ||
createIndex(cluster, indexName, Settings.EMPTY); | ||
} | ||
|
||
public static void createIndex(LocalCluster cluster, String indexName, Settings settings) { | ||
try (Client client = cluster.getInternalNodeClient()) { | ||
CreateIndexResponse createIndexResponse = client.admin() | ||
.indices() | ||
.create(new CreateIndexRequest(indexName).settings(settings)) | ||
.actionGet(); | ||
|
||
assertThat(createIndexResponse.isAcknowledged(), is(true)); | ||
assertThat(createIndexResponse.isShardsAcknowledged(), is(true)); | ||
assertThat(cluster, indexExists(indexName)); | ||
} | ||
} | ||
|
||
public static void closeIndex(LocalCluster cluster, String indexName) { | ||
try (Client client = cluster.getInternalNodeClient()) { | ||
CloseIndexRequest closeIndexRequest = new CloseIndexRequest(indexName); | ||
CloseIndexResponse response = client.admin().indices().close(closeIndexRequest).actionGet(); | ||
|
||
assertThat(response.isAcknowledged(), is(true)); | ||
assertThat(response.isShardsAcknowledged(), is(true)); | ||
assertThat(cluster, indexStateIsEqualTo(indexName, IndexMetadata.State.CLOSE)); | ||
} | ||
} | ||
|
||
public static void createMapping(LocalCluster cluster, String indexName, Map<String, Object> indexMapping) { | ||
try (Client client = cluster.getInternalNodeClient()) { | ||
AcknowledgedResponse response = client.admin().indices().putMapping(new PutMappingRequest(indexName).source(indexMapping)).actionGet(); | ||
|
||
assertThat(response.isAcknowledged(), is(true)); | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/integrationTest/java/org/opensearch/security/http/AsyncTests.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,107 @@ | ||
/* | ||
* 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 com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; | ||
|
||
import java.util.HashMap; | ||
import org.apache.http.HttpStatus; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.opensearch.security.IndexOperationsHelper; | ||
import org.opensearch.security.support.ConfigConstants; | ||
import org.opensearch.test.framework.AsyncActions; | ||
import org.opensearch.test.framework.RolesMapping; | ||
import org.opensearch.test.framework.TestSecurityConfig; | ||
import org.opensearch.test.framework.cluster.LocalCluster; | ||
import org.opensearch.test.framework.cluster.TestRestClient; | ||
import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse; | ||
|
||
import java.util.Map; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AUTHC_HTTPBASIC_INTERNAL; | ||
import static org.opensearch.test.framework.TestSecurityConfig.Role.ALL_ACCESS; | ||
|
||
@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class) | ||
@ThreadLeakScope(ThreadLeakScope.Scope.NONE) | ||
public class AsyncTests { | ||
private static final TestSecurityConfig.User ADMIN_USER = new TestSecurityConfig.User("admin").roles(ALL_ACCESS).backendRoles("admin"); | ||
|
||
private static Map<String, Object> createTestNodeSettings() { | ||
Map<String, Object> testNodeSettings = new HashMap<>(); | ||
List<String> rolesEnabled = new ArrayList<>(); | ||
rolesEnabled.add(ALL_ACCESS.getName()); | ||
testNodeSettings.put(ConfigConstants.SECURITY_RESTAPI_ROLES_ENABLED, rolesEnabled); | ||
return testNodeSettings; | ||
} | ||
|
||
@ClassRule | ||
public static LocalCluster cluster = new LocalCluster.Builder().singleNode() | ||
.authc(AUTHC_HTTPBASIC_INTERNAL) | ||
.users(ADMIN_USER) | ||
.rolesMapping(new RolesMapping(ALL_ACCESS).backendRoles("admin")) | ||
.anonymousAuth(false) | ||
.nodeSettings(createTestNodeSettings()) | ||
.build(); | ||
|
||
@Test | ||
public void testBulkAndCacheInvalidationMixed() throws Exception { | ||
String indexName = "test-index"; | ||
final String invalidateCachePath = "_plugins/_security/api/cache"; | ||
final String nodesPath = "_nodes"; | ||
final String bulkPath = "_bulk"; | ||
int repeatCount = 5; | ||
String docContents = "{ \"index\": { \"_index\": \"" + indexName + "\" }}\n{ \"foo\": \"bar\" }\n"; | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
for (int i = 0; i < repeatCount; i++) { | ||
builder.append(docContents); | ||
} | ||
|
||
String document = builder.toString(); | ||
final int parallelism = 5; | ||
final int totalNumberOfRequests = 30; | ||
|
||
try (final TestRestClient client = cluster.getRestClient(ADMIN_USER)) { | ||
IndexOperationsHelper.createIndex(cluster, indexName); | ||
|
||
final CountDownLatch countDownLatch = new CountDownLatch(1); | ||
|
||
List<CompletableFuture<HttpResponse>> allRequests = new ArrayList<CompletableFuture<HttpResponse>>(); | ||
|
||
allRequests.addAll(AsyncActions.generate(() -> { | ||
countDownLatch.await(); | ||
return client.delete(invalidateCachePath); | ||
}, parallelism, totalNumberOfRequests)); | ||
|
||
allRequests.addAll(AsyncActions.generate(() -> { | ||
countDownLatch.await(); | ||
return client.postJson(bulkPath, document); | ||
}, parallelism, totalNumberOfRequests)); | ||
|
||
allRequests.addAll(AsyncActions.generate(() -> { | ||
countDownLatch.await(); | ||
return client.get(nodesPath); | ||
}, parallelism, totalNumberOfRequests)); | ||
|
||
// Make sure all requests start at the same time | ||
countDownLatch.countDown(); | ||
|
||
AsyncActions.getAll(allRequests, 30, TimeUnit.SECONDS).forEach((response) -> { response.assertStatusCode(HttpStatus.SC_OK); }); | ||
} | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
...ntegrationTest/java/org/opensearch/test/framework/matcher/IndexStateIsEqualToMatcher.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,58 @@ | ||
/* | ||
* 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.test.framework.matcher; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
import org.opensearch.action.admin.cluster.state.ClusterStateRequest; | ||
import org.opensearch.action.admin.cluster.state.ClusterStateResponse; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.cluster.metadata.IndexMetadata; | ||
import org.opensearch.common.collect.ImmutableOpenMap; | ||
import org.opensearch.test.framework.cluster.LocalCluster; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
class IndexStateIsEqualToMatcher extends TypeSafeDiagnosingMatcher<LocalCluster> { | ||
|
||
private final String expectedIndexName; | ||
private final IndexMetadata.State expectedState; | ||
|
||
IndexStateIsEqualToMatcher(String expectedIndexName, IndexMetadata.State expectedState) { | ||
this.expectedIndexName = requireNonNull(expectedIndexName); | ||
this.expectedState = requireNonNull(expectedState); | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(LocalCluster cluster, Description mismatchDescription) { | ||
try (Client client = cluster.getInternalNodeClient()) { | ||
ClusterStateRequest clusterStateRequest = new ClusterStateRequest().indices(expectedIndexName); | ||
ClusterStateResponse clusterStateResponse = client.admin().cluster().state(clusterStateRequest).actionGet(); | ||
|
||
ImmutableOpenMap<String, IndexMetadata> indicesMetadata = clusterStateResponse.getState().getMetadata().indices(); | ||
if (!indicesMetadata.containsKey(expectedIndexName)) { | ||
mismatchDescription.appendValue("Index does not exist"); | ||
} | ||
IndexMetadata indexMetadata = indicesMetadata.get(expectedIndexName); | ||
if (expectedState != indexMetadata.getState()) { | ||
mismatchDescription.appendValue("Actual index state is equal to ").appendValue(indexMetadata.getState().name()); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("Index: ") | ||
.appendValue(expectedIndexName) | ||
.appendText(" . State should be equal to ") | ||
.appendValue(expectedState.name()); | ||
} | ||
} |