Skip to content

Commit

Permalink
Add ResourceParser
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 0d7c1c5 commit d7cf362
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@
import org.opensearch.security.sampleextension.actions.update.UpdateSampleResourceRestAction;
import org.opensearch.security.sampleextension.actions.update.UpdateSampleResourceTransportAction;
import org.opensearch.security.sampleextension.resource.SampleResource;
import org.opensearch.security.sampleextension.resource.SampleResourceFactory;
import org.opensearch.security.sampleextension.resource.SampleResourceParser;
import org.opensearch.security.sampleextension.resource.SampleResourceSharingServiceProvider;
import org.opensearch.security.spi.DefaultResourceSharingService;
import org.opensearch.security.spi.Resource;
import org.opensearch.security.spi.ResourceFactory;
import org.opensearch.security.spi.ResourceParser;
import org.opensearch.security.spi.ResourceSharingExtension;
import org.opensearch.security.spi.ResourceSharingService;
import org.opensearch.threadpool.ThreadPool;
Expand Down Expand Up @@ -92,7 +92,7 @@ public Collection<Object> createComponents(
if (SampleResourceSharingServiceProvider.getInstance().get() == null) {
System.out.println("Using DefaultResourceSharingService");
SampleResourceSharingServiceProvider.getInstance()
.set(new DefaultResourceSharingService<>(client, RESOURCE_INDEX_NAME, new SampleResourceFactory()));
.set(new DefaultResourceSharingService<>(client, RESOURCE_INDEX_NAME, new SampleResourceParser(), xContentRegistry));
}
System.out.println(
"SampleResourceSharingServiceProvider.getInstance(): " + SampleResourceSharingServiceProvider.getInstance().get()
Expand Down Expand Up @@ -145,8 +145,8 @@ public String getResourceIndex() {
}

@Override
public ResourceFactory<? extends Resource> getResourceFactory() {
return new SampleResourceFactory();
public ResourceParser<? extends Resource> getResourceParser() {
return new SampleResourceParser();
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.opensearch.security.sampleextension.resource;

import java.io.IOException;
import java.util.Map;

import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.security.spi.Resource;

public class SampleResource extends Resource {
public class SampleResource implements Resource {

private String name;

Expand All @@ -22,12 +21,6 @@ public static SampleResource from(StreamInput in) throws IOException {
return new SampleResource(in);
}

@Override
public void fromSource(String resourceId, Map<String, Object> sourceAsMap) {
super.fromSource(resourceId, sourceAsMap);
this.name = (String) sourceAsMap.get("name");
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject().field("name", name).endObject();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.opensearch.security.sampleextension.resource;

import java.io.IOException;

import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.core.xcontent.XContentParserUtils;
import org.opensearch.security.spi.ResourceParser;

public class SampleResourceParser implements ResourceParser<SampleResource> {

@Override
public SampleResource parse(XContentParser parser, String id) throws IOException {
SampleResource resource = new SampleResource();
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);

while (!parser.nextToken().equals(XContentParser.Token.END_OBJECT)) {
String fieldName = parser.currentName();
parser.nextToken();
switch (fieldName) {
case "name":
resource.setName(parser.text());
break;
default:
XContentParserUtils.throwUnknownToken(parser.currentToken(), parser.getTokenLocation());
}
}
return resource;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.opensearch.security.spi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -10,20 +11,32 @@
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 ResourceFactory<T> resourceFactory;
private final ResourceParser<T> resourceParser;
private final NamedXContentRegistry xContentRegistry;

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

@SuppressWarnings("unchecked")
Expand All @@ -40,9 +53,18 @@ public void listResources(ActionListener<List<T>> listResourceListener) {
public void onResponse(SearchResponse searchResponse) {
List<T> resources = new ArrayList<>();
for (SearchHit hit : searchResponse.getHits().getHits()) {
T resource = resourceFactory.createResource();
resource.fromSource(hit.getId(), hit.getSourceAsMap());
resources.add(resource);
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);
}
Expand All @@ -66,9 +88,18 @@ public void getResource(String resourceId, ActionListener<T> getResourceListener
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);
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
Expand Down
16 changes: 2 additions & 14 deletions spi/src/main/java/org/opensearch/security/spi/Resource.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
package org.opensearch.security.spi;

import java.util.Map;

import org.opensearch.core.common.io.stream.NamedWriteable;
import org.opensearch.core.xcontent.ToXContentFragment;

public abstract class Resource implements NamedWriteable, ToXContentFragment {
protected String resourceId;

public String getResourceId() {
return resourceId;
}
import org.opensearch.core.xcontent.ToXContentObject;

public void fromSource(String resourceId, Map<String, Object> sourceAsMap) {
this.resourceId = resourceId;
}
}
public interface Resource extends NamedWriteable, ToXContentObject {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright OpenSearch Contributors
* 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.security.spi;

import java.io.IOException;
import java.util.Locale;

import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;

/**
* Structure to represent resource document version.
*/
public class ResourceDocVersion implements Comparable<ResourceDocVersion>, Writeable {
private final long primaryTerm;
private final long seqNo;
private final long version;

public ResourceDocVersion(long primaryTerm, long seqNo, long version) {
this.primaryTerm = primaryTerm;
this.seqNo = seqNo;
this.version = version;
}

public ResourceDocVersion(StreamInput in) throws IOException {
this.primaryTerm = in.readLong();
this.seqNo = in.readLong();
this.version = in.readLong();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeLong(this.primaryTerm);
out.writeLong(this.seqNo);
out.writeLong(this.version);
}

public long getPrimaryTerm() {
return primaryTerm;
}

public long getSeqNo() {
return seqNo;
}

public long getVersion() {
return version;
}

/**
* Compare two doc versions. Refer to https://github.com/elastic/elasticsearch/issues/10708
*
* @param v the doc version to compare.
* @return -1 if this &lt; v, 0 if this == v, otherwise 1;
*/
@Override
public int compareTo(ResourceDocVersion v) {
if (v == null) {
return 1;
}
if (this.seqNo < v.seqNo) {
return -1;
}
if (this.seqNo > v.seqNo) {
return 1;
}
if (this.primaryTerm < v.primaryTerm) {
return -1;
}
if (this.primaryTerm > v.primaryTerm) {
return 1;
}
return 0;
}

@Override
public String toString() {
return String.format(
Locale.getDefault(),
"{_version: %s, _primary_term: %s, _seq_no: %s}",
this.version,
this.primaryTerm,
this.seqNo
);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.opensearch.security.spi;

import java.io.IOException;

import org.opensearch.core.xcontent.XContentParser;

public interface ResourceParser<T extends Resource> {
T parse(XContentParser xContentParser, String id) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public interface ResourceSharingExtension {
String getResourceIndex();

/**
* @return The class corresponding to this resource
* @return returns a parser for this resource
*/
ResourceFactory<? extends Resource> getResourceFactory();
ResourceParser<? extends Resource> getResourceParser();

void assignResourceSharingService(ResourceSharingService<? extends Resource> service);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,8 @@ public Collection<Object> createComponents(
ResourceSharingService<?> resourceSharingService = new SecurityResourceSharingService<>(
localClient,
extension.getResourceIndex(),
extension.getResourceFactory()
extension.getResourceParser(),
xContentRegistry
);
extension.assignResourceSharingService(resourceSharingService);
}
Expand Down
Loading

0 comments on commit d7cf362

Please sign in to comment.