使用Elasticsearch的插件“Shield”使ELK变得更加安全!尝试在Kibana上实现认证和授权
本文的概述
ELK(Elasticsearch + Logstash + Kibana)被广泛认可作为日志收集和分析的基础架构,其中以Elasticsearch + Kibana负责日志的管理和可视化。然而,目前Kibana并未实现诸如认证、授权和安全防护等功能,这在正式运营时会面临一些问题。没有认证和授权的情况下,比如管理员在Kibana上创建了可视化图表和仪表盘,任何有Kibana访问权限的人都可以修改这些内容。因此,我们决定尝试使用Elasticsearch的付费安全插件”Shield”来实现Kibana的认证和授权,并根据用户的权限将可视化图表和仪表盘设为只读模式。
※Kibana的身份验证功能最初计划在4.2版本中引入,但目前计划在4.4版本中引入。
https://github.com/elastic/kibana/issues/3904
环境信息
-
- OS:CentOS6.5 on VirtualBox + Vagrant
-
- Logstash 2.1.1
-
- Elasticsearch 2.1.0
-
- Kibana 4.3.0
- Java 1.8.65
官方网站
-
- Elasticsearch
-
- https://www.elastic.co/products/elasticsearch
-
- Logstash
-
- https://www.elastic.co/products/logstash
-
- Kibana
-
- https://www.elastic.co/products/kibana
-
- Shield
- https://www.elastic.co/products/shield
事前准备
安装ELK
日志聚合工具
wget -P /usr/local/src/ https://download.elastic.co/logstash/logstash/packages/centos/logstash-2.1.1-1.noarch.rpm
rpm -ivh /usr/local/src/logstash-2.1.1-1.noarch.rpm
弹性搜索
wget -P /usr/local/src/ https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/rpm/elasticsearch/2.1.0/elasticsearch-2.1.0.rpm
rpm -ivh /usr/local/src/elasticsearch-2.1.0.rpm
Elasticsearch可视化工具Kibana
wget -P /usr/local/src/ https://download.elastic.co/kibana/kibana/kibana-4.3.0-linux-x64.tar.gz
tar xzvf /usr/local/src/kibana-4.3.0-linux-x64.tar.gz -C /opt
为了启动ELK,假设Java已经安装。
ELK合作
使用Logstash的input-twitter-plugin将时间线导入到Elasticsearch中,并在Kibana中进行可视化。
尽管这不是重点,但考虑到即将到来的圣诞节,我们将尝试收集与“Xmas”关键词相关的时间线。
Logstash的配置
input {
twitter {
consumer_key => "<consumer_key>"
consumer_secret => "<consumer_secret>"
oauth_token => "<oauth_token>"
oauth_token_secret => "<oauth_token_secret>"
keywords => ["Xmas"]
full_tweet => true
ignore_retweets => true
}
}
output {
elasticsearch {
}
stdout {
codec => rubydebug
}
}
请使用在Twitter Apps中注册的应用程序的consumer_key,consumer_secret,oauth_token和oauth_token_secret。
参考:http://dev.classmethod.jp/cloud/aws/twitter-visualize-using-elastic/
启动Elasticsearch
service elasticsearch start
启动Kibana
/opt/kibana-4.3.0-linux-x64/bin/kibana &
启动Logstash
service logstash start
确认日志联动

安全设置
当准备工作已完成,现在要开始实现对Kibana的身份验证和授权。
盾牌安装
首先,安装Elasticsearch的安全插件“Shield”。该插件是一个付费订阅服务,并提供30天的试用许可证,您可以使用它来试用。
/usr/share/elasticsearch/bin/plugin install elasticsearch/license/latest
/usr/share/elasticsearch/bin/plugin install elasticsearch/shield/latest
鉴定(Authentication)
在Shield中的认证理念
Shield在受到用户资格信息并进行验证的认证机制中定义了“领域”,并支持四种类型的“领域”。
-
- esusers
-
- Shieldに組み込まれたネイティブの認証システムとなります。(デフォルト)
-
- ファイルベースのレルムとなり、コマンドラインからユーザの追加・削除等を実行することができます。
-
- LDAP
-
- 外部のLDAPサーバでユーザを認証するレルムとなります。
-
- Active Directory
-
- 外部のActive Directoryサーバでユーザを認証するレルムとなります。
-
- PKI
- 公開鍵を利用して認証を行うレルムとなります。
我想使用默认的esusers领域来处理本次事务。
添加用户
在esusers领域中,您可以通过esusers命令添加用户,但在添加用户之前,我希望先提及授权。
授权(Authorization)
Shield在许可方面的理念
Shiled利用了基于角色的访问控制(RBAC)模型来实现对每个用户的权限管理。简单来说,每个角色都有一组权限定义,而每个用户则与一组角色相绑定。
定义卷
Shild的角色由roles.yml文件定义。
默认情况下,已定义了几个角色,如管理员角色admin。
# All cluster rights
# All operations on all indices
admin:
cluster: all
indices:
'*':
privileges: all
# Monitoring cluster privileges
# All operations on all indices
power_user:
cluster: monitor
indices:
'*':
privileges: all
# Only read operations on indices
user:
indices:
'*':
privileges: read
# Only read operations on indices named events_*
events_user:
indices:
'events_*':
privileges: read
Shield将Elasticsearch的权限分为两个主要类型:cluster(群集)和indices(索引)。
cluster定义了对Elasticsearch群集的管理和监控操作的权限。
indices定义了对群集中特定索引的管理和监控操作的权限。
可以为每个cluster和索引设置all、monitor、read、write等预定义权限,也可以更细粒度地指定特定角色的特定操作,如下所示。
user01:
indices:
'*':
privileges: indices:admin/create, indices:admin/exists
增加卷
尽管前面的准备工作有点长,但这次我们将在Kibana中使用以下三种角色。
-
- kibana4_server:Kibanaサーバ用のロール
-
- kibana4:Kibana管理ロール(Visualization、Dashboardの読取・作成・更新・削除可)
- kibana4_monitor:Kibana監視ロール(Visualization、Dashboardは読取のみ可)
编辑roles.yml文件并添加以下内容,使用kibana4_server角色和kibana4角色的默认定义,创建一个新的kibana4_monitoring角色。
# The required permissions for the kibana 4 monitor
kibana4_monitoring:
cluster:
- cluster:monitor/nodes/info
- cluster:monitor/health
indices:
'logstash-*':
- indices:admin/get
- indices:admin/mappings/fields/get
- indices:admin/validate/query
- indices:data/read/search
- indices:data/read/msearch
- indices:data/read/field_stats
'.kibana':
- indices:admin/exists
- indices:admin/get
- indices:admin/mapping/put
- indices:admin/mappings/fields/get
- indices:admin/refresh
- indices:admin/validate/query
- indices:data/read/mget
- indices:data/read/search
- indices:data/read/msearch
- indices:data/read/field_stats
与Kibana 4角色的定义进行比较后,我们可以发现.kibana索引的数据写入权限被排除在外。
# The required permissions for kibana 4 users.
kibana4:
cluster:
- cluster:monitor/nodes/info
- cluster:monitor/health
indices:
'*':
privileges: indices:admin/mappings/fields/get, indices:admin/validate/query, indices:data/read/search, indices:data/read/msearch, indices:admin/get
'.kibana':
privileges: 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
各种用户创建:
创建与上述创建的角色相关联的用户。
在esusers命令中,使用useradd选项指定”要创建的用户名”,并使用-r选项指定”要关联的角色名”。
执行命令后,将要求输入密码。
# Kibanaサーバ用ユーザ
/usr/share/elasticsearch/bin/shield/esusers useradd kibana4_server -r kibana4_server
# Kibana管理用ユーザ
/usr/share/elasticsearch/bin/shield/esusers useradd kibana4 -r kibana4
# Kibana監視用ユーザ
/usr/share/elasticsearch/bin/shield/esusers useradd kibana4_monitor -r kibana4_monitoring
Kibana设置
编辑Kibana的配置文件(/config/kibana.yml),并添加Kibana服务器用户的定义。
elasticsearch.username: "kibana4_server"
elasticsearch.password: "password"
完成上述设置后,重新启动Elasticsearch和Kibana,并从Kibana界面上进行确认。
确认
以Kibana管理用户的身份登录。

创建数据可视化和仪表板
我会在Dashboard上创建一个适当的可视化,并将其保存为“dashboard_twitter”。作为管理员,我可以毫无问题地保存。

使用Kibana监 audit 请登录 你 用户 。

数据可视化、仪表板加载

可视化、仪表板的编辑和保存

权限错误

整理
请问您觉得怎么样呢?
在商业领域使用ELK,我认为要考虑的安全性是无法回避的。
虽然会有收费的插件,但我认为它们具有很大的使用价值。
除了我们今天介绍的”认证”和”授权”之外,“Shield”还支持“审计日志输出”、“加密通信”和“IP过滤”等功能。如果有机会,我想试试。
另外,如果还有除今天介绍的方法之外的最佳实践,请向我们指教。
只需一个选择,在中文中解释以下内容:
除了官方网站之外的参考来源
-
- http://blog.johtani.info/blog/2015/02/27/you-know-for-security-shield-goes-ga-ja/
-
- http://dev.classmethod.jp/cloud/aws/using-elasticsearch-plugin-shield/
- http://dev.classmethod.jp/cloud/aws/twitter-visualize-using-elastic/