Copilot commented on code in PR #14145:
URL: https://github.com/apache/gravitino/pull/14145#discussion_r4401677646
##########
clients/client-java/src/main/java/org/apache/gravitino/client/SemanticModelCatalogOperations.java:
##########
@@ -260,6 +299,18 @@ private Namespace getSemanticModelFullNamespace(Namespace
semanticModelNamespace
return Namespace.of(catalogNamespace.level(0), catalogName,
semanticModelNamespace.level(0));
}
+ private static String formatMetricRequestPath(Namespace namespace) {
+ return BaseSchemaCatalog.formatSchemaRequestPath(
+ Namespace.of(namespace.level(0), namespace.level(1)))
+ + "/"
+ + RESTUtils.encodeString(namespace.level(2))
+ + "/semantic-model-metrics";
+ }
+
+ private static Namespace relativeMetricNamespace(Namespace namespace) {
Review Comment:
relativeMetricNamespace assumes the server always returns a 4-level
namespace (metalake/catalog/schema/model). If a server bug/misconfiguration (or
a future API change) returns fewer levels, this will throw
IndexOutOfBoundsException, bypassing the existing response validation/error
mapping and producing a hard-to-debug client crash. Add an explicit level-count
validation here (and throw IllegalArgumentException with a clear message)
before indexing.
##########
clients/client-java/src/test/java/org/apache/gravitino/client/integration/test/SemanticModelIT.java:
##########
@@ -1236,6 +1341,25 @@ private static OssieDocument ossieSourceDocument(
primaryKey == null ? "" : " primary_key: [" + primaryKey +
"]\n"));
}
+ private void assertMetricDiscovery(NameIdentifier[] identifiers, Metric...
metrics) {
+ Namespace namespace = Namespace.of(SCHEMA_NAME);
+ assertArrayEquals(identifiers,
semanticModelCatalog.listMetrics(namespace));
+ SemanticModelMetric[] infos =
semanticModelCatalog.listMetricInfos(namespace);
+ assertEquals(metrics.length, infos.length);
+ for (int i = 0; i < infos.length; i++) {
+ assertEquals(
+ identifiers[i], NameIdentifier.of(infos[i].namespace(),
infos[i].metric().name()));
+ assertEquals(metrics[i], infos[i].metric());
+ }
+ }
Review Comment:
assertMetricDiscovery indexes identifiers[i] while looping over infos, but
only asserts metrics.length == infos.length. If a future caller passes
mismatched identifiers/metrics lengths, the failure will be an
ArrayIndexOutOfBoundsException rather than a clear assertion. Add an upfront
assertion that identifiers.length == metrics.length (or restructure to assert
both lengths explicitly) to improve diagnostics.
##########
clients/client-java/src/test/java/org/apache/gravitino/client/integration/test/authorization/SemanticModelMetricAuthorizationIT.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.client.integration.test.authorization;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.gravitino.Catalog;
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.MetadataObjects;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.Namespace;
+import org.apache.gravitino.authorization.Owner;
+import org.apache.gravitino.authorization.Privileges;
+import org.apache.gravitino.authorization.SecurableObjects;
+import org.apache.gravitino.client.GravitinoMetalake;
+import org.apache.gravitino.exceptions.ForbiddenException;
+import org.apache.gravitino.integration.test.util.GravitinoITUtils;
+import org.apache.gravitino.rel.Column;
+import org.apache.gravitino.rel.types.Types;
+import org.apache.gravitino.semantic.Dataset;
+import org.apache.gravitino.semantic.DialectExpression;
+import org.apache.gravitino.semantic.Expression;
+import org.apache.gravitino.semantic.Metric;
+import org.apache.gravitino.semantic.SemanticModelCatalog;
+import org.apache.gravitino.semantic.SemanticModelDefinition;
+import org.apache.gravitino.semantic.SemanticModelMetric;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+/** Exercises metric discovery with real schema and containing-model
authorization. */
+public class SemanticModelMetricAuthorizationIT extends
BaseRestApiAuthorizationIT {
+ private static final String CATALOG = "metric_discovery_catalog";
+ private static final String SCHEMA = "metric_discovery_schema";
+ private static final String ROLE = "metric_reader";
+ private static final Namespace NAMESPACE = Namespace.of(SCHEMA);
+
+ private GravitinoMetalake metalake;
+ private SemanticModelCatalog adminModels;
+ private SemanticModelCatalog readerModels;
+
+ /** Creates models and a reader with catalog access but no schema or model
privileges. */
+ @BeforeAll
+ public void setUpMetricDiscovery() {
+ metalake = client.loadMetalake(METALAKE);
+ String database = GravitinoITUtils.genRandomName("metric_authorization");
+ Catalog catalog =
+ metalake.createCatalog(
+ CATALOG,
+ Catalog.Type.RELATIONAL,
+ "lakehouse-iceberg",
+ null,
+ Map.of(
+ "catalog-backend", "jdbc",
+ "warehouse", System.getProperty("java.io.tmpdir") + "/" +
database,
+ "uri", "jdbc:h2:mem:" + database +
";DB_CLOSE_DELAY=-1;MODE=MYSQL",
+ "jdbc-driver", "org.h2.Driver",
+ "jdbc-initialize", "true"));
+ catalog.asSchemas().createSchema(SCHEMA, null, Map.of());
+ catalog
+ .asTableCatalog()
+ .createTable(
+ NameIdentifier.of(SCHEMA, "orders"),
+ new Column[] {Column.of("amount", Types.LongType.get())},
+ null,
+ Map.of());
+ adminModels = catalog.asSemanticModelCatalog();
+ SemanticModelDefinition definition =
+ SemanticModelDefinition.builder()
+ .withDatasets(
+ new Dataset[] {
+ Dataset.builder()
+ .withName("orders")
+ .withSource(NameIdentifier.of(CATALOG, SCHEMA, "orders"))
+ .build()
+ })
+ .withMetrics(new Metric[] {metric("count"), metric("revenue")})
+ .build();
+ for (String model : List.of("hidden", "modify", "owned", "use")) {
+ adminModels.createSemanticModel(NameIdentifier.of(SCHEMA, model), null,
definition, Map.of());
+ }
+ metalake.createRole(
+ ROLE,
+ Map.of(),
+ List.of(SecurableObjects.ofCatalog(CATALOG,
List.of(Privileges.UseCatalog.allow()))));
+ metalake.grantRolesToUser(List.of(ROLE), NORMAL_USER);
+ readerModels =
+
normalUserClient.loadMetalake(METALAKE).loadCatalog(CATALOG).asSemanticModelCatalog();
+ }
+
+ @Test
+ void testDiscoveryUsesSchemaAccessAndWholeModelVisibility() {
+ assertVisibleModels(adminModels, "hidden", "modify", "owned", "use");
+ assertSchemaDenied();
+ MetadataObject schema = MetadataObjects.of(CATALOG, SCHEMA,
MetadataObject.Type.SCHEMA);
+ metalake.grantPrivilegesToRole(ROLE, schema,
List.of(Privileges.UseSchema.allow()));
+ assertVisibleModels(readerModels);
+
+ metalake.grantPrivilegesToRole(
+ ROLE, modelObject("use"),
List.of(Privileges.UseSemanticModel.allow()));
+ metalake.grantPrivilegesToRole(
+ ROLE, modelObject("modify"),
List.of(Privileges.ModifySemanticModel.allow()));
+ metalake.setOwner(modelObject("owned"), NORMAL_USER, Owner.Type.USER);
+ assertVisibleModels(readerModels, "modify", "owned", "use");
+
+ metalake.revokePrivilegesFromRole(
+ ROLE, modelObject("use"), Set.of(Privileges.UseSemanticModel.allow()));
+ assertVisibleModels(readerModels, "modify", "owned");
+ metalake.revokePrivilegesFromRole(
+ ROLE, modelObject("modify"),
Set.of(Privileges.ModifySemanticModel.allow()));
+ assertVisibleModels(readerModels, "owned");
+ metalake.setOwner(modelObject("owned"), USER, Owner.Type.USER);
+ assertVisibleModels(readerModels);
+ metalake.revokePrivilegesFromRole(ROLE, schema,
Set.of(Privileges.UseSchema.allow()));
+ assertSchemaDenied();
+ }
+
+ private void assertSchemaDenied() {
+ assertThrows(ForbiddenException.class, () ->
readerModels.listMetrics(NAMESPACE));
+ assertThrows(ForbiddenException.class, () ->
readerModels.listMetricInfos(NAMESPACE));
+ }
+
+ private static void assertVisibleModels(SemanticModelCatalog catalog,
String... models) {
+ NameIdentifier[] expected =
+ Arrays.stream(models)
+ .flatMap(
+ model ->
+ Arrays.stream(new String[] {"count", "revenue"})
+ .map(name -> NameIdentifier.of(SCHEMA, model, name)))
+ .toArray(NameIdentifier[]::new);
+ assertArrayEquals(expected, catalog.listMetrics(NAMESPACE));
+ SemanticModelMetric[] infos = catalog.listMetricInfos(NAMESPACE);
+ assertEquals(expected.length, infos.length);
+ for (int i = 0; i < infos.length; i++) {
+ assertEquals(expected[i], NameIdentifier.of(infos[i].namespace(),
infos[i].metric().name()));
+ assertEquals(metric(expected[i].name()), infos[i].metric());
+ }
+ }
+
+ private static MetadataObject modelObject(String model) {
+ return MetadataObjects.of(List.of(CATALOG, SCHEMA, model),
MetadataObject.Type.SEMANTIC_MODEL);
+ }
+
+ private static Metric metric(String name) {
+ return Metric.builder()
+ .withName(name)
+ .withExpression(
+ Expression.builder()
+ .withDialects(
+ new DialectExpression[] {
+ DialectExpression.builder()
+ .withDialect("ANSI_SQL")
+ .withExpression("SUM(orders.amount)")
+ .build()
+ })
+ .build())
+ .build();
+ }
Review Comment:
This helper is used to create metrics named \"count\" and \"revenue\", but
the expression is always SUM(orders.amount). That makes the test data
internally inconsistent and harder to understand when debugging authorization
failures. Consider either (a) making the expression match the metric name
(e.g., COUNT(...) for \"count\") or (b) renaming the metric names to reflect
the SUM(...) expression.
--
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]