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


##########
core/src/main/java/org/apache/gravitino/policy/PolicyManager.java:
##########
@@ -73,6 +78,54 @@ public PolicyManager(IdGenerator idGenerator, EntityStore 
entityStore) {
     this.objectPolicyResolver = new ObjectPolicyResolver(entityStore);
   }
 
+  /**
+   * Applies a policy to an object through its hidden tag using the entity 
store.
+   *
+   * <p>The hidden tag is created with the policy. This method loads that tag 
and submits an
+   * idempotent object assignment. The relation operation validates the object 
endpoint and owns the
+   * transaction.
+   *
+   * <p>This internal method is not exposed through the policy dispatcher or 
client APIs.
+   *
+   * @param metalake The policy and object's metalake.
+   * @param policyName The policy name.
+   * @param object The metadata object to associate.
+   * @param auditInfo The audit information to preserve on newly created 
relations.
+   * @return The policy's hidden tag ID.
+   * @throws IOException If an entity or relation cannot be persisted.
+   * @throws NoSuchEntityException If the policy, hidden tag or target object 
does not exist.
+   * @throws IllegalArgumentException If the object type does not support tag 
assignments.
+   */
+  public long addPolicyToObject(
+      String metalake, String policyName, MetadataObject object, Audit 
auditInfo)
+      throws IOException {
+    Preconditions.checkNotNull(auditInfo, "Policy binding audit information is 
required");
+    Preconditions.checkArgument(
+        SUPPORTED_METADATA_OBJECT_TYPES_FOR_TAGS.contains(object.type()),
+        "Unsupported policy binding object type: %s",
+        object.type());
+    checkMetalake(NameIdentifier.of(metalake), entityStore);
+
+    NameIdentifier policyIdent = NameIdentifierUtil.ofPolicy(metalake, 
policyName);
+    PolicyEntity policy =
+        entityStore.get(policyIdent, Entity.EntityType.POLICY, 
PolicyEntity.class);
+    NameIdentifier tagIdent = NameIdentifierUtil.ofTag(metalake, 
HiddenPolicyTag.name(policy.id()));
+    TagEntity tag = entityStore.get(tagIdent, Entity.EntityType.TAG, 
TagEntity.class);

Review Comment:
   Policies created before this change have no hidden tag, so this get throws 
NoSuchEntityException and they can never be bound. Add a backfill/migration, or 
lazily create the hidden tag here when it is missing.



##########
core/src/main/java/org/apache/gravitino/storage/relational/service/PolicyMetaService.java:
##########
@@ -129,6 +140,8 @@ public void insertPolicy(PolicyEntity policyEntity, boolean 
overwritten) throws
       SessionUtils.doMultipleWithCommit(
           () -> lockMetalakeForPolicyCreate(metalakePO),
           () -> insertPolicyWithoutCommit(policyEntity, policyPO, 
overwritten));
+    } catch (UncheckedIOException e) {
+      throw e.getCause();

Review Comment:
   Rethrowing e.getCause() drops the UncheckedIOException wrapper, so confirm 
the enclosing method declares IOException and that the transaction is rolled 
back before it propagates. Add a test where hidden tag insertion fails and the 
policy row is not left behind.



##########
core/src/main/java/org/apache/gravitino/policy/HiddenPolicyTag.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.policy;
+
+import java.util.Collections;
+import java.util.Locale;
+import javax.annotation.Nullable;
+import org.apache.gravitino.meta.PolicyEntity;
+import org.apache.gravitino.meta.TagEntity;
+import org.apache.gravitino.utils.NamespaceUtil;
+
+/** Naming and visibility checks for server-managed policy tags. */
+public final class HiddenPolicyTag {
+
+  private static final String PREFIX = "gravitino_hidden_";
+  private static final String POLICY_ID_PROPERTY = 
"gravitino.internal.policy-id";
+
+  private HiddenPolicyTag() {}
+
+  /**
+   * Builds the server-managed tag for a policy using Gravitino's tag entity 
model.
+   *
+   * @param policy The policy that owns the hidden tag.
+   * @param tagId The generated tag entity ID.
+   * @return An unassigned, value-less tag with the policy's namespace and 
audit information.
+   */
+  public static TagEntity create(PolicyEntity policy, long tagId) {
+    return TagEntity.builder()
+        .withId(tagId)
+        .withName(name(policy.id()))
+        .withNamespace(NamespaceUtil.ofTag(policy.namespace().level(0)))
+        .withComment("Server-managed policy binding")
+        .withProperties(Collections.singletonMap(POLICY_ID_PROPERTY, 
Long.toString(policy.id())))
+        .withAllowedValues(new String[0])
+        .withAuditInfo(policy.auditInfo())
+        .build();
+  }
+
+  /**
+   * Returns a stable tag name that survives policy renames.
+   *
+   * @param policyId The policy entity ID.
+   * @return The reserved tag name.
+   */
+  public static String name(long policyId) {
+    return PREFIX + policyId;
+  }
+
+  /**
+   * Returns whether a tag name belongs to the reserved internal namespace.
+   *
+   * @param name The tag name.
+   * @return Whether the name is reserved.
+   */
+  public static boolean isReservedName(@Nullable String name) {

Review Comment:
   Existing user tags whose names start with gravitino_hidden_ 
(case-insensitive) will now silently disappear from list/get at REST. Is a 
pre-upgrade check or migration for such names planned?



-- 
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