|
| 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 scala.collection.mutable |
| 21 | + |
| 22 | +import org.apache.spark.sql.SparkSession |
| 23 | +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan |
| 24 | + |
| 25 | +import org.apache.kyuubi.plugin.spark.authz._ |
| 26 | +import org.apache.kyuubi.plugin.spark.authz.ranger.AccessType.AccessType |
| 27 | +import org.apache.kyuubi.plugin.spark.authz.ranger.SparkRangerAdminPlugin._ |
| 28 | +import org.apache.kyuubi.plugin.spark.authz.util.AuthZUtils._ |
| 29 | + |
| 30 | +case class RuleFunctionAuthorization(spark: SparkSession) extends (LogicalPlan => Unit) { |
| 31 | + override def apply(plan: LogicalPlan): Unit = { |
| 32 | + val auditHandler = new SparkRangerAuditHandler |
| 33 | + val ugi = getAuthzUgi(spark.sparkContext) |
| 34 | + val (inputs, _, opType) = PrivilegesBuilder.buildFunctions(plan, spark) |
| 35 | + |
| 36 | + // Use a HashSet to deduplicate the same AccessResource and AccessType, the requests will be all |
| 37 | + // the non-duplicate requests and in the same order as the input requests. |
| 38 | + val requests = new mutable.ArrayBuffer[AccessRequest]() |
| 39 | + val requestsSet = new mutable.HashSet[(AccessResource, AccessType)]() |
| 40 | + |
| 41 | + def addAccessRequest(objects: Iterable[PrivilegeObject], isInput: Boolean): Unit = { |
| 42 | + objects.foreach { obj => |
| 43 | + val resource = AccessResource(obj, opType) |
| 44 | + val accessType = ranger.AccessType(obj, opType, isInput) |
| 45 | + if (accessType != AccessType.NONE && !requestsSet.contains((resource, accessType))) { |
| 46 | + requests += AccessRequest(resource, ugi, opType, accessType) |
| 47 | + requestsSet.add(resource, accessType) |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + addAccessRequest(inputs, isInput = true) |
| 53 | + |
| 54 | + val requestArrays = requests.map(Seq(_)) |
| 55 | + if (authorizeInSingleCall) { |
| 56 | + verify(requestArrays.flatten, auditHandler) |
| 57 | + } else { |
| 58 | + requestArrays.flatten.foreach { req => |
| 59 | + verify(Seq(req), auditHandler) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments