This is an automated email from the ASF dual-hosted git repository. github-actions[bot] pushed a commit to branch cherry-pick-90eca331-to-branch-1.3 in repository https://gitbox.apache.org/repos/asf/gravitino.git
commit 942d5a48614ee9c4b039324fa40ba68db8ea065e Author: MaSai <[email protected]> AuthorDate: Wed Nov 4 16:56:35 2026 +0800 [#14428] fix(policy): Accept known policyType case-insensitively (#14520) ### What changes were proposed in this pull request? - Resolve known `policyType` values (built-ins and `custom`) case-insensitively via a scoped `PolicyContentTypeIdResolver`, then bind and validate the matching content. Explicitly register `custom` and use `defaultImpl = Void.class` so unknown types do not fall back to custom content. - Keep Jackson's default request-field binding (no whole-request deserializers). Missing `policyType` binds content as null and `validate()` returns a type-named 400; unknown types likewise get a type-named 400 such as `Unknown built-in policy type: system_iceberg_bogus`. - Persist and return the canonical lowercase `BuiltInType.policyType()` on create and `updateContent`. - Align OpenAPI request schemas with case-insensitive acceptance (patterns and request examples for lowercase, uppercase, and mixed-case), while response schemas keep the canonical lowercase enum. ### Why are the changes needed? Fixes #14428. Clients sending a typo such as `system_iceberg_bogus`, or a known type in another case such as `SYSTEM_ICEBERG_COMPACTION` / `System_Iceberg_Compaction`, previously got a misleading custom-content error (`supportedObjectTypes cannot be empty` or `Malformed json request`) instead of a clear type-named 400. `fromPolicyType()` already matched ignore-case; Jackson subtype selection and the OpenAPI request schemas did not. ### Does this PR introduce _any_ user-facing change? Yes. - Known `policyType` values are accepted case-insensitively on create/update; the server stores and returns the canonical lowercase form. - Unknown `policyType` values return HTTP 400 naming the type. - Missing `policyType` returns HTTP 400 naming the field (no longer HTTP 500). ### How was this patch tested? ``` ./gradlew :common:test --tests org.apache.gravitino.dto.requests.TestPolicyCreateRequest \ --tests org.apache.gravitino.dto.requests.TestPolicyUpdatesRequest \ :server:test --tests org.apache.gravitino.server.web.rest.TestPolicyOperations \ :docs:build \ -PskipITs ``` Coverage includes lowercase / uppercase / mixed-case known types on create and update (DTO + REST), unknown types with empty and non-empty content, missing `policyType` with empty / custom / null content, and OpenAPI request examples validated by `:docs:build` (`no-invalid-media-type-examples`). --------- Co-authored-by: Cursor <[email protected]> # Conflicts: # common/src/main/java/org/apache/gravitino/dto/requests/PolicyCreateRequest.java # common/src/main/java/org/apache/gravitino/dto/requests/PolicyUpdateRequest.java # docs/open-api/policies.yaml # server/src/main/java/org/apache/gravitino/server/web/rest/PolicyOperations.java # server/src/test/java/org/apache/gravitino/server/web/rest/TestPolicyOperations.java --- .../java/org/apache/gravitino/policy/Policy.java | 3 + .../dto/requests/PolicyContentTypeIdResolver.java | 103 ++++ .../dto/requests/PolicyCreateRequest.java | 17 +- .../dto/requests/PolicyUpdateRequest.java | 24 +- .../dto/requests/TestPolicyCreateRequest.java | 121 +++- .../dto/requests/TestPolicyUpdatesRequest.java | 75 ++- docs/manage-policies-in-gravitino.md | 5 + docs/open-api/policies.yaml | 657 ++++++++++++++++++++- .../server/web/rest/PolicyOperations.java | 10 +- .../server/web/rest/TestPolicyOperations.java | 149 +++++ 10 files changed, 1152 insertions(+), 12 deletions(-) diff --git a/api/src/main/java/org/apache/gravitino/policy/Policy.java b/api/src/main/java/org/apache/gravitino/policy/Policy.java index 7b27b01fcd..c38ef4c9ac 100644 --- a/api/src/main/java/org/apache/gravitino/policy/Policy.java +++ b/api/src/main/java/org/apache/gravitino/policy/Policy.java @@ -60,6 +60,9 @@ public interface Policy extends Auditable { /** * Get the built-in policy type from the policy type string. * + * <p>Matching is case-insensitive. Callers should persist and return {@link #policyType()} so + * responses use the canonical lowercase wire value. + * * @param policyType the policy type string * @return the matched built-in policy type * @throws IllegalArgumentException if the policy type is unknown or blank diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/PolicyContentTypeIdResolver.java b/common/src/main/java/org/apache/gravitino/dto/requests/PolicyContentTypeIdResolver.java new file mode 100644 index 0000000000..72ea1cbe9a --- /dev/null +++ b/common/src/main/java/org/apache/gravitino/dto/requests/PolicyContentTypeIdResolver.java @@ -0,0 +1,103 @@ +/* + * 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.dto.requests; + +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.databind.DatabindContext; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase; +import com.google.common.collect.ImmutableMap; +import java.util.Map; +import org.apache.gravitino.dto.policy.PolicyContentDTO; +import org.apache.gravitino.policy.Policy; + +/** + * Selects the policy content DTO from the sibling {@code policyType}, matching known types + * case-insensitively. + * + * <p>An unknown type resolves to no content class, so the content is bound as {@code null} and + * request validation reports the type by name. + */ +public class PolicyContentTypeIdResolver extends TypeIdResolverBase { + + private static final Map<Policy.BuiltInType, Class<? extends PolicyContentDTO>> CONTENT_CLASSES = + ImmutableMap.<Policy.BuiltInType, Class<? extends PolicyContentDTO>>builder() + .put( + Policy.BuiltInType.ICEBERG_COMPACTION, + PolicyContentDTO.IcebergCompactionContentDTO.class) + .put( + Policy.BuiltInType.ICEBERG_ORPHAN_FILE_REMOVAL, + PolicyContentDTO.IcebergOrphanFileRemovalContentDTO.class) + .put( + Policy.BuiltInType.ICEBERG_REWRITE_MANIFESTS, + PolicyContentDTO.IcebergRewriteManifestsContentDTO.class) + .put( + Policy.BuiltInType.ICEBERG_SNAPSHOT_EXPIRATION, + PolicyContentDTO.IcebergSnapshotExpirationContentDTO.class) + .put(Policy.BuiltInType.ROW_FILTER, PolicyContentDTO.RowFilterContentDTO.class) + .put(Policy.BuiltInType.COLUMN_MASK, PolicyContentDTO.ColumnMaskContentDTO.class) + .put(Policy.BuiltInType.ACCESS_CONTROL, PolicyContentDTO.AccessControlContentDTO.class) + .put(Policy.BuiltInType.CUSTOM, PolicyContentDTO.CustomContentDTO.class) + .build(); + + private JavaType baseType; + + @Override + public void init(JavaType baseType) { + this.baseType = baseType; + } + + @Override + public String idFromValue(Object value) { + return idFromValueAndType(value, value.getClass()); + } + + @Override + public String idFromValueAndType(Object value, Class<?> suggestedType) { + if (suggestedType == Void.class) { + // Jackson asks for the id of the Void defaultImpl when policyType is absent. A null id fails + // deserialization, so return one that resolves to no content class: the content then binds + // as null and request validation reports the missing policyType. + return ""; + } + for (Map.Entry<Policy.BuiltInType, Class<? extends PolicyContentDTO>> entry : + CONTENT_CLASSES.entrySet()) { + if (entry.getValue().isAssignableFrom(suggestedType)) { + return entry.getKey().policyType(); + } + } + throw new IllegalArgumentException("Unknown policy content class: " + suggestedType); + } + + @Override + public JavaType typeFromId(DatabindContext context, String id) { + for (Map.Entry<Policy.BuiltInType, Class<? extends PolicyContentDTO>> entry : + CONTENT_CLASSES.entrySet()) { + if (entry.getKey().policyType().equalsIgnoreCase(id)) { + return context.constructSpecializedType(baseType, entry.getValue()); + } + } + return null; + } + + @Override + public JsonTypeInfo.Id getMechanism() { + return JsonTypeInfo.Id.NAME; + } +} diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/PolicyCreateRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/PolicyCreateRequest.java index 158bbb3e43..53b09ff95a 100644 --- a/common/src/main/java/org/apache/gravitino/dto/requests/PolicyCreateRequest.java +++ b/common/src/main/java/org/apache/gravitino/dto/requests/PolicyCreateRequest.java @@ -19,8 +19,8 @@ package org.apache.gravitino.dto.requests; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver; import com.google.common.base.Preconditions; import javax.annotation.Nullable; import lombok.EqualsAndHashCode; @@ -28,9 +28,15 @@ import lombok.Getter; import lombok.ToString; import org.apache.commons.lang3.StringUtils; import org.apache.gravitino.dto.policy.PolicyContentDTO; +import org.apache.gravitino.policy.Policy; import org.apache.gravitino.rest.RESTRequest; -/** Represents a request to create a policy. */ +/** + * Represents a request to create a policy. + * + * <p>{@code policyType} accepts known type ids case-insensitively; storage and responses use the + * canonical lowercase value from {@link Policy.BuiltInType#policyType()}. + */ @Getter @EqualsAndHashCode @ToString @@ -54,6 +60,7 @@ public class PolicyCreateRequest implements RESTRequest { use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "policyType", +<<<<<<< HEAD defaultImpl = PolicyContentDTO.CustomContentDTO.class) @JsonSubTypes({ // add mappings for built-in types here @@ -63,6 +70,10 @@ public class PolicyCreateRequest implements RESTRequest { value = PolicyContentDTO.IcebergCompactionContentDTO.class, name = "system_iceberg_compaction") }) +======= + defaultImpl = Void.class) + @JsonTypeIdResolver(PolicyContentTypeIdResolver.class) +>>>>>>> 90eca3316 ([#14428] fix(policy): Accept known policyType case-insensitively (#14520)) private final PolicyContentDTO policyContent; /** @@ -99,6 +110,8 @@ public class PolicyCreateRequest implements RESTRequest { StringUtils.isNotBlank(name), "\"name\" is required and cannot be empty"); Preconditions.checkArgument( StringUtils.isNotBlank(policyType), "\"policyType\" is required and cannot be empty"); + // An unknown policyType binds content as null, so resolve the type before the content checks. + Policy.BuiltInType.fromPolicyType(policyType); Preconditions.checkArgument( policyContent != null, "\"content\" is required and cannot be null"); policyContent.validate(); diff --git a/common/src/main/java/org/apache/gravitino/dto/requests/PolicyUpdateRequest.java b/common/src/main/java/org/apache/gravitino/dto/requests/PolicyUpdateRequest.java index 2cc53c68d0..b81fef035b 100644 --- a/common/src/main/java/org/apache/gravitino/dto/requests/PolicyUpdateRequest.java +++ b/common/src/main/java/org/apache/gravitino/dto/requests/PolicyUpdateRequest.java @@ -24,12 +24,14 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver; import com.google.common.base.Preconditions; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.ToString; import org.apache.commons.lang3.StringUtils; import org.apache.gravitino.dto.policy.PolicyContentDTO; +import org.apache.gravitino.policy.Policy; import org.apache.gravitino.policy.PolicyChange; import org.apache.gravitino.rest.RESTRequest; @@ -121,7 +123,19 @@ public interface PolicyUpdateRequest extends RESTRequest { public void validate() throws IllegalArgumentException {} } +<<<<<<< HEAD /** The policy update request for updating a policy content. */ +======= + /** + * The policy update request for updating a policy content. + * + * <p>{@code newContent} fully replaces the previous content. Omitted typed fields use that + * content type's defaults; they are not merged with the previously stored values. + * + * <p>{@code policyType} accepts known type ids case-insensitively; the change stores the + * canonical lowercase value from {@link Policy.BuiltInType#policyType()}. + */ +>>>>>>> 90eca3316 ([#14428] fix(policy): Accept known policyType case-insensitively (#14520)) @EqualsAndHashCode @Getter @ToString @@ -135,6 +149,7 @@ public interface PolicyUpdateRequest extends RESTRequest { use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "policyType", +<<<<<<< HEAD defaultImpl = PolicyContentDTO.CustomContentDTO.class) @JsonSubTypes({ // add mappings for built-in types here @@ -144,6 +159,10 @@ public interface PolicyUpdateRequest extends RESTRequest { value = PolicyContentDTO.IcebergCompactionContentDTO.class, name = "system_iceberg_compaction") }) +======= + defaultImpl = Void.class) + @JsonTypeIdResolver(PolicyContentTypeIdResolver.class) +>>>>>>> 90eca3316 ([#14428] fix(policy): Accept known policyType case-insensitively (#14520)) private final PolicyContentDTO newContent; /** @@ -165,12 +184,15 @@ public interface PolicyUpdateRequest extends RESTRequest { @Override public PolicyChange policyChange() { - return PolicyChange.updateContent(policyType, fromDTO(newContent)); + return PolicyChange.updateContent( + Policy.BuiltInType.fromPolicyType(policyType).policyType(), fromDTO(newContent)); } @Override public void validate() throws IllegalArgumentException { Preconditions.checkArgument(policyType != null, "\"policyType\" must not be null"); + // An unknown policyType binds newContent as null, so resolve the type first. + Policy.BuiltInType.fromPolicyType(policyType); Preconditions.checkArgument(newContent != null, "\"newContent\" must not be null"); newContent.validate(); } diff --git a/common/src/test/java/org/apache/gravitino/dto/requests/TestPolicyCreateRequest.java b/common/src/test/java/org/apache/gravitino/dto/requests/TestPolicyCreateRequest.java index 81b33ca3de..6ececfaae1 100644 --- a/common/src/test/java/org/apache/gravitino/dto/requests/TestPolicyCreateRequest.java +++ b/common/src/test/java/org/apache/gravitino/dto/requests/TestPolicyCreateRequest.java @@ -24,8 +24,11 @@ import com.google.common.collect.ImmutableSet; import org.apache.gravitino.MetadataObject; import org.apache.gravitino.dto.policy.PolicyContentDTO; import org.apache.gravitino.json.JsonUtils; +import org.apache.gravitino.policy.Policy; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; public class TestPolicyCreateRequest { @@ -39,14 +42,14 @@ public class TestPolicyCreateRequest { .withProperties(null) .build(); PolicyCreateRequest request = - new PolicyCreateRequest("policy_test", "test_type", "policy comment", true, content); + new PolicyCreateRequest("policy_test", "custom", "policy comment", true, content); String serJson = JsonUtils.objectMapper().writeValueAsString(request); PolicyCreateRequest deserRequest = JsonUtils.objectMapper().readValue(serJson, PolicyCreateRequest.class); Assertions.assertEquals(request, deserRequest); Assertions.assertEquals("policy_test", deserRequest.getName()); Assertions.assertEquals("policy comment", deserRequest.getComment()); - Assertions.assertEquals("test_type", deserRequest.getPolicyType()); + Assertions.assertEquals("custom", deserRequest.getPolicyType()); Assertions.assertTrue(deserRequest.getEnabled()); Assertions.assertEquals(content, deserRequest.getPolicyContent()); @@ -59,17 +62,127 @@ public class TestPolicyCreateRequest { .build(); request = new PolicyCreateRequest( - "policy_test_null_types", "test_type", "policy comment", true, contentWithNullTypes); + "policy_test_null_types", "custom", "policy comment", true, contentWithNullTypes); serJson = JsonUtils.objectMapper().writeValueAsString(request); deserRequest = JsonUtils.objectMapper().readValue(serJson, PolicyCreateRequest.class); Assertions.assertEquals(request, deserRequest); Assertions.assertEquals("policy_test_null_types", deserRequest.getName()); Assertions.assertEquals("policy comment", deserRequest.getComment()); - Assertions.assertEquals("test_type", deserRequest.getPolicyType()); + Assertions.assertEquals("custom", deserRequest.getPolicyType()); Assertions.assertTrue(deserRequest.getEnabled()); Assertions.assertEquals(contentWithNullTypes, deserRequest.getPolicyContent()); Exception e = Assertions.assertThrows(IllegalArgumentException.class, request::validate); Assertions.assertEquals("supportedObjectTypes cannot be empty", e.getMessage()); } + + @ParameterizedTest + @ValueSource( + strings = { + "system_iceberg_compaction", + "SYSTEM_ICEBERG_COMPACTION", + "System_Iceberg_Compaction" + }) + public void testDeserializeAcceptsKnownPolicyTypeCaseInsensitively(String policyType) + throws JsonProcessingException { + String json = + "{\"name\":\"p_upper\",\"policyType\":\"" + + policyType + + "\",\"enabled\":true,\"content\":{\"maxPartitionNum\":5}}"; + PolicyCreateRequest request = + JsonUtils.objectMapper().readValue(json, PolicyCreateRequest.class); + Assertions.assertEquals(policyType, request.getPolicyType()); + Assertions.assertInstanceOf( + PolicyContentDTO.IcebergCompactionContentDTO.class, request.getPolicyContent()); + Assertions.assertEquals( + Policy.BuiltInType.ICEBERG_COMPACTION, + Policy.BuiltInType.fromPolicyType(request.getPolicyType())); + request.validate(); + } + + @ParameterizedTest + @ValueSource(strings = {"custom", "CUSTOM", "Custom"}) + public void testDeserializeAcceptsCustomPolicyTypeCaseInsensitively(String policyType) + throws JsonProcessingException { + String json = + "{\"name\":\"p\",\"policyType\":\"" + + policyType + + "\",\"enabled\":true," + + "\"content\":{\"customRules\":{\"a\":1},\"supportedObjectTypes\":[\"TABLE\"]}}"; + PolicyCreateRequest request = + JsonUtils.objectMapper().readValue(json, PolicyCreateRequest.class); + Assertions.assertEquals( + Policy.BuiltInType.CUSTOM, Policy.BuiltInType.fromPolicyType(request.getPolicyType())); + request.validate(); + } + + @Test + public void testDeserializeRejectsUnknownPolicyType() throws JsonProcessingException { + assertPolicyTypeNamedOnDeserialize( + "{\"name\":\"p_bogus\",\"policyType\":\"system_iceberg_bogus\",\"enabled\":true,\"content\":{}}", + "Unknown built-in policy type: system_iceberg_bogus"); + assertPolicyTypeNamedOnDeserialize( + "{\"name\":\"p_bogus\",\"policyType\":\"system_iceberg_bogus\",\"enabled\":true," + + "\"content\":{\"maxPartitionNum\":5}}", + "Unknown built-in policy type: system_iceberg_bogus"); + assertPolicyTypeNamedOnDeserialize( + "{\"name\":\"p_other\",\"policyType\":\"not_a_policy\",\"enabled\":true," + + "\"content\":{\"customRules\":{},\"supportedObjectTypes\":[\"TABLE\"]}}", + "Unknown policy type: not_a_policy, it should start with 'system_' or be 'custom'"); + } + + @ParameterizedTest + @ValueSource( + strings = {"{}", "{\"customRules\":{},\"supportedObjectTypes\":[\"TABLE\"]}", "null"}) + public void testDeserializeReportsMissingPolicyType(String content) + throws JsonProcessingException { + assertPolicyTypeNamedOnDeserialize( + "{\"name\":\"p\",\"content\":" + content + "}", + "\"policyType\" is required and cannot be empty"); + } + + @Test + public void testDeserializeRejectsMalformedEnabled() { + String content = "\"content\":{\"customRules\":{},\"supportedObjectTypes\":[\"table\"]}}"; + for (String enabled : new String[] {"\"typo\"", "{}", "[]"}) { + String json = + "{\"name\":\"p\",\"policyType\":\"custom\",\"enabled\":" + enabled + "," + content; + Assertions.assertThrows( + JsonProcessingException.class, + () -> JsonUtils.objectMapper().readValue(json, PolicyCreateRequest.class), + enabled); + } + } + + @Test + public void testDeserializeRejectsUnknownProperty() { + String json = + "{\"name\":\"p\",\"policyType\":\"custom\",\"enabld\":false," + + "\"content\":{\"customRules\":{},\"supportedObjectTypes\":[\"table\"]}}"; + JsonProcessingException thrown = + Assertions.assertThrows( + JsonProcessingException.class, + () -> JsonUtils.objectMapper().readValue(json, PolicyCreateRequest.class)); + Assertions.assertTrue(thrown.getMessage().contains("enabld"), thrown.getMessage()); + } + + @Test + public void testDeserializeEnabledFalse() throws JsonProcessingException { + String json = + "{\"name\":\"p\",\"policyType\":\"custom\",\"enabled\":false," + + "\"content\":{\"customRules\":{},\"supportedObjectTypes\":[\"table\"]}}"; + PolicyCreateRequest request = + JsonUtils.objectMapper().readValue(json, PolicyCreateRequest.class); + Assertions.assertFalse(request.getEnabled()); + } + + private static void assertPolicyTypeNamedOnDeserialize(String json, String expectedMessage) + throws JsonProcessingException { + PolicyCreateRequest request = + JsonUtils.objectMapper().readValue(json, PolicyCreateRequest.class); + Assertions.assertNull(request.getPolicyContent()); + IllegalArgumentException thrown = + Assertions.assertThrows(IllegalArgumentException.class, request::validate); + Assertions.assertEquals(expectedMessage, thrown.getMessage()); + } } diff --git a/common/src/test/java/org/apache/gravitino/dto/requests/TestPolicyUpdatesRequest.java b/common/src/test/java/org/apache/gravitino/dto/requests/TestPolicyUpdatesRequest.java index 98da580afe..52c88b569b 100644 --- a/common/src/test/java/org/apache/gravitino/dto/requests/TestPolicyUpdatesRequest.java +++ b/common/src/test/java/org/apache/gravitino/dto/requests/TestPolicyUpdatesRequest.java @@ -21,11 +21,16 @@ package org.apache.gravitino.dto.requests; import com.fasterxml.jackson.core.JsonProcessingException; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import java.util.List; +import org.apache.gravitino.MetadataObject; import org.apache.gravitino.dto.policy.PolicyContentDTO; import org.apache.gravitino.json.JsonUtils; +import org.apache.gravitino.policy.Policy; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; public class TestPolicyUpdatesRequest { @@ -57,19 +62,82 @@ public class TestPolicyUpdatesRequest { PolicyContentDTO contentDTO = PolicyContentDTO.CustomContentDTO.builder() .withCustomRules(ImmutableMap.of("rule1", "value1")) + .withSupportedObjectTypes(ImmutableSet.of(MetadataObject.Type.TABLE)) .withProperties(ImmutableMap.of("key", "value")) .build(); PolicyUpdateRequest.UpdatePolicyContentRequest request = - new PolicyUpdateRequest.UpdatePolicyContentRequest("test_type", contentDTO); + new PolicyUpdateRequest.UpdatePolicyContentRequest("custom", contentDTO); String serJson = JsonUtils.objectMapper().writeValueAsString(request); PolicyUpdateRequest.UpdatePolicyContentRequest deserRequest = JsonUtils.objectMapper() .readValue(serJson, PolicyUpdateRequest.UpdatePolicyContentRequest.class); Assertions.assertEquals(request, deserRequest); - Assertions.assertEquals("test_type", deserRequest.getPolicyType()); + Assertions.assertEquals("custom", deserRequest.getPolicyType()); Assertions.assertEquals(contentDTO, deserRequest.getNewContent()); } + @ParameterizedTest + @ValueSource( + strings = { + "system_iceberg_compaction", + "SYSTEM_ICEBERG_COMPACTION", + "System_Iceberg_Compaction" + }) + public void testUpdatePolicyContentAcceptsKnownPolicyTypeCaseInsensitively(String policyType) + throws JsonProcessingException { + String json = + "{\"@type\":\"updateContent\",\"policyType\":\"" + + policyType + + "\",\"newContent\":{\"maxPartitionNum\":5}}"; + PolicyUpdateRequest.UpdatePolicyContentRequest request = + JsonUtils.objectMapper() + .readValue(json, PolicyUpdateRequest.UpdatePolicyContentRequest.class); + Assertions.assertEquals(policyType, request.getPolicyType()); + Assertions.assertInstanceOf( + PolicyContentDTO.IcebergCompactionContentDTO.class, request.getNewContent()); + request.validate(); + Assertions.assertTrue( + request + .policyChange() + .toString() + .contains(Policy.BuiltInType.ICEBERG_COMPACTION.policyType()), + request.policyChange().toString()); + } + + @Test + public void testUpdatePolicyContentDeserializeRejectsUnknownPolicyType() + throws JsonProcessingException { + assertPolicyTypeNamedOnDeserialize( + "{\"@type\":\"updateContent\",\"policyType\":\"system_iceberg_bogus\"," + + "\"newContent\":{}}", + "Unknown built-in policy type: system_iceberg_bogus"); + assertPolicyTypeNamedOnDeserialize( + "{\"@type\":\"updateContent\",\"policyType\":\"system_iceberg_bogus\"," + + "\"newContent\":{\"maxPartitionNum\":5}}", + "Unknown built-in policy type: system_iceberg_bogus"); + } + + @ParameterizedTest + @ValueSource( + strings = {"{}", "{\"customRules\":{},\"supportedObjectTypes\":[\"TABLE\"]}", "null"}) + public void testUpdatePolicyContentDeserializeReportsMissingPolicyType(String content) + throws JsonProcessingException { + assertPolicyTypeNamedOnDeserialize( + "{\"@type\":\"updateContent\",\"newContent\":" + content + "}", + "\"policyType\" must not be null"); + } + + private static void assertPolicyTypeNamedOnDeserialize(String json, String expectedMessage) + throws JsonProcessingException { + PolicyUpdateRequest.UpdatePolicyContentRequest request = + JsonUtils.objectMapper() + .readValue(json, PolicyUpdateRequest.UpdatePolicyContentRequest.class); + Assertions.assertNull(request.getNewContent()); + IllegalArgumentException thrown = + Assertions.assertThrows(IllegalArgumentException.class, request::validate); + Assertions.assertEquals(expectedMessage, thrown.getMessage()); + } + @Test public void testPolicyUpdatesRequestSerDe() throws JsonProcessingException { PolicyUpdateRequest request = new PolicyUpdateRequest.RenamePolicyRequest("policy_test_new"); @@ -77,9 +145,10 @@ public class TestPolicyUpdatesRequest { new PolicyUpdateRequest.UpdatePolicyCommentRequest("policy comment new"); PolicyUpdateRequest request2 = new PolicyUpdateRequest.UpdatePolicyContentRequest( - "test_type", + "custom", PolicyContentDTO.CustomContentDTO.builder() .withCustomRules(ImmutableMap.of("rule1", "value1")) + .withSupportedObjectTypes(ImmutableSet.of(MetadataObject.Type.TABLE)) .withProperties(ImmutableMap.of("key", "value")) .build()); diff --git a/docs/manage-policies-in-gravitino.md b/docs/manage-policies-in-gravitino.md index f1edbba297..3c1ab3bfd9 100644 --- a/docs/manage-policies-in-gravitino.md +++ b/docs/manage-policies-in-gravitino.md @@ -23,6 +23,11 @@ The Python client does not cover policies, so the examples below are REST and Ja A policy needs a name and a type. Content carries the rules, the object types the policy supports, and optional properties. `supportedObjectTypes` cannot be changed after creation. +`policyType` accepts the known wire values case-insensitively (for example `CUSTOM` and +`System_Iceberg_Compaction`). Gravitino stores and returns the canonical lowercase value listed in +the OpenAPI schema. Unknown types such as `system_iceberg_bogus` return HTTP 400 that names the +type. + <Tabs groupId='language' queryString> <TabItem value="shell" label="REST"> diff --git a/docs/open-api/policies.yaml b/docs/open-api/policies.yaml index 7513b3bb72..09141d1ae7 100644 --- a/docs/open-api/policies.yaml +++ b/docs/open-api/policies.yaml @@ -70,6 +70,12 @@ paths: examples: PolicyCreate: $ref: "#/components/examples/PolicyCreate" + PolicyCreateLowercaseType: + $ref: "#/components/examples/PolicyCreateLowercaseType" + PolicyCreateUppercaseType: + $ref: "#/components/examples/PolicyCreateUppercaseType" + PolicyCreateMixedCaseType: + $ref: "#/components/examples/PolicyCreateMixedCaseType" responses: "200": @@ -139,6 +145,13 @@ paths: application/json: schema: $ref: "#/components/schemas/PolicyUpdatesRequest" + examples: + PolicyUpdateContentLowercaseType: + $ref: "#/components/examples/PolicyUpdateContentLowercaseType" + PolicyUpdateContentUppercaseType: + $ref: "#/components/examples/PolicyUpdateContentUppercaseType" + PolicyUpdateContentMixedCaseType: + $ref: "#/components/examples/PolicyUpdateContentMixedCaseType" responses: "200": description: Returns the updated policy object @@ -387,6 +400,408 @@ components: schemas: +<<<<<<< HEAD +======= + PolicyAssociationSelector: + type: object + required: + - type + properties: + type: + type: string + description: The selector type used to match tag assignments + enum: ["ALL_VALUES", "TAG_VALUE"] + value: + type: string + description: The assignment value required by a TAG_VALUE selector + nullable: true + maxLength: 256 + + TagForPolicyAssociation: + type: object + required: + - tag + - selector + properties: + tag: + $ref: "./tags.yaml#/components/schemas/Tag" + selector: + $ref: "#/components/schemas/PolicyAssociationSelector" + + TagForPolicyAssociationListResponse: + type: object + required: + - code + - associations + properties: + code: + type: integer + format: int32 + enum: [0] + associations: + type: array + items: + $ref: "#/components/schemas/TagForPolicyAssociation" + + IcebergCompactionContent: + type: object + description: Built-in Iceberg compaction thresholds, scoring weights, and rewrite options. + properties: + minDataFileMse: + type: integer + format: int64 + minimum: 0 + default: 405323966463344 + minDeleteFileNumber: + type: integer + format: int64 + minimum: 0 + default: 1 + dataFileMseWeight: + type: integer + format: int64 + minimum: 0 + default: 1 + deleteFileNumberWeight: + type: integer + format: int64 + minimum: 0 + default: 100 + maxPartitionNum: + type: integer + format: int64 + minimum: 1 + default: 50 + rewriteStrategy: + type: string + enum: [binpack, sort] + default: binpack + description: Iceberg rewrite_data_files top-level strategy. + sortOrder: + type: string + default: "" + description: Iceberg rewrite_data_files top-level sort_order. Required when rewriteStrategy is sort; must be empty for binpack. + rewriteOptions: + type: object + default: {} + description: Rewrite options with nonempty values and keys matching [A-Za-z0-9._-]+, without the job.options. prefix. + additionalProperties: + type: string + minLength: 1 + + IcebergCompactionPolicy: + allOf: + - $ref: '#/components/schemas/PolicyBase' + - type: object + required: [content] + properties: + policyType: + type: string + enum: [system_iceberg_compaction] + content: + $ref: '#/components/schemas/IcebergCompactionContent' + + IcebergCompactionPolicyCreateRequest: + allOf: + - $ref: '#/components/schemas/PolicyCreateRequestBase' + - type: object + required: [content] + properties: + policyType: + $ref: '#/components/schemas/IcebergCompactionRequestPolicyType' + content: + $ref: '#/components/schemas/IcebergCompactionContent' + + IcebergOrphanFileRemovalContent: + type: object + description: Table-level orphan cleanup options. Scheduling is configured separately. + properties: + olderThanDays: + type: integer + format: int64 + minimum: 1 + maximum: 36500 + default: 3 + description: Minimum age of orphan files in days, including for dry runs. + location: + type: string + nullable: true + description: Optional scan location within the target table's storage root. + dryRun: + type: boolean + default: false + description: List candidates without deleting them. + + IcebergOrphanFileRemovalPolicy: + allOf: + - $ref: '#/components/schemas/PolicyBase' + - type: object + required: [content] + properties: + policyType: + type: string + enum: [system_iceberg_orphan_file_removal] + content: + $ref: '#/components/schemas/IcebergOrphanFileRemovalContent' + + IcebergOrphanFileRemovalPolicyCreateRequest: + allOf: + - $ref: '#/components/schemas/PolicyCreateRequestBase' + - type: object + required: [content] + properties: + policyType: + $ref: '#/components/schemas/IcebergOrphanFileRemovalRequestPolicyType' + content: + $ref: '#/components/schemas/IcebergOrphanFileRemovalContent' + + IcebergRewriteManifestsContent: + type: object + description: Thresholds for one resolved Iceberg partition spec. Supports catalog, schema, and table policies. + additionalProperties: false + properties: + manifest_count_critical: + type: integer + format: int64 + minimum: 1 + default: 500 + description: Inclusive count trigger, regardless of size. Must be at least manifest_count_warning. + manifest_count_warning: + type: integer + format: int64 + minimum: 1 + default: 100 + description: Inclusive minimum count for the average-size trigger. + avg_manifest_size_threshold_bytes: + type: integer + format: int64 + minimum: 1 + default: 8388608 + description: Exclusive average manifest size threshold in bytes. + spec_id: + type: integer + format: int32 + minimum: 0 + nullable: true + description: Existing partition spec. Omission resolves Iceberg's current default spec from table metadata. If it cannot be resolved, the table is skipped. The evaluated ID is retained through job submission. + use_caching: + type: boolean + nullable: true + description: Optional caching setting. Omission preserves the Iceberg runtime default. + + IcebergRewriteManifestsPolicy: + allOf: + - $ref: '#/components/schemas/PolicyBase' + - type: object + required: [content] + properties: + policyType: + type: string + enum: [system_iceberg_rewrite_manifests] + content: + $ref: '#/components/schemas/IcebergRewriteManifestsContent' + + IcebergRewriteManifestsPolicyCreateRequest: + allOf: + - $ref: '#/components/schemas/PolicyCreateRequestBase' + - type: object + required: [content] + properties: + policyType: + $ref: '#/components/schemas/IcebergRewriteManifestsRequestPolicyType' + content: + $ref: '#/components/schemas/IcebergRewriteManifestsContent' + + IcebergSnapshotExpirationContent: + type: object + description: > + Snapshot expiration options for system_iceberg_snapshot_expiration. Scheduling is + configured separately. On updateContent, newContent fully replaces the previous content; + omitted fields use these defaults rather than merging with stored values. Integer fields + must be JSON integers (not strings or floats); streamResults must be a JSON boolean. + additionalProperties: false + properties: + olderThanDays: + type: integer + format: int64 + minimum: 1 + maximum: 36500 + default: 5 + description: Minimum age of snapshots eligible for expiration, in days (Iceberg default). + retainLast: + type: integer + format: int64 + minimum: 1 + default: 1 + description: Minimum number of recent snapshots to keep regardless of age. + streamResults: + type: boolean + default: false + description: Whether the expire job should stream intermediate delete results. + + IcebergSnapshotExpirationPolicy: + allOf: + - $ref: '#/components/schemas/PolicyBase' + - type: object + required: [content] + properties: + policyType: + type: string + enum: [system_iceberg_snapshot_expiration] + content: + $ref: '#/components/schemas/IcebergSnapshotExpirationContent' + + IcebergSnapshotExpirationPolicyCreateRequest: + allOf: + - $ref: '#/components/schemas/PolicyCreateRequestBase' + - type: object + required: [content] + properties: + policyType: + $ref: '#/components/schemas/IcebergSnapshotExpirationRequestPolicyType' + content: + $ref: '#/components/schemas/IcebergSnapshotExpirationContent' + + RowFilterContent: + type: object + description: Expression content for a built-in row-filter policy. + required: + - expression + properties: + expression: + type: string + minLength: 1 + pattern: '[\s\S]*\S[\s\S]*' + description: >- + A complete restricted-sql-v1 definition such as + `(region STRING) RETURNS BOOLEAN -> region = 'US'`. + Parameters must be declared and the body must return BOOLEAN. SQL CASE, predicates, + and allowlisted context and scalar functions are supported. The UTF-8 source must + not exceed 16 KiB; see the Policies documentation for grammar, types and limits. + + ColumnMaskContent: + type: object + description: Expression content for a built-in column-mask policy. + required: + - expression + properties: + expression: + type: string + minLength: 1 + pattern: '[\s\S]*\S[\s\S]*' + description: >- + A complete restricted-sql-v1 definition such as + `(value STRING) RETURNS STRING -> show_last_4(value)`. + Exactly one parameter is required; its type must equal the return type. Its local + name is independent of the selected column name. The body may use SQL CASE, + literals, NULL, and allowlisted functions. The UTF-8 source must not exceed 16 KiB; + see the Policies documentation for grammar, types and limits. + + AccessControlContent: + type: object + description: Rule content for a built-in access-control policy. + required: + - privileges + - applicableRoles + properties: + privileges: + type: array + minItems: 1 + description: >- + The privileges granted when the rule matches. Limited to privileges that apply to a + whole securable object; column-scoped privileges are not permitted. This enum mirrors + AccessControlContent.PERMITTED_PRIVILEGES; change both together. + items: + type: string + enum: + - SELECT_TABLE + - MODIFY_TABLE + - PROBE_TABLE_LIKE + - SELECT_VIEW + - READ_FILESET + - WRITE_FILESET + - CONSUME_TOPIC + - PRODUCE_TOPIC + - USE_MODEL + - EXECUTE_FUNCTION + - USE_SEMANTIC_MODEL + applicableRoles: + type: array + minItems: 1 + description: The names of the roles the rule applies to. + items: + type: string + + RowFilterPolicy: + allOf: + - $ref: '#/components/schemas/PolicyBase' + - type: object + required: [content] + properties: + policyType: + type: string + enum: [system_row_filter] + content: + $ref: '#/components/schemas/RowFilterContent' + + RowFilterPolicyCreateRequest: + allOf: + - $ref: '#/components/schemas/PolicyCreateRequestBase' + - type: object + required: [content] + properties: + policyType: + $ref: '#/components/schemas/RowFilterRequestPolicyType' + content: + $ref: '#/components/schemas/RowFilterContent' + + ColumnMaskPolicy: + allOf: + - $ref: '#/components/schemas/PolicyBase' + - type: object + required: [content] + properties: + policyType: + type: string + enum: [system_column_mask] + content: + $ref: '#/components/schemas/ColumnMaskContent' + + ColumnMaskPolicyCreateRequest: + allOf: + - $ref: '#/components/schemas/PolicyCreateRequestBase' + - type: object + required: [content] + properties: + policyType: + $ref: '#/components/schemas/ColumnMaskRequestPolicyType' + content: + $ref: '#/components/schemas/ColumnMaskContent' + + AccessControlPolicy: + allOf: + - $ref: '#/components/schemas/PolicyBase' + - type: object + required: [content] + properties: + policyType: + type: string + enum: [system_access_control] + content: + $ref: '#/components/schemas/AccessControlContent' + + AccessControlPolicyCreateRequest: + allOf: + - $ref: '#/components/schemas/PolicyCreateRequestBase' + - type: object + required: [content] + properties: + policyType: + $ref: '#/components/schemas/AccessControlRequestPolicyType' + content: + $ref: '#/components/schemas/AccessControlContent' + +>>>>>>> 90eca3316 ([#14428] fix(policy): Accept known policyType case-insensitively (#14520)) PolicyContentBase: type: object description: A base content structure for policies @@ -538,6 +953,46 @@ components: - "FUNCTION" - "COLUMN" + CustomRequestPolicyType: + type: string + description: Request value `custom`, matched case-insensitively. + pattern: '^[cC][uU][sS][tT][oO][mM]$' + + IcebergCompactionRequestPolicyType: + type: string + description: Request value `system_iceberg_compaction`, matched case-insensitively. + pattern: '^[sS][yY][sS][tT][eE][mM]_[iI][cC][eE][bB][eE][rR][gG]_[cC][oO][mM][pP][aA][cC][tT][iI][oO][nN]$' + + IcebergOrphanFileRemovalRequestPolicyType: + type: string + description: Request value `system_iceberg_orphan_file_removal`, matched case-insensitively. + pattern: '^[sS][yY][sS][tT][eE][mM]_[iI][cC][eE][bB][eE][rR][gG]_[oO][rR][pP][hH][aA][nN]_[fF][iI][lL][eE]_[rR][eE][mM][oO][vV][aA][lL]$' + + IcebergRewriteManifestsRequestPolicyType: + type: string + description: Request value `system_iceberg_rewrite_manifests`, matched case-insensitively. + pattern: '^[sS][yY][sS][tT][eE][mM]_[iI][cC][eE][bB][eE][rR][gG]_[rR][eE][wW][rR][iI][tT][eE]_[mM][aA][nN][iI][fF][eE][sS][tT][sS]$' + + IcebergSnapshotExpirationRequestPolicyType: + type: string + description: Request value `system_iceberg_snapshot_expiration`, matched case-insensitively. + pattern: '^[sS][yY][sS][tT][eE][mM]_[iI][cC][eE][bB][eE][rR][gG]_[sS][nN][aA][pP][sS][hH][oO][tT]_[eE][xX][pP][iI][rR][aA][tT][iI][oO][nN]$' + + RowFilterRequestPolicyType: + type: string + description: Request value `system_row_filter`, matched case-insensitively. + pattern: '^[sS][yY][sS][tT][eE][mM]_[rR][oO][wW]_[fF][iI][lL][tT][eE][rR]$' + + ColumnMaskRequestPolicyType: + type: string + description: Request value `system_column_mask`, matched case-insensitively. + pattern: '^[sS][yY][sS][tT][eE][mM]_[cC][oO][lL][uU][mM][nN]_[mM][aA][sS][kK]$' + + AccessControlRequestPolicyType: + type: string + description: Request value `system_access_control`, matched case-insensitively. + pattern: '^[sS][yY][sS][tT][eE][mM]_[aA][cC][cC][eE][sS][sS]_[cC][oO][nN][tT][rR][oO][lL]$' + PolicyCreateRequestBase: type: object description: A base policy create request without content @@ -554,7 +1009,11 @@ components: nullable: true policyType: type: string - description: The type of the policy + description: >- + The type of the policy, either `custom` or a built-in `system_*` type. Known + values are matched case-insensitively, so request schemas select the content by a + case-insensitive pattern; the server stores and returns the canonical lowercase + form. An unknown value is rejected with HTTP 400 naming the type. enabled: type: boolean description: Whether the policy is enabled @@ -568,6 +1027,11 @@ components: required: - content properties: +<<<<<<< HEAD +======= + policyType: + $ref: '#/components/schemas/CustomRequestPolicyType' +>>>>>>> 90eca3316 ([#14428] fix(policy): Accept known policyType case-insensitively (#14520)) content: $ref: '#/components/schemas/CustomPolicyContent' @@ -576,10 +1040,28 @@ components: type: object oneOf: - $ref: '#/components/schemas/CustomPolicyCreateRequest' +<<<<<<< HEAD discriminator: propertyName: policyType mapping: custom: '#/components/schemas/CustomPolicyCreateRequest' +======= + - $ref: '#/components/schemas/IcebergOrphanFileRemovalPolicyCreateRequest' + - $ref: '#/components/schemas/IcebergRewriteManifestsPolicyCreateRequest' + - $ref: '#/components/schemas/IcebergSnapshotExpirationPolicyCreateRequest' + - $ref: '#/components/schemas/IcebergCompactionPolicyCreateRequest' + - $ref: '#/components/schemas/RowFilterPolicyCreateRequest' + - $ref: '#/components/schemas/ColumnMaskPolicyCreateRequest' + - $ref: '#/components/schemas/AccessControlPolicyCreateRequest' + example: { + "name": "compaction_upper", + "policyType": "SYSTEM_ICEBERG_COMPACTION", + "enabled": true, + "content": { + "maxPartitionNum": 5 + } + } +>>>>>>> 90eca3316 ([#14428] fix(policy): Accept known policyType case-insensitively (#14520)) PolicyUpdatesRequest: @@ -660,6 +1142,7 @@ components: - updateContent policyType: type: string +<<<<<<< HEAD description: The type of the policy newContent: type: object @@ -670,6 +1153,105 @@ components: propertyName: policyType mapping: custom: '#/components/schemas/CustomPolicyContent' +======= + description: >- + The type of the policy, either `custom` or a built-in `system_*` type. Known + values are matched case-insensitively, so request schemas select the content by a + case-insensitive pattern; the server stores and returns the canonical lowercase + form. An unknown value is rejected with HTTP 400 naming the type. + + UpdateCustomPolicyContentRequest: + allOf: + - $ref: '#/components/schemas/UpdatePolicyContentRequestBase' + - type: object + properties: + policyType: + $ref: '#/components/schemas/CustomRequestPolicyType' + newContent: + $ref: '#/components/schemas/CustomPolicyContent' + + UpdateIcebergCompactionPolicyContentRequest: + allOf: + - $ref: '#/components/schemas/UpdatePolicyContentRequestBase' + - type: object + properties: + policyType: + $ref: '#/components/schemas/IcebergCompactionRequestPolicyType' + newContent: + $ref: '#/components/schemas/IcebergCompactionContent' + + UpdateIcebergOrphanFileRemovalPolicyContentRequest: + allOf: + - $ref: '#/components/schemas/UpdatePolicyContentRequestBase' + - type: object + properties: + policyType: + $ref: '#/components/schemas/IcebergOrphanFileRemovalRequestPolicyType' + newContent: + $ref: '#/components/schemas/IcebergOrphanFileRemovalContent' + + UpdateIcebergRewriteManifestsPolicyContentRequest: + allOf: + - $ref: '#/components/schemas/UpdatePolicyContentRequestBase' + - type: object + properties: + policyType: + $ref: '#/components/schemas/IcebergRewriteManifestsRequestPolicyType' + newContent: + $ref: '#/components/schemas/IcebergRewriteManifestsContent' + + UpdateIcebergSnapshotExpirationPolicyContentRequest: + allOf: + - $ref: '#/components/schemas/UpdatePolicyContentRequestBase' + - type: object + properties: + policyType: + $ref: '#/components/schemas/IcebergSnapshotExpirationRequestPolicyType' + newContent: + $ref: '#/components/schemas/IcebergSnapshotExpirationContent' + + UpdateRowFilterPolicyContentRequest: + allOf: + - $ref: '#/components/schemas/UpdatePolicyContentRequestBase' + - type: object + properties: + policyType: + $ref: '#/components/schemas/RowFilterRequestPolicyType' + newContent: + $ref: '#/components/schemas/RowFilterContent' + + UpdateColumnMaskPolicyContentRequest: + allOf: + - $ref: '#/components/schemas/UpdatePolicyContentRequestBase' + - type: object + properties: + policyType: + $ref: '#/components/schemas/ColumnMaskRequestPolicyType' + newContent: + $ref: '#/components/schemas/ColumnMaskContent' + + UpdateAccessControlPolicyContentRequest: + allOf: + - $ref: '#/components/schemas/UpdatePolicyContentRequestBase' + - type: object + properties: + policyType: + $ref: '#/components/schemas/AccessControlRequestPolicyType' + newContent: + $ref: '#/components/schemas/AccessControlContent' + + UpdatePolicyContentRequest: + description: Selects typed content using the sibling policyType field, matching REST deserialization. + oneOf: + - $ref: '#/components/schemas/UpdateCustomPolicyContentRequest' + - $ref: '#/components/schemas/UpdateIcebergCompactionPolicyContentRequest' + - $ref: '#/components/schemas/UpdateIcebergOrphanFileRemovalPolicyContentRequest' + - $ref: '#/components/schemas/UpdateIcebergRewriteManifestsPolicyContentRequest' + - $ref: '#/components/schemas/UpdateIcebergSnapshotExpirationPolicyContentRequest' + - $ref: '#/components/schemas/UpdateRowFilterPolicyContentRequest' + - $ref: '#/components/schemas/UpdateColumnMaskPolicyContentRequest' + - $ref: '#/components/schemas/UpdateAccessControlPolicyContentRequest' +>>>>>>> 90eca3316 ([#14428] fix(policy): Accept known policyType case-insensitively (#14520)) example: { "@type": "updateContent", "policyType": "custom", @@ -814,10 +1396,83 @@ components: } } +<<<<<<< HEAD PolicyAssociate: value: { "policiesToAdd": ["my_policy1", "my_policy2"], "policiesToRemove": ["my_policy3"] +======= + PolicyCreateLowercaseType: + summary: Create a compaction policy with the canonical lowercase policyType + value: { + "name": "compaction_policy", + "policyType": "system_iceberg_compaction", + "content": { + "maxPartitionNum": 5 + } + } + + PolicyCreateUppercaseType: + summary: Create a compaction policy with an uppercase policyType + value: { + "name": "compaction_policy", + "policyType": "SYSTEM_ICEBERG_COMPACTION", + "content": { + "maxPartitionNum": 5 + } + } + + PolicyCreateMixedCaseType: + summary: Create a compaction policy with a mixed-case policyType + value: { + "name": "compaction_policy", + "policyType": "System_Iceberg_Compaction", + "content": { + "maxPartitionNum": 5 + } + } + + PolicyUpdateContentLowercaseType: + summary: Update compaction content with the canonical lowercase policyType + value: { + "updates": [ + { + "@type": "updateContent", + "policyType": "system_iceberg_compaction", + "newContent": { + "maxPartitionNum": 5 + } + } + ] + } + + PolicyUpdateContentUppercaseType: + summary: Update compaction content with an uppercase policyType + value: { + "updates": [ + { + "@type": "updateContent", + "policyType": "SYSTEM_ICEBERG_COMPACTION", + "newContent": { + "maxPartitionNum": 5 + } + } + ] + } + + PolicyUpdateContentMixedCaseType: + summary: Update compaction content with a mixed-case policyType + value: { + "updates": [ + { + "@type": "updateContent", + "policyType": "System_Iceberg_Compaction", + "newContent": { + "maxPartitionNum": 5 + } + } + ] +>>>>>>> 90eca3316 ([#14428] fix(policy): Accept known policyType case-insensitively (#14520)) } PolicyResponse: diff --git a/server/src/main/java/org/apache/gravitino/server/web/rest/PolicyOperations.java b/server/src/main/java/org/apache/gravitino/server/web/rest/PolicyOperations.java index c1ad6c2766..b1f9a969f9 100644 --- a/server/src/main/java/org/apache/gravitino/server/web/rest/PolicyOperations.java +++ b/server/src/main/java/org/apache/gravitino/server/web/rest/PolicyOperations.java @@ -160,7 +160,6 @@ public class PolicyOperations { request.validate(); NameIdentifierUtil.checkMetadataObjectName( NameIdentifierUtil.ofPolicy(metalake, request.getName()), Entity.EntityType.POLICY); - validateCreatePolicyType(request.getPolicyType()); PolicyEntity policy = policyDispatcher.createPolicy( metalake, @@ -380,6 +379,7 @@ public class PolicyOperations { return builder.build(); } +<<<<<<< HEAD private static void validateCreatePolicyType(String policyType) { Policy.BuiltInType builtInType = Policy.BuiltInType.fromPolicyType(policyType); if (builtInType != Policy.BuiltInType.CUSTOM @@ -389,5 +389,13 @@ public class PolicyOperations { "Built-in policy type must use prefixed value '%s', but got: %s", builtInType.policyType(), policyType)); } +======= + static PolicyAssociationSelectorDTO toSelectorDTO(RelationalEntity<?> association) { + return PolicyAssociationSelectorDTO.fromSelector( + association + .relationValue() + .map(PolicyAssociationSelectorSerde::deserialize) + .orElseGet(AllValuesSelector::get)); +>>>>>>> 90eca3316 ([#14428] fix(policy): Accept known policyType case-insensitively (#14520)) } } diff --git a/server/src/test/java/org/apache/gravitino/server/web/rest/TestPolicyOperations.java b/server/src/test/java/org/apache/gravitino/server/web/rest/TestPolicyOperations.java index 91232fc7a9..640a36f775 100644 --- a/server/src/test/java/org/apache/gravitino/server/web/rest/TestPolicyOperations.java +++ b/server/src/test/java/org/apache/gravitino/server/web/rest/TestPolicyOperations.java @@ -77,6 +77,12 @@ import org.glassfish.jersey.test.TestProperties; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +<<<<<<< HEAD +======= +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; +>>>>>>> 90eca3316 ([#14428] fix(policy): Accept known policyType case-insensitively (#14520)) import org.mockito.Mockito; public class TestPolicyOperations extends BaseOperationsTest { @@ -446,6 +452,75 @@ public class TestPolicyOperations extends BaseOperationsTest { Assertions.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus()); } + @Test + public void testCreatePolicyUnknownTypeNamedInError() { + assertCreatePolicyTypeNamedInError( + "{\"name\":\"p_bogus\",\"comment\":\"\",\"policyType\":\"system_iceberg_bogus\"," + + "\"enabled\":true,\"content\":{}}", + "Unknown built-in policy type: system_iceberg_bogus"); + assertCreatePolicyTypeNamedInError( + "{\"name\":\"p_bogus\",\"comment\":\"\",\"policyType\":\"system_iceberg_bogus\"," + + "\"enabled\":true,\"content\":{\"maxPartitionNum\":5}}", + "Unknown built-in policy type: system_iceberg_bogus"); + } + + @ParameterizedTest + @ValueSource( + strings = {"{}", "{\"customRules\":{},\"supportedObjectTypes\":[\"TABLE\"]}", "null"}) + public void testCreatePolicyMissingTypeNamedInError(String content) { + assertCreatePolicyTypeNamedInError( + "{\"name\":\"p\",\"content\":" + content + "}", + "\\\"policyType\\\" is required and cannot be empty"); + } + + @ParameterizedTest + @ValueSource(strings = {"SYSTEM_ICEBERG_COMPACTION", "System_Iceberg_Compaction"}) + public void testCreatePolicyAcceptsMisCasedKnownType(String policyType) { + PolicyContent content = PolicyContents.icebergDataCompaction(); + PolicyEntity policy = + PolicyEntity.builder() + .withId(1L) + .withName("p_upper") + .withPolicyType(Policy.BuiltInType.ICEBERG_COMPACTION) + .withEnabled(true) + .withContent(content) + .withAuditInfo(testAuditInfo1) + .build(); + when(policyManager.createPolicy(any(), any(), any(), any(), anyBoolean(), any())) + .thenAnswer( + invocation -> { + Assertions.assertEquals( + Policy.BuiltInType.ICEBERG_COMPACTION, invocation.getArgument(2)); + return policy; + }); + + Response resp = + target(policyPath(metalake)) + .request(MediaType.APPLICATION_JSON_TYPE) + .accept("application/vnd.gravitino.v1+json") + .post( + Entity.entity( + "{\"name\":\"p_upper\",\"comment\":\"\",\"policyType\":\"" + + policyType + + "\",\"enabled\":true,\"content\":{\"maxPartitionNum\":5}}", + MediaType.APPLICATION_JSON_TYPE)); + Assertions.assertEquals( + Response.Status.OK.getStatusCode(), resp.getStatus(), resp.readEntity(String.class)); + } + + private void assertCreatePolicyTypeNamedInError(String body, String expectedMessage) { + Response resp = + target(policyPath(metalake)) + .request(MediaType.APPLICATION_JSON_TYPE) + .accept("application/vnd.gravitino.v1+json") + .post(Entity.entity(body, MediaType.APPLICATION_JSON_TYPE)); + Assertions.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus()); + String msg = resp.readEntity(String.class); + Assertions.assertTrue(msg.contains(expectedMessage), msg); + Assertions.assertFalse(msg.contains("supportedObjectTypes"), msg); + Assertions.assertFalse(msg.contains("Malformed json request"), msg); + } + @Test public void testCreatePolicyRejectsDottedName() { PolicyContent content = @@ -590,6 +665,80 @@ public class TestPolicyOperations extends BaseOperationsTest { } } + @ParameterizedTest + @ValueSource(strings = {"SYSTEM_ICEBERG_COMPACTION", "System_Iceberg_Compaction"}) + public void testAlterPolicyContentAcceptsMisCasedKnownType(String policyType) { + PolicyContent content = PolicyContents.icebergDataCompaction(); + PolicyEntity policy = + PolicyEntity.builder() + .withId(1L) + .withName("p_upper") + .withPolicyType(Policy.BuiltInType.ICEBERG_COMPACTION) + .withEnabled(true) + .withContent(content) + .withAuditInfo(testAuditInfo1) + .build(); + when(policyManager.alterPolicy(any(), any(), any())) + .thenAnswer( + invocation -> { + PolicyChange.UpdateContent change = invocation.getArgument(2); + Assertions.assertEquals( + Policy.BuiltInType.ICEBERG_COMPACTION.policyType(), change.getPolicyType()); + return policy; + }); + + Response resp = + target(policyPath(metalake)) + .path("p_upper") + .request(MediaType.APPLICATION_JSON_TYPE) + .accept("application/vnd.gravitino.v1+json") + .put( + Entity.entity( + "{\"updates\":[{\"@type\":\"updateContent\"," + + "\"policyType\":\"" + + policyType + + "\",\"newContent\":{\"maxPartitionNum\":5}}]}", + MediaType.APPLICATION_JSON_TYPE)); + Assertions.assertEquals( + Response.Status.OK.getStatusCode(), resp.getStatus(), resp.readEntity(String.class)); + } + + @ParameterizedTest + @ValueSource( + strings = {"{}", "{\"customRules\":{},\"supportedObjectTypes\":[\"TABLE\"]}", "null"}) + public void testAlterPolicyContentMissingTypeNamedInError(String content) { + Response resp = + target(policyPath(metalake)) + .path("p_missing") + .request(MediaType.APPLICATION_JSON_TYPE) + .accept("application/vnd.gravitino.v1+json") + .put( + Entity.entity( + "{\"updates\":[{\"@type\":\"updateContent\",\"newContent\":" + content + "}]}", + MediaType.APPLICATION_JSON_TYPE)); + Assertions.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus()); + String msg = resp.readEntity(String.class); + Assertions.assertTrue(msg.contains("\\\"policyType\\\" must not be null"), msg); + } + + @Test + public void testAlterPolicyContentUnknownTypeNamedInError() { + Response resp = + target(policyPath(metalake)) + .path("p_bogus") + .request(MediaType.APPLICATION_JSON_TYPE) + .accept("application/vnd.gravitino.v1+json") + .put( + Entity.entity( + "{\"updates\":[{\"@type\":\"updateContent\"," + + "\"policyType\":\"system_iceberg_bogus\"," + + "\"newContent\":{\"maxPartitionNum\":5}}]}", + MediaType.APPLICATION_JSON_TYPE)); + Assertions.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp.getStatus()); + String msg = resp.readEntity(String.class); + Assertions.assertTrue(msg.contains("Unknown built-in policy type: system_iceberg_bogus"), msg); + } + @Test public void testAlterPolicy() { ImmutableMap<String, Object> contentFields = ImmutableMap.of("target_file_size_bytes", 1000);
