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


##########
extensions/spark/kyuubi-spark-authz/src/main/scala/org/apache/kyuubi/plugin/spark/authz/ranger/SparkRangerAdminPlugin.scala:
##########
@@ -17,114 +17,354 @@
 
 package org.apache.kyuubi.plugin.spark.authz.ranger
 
+import java.util.Properties
+
 import scala.collection.JavaConverters._
 import scala.collection.mutable.{ArrayBuffer, LinkedHashMap}
 
+import org.apache.hadoop.conf.Configuration
 import org.apache.hadoop.util.ShutdownHookManager
-import org.apache.ranger.plugin.policyengine.RangerAccessRequest
-import org.apache.ranger.plugin.service.RangerBasePlugin
+import org.apache.ranger.authz.api.{RangerAuthorizer, RangerAuthorizerFactory}
+import org.apache.ranger.authz.model._
+import 
org.apache.ranger.authz.model.RangerAccessContext.CONTEXT_INFO_CLUSTER_NAME
+import org.apache.ranger.authz.model.RangerAuthzResult.AccessDecision
 import org.slf4j.LoggerFactory
 
 import org.apache.kyuubi.plugin.spark.authz.AccessControlException
+import org.apache.kyuubi.plugin.spark.authz.ObjectType
+import org.apache.kyuubi.plugin.spark.authz.ranger.AccessType._
 
-object SparkRangerAdminPlugin extends RangerBasePlugin("spark", "sparkSql")
-  with RangerConfigProvider {
+/**
+ * The entry point of Ranger authorization for Spark SQL, which delegates
+ * authorization requests to a Ranger authorizer:
+ * <ul>
+ *   <li>org.apache.ranger.authz.remote.RangerRemoteAuthorizer (default):
+ *       sends requests to a Ranger PDP server via REST APIs, with a thin 
client
+ *       that does not download policies to the client side.</li>
+ *   <li>org.apache.ranger.authz.embedded.RangerEmbeddedAuthorizer:
+ *       evaluates requests locally against the policies pulled from
+ *       the Ranger admin server, as the previous Ranger plugin did.</li>
+ * </ul>
+ *
+ * The authorizer implementation is selected by `ranger.authorizer.impl.class`.
+ */
+object SparkRangerAdminPlugin {
   final private val LOG = LoggerFactory.getLogger(getClass)
 
+  /**
+   * The service type of the Spark SQL service definition registered in
+   * Ranger admin.
+   */
+  final val SERVICE_TYPE: String = "spark"
+
+  final private val APP_TYPE: String = "sparkSql"
+
+  final private val KEY_IMPL_CLASS = "ranger.authorizer.impl.class"
+  final private val KEY_PDP_URL = "ranger.authz.remote.pdp.url"
+  final private val KEY_SERVICE_NAME = 
s"ranger.plugin.$SERVICE_TYPE.service.name"
+  final private val KEY_CLUSTER_NAME = 
s"ranger.plugin.$SERVICE_TYPE.access.cluster.name"
+  final private val KEY_AUTHORIZE_IN_SINGLE_CALL =
+    s"ranger.plugin.$SERVICE_TYPE.authorize.in.single.call"
+
+  final private val REMOTE_AUTHORIZER_CLASS: String =
+    classOf[org.apache.ranger.authz.remote.RangerRemoteAuthorizer].getName
+
+  /**
+   * The security-critical configurations which are read from the Ranger 
configuration
+   * resources only: JVM system properties can neither set nor override them, 
so a tenant
+   * who can control the JVM options of the engine process cannot point the 
authorization
+   * to a server they control or bypass the policies.
+   */
+  final private val ADMIN_ONLY_KEYS: Seq[String] =
+    Seq(KEY_IMPL_CLASS, KEY_PDP_URL, KEY_SERVICE_NAME)
+
+  final private val ADMIN_ONLY_KEY_PREFIXES: Seq[String] = Seq(
+    "ranger.authz.remote.authn.",
+    "ranger.authz.remote.header.",
+    "ranger.authz.remote.ssl.")
+
+  final private val CONFIG_PREFIXES = Seq("ranger.", "xasecure.")
+
+  final private val CONFIG_RESOURCES = Seq(
+    s"ranger-$SERVICE_TYPE-security.xml",
+    s"ranger-$SERVICE_TYPE-audit.xml")
+
+  private var authorizer: RangerAuthorizer = null
+
+  /**
+   * The Ranger configurations loaded from the Hadoop configuration resources,
+   * e.g. ranger-spark-security.xml.
+   */
+  private[ranger] var config: Configuration = null
+
   /**
    * For a Spark SQL query, it may contain 0 or more privilege objects to 
verify, e.g. a typical
    * JOIN operator may have two tables and their columns to verify.
    *
    * This configuration controls whether to verify the privilege objects in 
single call or
    * to verify them one by one.
    */
-  def authorizeInSingleCall: Boolean = getRangerConf.getBoolean(
-    s"ranger.plugin.${getServiceType}.authorize.in.single.call",
-    false)
+  def authorizeInSingleCall: Boolean = 
config.getBoolean(KEY_AUTHORIZE_IN_SINGLE_CALL, false)
 
-  /**
-   * This configuration controls whether to override user's usergroups
-   * by the mapping fetched from Ranger's UserStore.
-   *
-   * It relies on Ranger's UserStore is a feature supported since Ranger 2.1.
-   *
-   * If true, user bound usergroups will be looked up in in Ranger's UserStore
-   * and the usergroups of AccessRequest is overriden.
-   *
-   * Please make sure configs in Ranger set properly:
-   * 1. set `ranger.plugin.spark.enable.implicit.userstore.enricher` to true
-   * 2. set cache path for UserStore in `ranger.plugin.hive.policy.cache.dir`
-   * 3. at least one condition of policies containing scripts, e.g. 
{{USER.attr}} in row-filter
-   */
-  def useUserGroupsFromUserStoreEnabled: Boolean = getRangerConf.getBoolean(
-    s"ranger.plugin.$getServiceType.use.usergroups.from.userstore.enabled",
-    false)
+  def getServiceType: String = SERVICE_TYPE
+
+  private def serviceName: String = config.get(KEY_SERVICE_NAME)
 
   /**
-   * plugin initialization
+   * authorizer initialization
    * with cleanup shutdown hook registered
    */
   def initialize(): Unit = {
-    this.init()
-    registerCleanupShutdownHook(this)
+    ensureConfig()
+    val props = new Properties
+    config.iterator.asScala
+      .filter(entry => CONFIG_PREFIXES.exists(entry.getKey.startsWith))
+      .foreach(entry => props.put(entry.getKey, entry.getValue))
+    System.getProperties.asScala
+      .filter { case (key, _) =>
+        CONFIG_PREFIXES.exists(key.startsWith) && !isAdminOnlyKey(key)
+      }
+      .foreach { case (key, value) => props.put(key, value) }
+    validateAdminOnlyKeys(props)
+    initialize(props)
+  }
+
+  private def isAdminOnlyKey(key: String): Boolean =
+    ADMIN_ONLY_KEYS.contains(key) || 
ADMIN_ONLY_KEY_PREFIXES.exists(key.startsWith)
+
+  /**
+   * Fails fast when the Ranger configuration resources lack a configuration 
the
+   * authorizer needs: the authorizer implementation defaults to the Ranger 
PDP mode,
+   * which requires the Ranger PDP server address. The configuration cannot 
fall back
+   * to JVM system properties, which tenant users may control.
+   */
+  private def validateAdminOnlyKeys(props: Properties): Unit = {
+    val implClass = props.getProperty(KEY_IMPL_CLASS, REMOTE_AUTHORIZER_CLASS)

Review Comment:
   Fixed by 
[c3bc113](https://github.com/apache/kyuubi/pull/7437/commits/c3bc113ba33063ae5404167773c7349e999e23e5)



##########
docs/security/authorization/spark/install.md:
##########
@@ -37,8 +37,75 @@ Use either the shaded jar `kyuubi-spark-authz-shaded_*.jar` 
or the `kyuubi-spark
 
 ## Configure
 
+### Authorizer Modes
+
+The plugin supports two authorizer modes, selected by 
`ranger.authorizer.impl.class`:
+
+- `org.apache.ranger.authz.remote.RangerRemoteAuthorizer` (default) — Ranger 
PDP mode.
+  Authorization requests are sent to the Ranger PDP server via REST APIs. The 
plugin is a thin
+  client: policies are not downloaded to the client side, and access audits 
are recorded by
+  the PDP server.
+- `org.apache.ranger.authz.embedded.RangerEmbeddedAuthorizer` — embedded mode, 
working as the
+  previous Ranger plugin did: policies are pulled from the Ranger admin server 
and access
+  requests are evaluated locally on the Spark driver side.
+
+:::{warning}
+The security-critical configurations — the authorizer implementation
+(`ranger.authorizer.impl.class`), the Ranger PDP server address
+(`ranger.authz.remote.pdp.url`), the Ranger service name
+(`ranger.plugin.spark.service.name`) and the Ranger PDP client authentication 
and
+encryption settings (`ranger.authz.remote.authn.*`, 
`ranger.authz.remote.ssl.*`,
+`ranger.authz.remote.header.*`) — are read from `ranger-spark-security.xml` 
only:
+JVM system properties can neither set nor override them, and the plugin fails 
to
+initialize when the Ranger PDP server address is missing for the Ranger PDP 
mode.
+Other configurations with the `ranger.` or `xasecure.` prefix can still be 
overridden
+by JVM system properties, so make sure tenant users cannot control the JVM 
options of
+the engine process (e.g. `spark.driver.extraJavaOptions`).
+:::
+
+#### Ranger PDP mode (default)
+
+- Create `ranger-spark-security.xml` in `$SPARK_HOME/conf` and add the 
following configurations
+  for pointing to the right Ranger PDP server.
+
+```xml
+<configuration>
+    <property>
+        <name>ranger.plugin.spark.service.name</name>
+        <value>a ranger service name, e.g. a ranger hive service name</value>
+    </property>
+
+    <property>
+        <name>ranger.authz.remote.pdp.url</name>
+        <value>ranger pdp server address like 
https://ranger-pdp.org:8585</value>
+    </property>
+
+</configuration>
+```
+
+The PDP client supports authentication and encryption settings, configured with
+`ranger.authz.remote.authn.type` (`header`, `jwt` or `kerberos`),
+`ranger.authz.remote.authn.*`, `ranger.authz.remote.ssl.*` and
+`ranger.authz.remote.header.*` properties. Refer to the
+[Ranger client 
libraries](https://cwiki.apache.org/confluence/display/RANGER/Ranger+Client+Libraries)
+for the full list.
+
+#### Embedded mode
+
+Set the authorizer implementation to the embedded one in 
`ranger-spark-security.xml`:
+
+```xml
+<property>
+    <name>ranger.authorizer.impl.class</name>
+    <value>org.apache.ranger.authz.embedded.RangerEmbeddedAuthorizer</value>
+</property>
+```
+
 ### Settings for Connecting Ranger Admin
 
+The settings below apply to the embedded mode, where the plugin pulls policies 
from

Review Comment:
   Fixed by 
[c3bc113](https://github.com/apache/kyuubi/pull/7437/commits/c3bc113ba33063ae5404167773c7349e999e23e5)



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