roryqi commented on code in PR #11627:
URL: https://github.com/apache/gravitino/pull/11627#discussion_r3497102615


##########
spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/authorization/RequiredPrivilegesCheck.java:
##########
@@ -0,0 +1,46 @@
+/*
+ * 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.spark.connector.authorization;
+
+import java.util.Optional;
+import org.apache.gravitino.exceptions.ForbiddenException;
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan;
+import org.apache.spark.sql.catalyst.rules.Rule;
+
+/**
+ * A post-hoc resolution rule that reports the tables a query lacks Gravitino 
privileges for.
+ *
+ * <p>It runs after Spark's {@code ResolveRelations} has resolved every 
relation in the query, so
+ * all denied tables have been collected by {@link AuthorizationTable#deny}, 
but before {@code
+ * checkAnalysis} would fail the query for an unrelated reason (such as a 
column that cannot be
+ * resolved against a denied table's placeholder schema). This lets a single 
error list every
+ * inaccessible table and its required privileges instead of failing on the 
first one.
+ */
+public class RequiredPrivilegesCheck extends Rule<LogicalPlan> {
+
+  @Override
+  public LogicalPlan apply(LogicalPlan plan) {

Review Comment:
   Fixed by clearing stale authorization collector state at SQL parser entry 
before the next query is parsed, with a regression test covering the 
stale-state case.



##########
spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/authorization/AuthorizationTable.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.spark.connector.authorization;
+
+import com.google.common.collect.ImmutableSet;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.stream.Collectors;
+import org.apache.gravitino.authorization.Privilege;
+import org.apache.gravitino.exceptions.ForbiddenException;
+import org.apache.spark.sql.connector.catalog.SupportsRead;
+import org.apache.spark.sql.connector.catalog.SupportsWrite;
+import org.apache.spark.sql.connector.catalog.Table;
+import org.apache.spark.sql.connector.catalog.TableCapability;
+import org.apache.spark.sql.connector.expressions.Transform;
+import org.apache.spark.sql.connector.read.ScanBuilder;
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.WriteBuilder;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.sql.util.CaseInsensitiveStringMap;
+
+/**
+ * A placeholder Spark table returned when the caller lacks the Gravitino 
privileges to load a
+ * table.
+ *
+ * <p>The connector does not load the real Spark table for a denied table, 
because that load would
+ * bypass the very authorization the caller is missing. Instead {@link #deny} 
records the table and
+ * its required privileges in a per-thread collector and returns this 
placeholder so Spark's {@code
+ * ResolveRelations} can finish resolving every relation in the query. {@link
+ * RequiredPrivilegesCheck} then drains the collector once resolution 
completes and reports all
+ * denied tables together, before analysis fails on anything else.
+ *
+ * <p>The metadata methods ({@link #name}, {@link #schema}, ...) return 
harmless placeholders so the
+ * relation can be built during resolution, while the data-access methods 
({@link #newScanBuilder},
+ * {@link #newWriteBuilder}) fail closed: should the authorization check ever 
be bypassed, the table
+ * still cannot be read or written.
+ */
+public class AuthorizationTable implements Table, SupportsRead, SupportsWrite {
+
+  // Denied tables discovered while resolving the relations of a single query, 
keyed by the fully
+  // qualified table identifier. Held per-thread because Spark analyzes one 
query per thread, and
+  // drained by RequiredPrivilegesCheck once resolution completes.
+  private static final ThreadLocal<Map<String, Set<Privilege.Name>>> 
DENIED_TABLES =
+      ThreadLocal.withInitial(TreeMap::new);
+  private static final ThreadLocal<ForbiddenException> FIRST_FAILURE = new 
ThreadLocal<>();
+

Review Comment:
   Fixed by wrapping the per-thread denied table state in a dedicated collector 
helper with record/failure methods.



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