From b4aa89288b53fddbfa76586de5787746577ecba5 Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Fri, 13 Dec 2024 12:37:17 -0500 Subject: [PATCH] Remove abstract sharing service Signed-off-by: Craig Perkins --- .../spi/AbstractResourceSharingService.java | 97 ------------------- .../spi/DefaultResourceSharingService.java | 77 ++++++++++++++- .../spi/ResourceSharingServiceProvider.java | 41 -------- .../SecurityResourceSharingService.java | 12 ++- 4 files changed, 84 insertions(+), 143 deletions(-) delete mode 100644 spi/src/main/java/org/opensearch/security/spi/AbstractResourceSharingService.java delete mode 100644 spi/src/main/java/org/opensearch/security/spi/ResourceSharingServiceProvider.java diff --git a/spi/src/main/java/org/opensearch/security/spi/AbstractResourceSharingService.java b/spi/src/main/java/org/opensearch/security/spi/AbstractResourceSharingService.java deleted file mode 100644 index 53e529936e..0000000000 --- a/spi/src/main/java/org/opensearch/security/spi/AbstractResourceSharingService.java +++ /dev/null @@ -1,97 +0,0 @@ -package org.opensearch.security.spi; - -import java.util.ArrayList; -import java.util.List; - -import org.opensearch.OpenSearchException; -import org.opensearch.action.get.GetRequest; -import org.opensearch.action.get.GetResponse; -import org.opensearch.action.search.SearchRequest; -import org.opensearch.action.search.SearchResponse; -import org.opensearch.client.Client; -import org.opensearch.common.util.concurrent.ThreadContext; -import org.opensearch.core.action.ActionListener; -import org.opensearch.index.query.MatchAllQueryBuilder; -import org.opensearch.search.SearchHit; -import org.opensearch.search.builder.SearchSourceBuilder; - -public abstract class AbstractResourceSharingService implements ResourceSharingService { - protected final Client client; - protected final String resourceIndex; - protected final ResourceFactory resourceFactory; - // protected final Class resourceClass; - - public AbstractResourceSharingService(Client client, String resourceIndex, ResourceFactory resourceFactory) { - this.client = client; - this.resourceIndex = resourceIndex; - this.resourceFactory = resourceFactory; - } - - // protected T newResource() { - // return AccessController.doPrivileged(new PrivilegedAction() { - // @Override - // public T run() { - // try { - // return resourceClass.getDeclaredConstructor().newInstance(); - // } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { - // throw new RuntimeException(e); - // } - // } - // }); - // } - - @SuppressWarnings("unchecked") - @Override - public void listResources(ActionListener> listResourceListener) { - try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashContext()) { - SearchRequest sr = new SearchRequest(resourceIndex); - SearchSourceBuilder matchAllQuery = new SearchSourceBuilder(); - matchAllQuery.query(new MatchAllQueryBuilder()); - sr.source(matchAllQuery); - /* Index already exists, ignore and continue */ - ActionListener searchListener = new ActionListener() { - @Override - public void onResponse(SearchResponse searchResponse) { - List resources = new ArrayList<>(); - for (SearchHit hit : searchResponse.getHits().getHits()) { - System.out.println("SearchHit: " + hit); - T resource = resourceFactory.createResource(); - resource.fromSource(hit.getId(), hit.getSourceAsMap()); - resources.add(resource); - } - listResourceListener.onResponse(resources); - } - - @Override - public void onFailure(Exception e) { - throw new OpenSearchException("Caught exception while loading resources: " + e.getMessage()); - } - }; - client.search(sr, searchListener); - } - } - - @SuppressWarnings("unchecked") - @Override - public void getResource(String resourceId, ActionListener getResourceListener) { - try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashContext()) { - GetRequest gr = new GetRequest(resourceIndex); - gr.id(resourceId); - /* Index already exists, ignore and continue */ - ActionListener getListener = new ActionListener() { - @Override - public void onResponse(GetResponse getResponse) { - T resource = resourceFactory.createResource(); - resource.fromSource(getResponse.getId(), getResponse.getSourceAsMap()); - getResourceListener.onResponse(resource); - } - - @Override - public void onFailure(Exception e) { - throw new OpenSearchException("Caught exception while loading resources: " + e.getMessage()); - } - }; - client.get(gr, getListener); - } - } -} diff --git a/spi/src/main/java/org/opensearch/security/spi/DefaultResourceSharingService.java b/spi/src/main/java/org/opensearch/security/spi/DefaultResourceSharingService.java index faade29b3b..ab5c041e90 100644 --- a/spi/src/main/java/org/opensearch/security/spi/DefaultResourceSharingService.java +++ b/spi/src/main/java/org/opensearch/security/spi/DefaultResourceSharingService.java @@ -1,10 +1,83 @@ package org.opensearch.security.spi; +import java.util.ArrayList; +import java.util.List; + +import org.opensearch.OpenSearchException; +import org.opensearch.action.get.GetRequest; +import org.opensearch.action.get.GetResponse; +import org.opensearch.action.search.SearchRequest; +import org.opensearch.action.search.SearchResponse; import org.opensearch.client.Client; +import org.opensearch.common.util.concurrent.ThreadContext; +import org.opensearch.core.action.ActionListener; +import org.opensearch.index.query.MatchAllQueryBuilder; +import org.opensearch.search.SearchHit; +import org.opensearch.search.builder.SearchSourceBuilder; -public class DefaultResourceSharingService extends AbstractResourceSharingService { +public class DefaultResourceSharingService implements ResourceSharingService { + private final Client client; + private final String resourceIndex; + private final ResourceFactory resourceFactory; public DefaultResourceSharingService(Client client, String resourceIndex, ResourceFactory resourceFactory) { - super(client, resourceIndex, resourceFactory); + this.client = client; + this.resourceIndex = resourceIndex; + this.resourceFactory = resourceFactory; + } + + @SuppressWarnings("unchecked") + @Override + public void listResources(ActionListener> listResourceListener) { + try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashContext()) { + SearchRequest sr = new SearchRequest(resourceIndex); + SearchSourceBuilder matchAllQuery = new SearchSourceBuilder(); + matchAllQuery.query(new MatchAllQueryBuilder()); + sr.source(matchAllQuery); + /* Index already exists, ignore and continue */ + ActionListener searchListener = new ActionListener() { + @Override + public void onResponse(SearchResponse searchResponse) { + List resources = new ArrayList<>(); + for (SearchHit hit : searchResponse.getHits().getHits()) { + System.out.println("SearchHit: " + hit); + T resource = resourceFactory.createResource(); + resource.fromSource(hit.getId(), hit.getSourceAsMap()); + resources.add(resource); + } + listResourceListener.onResponse(resources); + } + + @Override + public void onFailure(Exception e) { + throw new OpenSearchException("Caught exception while loading resources: " + e.getMessage()); + } + }; + client.search(sr, searchListener); + } + } + + @SuppressWarnings("unchecked") + @Override + public void getResource(String resourceId, ActionListener getResourceListener) { + try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashContext()) { + GetRequest gr = new GetRequest(resourceIndex); + gr.id(resourceId); + /* Index already exists, ignore and continue */ + ActionListener getListener = new ActionListener() { + @Override + public void onResponse(GetResponse getResponse) { + T resource = resourceFactory.createResource(); + resource.fromSource(getResponse.getId(), getResponse.getSourceAsMap()); + getResourceListener.onResponse(resource); + } + + @Override + public void onFailure(Exception e) { + throw new OpenSearchException("Caught exception while loading resources: " + e.getMessage()); + } + }; + client.get(gr, getListener); + } } } diff --git a/spi/src/main/java/org/opensearch/security/spi/ResourceSharingServiceProvider.java b/spi/src/main/java/org/opensearch/security/spi/ResourceSharingServiceProvider.java deleted file mode 100644 index 1e0467976a..0000000000 --- a/spi/src/main/java/org/opensearch/security/spi/ResourceSharingServiceProvider.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.opensearch.security.spi; - -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -public class ResourceSharingServiceProvider { - private final static Logger log = LogManager.getLogger(ResourceSharingServiceProvider.class); - - private static final Map instances = new ConcurrentHashMap<>(); - - private boolean initialized; - private AbstractResourceSharingService resourceSharingService; - - private ResourceSharingServiceProvider() {} - - @SuppressWarnings("removal") - public static ResourceSharingServiceProvider getInstance() { - ClassLoader classLoader = AccessController.doPrivileged( - (PrivilegedAction) () -> Thread.currentThread().getContextClassLoader() - ); - instances.computeIfAbsent(classLoader, cl -> new ResourceSharingServiceProvider()); - return instances.get(classLoader); - } - - public void initialize(AbstractResourceSharingService resourceSharingService) { - if (initialized) { - return; - } - initialized = true; - this.resourceSharingService = resourceSharingService; - } - - public boolean isInitialized() { - return initialized; - } -} diff --git a/src/main/java/org/opensearch/security/resource/SecurityResourceSharingService.java b/src/main/java/org/opensearch/security/resource/SecurityResourceSharingService.java index f649e92e0e..f69d6a3f11 100644 --- a/src/main/java/org/opensearch/security/resource/SecurityResourceSharingService.java +++ b/src/main/java/org/opensearch/security/resource/SecurityResourceSharingService.java @@ -29,17 +29,23 @@ import org.opensearch.index.query.QueryBuilders; import org.opensearch.search.SearchHit; import org.opensearch.search.builder.SearchSourceBuilder; -import org.opensearch.security.spi.AbstractResourceSharingService; import org.opensearch.security.spi.Resource; import org.opensearch.security.spi.ResourceFactory; +import org.opensearch.security.spi.ResourceSharingService; import org.opensearch.security.support.ConfigConstants; import org.opensearch.security.user.User; import static org.opensearch.security.resource.ResourceSharingListener.RESOURCE_SHARING_INDEX; -public class SecurityResourceSharingService extends AbstractResourceSharingService { +public class SecurityResourceSharingService implements ResourceSharingService { + private final Client client; + private final String resourceIndex; + private final ResourceFactory resourceFactory; + public SecurityResourceSharingService(Client client, String resourceIndex, ResourceFactory resourceFactory) { - super(client, resourceIndex, resourceFactory); + this.client = client; + this.resourceIndex = resourceIndex; + this.resourceFactory = resourceFactory; } @SuppressWarnings("unchecked")