aajisaka commented on code in PR #7437:
URL: https://github.com/apache/kyuubi/pull/7437#discussion_r4631626093


##########
extensions/spark/kyuubi-spark-authz/src/main/scala/org/apache/kyuubi/plugin/spark/authz/ranger/AccessResource.scala:
##########
@@ -18,27 +18,167 @@
 package org.apache.kyuubi.plugin.spark.authz.ranger
 
 import java.io.File
-import java.util
 
-import scala.language.implicitConversions
+import scala.collection.JavaConverters._
 
-import org.apache.ranger.plugin.policyengine.RangerAccessResourceImpl
+import org.apache.commons.lang3.StringUtils
+import org.apache.ranger.authz.model.RangerResourceInfo
 
-import org.apache.kyuubi.plugin.spark.authz.{ObjectType, PrivilegeObject}
+import org.apache.kyuubi.plugin.spark.authz.{AccessControlException, 
ObjectType, PrivilegeObject}
 import org.apache.kyuubi.plugin.spark.authz.ObjectType._
 import org.apache.kyuubi.plugin.spark.authz.OperationType.OperationType
 
-class AccessResource private (val objectType: ObjectType, val catalog: 
Option[String])
-  extends RangerAccessResourceImpl {
-  implicit def asString(obj: Object): String = if (obj != null) 
obj.asInstanceOf[String] else null
-  def getDatabase: String = getValue("database")
-  def getUdf: String = getValue("udf")
-  def getTable: String = getValue("table")
-  def getColumn: String = getValue("column")
+/**
+ * A privilege object to authorize, which is converted to a Ranger resource
+ * (e.g. "table:default/src" or "column:default/src/id") in requests to
+ * the Ranger authorizer.
+ *
+ * @param objectType the type of the object
+ * @param database   the database name, or null if not applicable
+ * @param table      the table name, or null if not applicable
+ * @param column     the column names joined by comma, or null if not 
applicable
+ * @param udf        the function name, or null if not applicable
+ * @param uri        the uri path, or null if not applicable
+ * @param owner      the owner of the object, if any
+ * @param catalog    the catalog name, if any
+ */
+case class AccessResource private[ranger] (
+    objectType: ObjectType,
+    database: String,
+    table: String,
+    column: String,
+    udf: String,
+    uri: String,
+    owner: Option[String],
+    catalog: Option[String]) {
+
+  def getDatabase: String = database
+  def getUdf: String = udf
+  def getTable: String = table
+  def getColumn: String = column
+
   def getColumns: Seq[String] = {
-    val columnStr = getColumn
-    if (columnStr == null) Nil else columnStr.split(",").filter(_.nonEmpty)
+    if (column == null) Nil else column.split(",").filter(_.nonEmpty)
   }
+
+  def getOwnerUser: String = owner.orNull
+
+  /**
+   * The path-like representation of this resource used in error messages,
+   * e.g. "default/src" for a table, "default/src/id" for a column.
+   */
+  def getAsString: String = objectType match {
+    case COLUMN =>
+      Seq(database, table, column).filter(_ != null).mkString("/")
+    case FUNCTION =>
+      Seq(database, udf).filter(_ != null).mkString("/")
+    case URI =>
+      // the uri is matched against both the exact path and the path with a
+      // trailing slash, as the legacy plugin did
+      val path = Option(uri).map(_.stripSuffix(File.separator)).getOrElse("")
+      s"[$path, $path/]"
+    case _ =>
+      Seq(database, table).filter(_ != null).mkString("/")
+  }
+
+  private[ranger] def toResourceInfos: Seq[RangerResourceInfo] = {
+    val attributes = owner.map(o => 
java.util.Collections.singletonMap("OWNER", o: AnyRef)).orNull
+    objectType match {
+      case DATABASE =>
+        Seq(new RangerResourceInfo(
+          s"database:${requireRrnComponent(database, "database")}",
+          null,
+          null,
+          attributes))
+      case FUNCTION =>
+        // An unqualified function reference (e.g. a built-in or temporary 
function) has no
+        // database. The legacy plugin left the database blank, and blank 
values in the legacy
+        // resource matched the wildcard values in policies, so keep the 
wildcard marker for
+        // the blank database. Blank names in other resource levels indicate a 
broken command
+        // extraction, which matched no policy in the legacy resource, so deny 
them.
+        val db = if (StringUtils.isBlank(database)) "*" else 
escapeRrnMetaChars(database)
+        Seq(new RangerResourceInfo(
+          s"udf:$db/${requireRrnComponent(udf, "udf")}",
+          null,
+          null,
+          attributes))
+      case COLUMN =>
+        val columns = getColumns
+        // all the column requests need the database and the table
+        val parent =
+          s"column:${requireRrnComponent(database, "database")}" +
+            s"/${requireRrnComponent(table, "table")}"
+        if (columns.length == 1) {
+          Seq(new RangerResourceInfo(
+            s"$parent/${requireRrnComponent(columns.head, "column")}",
+            null,
+            null,
+            attributes))
+        } else if (columns.isEmpty) {
+          Seq(new RangerResourceInfo(parent, null, null, attributes))
+        } else {
+          val subResources = columns
+            .map(col => s"column:${requireRrnComponent(col, "column")}")
+            .toSet.asJava
+          Seq(new RangerResourceInfo(parent, subResources, null, attributes))
+        }
+      case URI =>
+        // Url policies may be written with or without a trailing slash, and 
the legacy
+        // plugin matched a uri against both the exact path and the path with 
a trailing
+        // slash. The RRN request carries a single resource value set, so the 
two variants
+        // are returned and authorized as alternatives by separate requests.
+        val path = requireRrnComponent(
+          Option(uri).map(_.stripSuffix(File.separator)).orNull,

Review Comment:
   Fixed in 5bcd38159. The root URI is kept as a single `url:\/` variant, and 
its error representation is now `[/]` instead of being reduced to an empty path.



-- 
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]

Reply via email to