From d9f52629e6b146e94789dc974273f100a517ea7c Mon Sep 17 00:00:00 2001
From: Darshit Chanpura <dchanp@amazon.com>
Date: Fri, 20 Dec 2024 12:15:20 -0500
Subject: [PATCH] Adds type bounding to Resource

Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
---
 .../resources/testplugins/TestResourcePlugin.java  |  3 ++-
 .../accesscontrol/resources/Resource.java          | 14 ++++++++++++++
 .../DefaultResourceAccessControlPlugin.java        |  3 ++-
 .../plugins/ResourceAccessControlPlugin.java       |  5 +++--
 4 files changed, 21 insertions(+), 4 deletions(-)
 create mode 100644 server/src/main/java/org/opensearch/accesscontrol/resources/Resource.java

diff --git a/server/src/internalClusterTest/java/org/opensearch/accesscontrol/resources/testplugins/TestResourcePlugin.java b/server/src/internalClusterTest/java/org/opensearch/accesscontrol/resources/testplugins/TestResourcePlugin.java
index f340f6d56e221..a198d9e78f2d9 100644
--- a/server/src/internalClusterTest/java/org/opensearch/accesscontrol/resources/testplugins/TestResourcePlugin.java
+++ b/server/src/internalClusterTest/java/org/opensearch/accesscontrol/resources/testplugins/TestResourcePlugin.java
@@ -8,6 +8,7 @@
 
 package org.opensearch.accesscontrol.resources.testplugins;
 
+import org.opensearch.accesscontrol.resources.Resource;
 import org.opensearch.accesscontrol.resources.ResourceService;
 import org.opensearch.common.inject.Inject;
 import org.opensearch.common.lifecycle.Lifecycle;
@@ -77,7 +78,7 @@ public void stop() {}
 
     }
 
-    public static class TestResource {
+    public static class TestResource implements Resource {
         public String id;
         public String name;
 
diff --git a/server/src/main/java/org/opensearch/accesscontrol/resources/Resource.java b/server/src/main/java/org/opensearch/accesscontrol/resources/Resource.java
new file mode 100644
index 0000000000000..376aceed80548
--- /dev/null
+++ b/server/src/main/java/org/opensearch/accesscontrol/resources/Resource.java
@@ -0,0 +1,14 @@
+/*
+ * 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.accesscontrol.resources;
+
+/**
+ * Marker interface for all resources
+ */
+public interface Resource {}
diff --git a/server/src/main/java/org/opensearch/accesscontrol/resources/fallback/DefaultResourceAccessControlPlugin.java b/server/src/main/java/org/opensearch/accesscontrol/resources/fallback/DefaultResourceAccessControlPlugin.java
index 246de3763aed6..2efc519e05852 100644
--- a/server/src/main/java/org/opensearch/accesscontrol/resources/fallback/DefaultResourceAccessControlPlugin.java
+++ b/server/src/main/java/org/opensearch/accesscontrol/resources/fallback/DefaultResourceAccessControlPlugin.java
@@ -13,6 +13,7 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.opensearch.OpenSearchException;
+import org.opensearch.accesscontrol.resources.Resource;
 import org.opensearch.action.search.ClearScrollRequest;
 import org.opensearch.action.search.SearchRequest;
 import org.opensearch.action.search.SearchResponse;
@@ -66,7 +67,7 @@ public DefaultResourceAccessControlPlugin(Client client, ThreadPool threadPool)
      * @return Set of resource ids
      */
     @Override
-    public <T> Set<T> getAccessibleResourcesForCurrentUser(String resourceIndex, Class<T> clazz) {
+    public <T extends Resource> Set<T> getAccessibleResourcesForCurrentUser(String resourceIndex, Class<T> clazz) {
         final Set<T> documents = new HashSet<>();
         final TimeValue scrollTimeout = TimeValue.timeValueMinutes(1);
         String scrollId;
diff --git a/server/src/main/java/org/opensearch/plugins/ResourceAccessControlPlugin.java b/server/src/main/java/org/opensearch/plugins/ResourceAccessControlPlugin.java
index ebdff187691b9..663f786323b6b 100644
--- a/server/src/main/java/org/opensearch/plugins/ResourceAccessControlPlugin.java
+++ b/server/src/main/java/org/opensearch/plugins/ResourceAccessControlPlugin.java
@@ -9,6 +9,7 @@
 package org.opensearch.plugins;
 
 import org.opensearch.accesscontrol.resources.EntityType;
+import org.opensearch.accesscontrol.resources.Resource;
 import org.opensearch.accesscontrol.resources.ResourceSharing;
 import org.opensearch.accesscontrol.resources.ShareWith;
 
@@ -27,10 +28,10 @@ public interface ResourceAccessControlPlugin {
     /**
      * Returns all accessible resources for current user for a given plugin index.
      * @param resourceIndex index where the resource exists
-     * @param clazz class of the resource. Required to parse the resource object retrieved from resourceIndex
+     * @param clazz class of the resource. Required to parse the resource object retrieved from resourceIndex. Must be a type of {@link Resource}
      * @return set of {@link ResourceSharing} items accessible by current user.
      */
-    default <T> Set<T> getAccessibleResourcesForCurrentUser(String resourceIndex, Class<T> clazz) {
+    default <T extends Resource> Set<T> getAccessibleResourcesForCurrentUser(String resourceIndex, Class<T> clazz) {
         return Set.of();
     }