Skip to content

Commit

Permalink
Simplify ResourceSharingService
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Dec 23, 2024
1 parent d7cf362 commit e280cf2
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ public Collection<Object> createComponents(
this.client = client;
if (SampleResourceSharingServiceProvider.getInstance().get() == null) {
System.out.println("Using DefaultResourceSharingService");
SampleResourceSharingServiceProvider.getInstance()
.set(new DefaultResourceSharingService<>(client, RESOURCE_INDEX_NAME, new SampleResourceParser(), xContentRegistry));
SampleResourceSharingServiceProvider.getInstance().set(new DefaultResourceSharingService<>());
}
System.out.println(
"SampleResourceSharingServiceProvider.getInstance(): " + SampleResourceSharingServiceProvider.getInstance().get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
import org.apache.logging.log4j.Logger;

import org.opensearch.action.support.ActionFilters;
import org.opensearch.client.Client;
import org.opensearch.common.inject.Inject;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.security.sampleextension.resource.SampleResource;
import org.opensearch.security.sampleextension.resource.SampleResourceParser;
import org.opensearch.security.sampleextension.resource.SampleResourceSharingServiceProvider;
import org.opensearch.security.spi.actions.resource.get.GetResourceTransportAction;
import org.opensearch.transport.TransportService;

import static org.opensearch.security.sampleextension.SampleExtensionPlugin.RESOURCE_INDEX_NAME;

/**
* Transport action for GetSampleResource.
*/
Expand All @@ -28,8 +33,19 @@ public class GetSampleResourceTransportAction extends GetResourceTransportAction
public GetSampleResourceTransportAction(
TransportService transportService,
ActionFilters actionFilters,
SampleResourceSharingServiceProvider resourceSharingService
SampleResourceSharingServiceProvider resourceSharingService,
Client client,
NamedXContentRegistry xContentRegistry
) {
super(transportService, actionFilters, GetSampleResourceAction.NAME, resourceSharingService.get());
super(
transportService,
actionFilters,
GetSampleResourceAction.NAME,
RESOURCE_INDEX_NAME,
resourceSharingService.get(),
new SampleResourceParser(),
client,
xContentRegistry
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
import org.apache.logging.log4j.Logger;

import org.opensearch.action.support.ActionFilters;
import org.opensearch.client.Client;
import org.opensearch.common.inject.Inject;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.security.sampleextension.resource.SampleResource;
import org.opensearch.security.sampleextension.resource.SampleResourceParser;
import org.opensearch.security.sampleextension.resource.SampleResourceSharingServiceProvider;
import org.opensearch.security.spi.actions.resource.list.ListResourceTransportAction;
import org.opensearch.transport.TransportService;

import static org.opensearch.security.sampleextension.SampleExtensionPlugin.RESOURCE_INDEX_NAME;

/**
* Transport action for ListSampleResource.
*/
Expand All @@ -28,8 +33,19 @@ public class ListSampleResourceTransportAction extends ListResourceTransportActi
public ListSampleResourceTransportAction(
TransportService transportService,
ActionFilters actionFilters,
SampleResourceSharingServiceProvider resourceSharingService
SampleResourceSharingServiceProvider resourceSharingService,
NamedXContentRegistry xContentRegistry,
Client client
) {
super(transportService, actionFilters, ListSampleResourceAction.NAME, resourceSharingService.get());
super(
transportService,
actionFilters,
ListSampleResourceAction.NAME,
RESOURCE_INDEX_NAME,
resourceSharingService.get(),
new SampleResourceParser(),
client,
xContentRegistry
);
}
}
Original file line number Diff line number Diff line change
@@ -1,113 +1,11 @@
package org.opensearch.security.spi;

import java.io.IOException;
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.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.index.query.MatchAllQueryBuilder;
import org.opensearch.search.SearchHit;
import org.opensearch.search.builder.SearchSourceBuilder;

public class DefaultResourceSharingService<T extends Resource> implements ResourceSharingService<T> {
private final Client client;
private final String resourceIndex;
private final ResourceParser<T> resourceParser;
private final NamedXContentRegistry xContentRegistry;

public DefaultResourceSharingService(
Client client,
String resourceIndex,
ResourceParser<T> resourceParser,
NamedXContentRegistry xContentRegistry
) {
this.client = client;
this.resourceIndex = resourceIndex;
this.resourceParser = resourceParser;
this.xContentRegistry = xContentRegistry;
}

@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()) {
try {
XContentParser parser = XContentHelper.createParser(
xContentRegistry,
LoggingDeprecationHandler.INSTANCE,
hit.getSourceRef(),
XContentType.JSON
);
T resource = resourceParser.parse(parser, hit.getId());
resources.add(resource);
} catch (IOException e) {
throw new OpenSearchException("Caught exception while loading resources: " + e.getMessage());
}
}
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) {
try {
XContentParser parser = XContentHelper.createParser(
xContentRegistry,
LoggingDeprecationHandler.INSTANCE,
getResponse.getSourceAsBytesRef(),
XContentType.JSON
);
T resource = resourceParser.parse(parser, getResponse.getId());
getResourceListener.onResponse(resource);
} catch (IOException e) {
throw new OpenSearchException("Caught exception while loading resources: " + e.getMessage());
}
}

@Override
public void onFailure(Exception e) {
throw new OpenSearchException("Caught exception while loading resources: " + e.getMessage());
}
};
client.get(gr, getListener);
}
public void hasResourceBeenSharedWith(String resourceId, ActionListener<Boolean> resourceSharingListener) {
resourceSharingListener.onResponse(Boolean.TRUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;

// TODO Job Scheduler keeps track of doc version. Should this keep track of version similarly?

/**
* Structure to represent resource document version.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package org.opensearch.security.spi;

import java.util.List;

import org.opensearch.core.action.ActionListener;

public interface ResourceSharingService<T extends Resource> {

void listResources(ActionListener<List<T>> listResourceListener);

void getResource(String resourceId, ActionListener<T> getResourceListener);
void hasResourceBeenSharedWith(String resourceId, ActionListener<Boolean> resourceSharingListener);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@

package org.opensearch.security.spi.actions.resource.get;

import java.io.IOException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.opensearch.OpenSearchException;
import org.opensearch.action.get.GetRequest;
import org.opensearch.action.get.GetResponse;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.client.Client;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.security.spi.Resource;
import org.opensearch.security.spi.ResourceParser;
import org.opensearch.security.spi.ResourceSharingService;
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;
Expand All @@ -27,14 +40,30 @@ public class GetResourceTransportAction<T extends Resource> extends HandledTrans

private final ResourceSharingService<T> resourceSharingService;

private final ResourceParser<T> resourceParser;

private final String resourceIndex;

private final Client client;

private final NamedXContentRegistry xContentRegistry;

public GetResourceTransportAction(
TransportService transportService,
ActionFilters actionFilters,
String actionName,
ResourceSharingService<T> resourceSharingService
String resourceIndex,
ResourceSharingService<T> resourceSharingService,
ResourceParser<T> resourceParser,
Client client,
NamedXContentRegistry xContentRegistry
) {
super(actionName, transportService, actionFilters, GetResourceRequest::new);
this.resourceSharingService = resourceSharingService;
this.resourceParser = resourceParser;
this.resourceIndex = resourceIndex;
this.client = client;
this.xContentRegistry = xContentRegistry;
}

@Override
Expand All @@ -43,10 +72,58 @@ protected void doExecute(Task task, GetResourceRequest request, ActionListener<G
}

private void getResource(GetResourceRequest request, ActionListener<GetResourceResponse<T>> listener) {
ActionListener<T> getResourceListener = ActionListener.wrap(sampleResource -> {
System.out.println("sampleResource: " + sampleResource);
listener.onResponse(new GetResourceResponse<T>(sampleResource));
ActionListener<T> getResourceListener = ActionListener.wrap(resource -> {
System.out.println("resource: " + resource);
listener.onResponse(new GetResourceResponse<T>(resource));
}, listener::onFailure);
resourceSharingService.getResource(request.getResourceId(), getResourceListener);

try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashContext()) {
GetRequest gr = new GetRequest(resourceIndex);
gr.id(request.getResourceId());
ActionListener<GetResponse> getListener = new ActionListener<>() {
@Override
public void onResponse(GetResponse getResponse) {
try {
XContentParser parser = XContentHelper.createParser(
xContentRegistry,
LoggingDeprecationHandler.INSTANCE,
getResponse.getSourceAsBytesRef(),
XContentType.JSON
);
T resource = resourceParser.parse(parser, getResponse.getId());
ActionListener<Boolean> shareListener = new ActionListener<>() {
@Override
public void onResponse(Boolean isShared) {
if (isShared) {
getResourceListener.onResponse(resource);
} else {
getResourceListener.onFailure(
new OpenSearchException("User is not authorized to access this resource")
);
}
}

@Override
public void onFailure(Exception e) {
getResourceListener.onFailure(
new OpenSearchException("Failed to check sharing status: " + e.getMessage(), e)
);
}
};

resourceSharingService.hasResourceBeenSharedWith(request.getResourceId(), shareListener);
} catch (IOException e) {
throw new OpenSearchException("Caught exception while loading resources: " + e.getMessage());
}
}

@Override
public void onFailure(Exception e) {
throw new OpenSearchException("Caught exception while loading resources: " + e.getMessage());
}
};
client.get(gr, getListener);
}
// resourceSharingService.getResource(request.getResourceId(), getResourceListener);
}
}
Loading

0 comments on commit e280cf2

Please sign in to comment.