Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import org.apache.ranger.plugin.policyengine.{RangerAccessRequestImpl, RangerPol

import org.apache.kyuubi.plugin.spark.authz.OperationType.OperationType
import org.apache.kyuubi.plugin.spark.authz.ranger.AccessType._
import org.apache.kyuubi.util.reflect.DynMethods.UnboundMethod
import org.apache.kyuubi.util.reflect.ReflectUtils
import org.apache.kyuubi.util.reflect.ReflectUtils._

case class AccessRequest private (accessType: AccessType) extends RangerAccessRequestImpl
Expand All @@ -44,13 +46,13 @@ object AccessRequest {
req.setUser(userName)
req.setUserGroups(userGroups)
req.setAction(opType.toString)

try {
val roles = invokeAs[JSet[String]](
SparkRangerAdminPlugin,
"getRolesFromUserAndGroups",
(classOf[String], userName),
(classOf[JSet[String]], userGroups))
invokeAs[Unit](req, "setUserRoles", (classOf[JSet[String]], roles))
getRolesFromUserAndGroupsMethod.zip(setUserRolesMethod).foreach {
case (getMethod, setMethod) =>
val roles = getMethod.invoke[JSet[String]](SparkRangerAdminPlugin, userName, userGroups)
setMethod.invoke[Unit](req, roles)
}
} catch {
case _: Exception =>
}
Expand All @@ -68,6 +70,26 @@ object AccessRequest {
req
}

private val getRolesFromUserAndGroupsMethod: Option[UnboundMethod] = {
try {
Some(ReflectUtils.getMethod(
SparkRangerAdminPlugin.getClass,
"getRolesFromUserAndGroups",
classOf[String],
classOf[JSet[String]]))
} catch {
case _: Exception => None
}
}

private val setUserRolesMethod: Option[UnboundMethod] = {
try {
Some(ReflectUtils.getMethod(classOf[AccessRequest], "setUserRoles", classOf[JSet[String]]))
} catch {
case _: Exception => None
}
}

private def getUserGroupsFromUgi(user: UserGroupInformation): JSet[String] = {
user.getGroupNames.toSet.asJava
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ trait RuleHelper extends Rule[LogicalPlan] {
plan.withNewChildren(newChildren)
}

def ugi: UserGroupInformation = AuthZUtils.getAuthzUgi(spark.sparkContext)
lazy val ugi: UserGroupInformation = AuthZUtils.getAuthzUgi(spark.sparkContext)

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import scala.language.existentials
import scala.reflect.ClassTag
import scala.util.Try

import org.apache.kyuubi.util.reflect.DynMethods.UnboundMethod

object ReflectUtils {

/**
Expand Down Expand Up @@ -64,6 +66,27 @@ object ReflectUtils {
}
}

/**
* Get a method with the given name and argument classes from the given class.
* @param clz the class to get the method from
* @param methodName the method name from declared field names
* @param argClasses the classes of the arguments
* @return an unbound method that can be invoked later
*/
def getMethod(clz: Class[_], methodName: String, argClasses: Class[_]*): UnboundMethod = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not simplify much code, can we inline it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not simplify much code, can we inline it?

Makes sense, changed.

try {
DynMethods.builder(methodName)
.hiddenImpl(clz, argClasses: _*)
.impl(clz, argClasses: _*)
.buildChecked
} catch {
case e: Exception =>
throw new RuntimeException(
s"$clz does not have $methodName${argClasses.map(_.getName).mkString("(", ", ", ")")}",
e)
}
}

/**
* Invoke a method with the given name and arguments on the given target object.
* @param target the target object
Expand Down
Loading