dimas-b commented on code in PR #1417:
URL: https://github.com/apache/polaris/pull/1417#discussion_r2080176437


##########
integration-tests/src/main/java/org/apache/polaris/service/it/test/PolarisManagementServiceIntegrationTest.java:
##########
@@ -2102,6 +2103,129 @@ public void testDropNamespaceStatus() {
     }
   }
 
+  @Test
+  public void testCreateAndUpdateCatalogRoleWithReservedProperties() {
+    String catalogName = client.newEntityName("mycatalog1");
+    Catalog catalog =
+        PolarisCatalog.builder()
+            .setType(Catalog.TypeEnum.INTERNAL)
+            .setName(catalogName)
+            .setProperties(new 
CatalogProperties("s3://required/base/location"))
+            .setStorageConfigInfo(
+                new AwsStorageConfigInfo(
+                    "arn:aws:iam::012345678901:role/jdoe", 
StorageConfigInfo.StorageTypeEnum.S3))
+            .build();
+    managementApi.createCatalog(catalog);
+
+    CatalogRole badCatalogRole =
+        new CatalogRole("mycatalogrole", Map.of("polaris.reserved", "foo"), 
0L, 0L, 1);
+    try (Response response =
+        managementApi
+            .request("v1/catalogs/{cat}/catalog-roles", Map.of("cat", 
catalogName))
+            .post(Entity.json(new CreateCatalogRoleRequest(badCatalogRole)))) {
+      assertThat(response)
+          .returns(Response.Status.BAD_REQUEST.getStatusCode(), 
Response::getStatus);

Review Comment:
   Could you add an assert on the message too? IMHO, tests can be an effective 
"user proxy" to assure usable error messages.



##########
quarkus/service/src/test/java/org/apache/polaris/service/quarkus/admin/PolarisAuthzTestBase.java:
##########
@@ -181,6 +182,13 @@ public Map<String, String> getConfigOverrides() {
               Map.of(
                   
FeatureConfiguration.ENFORCE_PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_CHECKING.key,
                   true)));
+  protected final ReservedProperties reservedProperties =
+      new ReservedProperties() {
+        @Override
+        public List<String> reservedPrefixes() {

Review Comment:
   Maybe add a static constant `ReservedProperties.NONE` with this impl?



##########
service/common/src/main/java/org/apache/polaris/service/config/ReservedProperties.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.polaris.service.config;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.iceberg.MetadataUpdate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Used to track entity properties reserved for use by the catalog. These 
properties may not be
+ * overridden by the end user.
+ */
+public interface ReservedProperties {
+  Logger LOGGER = LoggerFactory.getLogger(ReservedProperties.class);
+
+  /**
+   * A list of prefixes that are considered reserved. Any property starting 
with one of these
+   * prefixes is a reserved property.
+   */
+  List<String> reservedPrefixes();
+
+  /** If true, attempts to modify a reserved property should throw an 
exception. */
+  default boolean shouldThrow() {
+    return true;
+  }
+
+  /**
+   * Removes reserved properties from a planned change to an entity. If 
`shouldThrow`returns true,
+   * this will throw an IllegalArgumentException.
+   *
+   * @param existingProperties The properties currently present for an entity
+   * @param updateProperties The properties present in an update to an entity
+   * @return The keys from the new key list which are not reserved properties
+   */
+  default Map<String, String> removeReservedPropertiesFromUpdate(
+      Map<String, String> existingProperties, Map<String, String> 
updateProperties)
+      throws IllegalArgumentException {
+    Map<String, String> updatePropertiesWithoutReservedProperties =
+        removeReservedProperties(updateProperties);
+    for (var entry : updateProperties.entrySet()) {
+      // If a key was removed from the update, we substitute in the existing 
value as to not remove
+      // it
+      if 
(!updatePropertiesWithoutReservedProperties.containsKey(entry.getKey())) {

Review Comment:
   Would it not be simpler to check `isReserved(entry.getKey())`?



##########
integration-tests/src/main/java/org/apache/polaris/service/it/test/PolarisManagementServiceIntegrationTest.java:
##########
@@ -2102,6 +2103,129 @@ public void testDropNamespaceStatus() {
     }
   }
 
+  @Test
+  public void testCreateAndUpdateCatalogRoleWithReservedProperties() {
+    String catalogName = client.newEntityName("mycatalog1");
+    Catalog catalog =
+        PolarisCatalog.builder()
+            .setType(Catalog.TypeEnum.INTERNAL)
+            .setName(catalogName)
+            .setProperties(new 
CatalogProperties("s3://required/base/location"))
+            .setStorageConfigInfo(
+                new AwsStorageConfigInfo(
+                    "arn:aws:iam::012345678901:role/jdoe", 
StorageConfigInfo.StorageTypeEnum.S3))
+            .build();
+    managementApi.createCatalog(catalog);
+
+    CatalogRole badCatalogRole =
+        new CatalogRole("mycatalogrole", Map.of("polaris.reserved", "foo"), 
0L, 0L, 1);
+    try (Response response =
+        managementApi
+            .request("v1/catalogs/{cat}/catalog-roles", Map.of("cat", 
catalogName))
+            .post(Entity.json(new CreateCatalogRoleRequest(badCatalogRole)))) {
+      assertThat(response)
+          .returns(Response.Status.BAD_REQUEST.getStatusCode(), 
Response::getStatus);
+    }
+
+    CatalogRole okCatalogRole = new CatalogRole("mycatalogrole", Map.of("foo", 
"bar"), 0L, 0L, 1);
+    try (Response response =
+        managementApi
+            .request("v1/catalogs/{cat}/catalog-roles", Map.of("cat", 
catalogName))

Review Comment:
   Maybe `.createCatalogRole(catalogName, okCatalogRole)` (add new utility 
method)?



##########
service/common/src/main/java/org/apache/polaris/service/config/ReservedProperties.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.polaris.service.config;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.iceberg.MetadataUpdate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Used to track entity properties reserved for use by the catalog. These 
properties may not be
+ * overridden by the end user.
+ */
+public interface ReservedProperties {
+  Logger LOGGER = LoggerFactory.getLogger(ReservedProperties.class);
+
+  /**
+   * A list of prefixes that are considered reserved. Any property starting 
with one of these
+   * prefixes is a reserved property.
+   */
+  List<String> reservedPrefixes();
+
+  /** If true, attempts to modify a reserved property should throw an 
exception. */
+  default boolean shouldThrow() {
+    return true;
+  }
+
+  /**
+   * Removes reserved properties from a planned change to an entity. If 
`shouldThrow`returns true,
+   * this will throw an IllegalArgumentException.
+   *
+   * @param existingProperties The properties currently present for an entity
+   * @param updateProperties The properties present in an update to an entity
+   * @return The keys from the new key list which are not reserved properties
+   */
+  default Map<String, String> removeReservedPropertiesFromUpdate(
+      Map<String, String> existingProperties, Map<String, String> 
updateProperties)
+      throws IllegalArgumentException {
+    Map<String, String> updatePropertiesWithoutReservedProperties =
+        removeReservedProperties(updateProperties);
+    for (var entry : updateProperties.entrySet()) {
+      // If a key was removed from the update, we substitute in the existing 
value as to not remove
+      // it
+      if 
(!updatePropertiesWithoutReservedProperties.containsKey(entry.getKey())) {
+        if (existingProperties.containsKey(entry.getKey())) {
+          updatePropertiesWithoutReservedProperties.put(
+              entry.getKey(), existingProperties.get(entry.getKey()));
+        }
+      }
+    }
+    return updatePropertiesWithoutReservedProperties;
+  }
+
+  /**
+   * Removes reserved properties from a list of input property keys. If 
`shouldThrow`returns true,
+   * this will throw an IllegalArgumentException.
+   *
+   * @param properties A map of properties to remove reserved properties from
+   * @return The keys from the input list which are not reserved properties
+   */
+  default Map<String, String> removeReservedProperties(Map<String, String> 
properties)
+      throws IllegalArgumentException {
+    Map<String, String> results = new HashMap<>();
+    List<String> prefixes = reservedPrefixes();
+    for (var entry : properties.entrySet()) {
+      boolean isReserved = false;
+      for (String prefix : prefixes) {
+        if (entry.getKey().startsWith(prefix)) {
+          String message =
+              String.format("Property '%s' matches reserved prefix '%s'", 
entry.getKey(), prefix);
+          if (shouldThrow()) {
+            throw new IllegalArgumentException(message);
+          } else {
+            LOGGER.debug(message);
+            isReserved = true;

Review Comment:
   nit: it would be clearer to move this above line 89 and do a `break` here.



##########
quarkus/service/src/main/java/org/apache/polaris/service/quarkus/config/QuarkusReservedProperties.java:
##########
@@ -0,0 +1,31 @@
+/*
+ * 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.polaris.service.quarkus.config;
+
+import io.smallrye.config.ConfigMapping;
+import java.util.List;
+import org.apache.polaris.service.config.ReservedProperties;
+
+@ConfigMapping(prefix = "polaris.reserved-properties")
+public interface QuarkusReservedProperties extends ReservedProperties {
+  @Override
+  default List<String> reservedPrefixes() {

Review Comment:
   The total config name would be 
`polaris.reserved-properties.reserved-prefixes=<list>`, which looks awkward.
   
   WDYT about simply `prefixes()`?



##########
service/common/src/main/java/org/apache/polaris/service/config/ReservedProperties.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.polaris.service.config;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.iceberg.MetadataUpdate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Used to track entity properties reserved for use by the catalog. These 
properties may not be
+ * overridden by the end user.
+ */
+public interface ReservedProperties {
+  Logger LOGGER = LoggerFactory.getLogger(ReservedProperties.class);
+
+  /**
+   * A list of prefixes that are considered reserved. Any property starting 
with one of these
+   * prefixes is a reserved property.
+   */
+  List<String> reservedPrefixes();
+
+  /** If true, attempts to modify a reserved property should throw an 
exception. */
+  default boolean shouldThrow() {
+    return true;
+  }
+
+  /**
+   * Removes reserved properties from a planned change to an entity. If 
`shouldThrow`returns true,
+   * this will throw an IllegalArgumentException.
+   *
+   * @param existingProperties The properties currently present for an entity
+   * @param updateProperties The properties present in an update to an entity
+   * @return The keys from the new key list which are not reserved properties
+   */
+  default Map<String, String> removeReservedPropertiesFromUpdate(
+      Map<String, String> existingProperties, Map<String, String> 
updateProperties)
+      throws IllegalArgumentException {
+    Map<String, String> updatePropertiesWithoutReservedProperties =
+        removeReservedProperties(updateProperties);
+    for (var entry : updateProperties.entrySet()) {
+      // If a key was removed from the update, we substitute in the existing 
value as to not remove
+      // it
+      if 
(!updatePropertiesWithoutReservedProperties.containsKey(entry.getKey())) {
+        if (existingProperties.containsKey(entry.getKey())) {
+          updatePropertiesWithoutReservedProperties.put(
+              entry.getKey(), existingProperties.get(entry.getKey()));
+        }
+      }
+    }
+    return updatePropertiesWithoutReservedProperties;
+  }
+
+  /**
+   * Removes reserved properties from a list of input property keys. If 
`shouldThrow`returns true,
+   * this will throw an IllegalArgumentException.
+   *
+   * @param properties A map of properties to remove reserved properties from
+   * @return The keys from the input list which are not reserved properties
+   */
+  default Map<String, String> removeReservedProperties(Map<String, String> 
properties)
+      throws IllegalArgumentException {
+    Map<String, String> results = new HashMap<>();
+    List<String> prefixes = reservedPrefixes();
+    for (var entry : properties.entrySet()) {
+      boolean isReserved = false;
+      for (String prefix : prefixes) {
+        if (entry.getKey().startsWith(prefix)) {
+          String message =
+              String.format("Property '%s' matches reserved prefix '%s'", 
entry.getKey(), prefix);
+          if (shouldThrow()) {
+            throw new IllegalArgumentException(message);
+          } else {
+            LOGGER.debug(message);
+            isReserved = true;
+          }
+        }
+      }
+      if (!isReserved) {
+        results.put(entry.getKey(), entry.getValue());
+      }
+    }
+    return results;
+  }
+
+  /** See {@link #removeReservedProperties(Map)} */
+  default List<String> removeReservedProperties(List<String> properties)
+      throws IllegalArgumentException {
+    Map<String, String> propertyMap =
+        properties.stream().collect(Collectors.toMap(k -> k, k -> ""));
+    Map<String, String> filteredMap = removeReservedProperties(propertyMap);
+    return filteredMap.keySet().stream().toList();

Review Comment:
   If we had an `isReserved(name)` method, we could simply filter the original 
list.



##########
service/common/src/main/java/org/apache/polaris/service/config/ReservedProperties.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.polaris.service.config;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.iceberg.MetadataUpdate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Used to track entity properties reserved for use by the catalog. These 
properties may not be
+ * overridden by the end user.
+ */
+public interface ReservedProperties {
+  Logger LOGGER = LoggerFactory.getLogger(ReservedProperties.class);
+
+  /**
+   * A list of prefixes that are considered reserved. Any property starting 
with one of these
+   * prefixes is a reserved property.
+   */
+  List<String> reservedPrefixes();
+
+  /** If true, attempts to modify a reserved property should throw an 
exception. */
+  default boolean shouldThrow() {
+    return true;
+  }
+
+  /**
+   * Removes reserved properties from a planned change to an entity. If 
`shouldThrow`returns true,
+   * this will throw an IllegalArgumentException.
+   *
+   * @param existingProperties The properties currently present for an entity
+   * @param updateProperties The properties present in an update to an entity
+   * @return The keys from the new key list which are not reserved properties
+   */
+  default Map<String, String> removeReservedPropertiesFromUpdate(
+      Map<String, String> existingProperties, Map<String, String> 
updateProperties)
+      throws IllegalArgumentException {
+    Map<String, String> updatePropertiesWithoutReservedProperties =
+        removeReservedProperties(updateProperties);
+    for (var entry : updateProperties.entrySet()) {
+      // If a key was removed from the update, we substitute in the existing 
value as to not remove
+      // it

Review Comment:
   nit: maybe make a manual line break for better readability



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