jerryshao commented on code in PR #7842:
URL: https://github.com/apache/gravitino/pull/7842#discussion_r2262398996


##########
server/src/main/java/org/apache/gravitino/server/web/rest/MetadataObjectPolicyOperations.java:
##########
@@ -0,0 +1,348 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.server.web.rest;
+
+import com.codahale.metrics.annotation.ResponseMetered;
+import com.codahale.metrics.annotation.Timed;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Optional;
+import java.util.Set;
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Response;
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.MetadataObjects;
+import org.apache.gravitino.dto.policy.PolicyDTO;
+import org.apache.gravitino.dto.requests.PoliciesAssociateRequest;
+import org.apache.gravitino.dto.responses.NameListResponse;
+import org.apache.gravitino.dto.responses.PolicyListResponse;
+import org.apache.gravitino.dto.responses.PolicyResponse;
+import org.apache.gravitino.dto.util.DTOConverters;
+import org.apache.gravitino.exceptions.NoSuchPolicyException;
+import org.apache.gravitino.metrics.MetricNames;
+import org.apache.gravitino.policy.Policy;
+import org.apache.gravitino.policy.PolicyDispatcher;
+import org.apache.gravitino.server.web.Utils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Path("/metalakes/{metalake}/objects/{type}/{fullName}/policies")
+public class MetadataObjectPolicyOperations {
+  private static final Logger LOG = 
LoggerFactory.getLogger(MetadataObjectPolicyOperations.class);
+
+  private final PolicyDispatcher policyDispatcher;
+
+  @Context private HttpServletRequest httpRequest;
+
+  @Inject
+  public MetadataObjectPolicyOperations(PolicyDispatcher policyDispatcher) {
+    this.policyDispatcher = policyDispatcher;
+  }
+
+  @GET
+  @Path("{policy}")
+  @Produces("application/vnd.gravitino.v1+json")
+  @Timed(name = "get-object-policy." + MetricNames.HTTP_PROCESS_DURATION, 
absolute = true)
+  @ResponseMetered(name = "get-object-policy", absolute = true)
+  public Response getPolicyForObject(
+      @PathParam("metalake") String metalake,
+      @PathParam("type") String type,
+      @PathParam("fullName") String fullName,
+      @PathParam("policy") String policyName) {
+    LOG.info(
+        "Received get policy {} request for object type: {}, full name: {} 
under metalake: {}",
+        policyName,
+        type,
+        fullName,
+        metalake);
+
+    try {
+      return Utils.doAs(
+          httpRequest,
+          () -> {
+            MetadataObject object =
+                MetadataObjects.parse(
+                    fullName, 
MetadataObject.Type.valueOf(type.toUpperCase(Locale.ROOT)));
+            Optional<Policy> policy = getPolicyForObject(metalake, object, 
policyName);
+
+            if (policy.isPresent()) {
+              LOG.info(
+                  "Get policy: {} for object type: {}, full name: {} under 
metalake: {}",
+                  policyName,
+                  type,
+                  fullName,
+                  metalake);
+              return Utils.ok(
+                  new PolicyResponse(DTOConverters.toDTO(policy.get(), 
Optional.of(false))));
+            }
+
+            // ensure the policy exists and is inheritable
+            Policy targetPolicy = policyDispatcher.getPolicy(metalake, 
policyName);
+            if (!targetPolicy.inheritable()
+                || 
!targetPolicy.supportedObjectTypes().contains(object.type())) {
+              return logNotFoundPolicy(metalake, policyName, type, fullName);
+            }
+
+            if (hasConflictExclusivePolicy(metalake, object, targetPolicy)) {
+              return logNotFoundPolicy(metalake, policyName, type, fullName);
+            }
+
+            MetadataObject parentObject = MetadataObjects.parent(object);
+            while (parentObject != null) {
+              if 
(!targetPolicy.supportedObjectTypes().contains(parentObject.type())) {
+                // If the parent object type is not supported by the target 
policy, we skip it.
+                parentObject = MetadataObjects.parent(parentObject);
+                continue;
+              }
+
+              Policy[] parentPolicies =
+                  policyDispatcher.listPolicyInfosForMetadataObject(metalake, 
parentObject);

Review Comment:
   Why do you use "list" rather than "get" for policy?



##########
core/src/main/java/org/apache/gravitino/storage/relational/service/PolicyMetaService.java:
##########
@@ -86,6 +91,24 @@ public void insertPolicy(PolicyEntity policyEntity, boolean 
overwritten) throws
       PolicyPO.Builder builder = PolicyPO.builder().withMetalakeId(metalakeId);
       PolicyPO policyPO = 
POConverters.initializePolicyPOWithVersion(policyEntity, builder);
 
+      List<PolicyPO> diffPolicyPOs = 
getDiffPolicyPOsByMetalakeIdAndPolicy(metalakeId, policyPO);
+      if (!diffPolicyPOs.isEmpty()) {
+        throw new IllegalArgumentException(
+            String.format(
+                "Policy '%s' has inconsistent immutable attributes with 
existing policy '%s' of the same type '%s'. "
+                    + "expected: exclusive=%s, inheritable=%s, 
supportedObjectTypes=%s; "
+                    + "but got: exclusive=%s, inheritable=%s, 
supportedObjectTypes=%s.",
+                policyPO.getPolicyName(),
+                diffPolicyPOs.get(0).getPolicyName(),
+                policyPO.getPolicyType(),
+                diffPolicyPOs.get(0).isExclusive(),
+                diffPolicyPOs.get(0).isInheritable(),
+                diffPolicyPOs.get(0).getSupportedObjectTypes(),
+                policyPO.isExclusive(),
+                policyPO.isInheritable(),
+                policyPO.getSupportedObjectTypes()));
+      }

Review Comment:
   Shall we move this kind of check logic and below to `PolicyManager`? I think 
we involve too much logic in the storage layer.



##########
core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/PolicyMetaBaseSQLProvider.java:
##########
@@ -73,6 +73,27 @@ public String listPolicyPOsByMetalakeAndPolicyNames(
         + "</script>";
   }
 
+  public String listDiffPolicyPOsByMetalakeIdAndPolicy(
+      @Param("metalakeId") Long metalakeId, @Param("policy") PolicyPO 
policyPO) {
+    return "SELECT pm.policy_id, pm.policy_name, pm.policy_type, 
pm.metalake_id, pm.inheritable,"
+        + " pm.exclusive, pm.supported_object_types, pm.audit_info, 
pm.current_version, pm.last_version,"
+        + " pm.deleted_at, pvi.id, pvi.metalake_id as version_metalake_id, 
pvi.policy_id as version_policy_id,"
+        + " pvi.version, pvi.policy_comment, pvi.enabled, pvi.content, 
pvi.deleted_at as version_deleted_at"
+        + " FROM "
+        + POLICY_META_TABLE_NAME
+        + " pm JOIN "
+        + POLICY_VERSION_TABLE_NAME
+        + " pvi ON pm.policy_id = pvi.policy_id"
+        + " AND pm.current_version = pvi.version"
+        + " WHERE pm.metalake_id = #{metalakeId}"
+        + " AND pm.policy_id != #{policy.policyId}"
+        + " AND pm.policy_type = #{policy.policyType}"
+        + " AND (pm.inheritable != #{policy.inheritable} OR"
+        + " pm.exclusive != #{policy.exclusive} OR"
+        + " pm.supported_object_types != #{policy.supportedObjectTypes})"
+        + " AND pm.deleted_at = 0 AND pvi.deleted_at = 0";
+  }

Review Comment:
   Can you explain more about this SQL query?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to