Skip to content

Commit 681a77a

Browse files
Updates doc for spi and re-organizes a class
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
1 parent 3064cd2 commit 681a77a

20 files changed

+56
-104
lines changed

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ tasks.integrationTest.finalizedBy(jacocoTestReport) // report is always generate
645645
check.dependsOn integrationTest
646646

647647
dependencies {
648+
compileOnly project(path: ":opensearch-resource-sharing-spi", configuration: 'shadow')
648649
implementation project(path: ":${rootProject.name}-common", configuration: 'shadow')
649650
implementation "org.opensearch.plugin:transport-netty4-client:${opensearch_version}"
650651
implementation "org.opensearch.client:opensearch-rest-high-level-client:${opensearch_version}"

common/src/main/java/org/opensearch/security/common/resources/ResourceAccessHandler.java

+1-50
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public <T extends Resource> void getAccessibleResourcesForCurrentUser(String res
173173
try {
174174
validateArguments(resourceIndex);
175175

176-
ResourceParser<T> parser = ResourcePluginInfo.getInstance().getResourceProviders().get(resourceIndex).getResourceParser();
176+
ResourceParser<T> parser = ResourcePluginInfo.getInstance().getResourceProviders().get(resourceIndex).resourceParser();
177177

178178
StepListener<Set<String>> resourceIdsListener = new StepListener<>();
179179
StepListener<Set<T>> resourcesListener = new StepListener<>();
@@ -366,55 +366,6 @@ public void revokeAccess(
366366
);
367367
}
368368

369-
/**
370-
* Checks if the current user has permission to modify a resource.
371-
* NOTE: Only admins and owners of the resource can modify the resource.
372-
* TODO: update this method to allow for other users to modify the resource.
373-
* @param resourceId The resource ID to check.
374-
* @param resourceIndex The resource index containing the resource.
375-
* @param listener The listener to be notified with the permission check result.
376-
*/
377-
public void canModifyResource(String resourceId, String resourceIndex, ActionListener<Boolean> listener) {
378-
try {
379-
validateArguments(resourceId, resourceIndex);
380-
381-
final UserSubjectImpl userSubject = (UserSubjectImpl) threadContext.getPersistent(
382-
ConfigConstants.OPENDISTRO_SECURITY_AUTHENTICATED_USER
383-
);
384-
final User user = (userSubject == null) ? null : userSubject.getUser();
385-
386-
if (user == null) {
387-
listener.onFailure(new ResourceSharingException("No authenticated user available."));
388-
return;
389-
}
390-
391-
StepListener<ResourceSharing> fetchDocListener = new StepListener<>();
392-
resourceSharingIndexHandler.fetchDocumentById(resourceIndex, resourceId, fetchDocListener);
393-
394-
fetchDocListener.whenComplete(document -> {
395-
if (document == null) {
396-
LOGGER.info("Document {} does not exist in index {}", resourceId, resourceIndex);
397-
// Either the document was deleted or has not been created yet. No permission check is needed for this.
398-
listener.onResponse(true);
399-
return;
400-
}
401-
402-
boolean isAdmin = adminDNs.isAdmin(user);
403-
boolean isOwner = isOwnerOfResource(document, user.getName());
404-
405-
if (!isAdmin && !isOwner) {
406-
LOGGER.info("User {} does not have access to delete the record {}", user.getName(), resourceId);
407-
listener.onResponse(false);
408-
} else {
409-
listener.onResponse(true);
410-
}
411-
}, listener::onFailure);
412-
} catch (Exception e) {
413-
LOGGER.error("Failed to check delete permission for resource {}", resourceId, e);
414-
listener.onFailure(e);
415-
}
416-
}
417-
418369
/**
419370
* Deletes a resource sharing record by its ID and the resource index it belongs to.
420371
*

common/src/main/java/org/opensearch/security/common/resources/ResourcePluginInfo.java

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import com.google.common.collect.ImmutableMap;
99
import com.google.common.collect.ImmutableSet;
1010

11-
import org.opensearch.security.spi.resources.ResourceProvider;
12-
1311
/**
1412
* This class provides information about resource plugins and their associated resource providers and indices.
1513
* It follows the Singleton pattern to ensure that only one instance of the class exists.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.security.common.resources;
10+
11+
import org.opensearch.security.spi.resources.ResourceParser;
12+
13+
/**
14+
* This record class represents a resource provider.
15+
* It holds information about the resource type, resource index name, and a resource parser.
16+
*/
17+
public record ResourceProvider(String resourceType, String resourceIndexName, ResourceParser resourceParser) {
18+
19+
}

sample-resource-plugin/src/integrationTest/java/org/opensearch/sample/AbstractSampleResourcePluginFeatureEnabledTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
import org.junit.Test;
1515

1616
import org.opensearch.security.common.resources.ResourcePluginInfo;
17+
import org.opensearch.security.common.resources.ResourceProvider;
1718
import org.opensearch.security.spi.resources.ResourceAccessScope;
18-
import org.opensearch.security.spi.resources.ResourceProvider;
1919
import org.opensearch.test.framework.cluster.LocalCluster;
2020
import org.opensearch.test.framework.cluster.TestRestClient;
2121

sample-resource-plugin/src/integrationTest/java/org/opensearch/sample/SampleResourcePluginSystemIndexDisabledTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import org.opensearch.painless.PainlessModulePlugin;
1616
import org.opensearch.security.common.resources.ResourcePluginInfo;
17-
import org.opensearch.security.spi.resources.ResourceProvider;
17+
import org.opensearch.security.common.resources.ResourceProvider;
1818
import org.opensearch.test.framework.cluster.ClusterManager;
1919
import org.opensearch.test.framework.cluster.LocalCluster;
2020
import org.opensearch.test.framework.cluster.TestRestClient;

sample-resource-plugin/src/integrationTest/java/org/opensearch/sample/SampleResourcePluginTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import org.opensearch.painless.PainlessModulePlugin;
1818
import org.opensearch.security.common.resources.ResourcePluginInfo;
19-
import org.opensearch.security.spi.resources.ResourceProvider;
19+
import org.opensearch.security.common.resources.ResourceProvider;
2020
import org.opensearch.test.framework.cluster.ClusterManager;
2121
import org.opensearch.test.framework.cluster.LocalCluster;
2222
import org.opensearch.test.framework.cluster.TestRestClient;

spi/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
This SPI provides interfaces to implement Resource Sharing and Access Control.
44

5+
6+
## Usage
7+
8+
A plugin defining a resource and aiming to implement access control over that resource must extend ResourceSharingExtension class to register itself
9+
10+
511
## License
612

713
This code is licensed under the Apache 2.0 License.

spi/src/main/java/org/opensearch/security/spi/resources/Resource.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
public interface Resource extends NamedWriteable, ToXContentFragment {
1818
/**
1919
* Abstract method to get the resource name.
20-
* Must be implemented by subclasses.
20+
* Must be implemented by plugins defining resources.
2121
*
2222
* @return resource name
2323
*/

spi/src/main/java/org/opensearch/security/spi/resources/ResourceAccessScope.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.Arrays;
1212

1313
/**
14-
* This interface defines the two basic access scopes for resource-access.
14+
* This interface defines the two basic access scopes for resource-access. Plugins can decide whether to use these.
1515
* Each plugin must implement their own scopes and manage them.
1616
* These access scopes will then be used to verify the type of access being requested.
1717
*

spi/src/main/java/org/opensearch/security/spi/resources/ResourceProvider.java

-33
This file was deleted.

spi/src/main/java/org/opensearch/security/spi/resources/ResourceSharingExtension.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,27 @@
99
package org.opensearch.security.spi.resources;
1010

1111
/**
12-
* This interface should be implemented by all the plugins that define one or more resources.
12+
* This interface should be implemented by all the plugins that define one or more resources and need access control over those resources.
1313
*
1414
* @opensearch.experimental
1515
*/
1616
public interface ResourceSharingExtension {
1717

1818
/**
1919
* Type of the resource
20-
* @return a string containing the type of the resource
20+
* @return a string containing the type of the resource. A qualified class name can be supplied here.
2121
*/
2222
String getResourceType();
2323

2424
/**
25-
* The index where resource meta-data is stored
26-
* @return the name of the parent index where resource meta-data is stored
25+
* The index where resource is stored
26+
* @return the name of the parent index where resource is stored
2727
*/
2828
String getResourceIndex();
2929

30+
/**
31+
* The parser for the resource, which will be used by security plugin to parse the resource
32+
* @return the parser for the resource
33+
*/
3034
ResourceParser<? extends Resource> getResourceParser();
3135
}

spi/src/main/java/org/opensearch/security/spi/resources/package-info.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
*/
88

99
/**
10-
* This package defines class required to implement resource access control in OpenSearch.
10+
* This package defines classes required to implement resource access control in OpenSearch.
11+
* This package will be added as a dependency by all OpenSearch plugins that require resource access control.
1112
*
1213
* @opensearch.experimental
1314
*/
14-
package main.java.org.opensearch.security.spi.resources;
15+
package org.opensearch.security.spi.resources;

spi/src/main/java/org/opensearch/security/spi/resources/sharing/CreatedBy.java

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
/**
2121
* This class is used to store information about the creator of a resource.
22-
* Concrete implementation will be provided by security plugin
2322
*
2423
* @opensearch.experimental
2524
*/

spi/src/main/java/org/opensearch/security/spi/resources/sharing/Creator.java

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
package org.opensearch.security.spi.resources.sharing;
1010

11+
/**
12+
* This enum is used to store information about the creator of a resource.
13+
*
14+
* @opensearch.experimental
15+
*/
1116
public enum Creator {
1217
USER("user");
1318

spi/src/main/java/org/opensearch/security/spi/resources/sharing/Recipient.java

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
package org.opensearch.security.spi.resources.sharing;
1010

11+
/**
12+
* Enum representing the recipients of a shared resource.
13+
* It includes USERS, ROLES, and BACKEND_ROLES.
14+
*/
1115
public enum Recipient {
1216
USERS("users"),
1317
ROLES("roles"),

spi/src/main/java/org/opensearch/security/spi/resources/sharing/RecipientTypeRegistry.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
/**
1515
* This class determines a collection of recipient types a resource can be shared with.
16+
* Allows addition of other recipient types in the future.
1617
*
17-
* @opensearch.experimental
18+
* @opensearch.experimental
1819
*/
1920
public final class RecipientTypeRegistry {
2021
// TODO: Check what size should this be. A cap should be added to avoid infinite addition of objects

spi/src/main/java/org/opensearch/security/spi/resources/sharing/ResourceSharing.java

-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
/**
2121
* Represents a resource sharing configuration that manages access control for OpenSearch resources.
2222
* This class holds information about shared resources including their source, creator, and sharing permissions.
23-
*
24-
* <p>This class implements {@link ToXContentFragment} for JSON serialization and {@link NamedWriteable}
25-
* for stream-based serialization.</p>
26-
* <p>
2723
* The class maintains information about:
2824
* <ul>
2925
* <li>The source index where the resource is defined</li>

spi/src/main/java/org/opensearch/security/spi/resources/sharing/SharedWithScope.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.opensearch.core.xcontent.XContentParser;
2323

2424
/**
25-
* This class represents the scope at which a resource is shared with.
25+
* This class represents the scope at which a resource is shared with for a particular scope.
2626
* Example:
2727
* "read_only": {
2828
* "users": [],

src/main/java/org/opensearch/security/OpenSearchSecurityPlugin.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
import org.opensearch.security.common.resources.ResourceAccessHandler;
148148
import org.opensearch.security.common.resources.ResourceIndexListener;
149149
import org.opensearch.security.common.resources.ResourcePluginInfo;
150+
import org.opensearch.security.common.resources.ResourceProvider;
150151
import org.opensearch.security.common.resources.ResourceSharingConstants;
151152
import org.opensearch.security.common.resources.ResourceSharingIndexHandler;
152153
import org.opensearch.security.common.resources.ResourceSharingIndexManagementRepository;
@@ -195,7 +196,6 @@
195196
import org.opensearch.security.setting.TransportPassiveAuthSetting;
196197
import org.opensearch.security.spi.resources.Resource;
197198
import org.opensearch.security.spi.resources.ResourceParser;
198-
import org.opensearch.security.spi.resources.ResourceProvider;
199199
import org.opensearch.security.spi.resources.ResourceSharingExtension;
200200
import org.opensearch.security.ssl.ExternalSecurityKeyStore;
201201
import org.opensearch.security.ssl.OpenSearchSecureSettingsFactory;

0 commit comments

Comments
 (0)