forked from opensearch-project/OpenSearch
-
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: Darshit Chanpura <[email protected]>
- Loading branch information
1 parent
b58308e
commit b25a9a7
Showing
6 changed files
with
897 additions
and
1 deletion.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
...org/opensearch/accesscontrol/resources/fallback/DefaultResourceAccessControlPluginIT.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 @@ | ||
/* | ||
* 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.accesscontrol.resources.fallback; | ||
|
||
import org.opensearch.client.Client; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.plugins.Plugin; | ||
import org.opensearch.plugins.ResourceAccessControlPlugin; | ||
import org.opensearch.test.InternalTestCluster; | ||
import org.opensearch.test.OpenSearchIntegTestCase; | ||
import org.opensearch.threadpool.ThreadPool; | ||
import org.hamcrest.MatcherAssert; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.opensearch.accesscontrol.resources.fallback.SampleTestResourcePlugin.SAMPLE_TEST_INDEX; | ||
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder; | ||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.hamcrest.Matchers.hasProperty; | ||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class DefaultResourceAccessControlPluginIT extends OpenSearchIntegTestCase { | ||
@Override | ||
protected Collection<Class<? extends Plugin>> nodePlugins() { | ||
return List.of(SampleTestResourcePlugin.class); | ||
} | ||
|
||
public void testGetResources() throws IOException { | ||
final Client client = client(); | ||
|
||
createIndex(SAMPLE_TEST_INDEX); | ||
indexSampleDocuments(); | ||
|
||
Set<TestResource> resources; | ||
try ( | ||
InternalTestCluster cluster = internalCluster(); | ||
DefaultResourceAccessControlPlugin plugin = new DefaultResourceAccessControlPlugin( | ||
client, | ||
cluster.getInstance(ThreadPool.class) | ||
) | ||
) { | ||
|
||
resources = plugin.getAccessibleResourcesForCurrentUser(SAMPLE_TEST_INDEX, TestResource.class); | ||
} | ||
|
||
assertNotNull(resources); | ||
MatcherAssert.assertThat(resources, hasSize(2)); | ||
|
||
MatcherAssert.assertThat(resources, hasItem(hasProperty("id", is("1")))); | ||
MatcherAssert.assertThat(resources, hasItem(hasProperty("id", is("2")))); | ||
} | ||
|
||
public void testSampleResourcePluginCallsDefaultPlugin() throws IOException { | ||
createIndex(SAMPLE_TEST_INDEX); | ||
indexSampleDocuments(); | ||
|
||
ResourceAccessControlPlugin racPlugin = SampleTestResourcePlugin.GuiceHolder.getResourceService().getResourceAccessControlPlugin(); | ||
MatcherAssert.assertThat(racPlugin.getClass(), is(DefaultResourceAccessControlPlugin.class)); | ||
|
||
Set<TestResource> resources = racPlugin.getAccessibleResourcesForCurrentUser(SAMPLE_TEST_INDEX, TestResource.class); | ||
|
||
assertNotNull(resources); | ||
MatcherAssert.assertThat(resources, hasSize(2)); | ||
MatcherAssert.assertThat(resources, hasItem(hasProperty("id", is("1")))); | ||
MatcherAssert.assertThat(resources, hasItem(hasProperty("id", is("2")))); | ||
} | ||
|
||
private void indexSampleDocuments() throws IOException { | ||
XContentBuilder doc1 = jsonBuilder().startObject().field("id", "1").field("name", "Test Document 1").endObject(); | ||
|
||
XContentBuilder doc2 = jsonBuilder().startObject().field("id", "2").field("name", "Test Document 2").endObject(); | ||
|
||
try (Client client = client()) { | ||
|
||
client.prepareIndex(SAMPLE_TEST_INDEX).setId("1").setSource(doc1).get(); | ||
|
||
client.prepareIndex(SAMPLE_TEST_INDEX).setId("2").setSource(doc2).get(); | ||
|
||
client.admin().indices().prepareRefresh(SAMPLE_TEST_INDEX).get(); | ||
} | ||
} | ||
|
||
public static class TestResource { | ||
public String id; | ||
public String name; | ||
|
||
public TestResource() {} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...erTest/java/org/opensearch/accesscontrol/resources/fallback/SampleTestResourcePlugin.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. | ||
*/ | ||
|
||
package org.opensearch.accesscontrol.resources.fallback; | ||
|
||
import org.opensearch.accesscontrol.resources.ResourceService; | ||
import org.opensearch.common.inject.Inject; | ||
import org.opensearch.common.lifecycle.Lifecycle; | ||
import org.opensearch.common.lifecycle.LifecycleComponent; | ||
import org.opensearch.common.lifecycle.LifecycleListener; | ||
import org.opensearch.plugins.Plugin; | ||
import org.opensearch.plugins.ResourcePlugin; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
// Sample test resource plugin | ||
public class SampleTestResourcePlugin extends Plugin implements ResourcePlugin { | ||
|
||
public static final String SAMPLE_TEST_INDEX = ".sample_test_resource"; | ||
|
||
@Override | ||
public String getResourceType() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public String getResourceIndex() { | ||
return SAMPLE_TEST_INDEX; | ||
} | ||
|
||
@Override | ||
public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses() { | ||
final List<Class<? extends LifecycleComponent>> services = new ArrayList<>(1); | ||
services.add(GuiceHolder.class); | ||
return services; | ||
} | ||
|
||
public static class GuiceHolder implements LifecycleComponent { | ||
|
||
private static ResourceService resourceService; | ||
|
||
@Inject | ||
public GuiceHolder(final ResourceService resourceService) { | ||
GuiceHolder.resourceService = resourceService; | ||
} | ||
|
||
public static ResourceService getResourceService() { | ||
return resourceService; | ||
} | ||
|
||
@Override | ||
public void close() {} | ||
|
||
@Override | ||
public Lifecycle.State lifecycleState() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void addLifecycleListener(LifecycleListener listener) {} | ||
|
||
@Override | ||
public void removeLifecycleListener(LifecycleListener listener) {} | ||
|
||
@Override | ||
public void start() {} | ||
|
||
@Override | ||
public void stop() {} | ||
|
||
} | ||
} |
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.