Skip to content

Commit

Permalink
Default resource parser
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Dec 31, 2024
1 parent 1c51afd commit c576870
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public interface ResourceSharingExtension {
/**
* @return returns a parser for this resource
*/
ResourceParser<? extends Resource> getResourceParser();
default ResourceParser<? extends Resource> getResourceParser() {
return null;
};

void assignResourceSharingService(ResourceSharingService<? extends Resource> service);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.opensearch.security.spi.actions.resource.get;

import java.io.IOException;
import java.util.Objects;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -60,6 +61,7 @@ public GetResourceTransportAction(
) {
super(actionName, transportService, actionFilters, GetResourceRequest::new);
this.resourceSharingService = resourceSharingService;
Objects.requireNonNull(resourceParser);
this.resourceParser = resourceParser;
this.resourceIndex = resourceIndex;
this.client = client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import org.opensearch.OpenSearchException;
Expand Down Expand Up @@ -65,6 +66,7 @@ public ListResourceTransportAction(
this.resourceSharingService = resourceSharingService != null ? resourceSharingService : new DefaultResourceSharingService<>();
this.resourceIndex = resourceIndex;
this.xContentRegistry = xContentRegistry;
Objects.requireNonNull(resourceParser);
this.resourceParser = resourceParser;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,9 +1080,7 @@ public Collection<Object> createComponents(
for (ResourceSharingExtension extension : resourceSharingExtensions) {
ResourceSharingService<?> resourceSharingService = new SecurityResourceSharingService<>(
localClient,
extension.getResourceIndex(),
extension.getResourceParser(),
xContentRegistry
extension.getResourceIndex()
);
extension.assignResourceSharingService(resourceSharingService);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,19 @@

package org.opensearch.security.resource;

import java.io.IOException;

import org.opensearch.OpenSearchException;
import org.opensearch.ResourceNotFoundException;
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.BoolQueryBuilder;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.search.SearchHit;
import org.opensearch.search.builder.SearchSourceBuilder;
import org.opensearch.security.rest.resource.ShareWith;
import org.opensearch.security.spi.Resource;
import org.opensearch.security.spi.ResourceParser;
import org.opensearch.security.spi.ResourceSharingService;
import org.opensearch.security.support.ConfigConstants;
import org.opensearch.security.support.WildcardMatcher;
Expand All @@ -44,19 +34,10 @@
public class SecurityResourceSharingService<T extends Resource> implements ResourceSharingService<T> {
private final Client client;
private final String resourceIndex;
private final ResourceParser<T> resourceParser;
private final NamedXContentRegistry xContentRegistry;

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

private boolean hasPermissionsFor(User authenticatedUser, ResourceSharingEntry sharedWith) {
Expand Down Expand Up @@ -122,34 +103,4 @@ public void onFailure(Exception e) {
client.search(searchRequest, searchListener);
}
}

private void finishGetResourceIfUserIsAllowed(String resourceId, ActionListener<T> getResourceListener) {
try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashContext()) {
GetRequest gr = new GetRequest(resourceIndex);
gr.id(resourceId);
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());
getResourceListener.onResponse(resource);
} catch (IOException e) {
throw new OpenSearchException("Caught exception while loading resources: " + e.getMessage());
}
}

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

0 comments on commit c576870

Please sign in to comment.