mchades commented on code in PR #14881:
URL: https://github.com/apache/gravitino/pull/14881#discussion_r4547345419
##########
clients/client-java/src/main/java/org/apache/gravitino/client/GenericDomain.java:
##########
@@ -293,18 +291,10 @@ public int hashCode() {
}
static String domainPath(String metalake, String name) {
- validateName(name);
return "api/metalakes/"
+ RESTUtils.encodeString(metalake)
+ "/domains/"
- + RESTUtils.encodeString(name);
- }
-
- static void validateName(String name) {
- Preconditions.checkArgument(StringUtils.isNotBlank(name), "domain name
must not be blank");
- // A proxy can normalize a dot segment and route the request to a
different resource.
- Preconditions.checkArgument(
- !".".equals(name) && !"..".equals(name), "Unsafe domain path segment:
%s", name);
+ + RESTUtils.encodeDomainPathSegment(name);
Review Comment:
Related nit: the comment at line 237 in this file still says "commas are
valid in domain names" — they are not anymore under the new naming rules (same
in `clients/client-python/gravitino/client/generic_domain.py:219`, "preserving
commas within names"). The repeated-parameter mechanism is still needed, but
the justification is stale; suggest rewording to something like "keep each
scope as a separate parameter".
##########
core/src/test/java/org/apache/gravitino/domain/TestDomainNormalizeDispatcher.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+
+import java.util.function.Consumer;
+import org.apache.gravitino.SupportsDomainOperations.LockedDomainState;
+import org.apache.gravitino.meta.DomainEntity;
+import org.apache.gravitino.tag.TagValue;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.NullAndEmptySource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+class TestDomainNormalizeDispatcher {
+ private final DomainDispatcher delegate = mock(DomainDispatcher.class);
+ private final DomainDispatcher dispatcher = new
DomainNormalizeDispatcher(delegate);
+ private final TagValue tag = TagValue.noValue("department");
+
+ @ParameterizedTest
+ @NullAndEmptySource
+ @ValueSource(
+ strings = {
+ " ",
+ ".",
+ "..",
+ "-start",
+ "a.b",
+ "A/B",
+ "a b",
+ "财务",
+ "*",
+ "SyStEm",
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ })
+ void rejectsInvalidNewNames(String name) {
Review Comment:
This covers create/createSubdomain/alter(rename)/get/delete/attach, but not
the other name-taking methods that also validate: `publishDomain`,
`unpublishDomain`, `updateBackingTag`, `listSubdomainInfos`, `detachDomain`,
and the name-based `listMembers`. Cheap to add to the same parameterized test.
##########
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()) {
+ throw new IllegalArgumentException("The " + type + " name '" + name + "'
is illegal.");
+ }
+ }
+
+ /**
+ * Validates the domain names in a member filter.
+ *
+ * @param names The requested domain names.
+ * @throws IllegalArgumentException If any name is reserved or illegal.
+ */
+ public static void validateDomainNames(Iterable<String> names) {
+ names.forEach(name -> validateName("domain", name));
Review Comment:
Two small robustness nits in this util: `names` itself is not null-checked,
so a null iterable NPEs at `forEach` instead of failing with
`IllegalArgumentException` — all current callers pass non-null, but a
`Preconditions.checkNotNull` would make the contract explicit. Also,
`validateName` is null-safe on the reserved checks but then reports `The domain
name 'null' is illegal.` for a null name; a `checkNotNull` up front would give
a clearer error.
##########
server/src/main/java/org/apache/gravitino/server/web/filter/GravitinoInterceptionService.java:
##########
@@ -182,10 +183,21 @@ public Object invoke(MethodInvocation methodInvocation)
throws Throwable {
String expression = expressionAnnotation.expression();
Object[] args = methodInvocation.getArguments();
Optional<String> entityType =
extractMetadataObjectTypeFromParameters(parameters, args);
+ Map<String, Object> pathParams =
Utils.extractPathParamsFromParameters(parameters, args);
+ // Grant and owner expressions may resolve this path before the
resource method runs.
+ Object objectType =
+ pathParams.getOrDefault("p_metadataObjectType",
pathParams.get("p_type"));
+ if ("domain".equalsIgnoreCase(String.valueOf(objectType))
+ && pathParams.containsKey("p_fullName")) {
+ try {
+ NameValidationUtils.validateName("domain", (String)
pathParams.get("p_fullName"));
Review Comment:
Minor: `(String) pathParams.get("p_fullName")` assumes every `fullName` path
param is a String — true for all current endpoints, but a future resource
declaring it as another type would CCE into a 500 here. `String.valueOf(...)`
or an `instanceof` guard would be more defensive.
##########
docs/open-api/domains.yaml:
##########
@@ -678,8 +675,11 @@ components:
type: string
enum: [draft, published]
parent:
- type: string
- nullable: true
+ oneOf:
+ - $ref: "#/components/schemas/DomainName"
+ - type: string
+ nullable: true
+ enum: [null]
Review Comment:
`enum: [null]` correctly restricts this `oneOf` branch to null only —
otherwise valid names would match both branches — but `type: string` combined
with `enum: [null]` is unusual. Please confirm `./gradlew :docs:build` accepts
it (same pattern at line ~827).
##########
common/src/main/java/org/apache/gravitino/rest/RESTUtils.java:
##########
@@ -110,6 +112,32 @@ public static String encodeString(String toEncode) {
return formEncode(toEncode).replace(FORM_ENCODED_SPACE,
PERCENT_ENCODED_SPACE);
}
+ /**
+ * Encodes a domain name as a single path segment, guarding against path
normalization.
+ *
+ * @param name The domain name.
+ * @return The encoded path segment.
+ */
+ public static String encodeDomainPathSegment(String name) {
+ Preconditions.checkArgument(
+ StringUtils.isNotBlank(name) && !".".equals(name) &&
!"..".equals(name),
+ "Unsafe domain path segment: %s",
+ name);
+ return encodeString(name);
+ }
+
+ /**
+ * Encodes the full name used to address a metadata object in a REST path.
+ *
+ * @param object The metadata object.
+ * @return The encoded path segment.
+ */
+ public static String encodeMetadataObjectName(MetadataObject object) {
Review Comment:
`encodeMetadataObjectName` is currently only applied to the role/owner/grant
paths. The other `objects/{type}/{fullName}/...` client paths still use
`encodeString(object.fullName())`: `MetadataObjectTagOperations`,
`MetadataObjectPolicyOperations`, `MetadataObjectSecretOperations`,
`MetadataObjectCredentialOperations`, `MetadataObjectStatisticsOperations`,
`MetadataObjectPartitionStatisticsOperations`, `MetadataObjectDomainOperations`
— and the Python equivalents
(`metadata_object_tag/credential/secret/statistics/domain_operations.py`). For
`type=domain` with a crafted name like `..`, those paths still emit an
unguarded path segment client-side before the server interceptor rejects it.
DOMAIN is not a supported type for most of these endpoints so the risk is low —
should this helper be applied uniformly, or is the narrower scope intentional?
--
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]