Skip to content

Commit

Permalink
Use action group
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Dec 17, 2024
1 parent c33d42a commit 4a0a8c3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
package org.opensearch.security.resource;

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

import org.opensearch.core.xcontent.ToXContentFragment;
Expand All @@ -25,9 +24,9 @@ public class ResourceSharingEntry implements ToXContentFragment {
private final String resourceIndex;
private final String resourceId;
private final ResourceUser resourceUser;
private final List<ShareWith> shareWith;
private final Map<String, ShareWith> shareWith;

public ResourceSharingEntry(String resourceIndex, String resourceId, ResourceUser resourceUser, List<ShareWith> shareWith) {
public ResourceSharingEntry(String resourceIndex, String resourceId, ResourceUser resourceUser, Map<String, ShareWith> shareWith) {
this.resourceIndex = resourceIndex;
this.resourceId = resourceId;
this.resourceUser = resourceUser;
Expand All @@ -39,10 +38,11 @@ public static ResourceSharingEntry fromSource(Map<String, Object> sourceAsMap) {
String resourceIndex = (String) sourceAsMap.get("resource_index");
String resourceId = (String) sourceAsMap.get("resource_id");
ResourceUser resourceUser = ResourceUser.fromSource((Map<String, Object>) sourceAsMap.get("resource_user"));
List<Map<String, Object>> sharedWithList = (List<Map<String, Object>>) sourceAsMap.get("share_with");
List<ShareWith> sharedWith = new ArrayList<>();
for (Map<String, Object> sharedWithMap : sharedWithList) {
sharedWith.add(ShareWith.fromSource(sharedWithMap));
Map<String, Object> sharedWithMap = (Map<String, Object>) sourceAsMap.get("share_with");
Map<String, ShareWith> sharedWith = new HashMap<>();
for (Map.Entry<String, Object> entry : sharedWithMap.entrySet()) {
ShareWith shareWith = ShareWith.fromSource((Map<String, Object>) entry.getValue());
sharedWith.put(entry.getKey(), shareWith);
}
return new ResourceSharingEntry(resourceIndex, resourceId, resourceUser, sharedWith);
}
Expand All @@ -51,7 +51,7 @@ public ResourceUser getResourceUser() {
return resourceUser;
}

public List<ShareWith> getShareWith() {
public Map<String, ShareWith> getShareWith() {
return shareWith;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
package org.opensearch.security.resource;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -45,6 +45,8 @@ public class ResourceSharingListener implements IndexingOperationListener {

public static final String RESOURCE_SHARING_INDEX = ".resource-sharing";

private static final String UNLIMITED = "unlimited";

private boolean initialized;
private ThreadPool threadPool;
private Client client;
Expand Down Expand Up @@ -115,7 +117,7 @@ private void createResourceSharingIndexIfNotExists(Callable<Boolean> callable) {
public void indexResourceSharing(String resourceId, String resourceIndex, ResourceUser resourceUser, ShareWith shareWith)
throws IOException {
createResourceSharingIndexIfNotExists(() -> {
ResourceSharingEntry entry = new ResourceSharingEntry(resourceIndex, resourceId, resourceUser, List.of(shareWith));
ResourceSharingEntry entry = new ResourceSharingEntry(resourceIndex, resourceId, resourceUser, Map.of(UNLIMITED, shareWith));

IndexRequest ir = client.prepareIndex(RESOURCE_SHARING_INDEX)
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private boolean hasPermissionsFor(User authenticatedUser, ResourceSharingEntry s
return true;
}

for (ShareWith shareWith : sharedWith.getShareWith()) {
for (ShareWith shareWith : sharedWith.getShareWith().values()) {
WildcardMatcher userMatcher = WildcardMatcher.from(shareWith.getUsers());
if (userMatcher.test(authenticatedUser.getName())) {
return true;
Expand Down

0 comments on commit 4a0a8c3

Please sign in to comment.