17

ELK-学习笔记–elasticsearch-7.x使用xpack进行安全认证 |坐而言不如起而行! 二丫讲梵

 3 years ago
source link: http://www.eryajf.net/3500.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
本文预计阅读时间 22 分钟

2019年5月21日,Elastic官方发布消息: Elastic Stack 新版本6.8.0 和7.1.0的核心安全功能现免费提供。
这意味着用户现在能够对网络流量进行加密、创建和管理用户、定义能够保护索引和集群级别访问权限的角色,并且使用 Spaces 为 Kibana提供全面保护。 免费提供的核心安全功能如下:

1)TLS 功能。 可对通信进行加密;
2)文件和原生 Realm。 可用于创建和管理用户;
3)基于角色的访问控制。 可用于控制用户对集群 API 和索引的访问权限;
通过针对 Kibana Spaces 的安全功能,还可允许在Kibana 中实现多租户。

1、x-pack演变

1,5.X版本之前:没有x-pack,是独立的:security安全,watch查看,alert警告等独立单元。
2,5.X版本:对原本的安全,警告,监视,图形和报告做了一个封装,形成了x-pack。
3,6.3 版本之前:需要额外安装。
4,6.3版本及之后:已经集成在一起发布,无需额外安装,基础安全属于付费黄金版内容。 7 .1版本:基础安全免费。

2、安全部分免费之前,大家怎么保证基础安全?

场景一:全部“裸奔”,相信这在国内占据了非常大的比重。
内网部署,不对外提供服务。或者ES作为业务基础支撑,不公网开放9200等常用端口,开放的是业务的服务端口。
可能暴露问题:公司或团队内部开放9200、5601端口,基本head插件、kibana都能连接,极易导致线上索引或数据可能被误删。

场景二:加了简单防护。
一般使用Nginx身份认证+防火墙策略控制。

场景三:整合使用了第三方安全认证方案。
比如:SearchGuard、ReadonlyREST。

场景四:付费购买了Elastic-Xpack黄金版或白金版服务。
一般是银行等土豪大客户,对安全、预警、机器学习等付费功能需求迫切,如:宁波银行付费购买白金服务。

以上内容摘自:https://blog.csdn.net/laoyang360/article/details/90554761

既然新版本的es已经提供了基础的用户认证,那么多一层总比没有要强,因此接下来就实际配置一下单机版的elk和集群版的认证,两个稍微有一些不同,因此区分对待。

因为主要体现在es与kibana的认证,因此这里就只展现这两者的配置,其他logstash方面的,后续再完善。

3,单机版elk认证

本次实验elk全系版本都是基于7.4.0进行。

安装过程略过,直接来到配置阶段。

1,配置es。

  1. $ cat elasticsearch.yml
  2. cluster.name: eryajf-search
  3. node.name: es-node1
  4. path.data: /data/elasticsearch7/data
  5. path.logs: /data/elasticsearch7/log
  6. network.host: 0.0.0.0
  7. http.port: 9200
  8. xpack.security.enabled: true # 这条配置表示开启xpack认证机制
  9. xpack.security.transport.ssl.enabled: true
  10. cluster.initial_master_nodes: ["es-node1"]
  • xpack.security.enabled:表示开启xpack认证机制。
  • xpack.security.transport.ssl.enabled:这条如果不配,es将起不来,会报如下错误:Transport SSL must be enabled if security is enabled on a [basic] license. Please set [xpack.security.transport.ssl.enabled] to [true] or disable security by setting [xpack.security.enabled] to [false]

配置完成,记得创建配置中涉及到的目录并授权,然后启动es。启动成功之后,再次访问es,就需要基于用户来访问了,但是在这之前,需要先给对应的用户创建密码才行。

2,为内置账号添加密码

ES中内置了几个管理其他集成组件的账号即:apm_system, beats_system, elastic, kibana, logstash_system, remote_monitoring_user,使用之前,首先需要添加一下密码。

  1. $ /usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive
  2. Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
  3. You will be prompted to enter passwords as the process progresses.
  4. Please confirm that you would like to continue [y/N]y
  5. Enter password for [elastic]:
  6. Reenter password for [elastic]:
  7. Enter password for [apm_system]:
  8. Reenter password for [apm_system]:
  9. Enter password for [kibana]:
  10. Reenter password for [kibana]:
  11. Enter password for [logstash_system]:
  12. Reenter password for [logstash_system]:
  13. Enter password for [beats_system]:
  14. Reenter password for [beats_system]:
  15. Enter password for [remote_monitoring_user]:
  16. Reenter password for [remote_monitoring_user]:
  17. Changed password for user [apm_system]
  18. Changed password for user [kibana]
  19. Changed password for user [logstash_system]
  20. Changed password for user [beats_system]
  21. Changed password for user [remote_monitoring_user]
  22. Changed password for user [elastic]
  • interactive:给用户一一设置密码。
  • auto:自动生成密码。

如果这个地方报如下错误:

  1. Failed to determine the health of the cluster running at http://192.168.3.42:9200
  2. Unexpected response code [503] from calling GET http://192.168.3.42:9200/_cluster/health?pretty
  3. Cause: master_not_discovered_exception
  4. It is recommended that you resolve the issues with your cluster before running elasticsearch-setup-passwords.
  5. It is very likely that the password changes will fail when run against an unhealthy cluster.
  6. Do you want to continue with the password setup process [y/N]y

可能是有脏数据导致,此时可以停掉es,删除 data 数据目录,然后重新启动在进行操作。

配置完毕之后,可以通过如下方式访问es服务:

  1. curl -XGET -u elastic 'localhost:9200/_xpack/security/user?pretty'
  2. curl 127.0.0.1:9200 -u elastic

3,配置kibana连接。

开启了安全认证之后,kibana连接es以及访问es都需要认证。

变更kibana的配置,一共有两种方法,一种明文的,一种密文的。

1,明文配置

  1. server.port: 5601
  2. server.host: "0.0.0.0"
  3. server.name: "es-node1"
  4. elasticsearch.hosts: ["http://192.168.3.42:9200"]
  5. kibana.index: ".kibana"
  6. i18n.locale: "zh-CN"
  7. elasticsearch.username: "kibana"
  8. elasticsearch.password: "kibana_passwd"
  9. xpack.reporting.encryptionKey: "a_random_string"
  10. xpack.security.encryptionKey: "something_at_least_32_characters"
  • elasticsearch.username:连接es的用户名。
  • elasticsearch.password:连接es的密码。
  • xpack.reporting.encryptionKey:如果不添加这条配置,将会报错 Generating a random key for xpack.reporting.encryptionKey. To prevent pending reports from failing on restart, please set xpack.reporting.encryptionKey in kibana.yml
  • xpack.security.encryptionKey:如果不配置这条,将会报错 Generating a random key for xpack.security.encryptionKey. To prevent sessions from being invalidated on restart, please set xpack.security.encryptionKey in kibana.yml

2,密文配置

当然肯定推荐使用这种密文的方式进行认证,认证之前,需要首先将用户名密码保存到内置的ketstore里。

  1. /usr/share/kibana/bin/kibana-keystore --allow-root create
  2. /usr/share/kibana/bin/kibana-keystore --allow-root add elasticsearch.username
  3. /usr/share/kibana/bin/kibana-keystore --allow-root add elasticsearch.password

原封不动执行如上三条命令,用户名的时候输入kibana,密码写入对应密码,接着调整kibana的配置:

  1. server.port: 5601
  2. server.host: "0.0.0.0"
  3. server.name: "es-node1"
  4. elasticsearch.hosts: ["http://192.168.3.42:9200"]
  5. kibana.index: ".kibana"
  6. i18n.locale: "zh-CN"
  7. xpack.reporting.encryptionKey: "a_random_string"
  8. xpack.security.encryptionKey: "something_at_least_32_characters"

然后重启kibana即可访问,访问的时候使用elastic的用户密码登入,将是全局管理权限,如果需要创建kibana的只读用户,则可以通过管理–用户–新建用户,对用户进行角色授权即可。

image-20191125154819629.png

3,logstash配置认证。

打开自定义的logstash的配置文件logstash.conf,在output中增加elasticsearch的用户名和密码

  1. [root@ELK1 ~]# vim /home/elk/logstash-7.2.1/config/logstash.conf
  2. input {
  3. beats {
  4. port => 5044
  5. }
  6. }
  7. output {
  8. stdout {
  9. codec => rubydebug
  10. }
  11. elasticsearch {
  12. hosts => ["192.168.3.42:9200"]
  13. user => "elastic"
  14. password => "123456"
  15. }
  16. }

4,集群配置。

在我配置过程中,发现集群认证需要首先配置秘钥才行,否则在给内置用户创建秘钥的时候将会报错。

1,证书。

如下操作在其中一个node节点执行即可,生成完证书传到集群其他节点即可。

  1. /usr/share/elasticsearch/bin/elasticsearch-certutil ca
  2. /usr/share/elasticsearch/bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12

两条命令均一路回车即可,不需要给秘钥再添加密码。

证书创建完成之后,默认在es的数据目录,这里统一放到etc下:

  1. $ ls elastic-*
  2. elastic-certificates.p12 elastic-stack-ca.p12
  3. mv elastic-* /etc/elasticsearch/
  4. chown elasticsearch.elasticsearch elastic*

同样,将如上命令生成的两个证书文件拷贝到另外两台机器作为通信依据。

2,配置。

三台机器配置文件如下:

  1. cluster.name: test-search
  2. node.name: es-node-1
  3. path.data: /data/elasticsearch/data
  4. path.logs: /data/elasticsearch/log
  5. network.host: 0.0.0.0
  6. http.port: 9200
  7. transport.tcp.port: 9300
  8. discovery.seed_hosts: ["192.168.3.3:9300","192.168.3.4:9300","192.168.3.5:9300"]
  9. cluster.initial_master_nodes: ["192.168.3.3:9300","192.168.3.4:9300","192.168.3.5:9300"]
  10. xpack.security.enabled: true
  11. xpack.security.transport.ssl.enabled: true
  12. xpack.security.transport.ssl.verification_mode: certificate
  13. xpack.security.transport.ssl.keystore.path: /etc/elasticsearch/elastic-certificates.p12
  14. xpack.security.transport.ssl.truststore.path: /etc/elasticsearch/elastic-certificates.p12

除了node.name使用各自主机名之外,其他配置都一样。

如上配置无误,则可以启动es。

3,为内置账号添加密码

ES中内置了几个管理其他集成组件的账号即:apm_system, beats_system, elastic, kibana, logstash_system, remote_monitoring_user,使用之前,首先需要添加一下密码。

  1. $ /usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive
  2. Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
  3. You will be prompted to enter passwords as the process progresses.
  4. Please confirm that you would like to continue [y/N]y
  5. Enter password for [elastic]:
  6. Reenter password for [elastic]:
  7. Enter password for [apm_system]:
  8. Reenter password for [apm_system]:
  9. Enter password for [kibana]:
  10. Reenter password for [kibana]:
  11. Enter password for [logstash_system]:
  12. Reenter password for [logstash_system]:
  13. Enter password for [beats_system]:
  14. Reenter password for [beats_system]:
  15. Enter password for [remote_monitoring_user]:
  16. Reenter password for [remote_monitoring_user]:
  17. Changed password for user [apm_system]
  18. Changed password for user [kibana]
  19. Changed password for user [logstash_system]
  20. Changed password for user [beats_system]
  21. Changed password for user [remote_monitoring_user]
  22. Changed password for user [elastic]
  • interactive:给用户一一设置密码。
  • auto:自动生成密码。

配置完毕之后,可以通过如下方式访问es服务:

  1. curl -XGET -u elastic 'localhost:9200/_xpack/security/user?pretty'
  2. curl 127.0.0.1:9200 -u elastic

剩下的就是与上边的使用方式一致了,kibana的认证,logstash的认证等等。

其中kibana通过密文认证之后,配置如下:

  1. server.port: 5601
  2. server.host: "0.0.0.0"
  3. server.name: "es-node3"
  4. elasticsearch.hosts: ["http://192.168.3.3:9208"]
  5. kibana.index: ".kibana"
  6. i18n.locale: "zh-CN"

参考:


weinxin


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK