You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@kyuubi.apache.org by GitBox <gi...@apache.org> on 2022/12/06 12:23:05 UTC

[GitHub] [incubator-kyuubi] ulysses-you commented on a diff in pull request #3904: [Spark] New Authz Plan Serde Layer

ulysses-you commented on code in PR #3904:
URL: https://github.com/apache/incubator-kyuubi/pull/3904#discussion_r1040882683


##########
extensions/spark/kyuubi-spark-authz/src/main/scala/org/apache/kyuubi/plugin/spark/authz/serde/Descriptor.scala:
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.plugin.spark.authz.serde
+
+import org.apache.spark.SPARK_VERSION
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+
+import org.apache.kyuubi.plugin.spark.authz.PrivilegeObjectActionType
+import org.apache.kyuubi.plugin.spark.authz.PrivilegeObjectActionType.PrivilegeObjectActionType
+import org.apache.kyuubi.plugin.spark.authz.serde.ActionTypeExtractor.actionTypeExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.ColumnExtractor.columnExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.DatabaseExtractor.dbExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.FunctionExtractor.functionExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.FunctionType.FunctionType
+import org.apache.kyuubi.plugin.spark.authz.serde.FunctionTypeExtractor.functionTypeExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.QueryExtractor.queryExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.TableExtractor.tableExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.TableType.TableType
+import org.apache.kyuubi.plugin.spark.authz.serde.TableTypeExtractor.tableTypeExtractors
+import org.apache.kyuubi.plugin.spark.authz.util.AuthZUtils._
+
+/**
+ * A database object(such as database, table, function) descriptor describes its name and getter

Review Comment:
   nit: `A object`



##########
extensions/spark/kyuubi-spark-authz/src/main/scala/org/apache/kyuubi/plugin/spark/authz/serde/Descriptor.scala:
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.plugin.spark.authz.serde
+
+import org.apache.spark.SPARK_VERSION
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+
+import org.apache.kyuubi.plugin.spark.authz.PrivilegeObjectActionType
+import org.apache.kyuubi.plugin.spark.authz.PrivilegeObjectActionType.PrivilegeObjectActionType
+import org.apache.kyuubi.plugin.spark.authz.serde.ActionTypeExtractor.actionTypeExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.ColumnExtractor.columnExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.DatabaseExtractor.dbExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.FunctionExtractor.functionExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.FunctionType.FunctionType
+import org.apache.kyuubi.plugin.spark.authz.serde.FunctionTypeExtractor.functionTypeExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.QueryExtractor.queryExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.TableExtractor.tableExtractors
+import org.apache.kyuubi.plugin.spark.authz.serde.TableType.TableType
+import org.apache.kyuubi.plugin.spark.authz.serde.TableTypeExtractor.tableTypeExtractors
+import org.apache.kyuubi.plugin.spark.authz.util.AuthZUtils._
+
+/**
+ * A database object(such as database, table, function) descriptor describes its name and getter
+ * in/from another object(such as a spark sql command).
+ */
+sealed trait Descriptor {
+
+  /**
+   * Describes the field name, such as a table field name in the spark logical plan.
+   *
+   * @return the database object field name
+   */
+  def fieldName: String
+
+  /**
+   * The extractor/getter classname for the field
+   */
+  def fieldExtractor: String
+
+  def getValue(v: AnyRef): AnyRef
+
+  final def error(v: AnyRef, e: Throwable): String = {
+    val resourceName = getClass.getSimpleName.stripSuffix("Desc")
+    val objectClass = v.getClass.getName
+    s"[Spark$SPARK_VERSION] failed to get $resourceName from $objectClass by" +
+      s" $fieldExtractor/$fieldName, due to ${e.getMessage}"
+  }
+}
+
+case class ColumnDesc(
+    fieldName: String,
+    fieldExtractor: String) extends Descriptor {
+
+  override def getValue(v: AnyRef): Seq[String] = {
+    val columnsVal = invoke(v, fieldName)
+    val columnExtractor = columnExtractors(fieldExtractor)
+    columnExtractor(columnsVal)
+  }
+}
+
+case class DatabaseDesc(
+    fieldName: String,
+    fieldExtractor: String,
+    isInput: Boolean = false) extends Descriptor {
+  override def getValue(v: AnyRef): String = {
+    val databaseVal = invoke(v, fieldName)
+    val databaseExtractor = dbExtractors(fieldExtractor)
+    databaseExtractor(databaseVal)
+  }
+}
+
+case class FunctionTypeDesc(
+    fieldName: String,
+    fieldExtractor: String,
+    skipTypes: Seq[String]) extends Descriptor {
+
+  override def getValue(v: AnyRef): FunctionType = {
+    getValue(v, SparkSession.active)
+  }
+
+  def getValue(v: AnyRef, spark: SparkSession): FunctionType = {
+    val functionTypeVal = invoke(v, fieldName)
+    val functionTypeExtractor = functionTypeExtractors(fieldExtractor)
+    functionTypeExtractor(functionTypeVal, spark)
+  }
+
+  def skip(v: AnyRef, spark: SparkSession): Boolean = {
+    skipTypes.exists(skipType => getValue(v, spark) == FunctionType.withName(skipType))
+  }
+}
+
+case class FunctionDesc(
+    fieldName: String,
+    fieldExtractor: String,
+    databaseDesc: Option[DatabaseDesc] = None,
+    functionTypeDesc: Option[FunctionTypeDesc] = None,
+    isInput: Boolean = false) extends Descriptor {
+  override def getValue(v: AnyRef): Function = {
+    val functionVal = invoke(v, fieldName)
+    val functionExtractor = functionExtractors(fieldExtractor)
+    var function = functionExtractor(functionVal)
+    if (function.database.isEmpty) {
+      function = function.copy(database = databaseDesc.map(_.getValue(v)))

Review Comment:
   why `TableDesc. getValue` does not need fill database ?



-- 
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: notifications-unsubscribe@kyuubi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@kyuubi.apache.org
For additional commands, e-mail: notifications-help@kyuubi.apache.org