World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#15180] fix(spark-connector): Report a denied Hive view as denied instead of not found (#15181)
YangJief0b2293eb711
diff --git a/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/catalog/BaseCatalog.java b/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/catalog/BaseCatalog.javaindex 88ac26ff35..8a6ab84dc1 100644--- a/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/catalog/BaseCatalog.java+++ b/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/catalog/BaseCatalog.java@@ -606,10 +606,12 @@ public abstract class BaseCatalog implements TableCatalog, SupportsNamespaces, F/*** Loads a Gravitino view as a Spark Table when {@link #loadTable(Identifier)} fails because the- * object is a view. Subclasses can override {@link #createSparkView} to enable view support.+ * object is a view. Subclasses can override {@link #createSparkView} to enable view support. When+ * {@link #supportsSparkViews} is true, a view the caller is denied from loading returns the+ * authorization placeholder instead, as a denied table does.** @param ident the identifier to load- * @return Spark Table wrapping the view+ * @return Spark Table wrapping the view, or the authorization placeholder of a denied view* @throws NoSuchTableException if no view exists or views are not supported by this catalog*/protected Table loadViewAsTable(Identifier ident) throws NoSuchTableException {@@ -619,7 +621,18 @@ public abstract class BaseCatalog implements TableCatalog, SupportsNamespaces, FgravitinoCatalog().asViewCatalog().loadView(NameIdentifier.of(getDatabase(ident), ident.name()));- } catch (NoSuchViewException | UnsupportedOperationException | ForbiddenException e) {+ } catch (ForbiddenException e) {+ // Only a catalog that can read the view reports the denial. The server also denies a name+ // that does not exist when its catalog has no views.+ if (!supportsSparkViews()) {+ throw new NoSuchTableException(ident);+ }+ return AuthorizationTable.deny(+ ident.name(),+ String.format("%s.%s.%s", catalogName, getDatabase(ident), ident.name()),+ Sets.newHashSet(Privilege.Name.SELECT_VIEW),+ e);+ } catch (NoSuchViewException | UnsupportedOperationException e) {throw new NoSuchTableException(ident);}// Call sparkCatalog.loadTable directly to surface NoSuchTableException as checked,@@ -628,6 +641,17 @@ public abstract class BaseCatalog implements TableCatalog, SupportsNamespaces, Freturn createSparkView(ident, gravitinoView, sparkTable);}+ /**+ * Returns whether this catalog reads Gravitino views, so that a denied view lookup is reported as+ * denied rather than as not found. Subclasses that override {@link #createSparkView} must return+ * true.+ *+ * @return true if this catalog reads views+ */+ protected boolean supportsSparkViews() {+ return false;+ }+/*** Creates a catalog-specific Spark Table wrapping a Gravitino view. Subclasses that support views* must override this method.diff --git a/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/hive/GravitinoHiveCatalog.java b/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/hive/GravitinoHiveCatalog.javaindex f001cbee62..96c6cb5c60 100644--- a/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/hive/GravitinoHiveCatalog.java+++ b/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/hive/GravitinoHiveCatalog.java@@ -113,6 +113,11 @@ public class GravitinoHiveCatalog extends BaseCatalog {return new SparkHiveTypeConverter();}+ @Override+ protected boolean supportsSparkViews() {+ return true;+ }+@Overrideprotected org.apache.spark.sql.connector.catalog.Table createSparkView(Identifier ident,diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/catalog/TestBaseCatalogAuthorization.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/catalog/TestBaseCatalogAuthorization.javaindex c4e982b048..c1446188c7 100644--- a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/catalog/TestBaseCatalogAuthorization.java+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/catalog/TestBaseCatalogAuthorization.java@@ -31,14 +31,18 @@ import static org.mockito.Mockito.when;import com.google.common.collect.ImmutableSet;import java.lang.reflect.Field;import java.time.OffsetDateTime;+import java.util.Collections;import java.util.Map;import org.apache.gravitino.Catalog;import org.apache.gravitino.NameIdentifier;import org.apache.gravitino.authorization.Privilege;import org.apache.gravitino.client.GravitinoClient;import org.apache.gravitino.exceptions.ForbiddenException;+import org.apache.gravitino.exceptions.NoSuchViewException;import org.apache.gravitino.rel.Column;import org.apache.gravitino.rel.TableCatalog;+import org.apache.gravitino.rel.View;+import org.apache.gravitino.rel.ViewCatalog;import org.apache.gravitino.rel.expressions.literals.Literals;import org.apache.gravitino.rel.types.Types;import org.apache.gravitino.spark.connector.PropertiesConverter;@@ -46,7 +50,9 @@ import org.apache.gravitino.spark.connector.SparkTableChangeConverter;import org.apache.gravitino.spark.connector.SparkTransformConverter;import org.apache.gravitino.spark.connector.SparkTypeConverter;import org.apache.gravitino.spark.connector.authorization.AuthorizationTable;+import org.apache.gravitino.spark.connector.hive.GravitinoHiveCatalog;import org.apache.spark.SparkConf;+import org.apache.spark.sql.catalyst.analysis.NoSuchTableException;import org.apache.spark.sql.connector.catalog.Identifier;import org.apache.spark.sql.connector.catalog.Table;import org.apache.spark.sql.connector.catalog.TableChange;@@ -130,6 +136,78 @@ public class TestBaseCatalogAuthorization {assertEquals(ImmutableSet.of(Privilege.Name.SELECT_TABLE), deniedTable.requiredPrivileges());}+ /**+ * In a catalog that reads views, a user denied an existing view gets the deny placeholder, as for+ * a denied table, and the underlying Spark table is not loaded.+ */+ @Test+ void testDeniedViewSurfacesAuthorizationPlaceholder() throws Exception {+ Identifier identifier = Identifier.of(new String[] {"schema"}, "view_a");+ ViewCatalog viewCatalog = stubViewFallback(identifier);+ ForbiddenException denied = new ForbiddenException("denied view");+ when(viewCatalog.loadView(NameIdentifier.of("schema", "view_a"))).thenThrow(denied);++ Table result = viewCapableCatalog(mock(Table.class)).loadTable(identifier);++ assertTrue(result instanceof AuthorizationTable);+ AuthorizationTable deniedView = (AuthorizationTable) result;+ assertEquals("null.schema.view_a", deniedView.tableIdentifier());+ assertEquals(ImmutableSet.of(Privilege.Name.SELECT_VIEW), deniedView.requiredPrivileges());+ assertSame(+ denied,+ AuthorizationTable.aggregateFailure(Collections.singletonList(deniedView))+ .get()+ .getCause());+ Mockito.verifyNoInteractions(sparkCatalog);+ }++ /**+ * A catalog that cannot read views keeps reporting a denied view lookup as not found: its server+ * catalog may have no views, and then the denial says nothing about whether the name exists.+ */+ @Test+ void testDeniedViewInCatalogWithoutViewsThrowsNoSuchTable() throws Exception {+ Identifier identifier = Identifier.of(new String[] {"schema"}, "view_a");+ ViewCatalog viewCatalog = stubViewFallback(identifier);+ when(viewCatalog.loadView(NameIdentifier.of("schema", "view_a")))+ .thenThrow(new ForbiddenException("denied view"));++ assertThrows(NoSuchTableException.class, () -> catalog.loadTable(identifier));+ }++ /** A missing view, or a view lookup the catalog cannot perform, raises NoSuchTableException. */+ @ParameterizedTest+ @ValueSource(booleans = {true, false})+ void testMissingViewStillThrowsNoSuchTable(boolean lookupUnsupported) throws Exception {+ Identifier identifier = Identifier.of(new String[] {"schema"}, "view_a");+ ViewCatalog viewCatalog = stubViewFallback(identifier);+ when(viewCatalog.loadView(NameIdentifier.of("schema", "view_a")))+ .thenThrow(+ lookupUnsupported+ ? new UnsupportedOperationException("no views")+ : new NoSuchViewException("no such view"));++ assertThrows(+ NoSuchTableException.class,+ () -> viewCapableCatalog(mock(Table.class)).loadTable(identifier));+ }++ /**+ * A permitted view still loads through the fallback with its catalog-specific wrapper: the deny+ * branch must not swallow the success path.+ */+ @Test+ void testPermittedViewLoadsThroughFallback() throws Exception {+ Identifier identifier = Identifier.of(new String[] {"schema"}, "view_a");+ ViewCatalog viewCatalog = stubViewFallback(identifier);+ when(viewCatalog.loadView(NameIdentifier.of("schema", "view_a"))).thenReturn(mock(View.class));+ when(sparkCatalog.loadTable(identifier)).thenReturn(mock(Table.class));+ Table viewWrapper = mock(Table.class);++ assertSame(viewWrapper, viewCapableCatalog(viewWrapper).loadTable(identifier));+ verify(sparkCatalog).loadTable(identifier);+ }+@Testvoid testDeniedWriteReportsTheWriteTarget() throws Exception {Identifier identifier = Identifier.of(new String[] {"schema"}, "table_a");@@ -317,6 +395,41 @@ public class TestBaseCatalogAuthorization {verify(table).columns();}+ /** The Hive catalog, the one that reads views, reports denied views as denied. */+ @Test+ void testHiveCatalogSupportsSparkViews() {+ BaseCatalog hiveCatalog = new GravitinoHiveCatalog();+ assertTrue(hiveCatalog.supportsSparkViews());+ }++ /** Stubs the table load to miss so that loadTable falls back to the returned view catalog. */+ private ViewCatalog stubViewFallback(Identifier identifier) {+ NameIdentifier gravitinoIdentifier =+ NameIdentifier.of(identifier.namespace()[0], identifier.name());+ when(gravitinoTableCatalog.loadTable(+ eq(gravitinoIdentifier), eq(ImmutableSet.of(Privilege.Name.SELECT_TABLE))))+ .thenThrow(new org.apache.gravitino.exceptions.NoSuchTableException("not a table"));+ ViewCatalog viewCatalog = mock(ViewCatalog.class);+ when(gravitinoCatalog.asViewCatalog()).thenReturn(viewCatalog);+ return viewCatalog;+ }++ /** Returns a catalog that reads views, wrapping each loaded view as {@code viewWrapper}. */+ private TestCatalog viewCapableCatalog(Table viewWrapper) {+ return new TestCatalog(gravitinoCatalog, sparkCatalog) {+ @Override+ protected boolean supportsSparkViews() {+ return true;+ }++ @Override+ protected Table createSparkView(+ Identifier ident, View gravitinoView, Table underlyingSparkTable) {+ return viewWrapper;+ }+ };+ }+private static class TestCatalog extends BaseCatalog {private final Catalog gravitinoCatalog;