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


##########
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:
   This check now rejects dots for domains too, but a metadata object path for 
a domain is validated separately in the REST interceptor, so errors can differ 
(400 versus a name-identifier error) by entry point. Consider one shared 
validation path so all entry points return the same error.



##########
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:
   Is it intended that names may start with an underscore and are 
case-sensitively unique, while only system is reserved case-insensitively? 
Please confirm this matches the catalog rules and add a test for a leading 
underscore and for a name of exactly 64 and 65 characters.



##########
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:
   Deleting is the only way to clean up a legacy badly named domain, and it is 
now blocked by name validation. Skip validation in deleteDomain, or confirm no 
such domains can exist in any released version.



##########
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:
   The title says validation is on creation and rename, but get, delete, 
publish, alter, list and attach also reject names that do not match the new 
pattern. Any existing domain created earlier with a slash, space, dot or 
Unicode name can no longer be read, renamed or deleted. Validate only new names 
(create, subdomain, rename), or add a migration or escape hatch for existing 
data.



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