Skip to content

Commit

Permalink
Convert sets to lists
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Dec 5, 2024
1 parent bc67926 commit ccd5d07
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package org.opensearch.sample.actions.access.list;

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

import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.io.stream.StreamInput;
Expand All @@ -21,9 +21,9 @@
* Response to a ListAccessibleResourcesRequest
*/
public class ListAccessibleResourcesResponse extends ActionResponse implements ToXContentObject {
private final List<String> resourceIds;
private final Set<String> resourceIds;

public ListAccessibleResourcesResponse(List<String> resourceIds) {
public ListAccessibleResourcesResponse(Set<String> resourceIds) {
this.resourceIds = resourceIds;
}

Expand All @@ -33,7 +33,7 @@ public void writeTo(StreamOutput out) throws IOException {
}

public ListAccessibleResourcesResponse(final StreamInput in) throws IOException {
resourceIds = in.readStringList();
resourceIds = in.readSet(StreamInput::readString);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@
package org.opensearch.sample.actions.access.revoke;

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

import org.opensearch.accesscontrol.resources.EntityType;
import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.sample.utils.Validation;

public class RevokeResourceAccessRequest extends ActionRequest {

private final String resourceId;
private final Map<EntityType, List<String>> revokeAccess;
private final List<String> scopes;
private final Map<EntityType, Set<String>> revokeAccess;
private final Set<String> scopes;

public RevokeResourceAccessRequest(String resourceId, Map<EntityType, List<String>> revokeAccess, List<String> scopes) {
public RevokeResourceAccessRequest(String resourceId, Map<EntityType, Set<String>> revokeAccess, Set<String> scopes) {
this.resourceId = resourceId;
this.revokeAccess = revokeAccess;
this.scopes = scopes;
}

public RevokeResourceAccessRequest(StreamInput in) throws IOException {
this.resourceId = in.readString();
this.revokeAccess = in.readMap(input -> EntityType.valueOf(input.readString()), StreamInput::readStringList);
this.scopes = in.readStringList();
this.revokeAccess = in.readMap(input -> EntityType.valueOf(input.readString()), input -> input.readSet(StreamInput::readString));
this.scopes = in.readSet(StreamInput::readString);
}

@Override
Expand All @@ -49,18 +50,23 @@ public void writeTo(final StreamOutput out) throws IOException {

@Override
public ActionRequestValidationException validate() {

if (!(this.scopes == null)) {
return Validation.validateScopes(this.scopes);
}

return null;
}

public String getResourceId() {
return resourceId;
}

public Map<EntityType, List<String>> getRevokeAccess() {
public Map<EntityType, Set<String>> getRevokeAccess() {
return revokeAccess;
}

public List<String> getScopes() {
public Set<String> getScopes() {
return scopes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.opensearch.accesscontrol.resources.EntityType;
Expand Down Expand Up @@ -47,8 +48,8 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli

String resourceId = (String) source.get("resource_id");
@SuppressWarnings("unchecked")
Map<String, List<String>> revokeSource = (Map<String, List<String>>) source.get("entities");
Map<EntityType, List<String>> revoke = revokeSource.entrySet().stream().collect(Collectors.toMap(entry -> {
Map<String, Set<String>> revokeSource = (Map<String, Set<String>>) source.get("entities");
Map<EntityType, Set<String>> revoke = revokeSource.entrySet().stream().collect(Collectors.toMap(entry -> {
try {
return EntityType.fromValue(entry.getKey());
} catch (IllegalArgumentException e) {
Expand All @@ -58,7 +59,7 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
}
}, Map.Entry::getValue));
@SuppressWarnings("unchecked")
List<String> scopes = source.containsKey("scopes") ? (List<String>) source.get("scopes") : List.of();
Set<String> scopes = source.containsKey("scopes") ? (Set<String>) source.get("scopes") : Set.of();
final RevokeResourceAccessRequest revokeResourceAccessRequest = new RevokeResourceAccessRequest(resourceId, revoke, scopes);
return channel -> client.executeLocally(
RevokeResourceAccessAction.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
package org.opensearch.sample.actions.access.share;

import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Collectors;

import org.opensearch.accesscontrol.resources.ShareWith;
import org.opensearch.accesscontrol.resources.SharedWithScope;
import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.sample.SampleResourceScope;
import org.opensearch.sample.utils.Validation;

public class ShareResourceRequest extends ActionRequest {

Expand All @@ -43,19 +43,9 @@ public void writeTo(final StreamOutput out) throws IOException {
@Override
public ActionRequestValidationException validate() {

for (SharedWithScope s : shareWith.getSharedWithScopes()) {
try {
SampleResourceScope.valueOf(s.getScope());
} catch (IllegalArgumentException | NullPointerException e) {
ActionRequestValidationException exception = new ActionRequestValidationException();
exception.addValidationError(
"Invalid scope: " + s.getScope() + ". Scope must be one of: " + Arrays.toString(SampleResourceScope.values())
);
return exception;
}
return null;
}
return null;
return Validation.validateScopes(
shareWith.getSharedWithScopes().stream().map(SharedWithScope::getScope).collect(Collectors.toSet())
);
}

public String getResourceId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
package org.opensearch.sample.actions.access.verify;

import java.io.IOException;
import java.util.Arrays;
import java.util.Set;

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.sample.SampleResourceScope;
import org.opensearch.sample.utils.Validation;

public class VerifyResourceAccessRequest extends ActionRequest {

Expand Down Expand Up @@ -49,16 +49,7 @@ public void writeTo(final StreamOutput out) throws IOException {

@Override
public ActionRequestValidationException validate() {
try {
SampleResourceScope.valueOf(scope);
} catch (IllegalArgumentException | NullPointerException e) {
ActionRequestValidationException exception = new ActionRequestValidationException();
exception.addValidationError(
"Invalid scope: " + scope + ". Scope must be one of: " + Arrays.toString(SampleResourceScope.values())
);
return exception;
}
return null;
return Validation.validateScopes(Set.of(scope));
}

public String getResourceId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

package org.opensearch.sample.transport.access;

import java.util.List;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -41,7 +41,7 @@ public ListAccessibleResourcesTransportAction(TransportService transportService,
protected void doExecute(Task task, ListAccessibleResourcesRequest request, ActionListener<ListAccessibleResourcesResponse> listener) {
try {
ResourceService rs = SampleResourcePlugin.GuiceHolder.getResourceService();
List<String> resourceIds = rs.getResourceAccessControlPlugin().listAccessibleResourcesInPlugin(RESOURCE_INDEX_NAME);
Set<String> resourceIds = rs.getResourceAccessControlPlugin().listAccessibleResourcesInPlugin(RESOURCE_INDEX_NAME);
log.info("Successfully fetched accessible resources for current user : {}", resourceIds);
listener.onResponse(new ListAccessibleResourcesResponse(resourceIds));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.sample.utils;

import java.util.Arrays;
import java.util.Set;

import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.sample.SampleResourceScope;

public class Validation {
public static ActionRequestValidationException validateScopes(Set<String> scopes) {
for (String s : scopes) {
try {
SampleResourceScope.valueOf(s);
} catch (IllegalArgumentException | NullPointerException e) {
ActionRequestValidationException exception = new ActionRequestValidationException();
exception.addValidationError(
"Invalid scope: " + s + ". Scope must be one of: " + Arrays.toString(SampleResourceScope.values())
);
return exception;
}
}
return null;
}
}

0 comments on commit ccd5d07

Please sign in to comment.