forked from opensearch-project/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Craig Perkins <[email protected]>
- Loading branch information
Showing
9 changed files
with
239 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 2 additions & 104 deletions
106
spi/src/main/java/org/opensearch/security/spi/DefaultResourceSharingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 1 addition & 6 deletions
7
spi/src/main/java/org/opensearch/security/spi/ResourceSharingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.