Jackie-Jiang commented on code in PR #20425: URL: https://github.com/apache/pinot/pull/20425#discussion_r4806946792
########## 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); Review Comment: [P1] Authorize the actual source variant For an untyped source, this always selects the OFFLINE config. With an existing REALTIME-only source, `POST /tables?validationTypesToSkip=TASK` bypasses the analyzer's REALTIME restriction, and this guard skips expression-override checks because the OFFLINE config is absent. For example, `SELECT ts, region FROM orders` passes when `orders_REALTIME` has a `region` override that calls `lookup` on a table the caller cannot read. The scheduler subsequently resolves `orders_REALTIME` via `MaterializedViewTaskUtils.resolveTableNameWithType` and preserves the raw source in the generated SQL. The broker applies the REALTIME overrides using the minion's credentials, so restricted data can be materialized when the minion has access. This requires no later configuration change. Reject unsupported source variants in this mandatory guard, or authorize and inspect the actual variants independently of skippable task validation. -- 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]
