使用Solr的 Rule-Based Authorization 插件在CDH上实现权限管理!

Rule-Based Authorization Plugin 是Apache Solr提供的唯一一个权限插件。

实际实践过程中发现,该插件易用性不高。虽然可以进行细粒度权限控制,但是在网上参考资料很少,配置方式也相当反人类!

权限模型

Rule-Based Authorization通过:Permission和Role绑定,Role和User绑定的方式给用户配置权限,所有相关的权限&用户定义在security.json文件中。

Rule-Based Authorization中包括两种类型的Permission:预定义的Permisson和自定义的Permisson。

所有预定义权限的限制对象都是所有Collection,包括以下13种:

Permisson 说明
security-edit 编辑security.json权限
security-read 读security.json权限
schema-edit 编辑任意Collection的schema的权限
schema-read 读任意Collection的schema的权限
config-read 编辑任意Collection的solrconfig的权限
config-edit 读任意Collection的solrconfig的权限
collection-admin-read 执行/admin/collections地址下读操作的权限(不限制Collection)
collection-admin-edit 执行/admin/collections地址下写操作的权限(不限制Collection)
core-admin-read 执行Core API中读操作的权限(不限制Collection)
core-admin-edit 执行Core API中写操作的权限(不限制Collection)
update 执行任意Collection中/update操作的权限
read 执行任意Collection中读操作的权限,如/get、/select
all 上述所有权限

自定义权限可以限制请求中的以下实体:

  • Collection名称
  • 请求类型,如GET、POST、PUT等
  • 请求的地址,如/select、/collection 等

下面的自定义权限,定义了访问securecollection的/select的权限。官方文档关于 Authorization API 有比较详细说明。

1
2
3
4
5
6
7
{
"name":"secure-collection1-permission",
"collection":"securecollection",
"path":"/select",
"before":"collection-admin-read",
"role":"admin"
}

例子

下面的权限配置实现以下效果:

  • 任意用户有除security.json以外的Read权限
  • dev用户有除security.json以外的完整编辑权限
  • solr、solr/node1、solr/node2有所有权限
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"authorization": {
"class": "org.apache.solr.security.RuleBasedAuthorizationPlugin",
"permissions": [
{"name": "security-read", "role": "security_role"},
{"name": "security-edit", "role": "security_role"},

{"name": "read", "role": "*"},
{"name": "schema-read", "role": "*"},
{"name": "config-read", "role": "*"},
{"name": "collection-admin-read", "role": "*"},
{"name": "core-admin-read", "role": "*"},

{"name": "update", "role": "dev"},
{"name": "schema-edit", "role": "dev"},
{"name": "config-edit", "role": "dev"},
{"name": "collection-admin-edit", "role": "dev"},
{"name": "core-admin-edit", "role": "dev"}
],
"user-role": {
"solr/bdnode2@LINQING.COM": ["security_role","dev"],
"solr/bdnode3@LINQING.COM": ["security_role","dev"],
"solr@LINQING.COM": ["security_role","dev"],
"dev@LINQING.COM": "dev"
}
}

使用下面的curl请求可以测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# 创建用户-Role映射,需要security-edit权限
curl --negotiate -u : -H 'Content-type:application/json' -d '{ "set-user-role" : {"demo@LINQING.COM": null}}' http://bdnode2:8983/solr/admin/authorization

# 创建Collection、删除Collection、重载Collection,需要collection-admin-edit权限

curl --negotiate -u : 'http://bdnode2:8983/solr/admin/collections?action=CREATE&name=film&numShards=2&replicationFactor=2&maxShardsPerNode=2&collection.configName=film'
curl --negotiate -u : 'http://bdnode3:8983/solr/admin/collections?action=DELETE&name=film'
curl --negotiate -u : 'http://bdnode3:8983/solr/admin/collections?action=RELOAD&name=film'

# 修改Schema,需要schema-edit权限

curl --negotiate -u : -X POST -H 'Content-type:application/json' --data-binary '{"add-field": {"name":"name", "type":"text_general", "multiValued":false, "stored":true}}' http://bdnode2:8983/solr/film/schema
curl --negotiate -u : -X POST -H 'Content-type:application/json' --data-binary '{"add-copy-field" : {"source":"*","dest":"_text_"}}' http://bdnode3:8983/solr/film/schema


# update数据,需要update权限
curl --negotiate -u : -X POST -H 'Content-Type: application/json' --data-binary '
{
"id": "xxxxxxxxxxxxxxxxxxxx",
"initial_release_date": "2007-06-28",
"name": "Harry Potter and the Order of the Phoenix",
"genre": [
"Family",
"Mystery",
"Adventure Film",
"Fantasy",
"Fantasy Adventure",
"Fiction"
],
"directed_by": [
"David Yates"
]
}' http://bdnode2:8983/solr/film/update/json/docs


# 查询security配置,需要security-read权限
curl --negotiate -u : 'http://bdnode2:8983/solr/admin/authorization'

# 查询所有Collection配置,需要collection-admin-read权限
curl --negotiate -u : 'http://bdnode2:8983/solr/admin/collections?action=LIST'

# 查询某个Collection,read权限
curl --negotiate -u : 'http://bdnode2:8983/solr/film/select?q=*:*&rows=0'

注意事项

  • 使用Kerberos认证时,在security.json中需要定义principal的fully name,不能定义短名称
  • 使用Kerberos认证时,所有Solr节点使用的principal都需要手工配置权限
  • Solr确认Permission是按照顺序的,定义permissions时需要将小权限放在大权限之前,因此ALL需要放在最后定义
  • 配置该插件之后,如果出现资源不存在的情况,错误提示也会出现403

参考文档

Rule-Based Authorization

Securing Solr: Tips and Tricks You Really Need to Know

Securing Solr with Basic Authentication