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


##########
core/src/main/java/org/apache/gravitino/domain/DomainNormalizeDispatcher.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.domain;
+
+import java.util.Map;
+import java.util.function.Consumer;
+import javax.annotation.Nullable;
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.SupportsDomainOperations.LockedDomainState;
+import org.apache.gravitino.meta.DomainEntity;
+import org.apache.gravitino.tag.TagValue;
+import org.apache.gravitino.utils.NameValidationUtils;
+
+/** Validates domain names before dispatching domain operations. */
+public final class DomainNormalizeDispatcher implements DomainDispatcher {
+
+  private final DomainDispatcher dispatcher;
+
+  /**
+   * Creates the domain name validation dispatcher.
+   *
+   * @param dispatcher The internal dispatcher.
+   */
+  public DomainNormalizeDispatcher(DomainDispatcher dispatcher) {
+    this.dispatcher = dispatcher;
+  }
+
+  @Override
+  public DomainDispatcher withMutationCheck(Consumer<LockedDomainState> check) 
{
+    return new DomainNormalizeDispatcher(dispatcher.withMutationCheck(check));
+  }
+
+  @Override
+  public DomainEntity[] listDomainInfos(String metalake) {
+    return dispatcher.listDomainInfos(metalake);
+  }
+
+  @Override
+  public DomainEntity getDomain(String metalake, String name) {

Review Comment:
   Domain is a new feature on `branch-domain-dev`. The same naming rule applies 
when creating a domain and when addressing it later, including lookup and 
rename. I updated the PR title to “Align domain naming rules with catalogs” to 
reflect that scope.



##########
core/src/main/java/org/apache/gravitino/domain/DomainNormalizeDispatcher.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.domain;
+
+import java.util.Map;
+import java.util.function.Consumer;
+import javax.annotation.Nullable;
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.SupportsDomainOperations.LockedDomainState;
+import org.apache.gravitino.meta.DomainEntity;
+import org.apache.gravitino.tag.TagValue;
+import org.apache.gravitino.utils.NameValidationUtils;
+
+/** Validates domain names before dispatching domain operations. */
+public final class DomainNormalizeDispatcher implements DomainDispatcher {
+
+  private final DomainDispatcher dispatcher;
+
+  /**
+   * Creates the domain name validation dispatcher.
+   *
+   * @param dispatcher The internal dispatcher.
+   */
+  public DomainNormalizeDispatcher(DomainDispatcher dispatcher) {
+    this.dispatcher = dispatcher;
+  }
+
+  @Override
+  public DomainDispatcher withMutationCheck(Consumer<LockedDomainState> check) 
{
+    return new DomainNormalizeDispatcher(dispatcher.withMutationCheck(check));
+  }
+
+  @Override
+  public DomainEntity[] listDomainInfos(String metalake) {
+    return dispatcher.listDomainInfos(metalake);
+  }
+
+  @Override
+  public DomainEntity getDomain(String metalake, String name) {
+    NameValidationUtils.validateName("domain", name);
+    return dispatcher.getDomain(metalake, name);
+  }
+
+  @Override
+  public DomainEntity createDomain(
+      String metalake,
+      String name,
+      @Nullable String comment,
+      @Nullable Map<String, String> properties,
+      TagValue backingTag,
+      boolean createBackingTag) {
+    NameValidationUtils.validateName("domain", name);
+    return dispatcher.createDomain(
+        metalake, name, comment, properties, backingTag, createBackingTag);
+  }
+
+  @Override
+  public DomainEntity createSubdomain(
+      String metalake,
+      String parent,
+      String name,
+      @Nullable String comment,
+      @Nullable Map<String, String> properties,
+      TagValue backingTag,
+      boolean createBackingTag) {
+    NameValidationUtils.validateName("domain", name);
+    NameValidationUtils.validateName("domain", parent);
+    return dispatcher.createSubdomain(
+        metalake, parent, name, comment, properties, backingTag, 
createBackingTag);
+  }
+
+  @Override
+  public DomainEntity alterDomain(String metalake, String name, 
DomainChange... changes) {
+    NameValidationUtils.validateName("domain", name);
+    for (DomainChange change : changes) {
+      if (change instanceof DomainChange.RenameDomain) {
+        NameValidationUtils.validateName(
+            "domain", ((DomainChange.RenameDomain) change).getNewName());
+      }
+    }
+    return dispatcher.alterDomain(metalake, name, changes);
+  }
+
+  @Override
+  public boolean deleteDomain(String metalake, String name) {

Review Comment:
   Domain has not shipped in a release, so there are no released Domain records 
that require a legacy delete path. Keeping the check here gives delete and 
lookup the same behavior for an invalid name.



##########
core/src/main/java/org/apache/gravitino/utils/NameValidationUtils.java:
##########
@@ -0,0 +1,59 @@
+/*
+ * 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.utils;
+
+import java.util.regex.Pattern;
+import org.apache.gravitino.MetadataObjects;
+
+/** Naming rules shared by catalogs and domains. */
+public final class NameValidationUtils {
+  /** Reserved name of the system catalog. */
+  public static final String SYSTEM_CATALOG_RESERVED_NAME = "system";
+
+  private static final Pattern NAME_PATTERN = 
Pattern.compile("^\\w[\\w-]{0,63}$");
+
+  private NameValidationUtils() {}
+
+  /**
+   * Validates a catalog or domain name.
+   *
+   * @param type The entity type used in validation messages.
+   * @param name The proposed name.
+   * @throws IllegalArgumentException If the name is reserved or illegal.
+   */
+  public static void validateName(String type, String name) {
+    if (SYSTEM_CATALOG_RESERVED_NAME.equalsIgnoreCase(name)
+        || MetadataObjects.METADATA_OBJECT_RESERVED_NAME.equals(name)) {
+      throw new IllegalArgumentException("The " + type + " name '" + name + "' 
is reserved.");
+    }
+    if (name == null || !NAME_PATTERN.matcher(name).matches()) {

Review Comment:
   Yes. Catalogs and domains share `NameValidationUtils.validateName`. A 
leading underscore is valid, and `system` is reserved in every case 
combination. Ordinary names retain their case, and the database enforces 
uniqueness within a metalake (the MySQL Domain table uses `utf8mb4_bin`). 
`TestDomainNormalizeDispatcher.preservesValidNames` already covers `_` and a 
64-character name, and `rejectsInvalidNewNames` covers 65 characters and 
`SyStEm`.



##########
core/src/main/java/org/apache/gravitino/utils/NameIdentifierUtil.java:
##########
@@ -646,14 +646,13 @@ public static void checkDomain(NameIdentifier ident) {
    *
    * @param ident The metadata object identifier to check
    * @param entityType The metadata object entity type
-   * @throws IllegalNameIdentifierException If a non-domain name contains the 
qualified-name
-   *     separator
+   * @throws IllegalNameIdentifierException If the name contains the 
qualified-name separator
    */
   public static void checkMetadataObjectName(NameIdentifier ident, 
Entity.EntityType entityType) {
     Preconditions.checkArgument(
         ident != null && entityType != null, "The identifier and entity type 
must not be null");
 
-    if (entityType != Entity.EntityType.DOMAIN && ident.name().contains(".")) {
+    if (ident.name().contains(".")) {

Review Comment:
   I checked the HTTP error mapping. `IllegalNameIdentifierException` extends 
`IllegalArgumentException`, so the qualified-name check and the REST 
authorization guard both produce HTTP 400 for a dotted Domain name. The 
diagnostic text can differ. The interceptor needs to reject the raw target 
before evaluating permissions, while `checkMetadataObjectName` also serves 
callers that convert identifiers directly.



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