Skip to content

Commit

Permalink
Remove abstract sharing service
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Dec 13, 2024
1 parent a1e80b8 commit b4aa892
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 143 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<T extends Resource> extends AbstractResourceSharingService<T> {
public class DefaultResourceSharingService<T extends Resource> implements ResourceSharingService<T> {
private final Client client;
private final String resourceIndex;
private final ResourceFactory<T> resourceFactory;

public DefaultResourceSharingService(Client client, String resourceIndex, ResourceFactory<T> resourceFactory) {
super(client, resourceIndex, resourceFactory);
this.client = client;
this.resourceIndex = resourceIndex;
this.resourceFactory = resourceFactory;
}

@SuppressWarnings("unchecked")
@Override
public void listResources(ActionListener<List<T>> 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<SearchResponse> searchListener = new ActionListener<SearchResponse>() {
@Override
public void onResponse(SearchResponse searchResponse) {
List<T> 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<T> getResourceListener) {
try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashContext()) {
GetRequest gr = new GetRequest(resourceIndex);
gr.id(resourceId);
/* Index already exists, ignore and continue */
ActionListener<GetResponse> getListener = new ActionListener<GetResponse>() {
@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);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Resource> extends AbstractResourceSharingService<T> {
public class SecurityResourceSharingService<T extends Resource> implements ResourceSharingService<T> {
private final Client client;
private final String resourceIndex;
private final ResourceFactory<T> resourceFactory;

public SecurityResourceSharingService(Client client, String resourceIndex, ResourceFactory<T> resourceFactory) {
super(client, resourceIndex, resourceFactory);
this.client = client;
this.resourceIndex = resourceIndex;
this.resourceFactory = resourceFactory;
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit b4aa892

Please sign in to comment.