xiangfu0 commented on code in PR #20425:
URL: https://github.com/apache/pinot/pull/20425#discussion_r4806864897


##########
pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/MaterializedViewSourceAuthorization.java:
##########
@@ -0,0 +1,136 @@
+/**
+ * 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.pinot.controller.api.resources;
+
+import java.util.List;
+import java.util.Map;
+import javax.annotation.Nullable;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Response;
+import org.apache.pinot.common.config.provider.TableCache;
+import org.apache.pinot.common.request.DataSource;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.common.request.PinotQuery;
+import org.apache.pinot.common.utils.DatabaseUtils;
+import org.apache.pinot.common.utils.config.QueryOptionsUtils;
+import org.apache.pinot.common.utils.request.RequestUtils;
+import org.apache.pinot.controller.api.access.AccessControl;
+import org.apache.pinot.controller.api.access.AccessControlFactory;
+import org.apache.pinot.controller.api.access.AccessControlUtils;
+import org.apache.pinot.controller.api.access.AccessType;
+import 
org.apache.pinot.controller.api.exception.ControllerApplicationException;
+import org.apache.pinot.controller.helix.core.PinotHelixResourceManager;
+import org.apache.pinot.core.auth.Actions;
+import org.apache.pinot.core.auth.TargetType;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.utils.CommonConstants.MaterializedViewTask;
+import org.apache.pinot.spi.utils.builder.TableNameBuilder;
+import org.apache.pinot.sql.parsers.CalciteSqlParser;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/// Stateless authorization checks for source tables read by built-in 
materialized views.
+final class MaterializedViewSourceAuthorization {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(MaterializedViewSourceAuthorization.class);
+
+  private MaterializedViewSourceAuthorization() {
+  }
+
+  static void check(@Nullable TableConfig tableConfig, HttpHeaders headers,
+      PinotHelixResourceManager resourceManager, AccessControlFactory 
accessControlFactory) {
+    Map<String, String> taskConfig = tableConfig != null ? 
tableConfig.getMaterializedViewTaskConfigs() : null;
+    if (taskConfig != null) {
+      check(taskConfig.get(MaterializedViewTask.DEFINED_SQL_KEY), null, 
headers, resourceManager, accessControlFactory);
+    }
+  }
+
+  static void check(@Nullable String definedSql, @Nullable String 
inferenceDatabase, HttpHeaders headers,
+      PinotHelixResourceManager resourceManager, AccessControlFactory 
accessControlFactory) {
+    String source = getSourceTable(definedSql);
+    // Refresh runs the stored query independently of the provisioning 
request's database header.
+    AccessControl accessControl = accessControlFactory.create();
+    authorizeSource(source, headers, resourceManager, accessControl);
+    // Schema inference can resolve an unqualified source against the DDL's 
database.
+    if (inferenceDatabase != null && source.indexOf('.') < 0) {
+      String inferenceSource = 
DatabaseUtils.constructFullyQualifiedTableName(inferenceDatabase, source);
+      if (!source.equals(inferenceSource)) {
+        authorizeSource(inferenceSource, headers, resourceManager, 
accessControl);
+      }
+    }
+  }
+
+  private static void authorizeSource(String source, HttpHeaders headers, 
PinotHelixResourceManager resourceManager,
+      AccessControl accessControl) {
+    String sourceTable;
+    try {
+      sourceTable = resourceManager.getActualTableName(source, null);
+    } catch (IllegalArgumentException e) {
+      throw new ControllerApplicationException(LOGGER, "Invalid materialized 
view source table",
+          Response.Status.BAD_REQUEST);
+    }
+    String rawTable = TableNameBuilder.extractRawTableName(sourceTable);
+    String typedTable = 
TableNameBuilder.getTableTypeFromTableName(sourceTable) != null
+        ? sourceTable : TableNameBuilder.OFFLINE.tableNameWithType(rawTable);
+    AccessControlUtils.validatePermission(rawTable, AccessType.READ, headers, 
"/sql", accessControl);
+    if (!accessControl.hasAccess(headers, TargetType.TABLE, rawTable, 
Actions.Table.QUERY)
+        || !accessControl.hasAccess(headers, TargetType.TABLE, typedTable, 
Actions.Table.QUERY)) {
+      throw new ControllerApplicationException(LOGGER, "Permission denied", 
Response.Status.FORBIDDEN);
+    }
+    TableConfig sourceConfig = resourceManager.getTableConfig(typedTable);

Review Comment:
   The controller check is a write-time guard for provisioning. Later refreshes 
need a check against the route and overrides the broker actually uses. I’d 
handle that in a separate refresh-path change so this PR stays focused on 
provisioning.
   



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to