|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.kyuubi.plugin.spark.authz.ranger |
| 19 | + |
| 20 | +import org.apache.hadoop.security.UserGroupInformation |
| 21 | +import org.apache.spark.sql.{Row, SparkSession} |
| 22 | +import org.apache.spark.sql.catalyst.expressions.Attribute |
| 23 | +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan |
| 24 | +import org.apache.spark.sql.catalyst.rules.Rule |
| 25 | +import org.apache.spark.sql.execution.command.RunnableCommand |
| 26 | + |
| 27 | +import org.apache.kyuubi.plugin.spark.authz.{ObjectType, OperationType} |
| 28 | +import org.apache.kyuubi.plugin.spark.authz.util.{AuthZUtils, WithInternalChild} |
| 29 | + |
| 30 | +class RuleReplaceShowObjectCommands extends Rule[LogicalPlan] { |
| 31 | + override def apply(plan: LogicalPlan): LogicalPlan = plan match { |
| 32 | + case r: RunnableCommand if r.nodeName == "ShowTablesCommand" => FilteredShowTablesCommand(r) |
| 33 | + case _ => plan |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +case class FilteredShowTablesCommand(delegated: RunnableCommand) |
| 38 | + extends RunnableCommand with WithInternalChild { |
| 39 | + |
| 40 | + override val output: Seq[Attribute] = delegated.output |
| 41 | + |
| 42 | + override def run(spark: SparkSession): Seq[Row] = { |
| 43 | + val rows = delegated.run(spark) |
| 44 | + val ugi = AuthZUtils.getAuthzUgi(spark.sparkContext) |
| 45 | + rows.filter(r => isAllowed(r, ugi)) |
| 46 | + } |
| 47 | + |
| 48 | + private def isAllowed(r: Row, ugi: UserGroupInformation): Boolean = { |
| 49 | + val database = r.getString(0) |
| 50 | + val table = r.getString(1) |
| 51 | + val isTemp = r.getBoolean(2) |
| 52 | + val objectType = if (isTemp) ObjectType.VIEW else ObjectType.TABLE |
| 53 | + val resource = AccessResource(objectType, database, table, null) |
| 54 | + val request = AccessRequest(resource, ugi, OperationType.SHOWTABLES, AccessType.USE) |
| 55 | + val result = SparkRangerAdminPlugin.isAccessAllowed(request) |
| 56 | + result != null && result.getIsAllowed |
| 57 | + } |
| 58 | + |
| 59 | + override def withNewChildrenInternal(newChildren: IndexedSeq[LogicalPlan]): LogicalPlan = this |
| 60 | +} |
0 commit comments