Skip to content

Commit 939dd98

Browse files
committed
add RuleFunctionAuthorization to support hive udf Authorization
1 parent d9e0d36 commit 939dd98

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

extensions/spark/kyuubi-spark-authz/src/main/scala/org/apache/kyuubi/plugin/spark/authz/ranger/RangerSparkExtension.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class RangerSparkExtension extends (SparkSessionExtensions => Unit) {
4545

4646
override def apply(v1: SparkSessionExtensions): Unit = {
4747
v1.injectCheckRule(AuthzConfigurationChecker)
48+
v1.injectCheckRule(RuleFunctionAuthorization)
4849
v1.injectResolutionRule(_ => RuleReplaceShowObjectCommands)
4950
v1.injectResolutionRule(_ => RuleApplyPermanentViewMarker)
5051
v1.injectResolutionRule(_ => RuleApplyTypeOfMarker)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)