Created on 05-04-2017 08:42 PM
Security is one of fundamental features for enterprise adoption. Specifically, for SQL users, row/column-level access control is important. However, when a cluster is used as a data warehouse accessed by various user groups via different ways like Apache Spark™ 1.6/2.1 and Apache Hive, it is difficult to guarantee access control in a consistent way. In this article, we show the use of Apache Ranger™ to manage access control policy for Spark SQL. This enhancement is done by integrating Apache Ranger and Apache Spark via `spark-llap`. We also show how to provide finer grained access control(row/column level filtering and column masking) to Apache Spark.
For all use cases, make it sure that the permission of Hive warehouse is 700. It means normal users are unable to access the secured tables. In addition, make sure that `hive.warehouse.subdir.inherit.perms=true`. With this, newly created tables will inherit the permission, 700, by default.
$ hadoop fs -ls /apps/hive Found 1 items drwx------ - hive hdfs 0 2017-07-10 17:04 /apps/hive/warehouse
Hive Interactive Query is needed to be enabled.
Hive Ranger Plugin is needed to be enabled.
In this article, we will use two user principals, `billing` and `datascience`. `billing` principal can access all rows and columns while `datascience` principal can access some of filtered and masked data. You can use `hive` and `spark` principal instead.
First of all, find the following five values from Hive configuration via Ambari for your cluster.
Set the following parameter.
*
Set the following two parameters.
hive.llap.task.keytab.file=/etc/security/keytabs/hive.service.keytab hive.llap.task.principal=hive/_HOST@EXAMPLE.COM
For Spark2 shells (spark-shell, pyspark, sparkR, spark-sql) and applications, setup the following configurations via Ambari and restart required services.
spark.hadoop.hive.llap.daemon.service.hosts=the value of hive.llap.daemon.service.hosts spark.hadoop.hive.zookeeper.quorum=The value of hive.zookeeper.quorum spark.sql.hive.hiveserver2.jdbc.url=jdbc:hive2://YourHiveServer2HostName:10500/ spark.sql.hive.hiveserver2.jdbc.url.principal=The value of hive.server2.authentication.kerberos.principal
For Spark2 Thrift Server, setup the following configurations via Ambari and restart required services.
--packages com.hortonworks.spark:spark-llap-assembly_2.11:1.1.3-2.1 --repositories http://repo.hortonworks.com/content/groups/public --conf spark.sql.hive.llap=true
Note that this is one line.
spark.hadoop.hive.llap.daemon.service.hosts=the value of hive.llap.daemon.service.hosts spark.hadoop.hive.zookeeper.quorum=The value of hive.zookeeper.quorum spark.sql.hive.hiveserver2.jdbc.url=jdbc:hive2://YourHiveServer2HostName:10500/;hive.server2.proxy.user=${user} spark.sql.hive.hiveserver2.jdbc.url.principal=The value of hive.server2.authentication.kerberos.principal
Since normal users are unable to access Hive databases and tables until now due to HDFS permissions, use Beeline CLI with `hive` principal to prepare a database and a table for users who will use Spark-LLAP. Please note that the following is an example schema setup for demo scenario in this article. In case of audit-only scenarios, you don’t need to make new databases and tables. Instead, you can make all existing databases and tables accessible for all users.
$ kdestroy $ kinit -kt /etc/security/keytabs/hive.service.keytab hive/`hostname -f`@EXAMPLE.COM $ beeline -u "jdbc:hive2://YourHiveServerHost:10500/default;principal=hive/_HOST@EXAMPLE.COM" -e "CREATE DATABASE db_spark; USE db_spark; CREATE TABLE t_spark(name STRING, gender STRING); INSERT INTO t_spark VALUES ('Barack Obama', 'M'), ('Michelle Obama', 'F'), ('Hillary Clinton', 'F'), ('Donald Trump', 'M');"
To use a fine-grained access control, you need to setup Apache Ranger™ policies which rule Spark and Hive together seamlessly as a single control center.
Open `Ranger Admin UI`. The default login information is `admin/admin`. After login, `Access Manager` page shows nine service managers. The following screenshot means there exists HDFS / Hive / YARN policies. Since Spark shares the same policies with Hive, visit `Hive` among service managers.
In Hive Service Manager page, there are three tabs corresponding three types of policies: `Access`, `Masking`, and `Row Level Filter`.
Let’s make some policies for a user to access some rows and columns.
For examples,
Name | Table | Column | Select User | Permissions |
spark_access | t_spark | * | billing | Select |
spark_access | t_spark | * | datascience | Select |
Name | Table | Column | Select User | Access Types | Select Masking Option |
spark_mask | t_spark | name | datascience | Select | partial mask:'show first 4' |
Name | Table | Access Types | Row Level Filter |
spark_filter | t_spark | Select | gender='M' |
In HDFS Ranger plugin, add a rule `spark_tmp` to allow all accesses on `/tmp`.
A user can access the Spark Thrift Server via beeline or Apache Zeppelin™. First, based on the kerberos principal, the user can see only the accessible data.
$ kdestroy $ kinit billing/billing@EXAMPLE.COM $ beeline -u "jdbc:hive2://YourSparkThriftServer:10016/db_spark;principal=hive/_HOST@EXAMPLE.COM" -e 'select * from db_spark.t_spark' +------------------+---------+--+ | name | gender | +------------------+---------+--+ | Barack Obama | M | | Michelle Obama | F | | Hillary Clinton | F | | Donald Trump | M | +------------------+---------+--+ $ kdestroy $ kinit datascience/datascience@EXAMPLE.COM $ beeline -u "jdbc:hive2://YourSparkThriftServer:10016/db_spark;principal=hive/_HOST@EXAMPLE.COM" -e 'select * from db_spark.t_spark' +---------------+---------+--+ | name | gender | +---------------+---------+--+ | Baraxx xxxxx | M | | Donaxx xxxxx | M | +---------------+---------+--+
Second, in case of Zeppelin, a proxy user name is used. You can see this youtube demo to see how Zeppelin works. The following example illustrates the usage of proxy user name with beeline. Zeppelin does the same thing via JDBC under the hood.
$ kdestroy $ kinit -kt /etc/security/keytabs/hive.service.keytab hive/`hostname -f`@EXAMPLE.COM $ beeline -u "jdbc:hive2://YourSparkThriftServerHost:10016/db_spark;principal=hive/_HOST@EXAMPLE.COM;hive.server2.proxy.user=billing" -e "select * from db_spark.t_spark" +------------------+---------+--+ | name | gender | +------------------+---------+--+ | Barack Obama | M | | Michelle Obama | F | | Hillary Clinton | F | | Donald Trump | M | +------------------+---------+--+ $ beeline -u "jdbc:hive2://YourSparkThriftServerHost:10016/db_spark;principal=hive/_HOST@EXAMPLE.COM;hive.server2.proxy.user=datascience" -e "select * from db_spark.t_spark" +---------------+---------+--+ | name | gender | +---------------+---------+--+ | Baraxx xxxxx | M | | Donaxx xxxxx | M | +---------------+---------+--+
A user can run `spark-shell` or `pyspark` like the followings. Please note that the user can access own data sources in addition to the secure data source provided by `spark-llap`. For the next example, log in as the user `spark`.
$ kdestroy $ kinit billing/billing@EXAMPLE.COM $ SPARK_MAJOR_VERSION=2 spark-shell --packages com.hortonworks.spark:spark-llap-assembly_2.11:1.1.3-2.1 --repositories http://repo.hortonworks.com/content/groups/public --conf spark.sql.hive.llap=true scala> sql("select * from db_spark.t_spark").show +---------------+------+ | name|gender| +---------------+------+ | Barack Obama| M| | Michelle Obama| F| |Hillary Clinton| F| | Donald Trump| M| +---------------+------+ $ kdestroy $ kinit datascience/datascience@EXAMPLE.COM $ SPARK_MAJOR_VERSION=2 spark-shell --packages com.hortonworks.spark:spark-llap-assembly_2.11:1.1.3-2.1 --repositories http://repo.hortonworks.com/content/groups/public --conf spark.sql.hive.llap=true scala> sql("select * from db_spark.t_spark").show +------------+------+ | name|gender| +------------+------+ |Baraxx xxxxx| M| |Donaxx xxxxx| M| +------------+------+
$ kdestroy $ kinit billing/billing@EXAMPLE.COM $ SPARK_MAJOR_VERSION=2 pyspark --packages com.hortonworks.spark:spark-llap-assembly_2.11:1.1.3-2.1 --repositories http://repo.hortonworks.com/content/groups/public --conf spark.sql.hive.llap=true >>> sql("select * from db_spark.t_spark").show() +---------------+------+ | name|gender| +---------------+------+ | Barack Obama| M| | Michelle Obama| F| |Hillary Clinton| F| | Donald Trump| M| +---------------+------+ $ kdestroy $ kinit datascience/datascience@EXAMPLE.COM $ SPARK_MAJOR_VERSION=2 pyspark --packages com.hortonworks.spark:spark-llap-assembly_2.11:1.1.3-2.1 --repositories http://repo.hortonworks.com/content/groups/public --conf spark.sql.hive.llap=true >>> sql("select * from db_spark.t_spark").show() +------------+------+ | name|gender| +------------+------+ |Baraxx xxxxx| M| |Donaxx xxxxx| M| +------------+------+
$ kdestroy $ kinit billing/billing@EXAMPLE.COM $ SPARK_MAJOR_VERSION=2 sparkR --packages com.hortonworks.spark:spark-llap-assembly_2.11:1.1.3-2.1 --repositories http://repo.hortonworks.com/content/groups/public --conf spark.sql.hive.llap=true > head(sql("select * from db_spark.t_spark")) name gender 1 Barack Obama M 2 Michelle Obama F 3 Hillary Clinton F 4 Donald Trump M $ kdestroy $ kinit datascience/datascience@EXAMPLE.COM $ SPARK_MAJOR_VERSION=2 sparkR --packages com.hortonworks.spark:spark-llap-assembly_2.11:1.1.3-2.1 --repositories http://repo.hortonworks.com/content/groups/public --conf spark.sql.hive.llap=true > head(sql("select * from db_spark.t_spark")) name gender 1 Baraxx xxxxx M 2 Donaxx xxxxx M
$ kdestroy $ kinit billing/billing@EXAMPLE.COM $ SPARK_MAJOR_VERSION=2 spark-sql --packages com.hortonworks.spark:spark-llap-assembly_2.11:1.1.3-2.1 --repositories http://repo.hortonworks.com/content/groups/public --conf spark.sql.hive.llap=true spark-sql> select * from db_spark.t_spark; Barack ObamaM Michelle ObamaF Hillary ClintonF Donald TrumpM $ kdestroy $ kinit datascience/datascience@EXAMPLE.COM $ SPARK_MAJOR_VERSION=2 spark-sql --packages com.hortonworks.spark:spark-llap-assembly_2.11:1.1.3-2.1 --repositories http://repo.hortonworks.com/content/groups/public --conf spark.sql.hive.llap=true spark-sql> select * from db_spark.t_spark Baraxx xxxxxM Donaxx xxxxxM
A user can submit his Spark job like the following. Like `spark-shell` scenario, the user can access own data sources in addition to the secure data source provided by `spark-llap`.
from pyspark.sql import SparkSession spark = SparkSession \ .builder \ .appName("Spark LLAP SQL Python") \ .enableHiveSupport() \ .getOrCreate() spark.sql("show databases").show() spark.sql("select * from db_spark.t_spark").show() spark.stop()
Launch the app with YARN client mode.
SPARK_MAJOR_VERSION=2 spark-submit --packages com.hortonworks.spark:spark-llap-assembly_2.11:1.1.3-2.1 --repositories http://repo.hortonworks.com/content/groups/public --master yarn --deploy-mode client --conf spark.sql.hive.llap=true spark_llap_sql.py
YARN cluster mode will be supported in next HDP release. You can download the examples at spark_llap_sql.py, too.
Please refer https://github.com/hortonworks-spark/spark-llap/wiki/7.-Support-Matrix
Known Issues
If you see warning messages in Spark shells, you can turn off via `sc.setLogLevel` or `conf/log4j.properties`.
scala> sql("select * from db_spark.t_spark").show ... 17/03/09 22:06:26 WARN TaskSetManager: Stage 5 contains a task of very large size (248 KB). The maximum recommended task size is 100 KB. 17/03/09 22:06:27 WARN LlapProtocolClientProxy: RequestManager shutdown with error java.util.concurrent.CancellationException ... scala> sc.setLogLevel("ERROR") scala> sql("select * from db_spark.t_spark").show ...
Created on 02-11-2018 09:57 PM
Hi, @Mai Nakagawa
You are using a mismatched jar file as you saw in your first exception message.
because LLAP or Hive classes are not found.
This document is about HDP 2.6.1 using Spark 2.1.1.
Since HDP 2.6.3, `spark-llap` for Spark 2.2 is built-in. Please use it.
$ ls -al /usr/hdp/2.6.3.0-235/spark_llap/spark-llap-assembly-1.0.0.2.6.3.0-235.jar
-rw-r--r-- 1 root root 61306448 Oct 30 02:39 /usr/hdp/2.6.3.0-235/spark_llap/spark-llap-assembly-1.0.0.2.6.3.0-235.jar
Created on 02-13-2018 01:29 AM
Thank you @Dongjoon Hyun! Confirmed it works in HDP 2.6.3 by replacing the jar file with spark-llap-assembly-1.0.0.2.6.3.0-235.jar
Created on 02-13-2018 04:45 AM
Thank you for confirming.
Created on 05-31-2019 08:38 AM
Hello,
My ENV is HDP3.0, spark-2.11.2.3.1,hive3.0, kerberos enabled, I followed as mentioned above, and connected the sparkthriftserver to execute sql: explain select * from tb1,finally got the results:
Physical plan:HiveTableScan,HiveTableRelation'''',org.apche.hadoop,hive.serde2.lazy.lazySimpleSerDe
instead of llapRealtion. it seems that llap does not work.
ps I use the package spark-llap_2-11-1.0.2.1-assembly.jar.