前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用命令行创建collection时Sentry给Solr赋权的问题

使用命令行创建collection时Sentry给Solr赋权的问题

作者头像
Fayson
发布2018-07-12 15:31:26
1.2K0
发布2018-07-12 15:31:26
举报
文章被收录于专栏:Hadoop实操Hadoop实操

温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。

Fayson的github: https://github.com/fayson/cdhproject

提示:代码块部分可以左右滑动查看噢

1.故障描述


Fayson在前面的文章介绍过《如何使用Sentry为Solr赋权》,但当时Fayson是在Hue中创建的collection,如果我们采用先创建schema的xml文件,然后通过命令行创建Solr的collection,使用Sentry赋权后,会出现权限不生效的情况。以下我们具体看看故障现象。我们依旧以《如何使用Sentry为Solr赋权》里的测试样例数据为例子,参考Hue中创建collection的方式来定义一个schema文件。

2.collection创建以及导入数据


1.首先准备一个8个字段的csv文件,一共10行,使用逗号分隔,用来导入Solr并实现全文索引。

注意:这个csv我们定义了文件头,一共8个字段,从field_1到field_8。

2.定义一个schema文件,一共8个字段,从field_1到field_8,其中field_1为主键。

<?xml version="1.0" encoding="UTF-8" ?>
<schema name="example" version="1.5">
<fields>
  <field name="field_1" type="string" indexed="true" stored="true" required="true" multiValued="false" />
  <field name="field_2" type="string" indexed="true" stored="true" />
  <field name="field_3" type="text_en" indexed="true" stored="true" />
  <field name="field_4" type="string" indexed="true" stored="true" />
  <field name="field_5" type="string" indexed="true" stored="true" />
  <field name="field_6" type="string" indexed="true" stored="true" />
  <field name="field_7" type="string" indexed="true" stored="true" />
  <field name="field_8" type="string" indexed="true" stored="true" />
<field name="_version_" type="long" indexed="true" stored="true"/>
</fields>
<uniqueKey>field_1</uniqueKey>
<types>
<!-- The StrField type is not analyzed, but indexed/stored verbatim.
               It supports doc values but in that case the field needs to be
     single-valued and either required or have a default value.
     -->
<fieldType name="string" class="solr.StrField" sortMissingLast="true"/>
<!--
               Default numeric field types. For faster range queries, consider the tint/tfloat/tlong/tdouble types.
These fields support doc values, but they require the field to be
single-valued and either be required or have a default value.
-->
<fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="float" class="solr.TrieFloatField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="text_en" class="solr.TextField" positionIncrementGap="100" /> 
</types>
</schema>

(可左右滑动)

3.准备创建collection的脚本,并创建collection

#!/bin/sh

ZK="cdh01.fayson.com"
COLLECTION="collection1"
BASE=`pwd`
SHARD=3
REPLICA=1
echo "create solr collection"
rm -rf tmp/*
solrctl --zk $ZK:2181/solr instancedir --generate tmp/${COLLECTION}_configs
cp conf/schema.xml tmp/${COLLECTION}_configs/conf/
solrctl --zk $ZK:2181/solr instancedir --create $COLLECTION tmp/${COLLECTION}_configs
solrctl --zk $ZK:2181/solr collection --create $COLLECTION -s $SHARD -r $REPLICA
solrctl --zk $ZK:2181/solr collection --list

(可左右滑动)

注意:因为我们开启了Sentry,在创建collection的时候我们使用的是solr用户的principle。否则是没有权限创建collection的。这里跟在Hive/Impala中使用Sentry时,使用hive作为管理员用户是相似的。

4.将准备好的csv文件导入到collection1.

curl --negotiate -u : 'http://cdh04.fayson.com:8983/solr/collection1/update/csv?commit=true' \
-H 'Content-Type: application/csv' \
--data-binary @/root/_fayson/data.csv

(可左右滑动)

5.查询collection1确认数据都已经导入成功。

curl --negotiate -u : "http://cdh04.fayson.com:8983/solr/collection1/query?q=*%3A*&wt=json&indent=true"

(可左右滑动)

3.故障重现


1.首先我们创建一个admin角色,并赋予所有权限,并对应到solr用户组。

solrctl sentry --create-role admin
solrctl sentry --add-role-group admin solr
solrctl sentry --grant-privilege admin 'collection=*->action=*'
solrctl sentry --grant-privilege admin 'config=*->action=*'
solrctl sentry --list-privileges admin

(可左右滑动)

注意:必须使用solr用户的principal。

2.我们在不给fayson用户组分配任何权限的情况下,使用fayson用户对collection1进行查询。

[root@cdh02 solr]# klist
Ticket cache: FILE:/tmp/krb5cc_0
Default principal: fayson@FAYSON.COM
Valid starting       Expires              Service principal
06/13/2018 21:35:08  06/14/2018 21:35:08  krbtgt/FAYSON.COM@FAYSON.COM
        renew until 06/20/2018 21:35:08
06/13/2018 21:35:17  06/14/2018 21:35:08  HTTP/cdh04.fayson.com@FAYSON.COM
        renew until 06/18/2018 21:35:17
[root@cdh02 solr]# curl --negotiate -u : "http://cdh04.fayson.com:8983/solr/collection1/query?q=*%3A*&wt=json&indent=true"

(可左右滑动)

可以查询出collection中的数据,说明Sentry的权限控制没有生效,故障重现。

4.故障分析与解决


我们比较了Hue和CLI方式的solrconfig.xml文件,发现Hue的solrconfig.xml里面启用了Secure相关的配置,但是CLI方式下的却没有。Hue会自动识别到启用Sentry并修改solrconfig,但是通过Solr的命令行在创建collection的config文件时并不会自动启用Sentry的xml配置文件。所以如果想要通过命令行创建能够让Sentry支持的collection的话,我们需要手动指定collection的solrconfig文件。

注:查看collection的config文件的命令如下,将collection的config文件导出到/tmp/collection1目录

solrctl instancedir --get collection1 /tmp/collection1

(可左右滑动)

以下我们具体看看如何解决。

1.首先我们通过solr用户删掉之前创建的collection1

[root@cdh02 solr]# klist
Ticket cache: FILE:/tmp/krb5cc_0
Default principal: fayson@FAYSON.COM
Valid starting       Expires              Service principal
06/13/2018 21:35:08  06/14/2018 21:35:08  krbtgt/FAYSON.COM@FAYSON.COM
        renew until 06/20/2018 21:35:08
06/13/2018 21:35:17  06/14/2018 21:35:08  HTTP/cdh04.fayson.com@FAYSON.COM
        renew until 06/18/2018 21:35:17
[root@cdh02 solr]# kinit solr/admin
Password for solr/admin@FAYSON.COM: 
[root@cdh02 solr]# solrctl collection --delete collection1
[root@cdh02 solr]# solrctl collection --list
[root@cdh02 solr]#

(可左右滑动)

2.修改创建collection的脚本,再次创建collection

#!/bin/sh

ZK="cdh01.fayson.com,cdh02.fayson.com,cdh03.fayson.com"
COLLECTION="collection2"
BASE=`pwd`
SHARD=3
REPLICA=1
echo "create solr collection"
rm -rf tmp/*
solrctl --zk $ZK:2181/solr instancedir --generate tmp/${COLLECTION}_configs
mv tmp/${COLLECTION}_configs/conf/solrconfig.xml tmp/${COLLECTION}_configs/conf/solrconfig.xml.bk
mv tmp/${COLLECTION}_configs/conf/solrconfig.xml.secure tmp/${COLLECTION}_configs/conf/solrconfig.xml
cp conf/schema.xml tmp/${COLLECTION}_configs/conf/
solrctl --zk $ZK:2181/solr instancedir --create $COLLECTION tmp/${COLLECTION}_configs
solrctl --zk $ZK:2181/solr collection --create $COLLECTION -s $SHARD -r $REPLICA
solrctl --zk $ZK:2181/solr collection --list

(可左右滑动)

注意红色框框部分我们使用solrconfig.xml.secure替换solrconfig.xml

运行该脚本建立collection

[root@cdh02 solr]# sh create_sentry.sh 
create solr collection
Uploading configs from tmp/collection2_configs/conf to cdh01.fayson.com,cdh02.fayson.com,cdh03.fayson.com:2181/solr. This may take up to a minute.
collection2 (2)

(可左右滑动)

3.使用命令导入数据,依旧使用solr用户

root@cdh02 solr]# klist
Ticket cache: FILE:/tmp/krb5cc_0
Default principal: solr/admin@FAYSON.COM
Valid starting       Expires              Service principal
06/13/2018 21:45:40  06/14/2018 21:45:40  krbtgt/FAYSON.COM@FAYSON.COM
        renew until 06/20/2018 21:45:40
06/13/2018 21:46:02  06/14/2018 21:45:40  HTTP/cdh02.fayson.com@FAYSON.COM
        renew until 06/18/2018 21:46:02
06/13/2018 21:52:16  06/14/2018 21:45:40  HTTP/cdh04.fayson.com@FAYSON.COM
        renew until 06/18/2018 21:52:16
[root@cdh02 solr]# curl --negotiate -u : 'http://cdh04.fayson.com:8983/solr/collection2/update/csv?commit=true' \
> -H 'Content-Type: application/csv' \
> --data-binary @/root/_fayson/data.csv
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader"><int name="status">0</int><int name="QTime">284</int></lst>
</response>

(可左右滑动)

4.使用fayson用户登录Kerberos对collection2进行查询

[root@cdh02 solr]# kinit fayson
Password for fayson@FAYSON.COM: 
[root@cdh02 solr]# klist
Ticket cache: FILE:/tmp/krb5cc_0
Default principal: fayson@FAYSON.COM
Valid starting       Expires              Service principal
06/13/2018 21:53:50  06/14/2018 21:53:50  krbtgt/FAYSON.COM@FAYSON.COM
        renew until 06/20/2018 21:53:50
[root@cdh02 solr]# curl --negotiate -u : "http://cdh04.fayson.com:8983/solr/collection2/query?q=*%3A*&wt=json&indent=true"

(可左右滑动)

查询失败,说明通过命令行创建的collection,在不通过Sentry分配权限的情况下,无法进行对collection查询。

5.通过solr用户重新创建一个fayson角色,并赋予所有collection的Update权限。注意需要使用solr用户登录Kerberos

solrctl sentry --create-role fayson
solrctl sentry --grant-privilege fayson 'collection=*->action=Update'
solrctl sentry --add-role-group fayson fayson
solrctl sentry --list-privileges fayson

(可左右滑动)

6.再次使用fayson用户登录Kerberos,再次对collection2进行查询。

[root@cdh02 solr]# kinit fayson
Password for fayson@FAYSON.COM: 
[root@cdh02 solr]# curl --negotiate -u : "http://cdh04.fayson.com:8983/solr/collection2/query?q=*%3A*&wt=json&indent=true"

(可左右滑动)

依旧查询失败,说明collection的update权限只能让用户组有创建或者更新collection的权限。

7.使用solr用户登录Kerberos,将fayson用户组的权限改为所有collection的查询权限。

[root@cdh02 solr]# kinit solr/admin
Password for solr/admin@FAYSON.COM: 
[root@cdh02 solr]# solrctl sentry --drop-role fayson
[root@cdh02 solr]# solrctl sentry --create-role fayson
[root@cdh02 solr]# solrctl sentry --grant-privilege fayson 'collection=*->action=Query'
[root@cdh02 solr]# solrctl sentry --add-role-group fayson fayson
[root@cdh02 solr]# solrctl sentry --list-privileges fayson

(可左右滑动)

8.再次使用fayson登录Kerberos并对collection2进行查询。

[root@cdh02 solr]# kinit fayson
Password for fayson@FAYSON.COM: 
[root@cdh02 solr]# curl --negotiate -u : "http://cdh04.fayson.com:8983/solr/collection2/query?q=*%3A*&wt=json&indent=true"

(可左右滑动)

查询成功,说明通过Sentry给fayson用户组分配的collection的query权限成功。

5.总结


如果在Hue中创建solr的collection,Hue会自动识别到启用Sentry并修改solrconfig,但是通过Solr的命令行在创建collection的config文件时并不会自动启用Sentry的xml配置文件。通过命令行在创建collection的时候,需要在通过solrctl instancedir –generate创建好config文件后,手动将solrconfig.xml.secure替换为solrconfig.xml,然后再创建collection。这是通过Sentry对该collection的赋权才能生效。

提示:代码块部分可以左右滑动查看噢

为天地立心,为生民立命,为往圣继绝学,为万世开太平。 温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。

推荐关注Hadoop实操,第一时间,分享更多Hadoop干货,欢迎转发和分享。

原创文章,欢迎转载,转载请注明:转载自微信公众号Hadoop实操

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Hadoop实操 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档