Skip to content

Commit

Permalink
Adds unit and integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Dec 18, 2024
1 parent b58308e commit b25a9a7
Show file tree
Hide file tree
Showing 6 changed files with 897 additions and 1 deletion.
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;
}
}
}
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() {}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ public ResourceAccessControlPlugin getResourceAccessControlPlugin() {
* List active plugins that define resources
*/
public List<ResourcePlugin> listResourcePlugins() {
return resourcePlugins;
return List.copyOf(resourcePlugins);
}
}
Loading

0 comments on commit b25a9a7

Please sign in to comment.