roryqi commented on code in PR #14326:
URL: https://github.com/apache/gravitino/pull/14326#discussion_r4509093278
##########
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:
The migration tool (#14375) can check for existing policies missing their
canonical hidden tag or policy-tag relation during preflight. Initialization of
those policies belongs to that migration step, before object bindings are
converted. `addPolicyToObject` remains an internal assignment-only helper and
requires the hidden tag and policy-tag relation to exist before it is called.
##########
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:
The migration tool (#14375) can handle this in its offline preflight: scan
existing tags for the case-insensitive `gravitino_hidden_` prefix and report
user-tag collisions before upgraded servers start. Conflicting user tags can
then be renamed during migration before the REST visibility rules apply. We
will keep the current namespace convention; the distinct prefix makes
accidental collisions unlikely.
##########
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:
Added
[`testHiddenTagIOExceptionRollsBackPolicy`](https://github.com/apache/gravitino/blob/984d41c119ebd59331f68af0c8821d967804401a/core/src/test/java/org/apache/gravitino/storage/relational/service/TestPolicyMetaService.java#L389)
in `984d41c119`. It executes the real tag insert, confirms that both policy
and tag exist inside the transaction, and then injects an `IOException`. The
test verifies that `insertPolicy` propagates the exact original exception and
that the policy, version, hidden tag and policy-tag relation are not committed.
`insertPolicy` already declares `throws IOException`; the enclosing
`SessionUtils` transaction rolls back before the `UncheckedIOException` is
unwrapped. All 35 `TestPolicyMetaService` tests passed on H2, along with
Spotless and `git diff --check`.
--
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]