您的位置:首页 > 其它

Elasticsearch shield权限管理详解

2017-12-01 00:00 281 查看
ElasticSearch本身没有权限管理模块,只要获取服务器的地址和端口,任何人都可以随意读写ElasticSearch的API并获取数据,这样非常不安全。如果获取了ES的访问IP和端口,一条命令就可以删除整个索引库。好在Elastic公司开发了安全插件shield来解决权限管理问题. https://www.elastic.co/products/shield

一、Shield是什么

Shield是Elastic公司为ElasticSearch开发的一个安全插件。在安装此插件后,Shield会拦截所有对ElasticSearch的请求,并加上认证与加密,保障ElasticSearch及相关系统的安全性。
它支持下面的功能:

用户认证

使用Shield可以定义一系列已知的用户,并用其认证用户请求。这些用户存在于抽象的“域”中。一个域可能是下面几种类型:

LDAP服务

Active Directory服务

本地esusers配置文件(类似/etc/passwd)

权限控制

Shield的权限控制包含下面几种元素:

被保护的资源Secured Resource:权限所应用到的对象,比如某个index,cluster等等

特权Priviliege:角色对对象可以执行的一种或多种操作,比如
read
,
write
等。还可以是
indicies:/data/read/perlocate
等某种对象特有的操作。

许可Permissions:对被保护的资源拥有的一个或多个特权,如
read on the "products" index


角色Role:由许可组成的有名字的集合

用户Users:用户实体,可以被赋予0种,1种或多种角色,他们可以对被保护的资源执行相应角色所拥有的各种特权。

集群节点认证与信道加密

Shield使用SSL/TLS加密相应端口(9300),防止集群被未授权的机器监听或干扰。

IP 过滤

Shield支持基于IP的访问控制。

审计

Shield可以在ElasticSearch的日志中输出每次鉴权操作的详细信息,包括用户名,操作,操作是否被允许等等。

二、shield安装

Shield是商业插件,需要ElasticSearch的商业许可。第一次安装许可的时候,会提供30天的免费试用权限。30天后,Shield将会屏蔽
cluster health
,
cluster stats
,
index stats
这几个API,其余功能不受影响。

bin/plugin install license
bin/plugin install shield

第二步:重启Elasticsearch

bin/elasticsearch

第三步:添加管理员

bin/shield/esusers useradd adminName -r admin

adminName是可以自定义的,执行该命令以后会提示输入密码,再次输入密码进行确认,之后管理员用户就添加成功了。

再访问ES服务器中的数据就需要密码验证了.在终端中执行之前的命令试一下:

curl -XGET "http://192.168.0.224:9200/blog/article/1?pretty"
{
"error" : {
"root_cause" : [ {
"type" : "security_exception",
"reason" : "missing authentication token for REST request [/blog/article/1?pretty]",
"header" : {
"WWW-Authenticate" : "Basic realm=\"shield\""
}
} ],
"type" : "security_exception",
"reason" : "missing authentication token for REST request [/blog/article/1?pretty]",
"header" : {
"WWW-Authenticate" : "Basic realm=\"shield\""
}
},
"status" : 401
}

报安全异常,这时候需要把-u 管理员名称加到命令中,截图方便理解:



加入权限以后,在终端中每次执行操作都需要加参数并输入正确的密码。
在浏览器中访问ES同样需要验证:



三、Java Api中使用shield

加入权限管理以后在JAVA Api中需要做修改:

第一步:加入jar包
直接导入
到elasticsearch-2.3.3/plugins/shield目录下拷贝shield-2.3.3.jar,加到CLASSPATH中.
或者maven导入

<project ...>
<repositories>
<!-- add the elasticsearch repo -->
<repository>
<id>elasticsearch-releases</id>
<url>https://maven.elasticsearch.org/releases</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
...
</repositories>
...
<dependencies>
<!-- add the shield jar as a dependency -->
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>shield</artifactId>
<version>2.2.0</version>
</dependency>
...
</dependencies>
...
</project>

第二步:修改setting:

import org.elasticsearch.shield.ShieldPlugin;

Settings settings = Settings.settingsBuilder()
.put("cluster.name", "bropen")
.put("shield.user","bropen:password")
.build();

第三步:修改client

Client client = TransportClient.builder()
.addPlugin(ShieldPlugin.class)
.settings(settings).build()
.addTransportAddress(new
InetSocketTransportAddress(InetAddress.getByName("192.168.0.224"), 9300));


四、shield 用户管理

3.1 新增用户

./elasticsearch-2.3.3/bin/shield/esusers  useradd bropen

回车后输入两次密码确认
Enter new password: Retype new password:

3.2 查看用户

./elasticsearch-2.3.3/bin/shield/esusers list

会列出当前所有的user和角色:

es_admin       : admin
bropen         : admin


3.3 修改密码

给用户名为bropen的管理员修改密码:

./elasticsearch-2.3.3/bin/shield/esusers  passwd bropen

回车后输入两次密码确认

Enter new password:
Retype new password:


3.4 删除用户

./elasticsearch-2.3.3/bin/shield/esusers userdel bropen

查看所有可用命令:

./elasticsearch-2.3.3/bin/shield/esusers  -h

如果在head中出现cluster health: not connected的情况,如下图:



可以查看当前用户是不是管理员角色:

./elasticsearch-2.3.3/bin/shield/esusers list

给名为bropen的管理员添加角色:

./elasticsearch-2.3.3/bin/shield/esusers roles bropen -a admin


3.5 控制访问的索引

在实际的生产环境中,经常需要让不同的角色访问不同的index,比如让Nginx管理员只能看到nginx相关的日志,Mail管理员只看到mail相关的用户,这时候就需要使用到权限控制功能。
首先我们编辑ElasticSearch服务器的
/etc/elasticsearch/shield/roles.yml
,注释掉
kibana4.indicies.*
部分,即去掉用户读取所有index的权限。如下:

# The required permissions for kibana 4 users.
kibana4:
cluster:
- cluster:monitor/nodes/info
- cluster:monitor/health
indices:
#    '*':
#      - indices:admin/mappings/fields/get
#      - indices:admin/validate/query
#      - indices:data/read/search
#      - indices:data/read/msearch
#      - indices:admin/get
'.kibana':
- indices:admin/exists
- indices:admin/mapping/put
- indices:admin/mappings/fields/get
- indices:admin/refresh
- indices:admin/validate/query
- indices:data/read/get
- indices:data/read/mget
- indices:data/read/search
- indices:data/write/delete
- indices:data/write/index
- indices:data/write/update
- indices:admin/create

之后再
roles.yml
的末尾加上相关用户的权限配置:

nginx_user: #nginx_user 角色定义
indices:  #index部分
'logstash-nginx*': read  #指定nginx_user可以读取所有匹配'logstash-nginx*'的索引。

mail_user:  #mail_user 角色定义,用法同上
indices:
'logstash-mail*': read

现在我们使用
esuser
新建两个用户,分属两个组

/usr/share/elasticsearch/bin/shield/esusers useradd demo_nginx -r nginx_user
/usr/share/elasticsearch/bin/shield/esusers useradd demo_mail -r mail_user

再把它们同时加入
kibana4
组中:

/usr/share/elasticsearch/bin/shield/esusers roles demo_nginx -a kibana4
/usr/share/elasticsearch/bin/shield/esusers roles demo_mail -a kibana4

这时再用不同的用户登录Kibana界面,就可以看到不同的内容了。

官网关于Shield的Java api说明:
https://www.elastic.co/guide/en/shield/current/_using_elasticsearch_java_clients_with_shield.html

Shield参考手册
https://www.elastic.co/guide/en/shield/current/index.html

五、配置Logstash

在ElasticSearch服务器上,使用esusers创建Logstash用户:
/usr/share/elasticsearch/bin/shield/esusers useradd logstashserver -r logstash


在Logstash服务器上,修改output模块的配置文件,例如:

output {
elasticsearch {
host => "192.168.6.144"
protocol => "http"
index => "logstash-%{type}-%{+YYYY.MM.dd}"
user => "logstashserver" #在这里加上Shield中role为Logstash的用户名
password => "woshimima" #别忘了密码
}
#  stdout { codec => rubydebug }
}


之后重启Logstash服务即可。

六、配置Kibana

基本配置

在ElasticSearch服务器上,使用esusers创建属于kibana4_server的用户:
/usr/share/elasticsearch/bin/shield/esusers useradd kibanaserver -r kibana4_server


在Kibana服务器上,编辑
/opt/kibana/config/kibana.yml
,找到下面这一部分并修改:

# If your Elasticsearch is protected with basic auth, this is the user credentials
# used by the Kibana server to perform maintence on the kibana_index at statup. Your Kibana
# users will still need to authenticate with Elasticsearch (which is proxied thorugh
# the Kibana server)
kibana_elasticsearch_username: kibanaserver  #Kibana服务将用这个用户名访问ElasticSearch服务器。
kibana_elasticsearch_password: woshimima     #密码

之后重启Kibana服务即可。您可能需要使用前面的
es_admin
账户登录Kibana网页端。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  elasticsearch