Elasticsearch开启安全认证详细步骤

2021/4/19 18:27:42

本文主要是介绍Elasticsearch开启安全认证详细步骤,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Elasticsearch开启安全认证详细步骤

  • Elasticsearch开启安全认证详细步骤
    • Elasticsearch搭建环境
    • 问题描述
    • 解决步骤
      • 一、生成证书:
      • 二、生成秘钥
      • 三、将凭证迁移到指定目录
      • 四、凭证移动至每一台集群下面
      • 五、修改配置文件(每一台es都需要添加)
      • 六、在各个节点上添加密码(每一台es都需要操作)
      • 七、逐个启动节点
      • 八、设置密码
      • 九、浏览器查看9200端口
      • 十、es-head访问认证的es

Elasticsearch开启安全认证详细步骤

Elasticsearch搭建环境

Elasticsearch版本号:7.3.2
服务器版本: CentOS 7.4

问题描述

项目等保协议扫描出了ES漏洞:检测到ES(ElasticSearch)数据库没有开启授权认证,建议开启授权认证,防止非法访问。

解决步骤

一、生成证书:

​ 输入./bin/elasticsearch-certutil ca
​ 碰到第一个直接回车,不用管
​ 碰到第二个输入密码,例如123456

[cjpt@localhost elasticsearch-7.3.2]$ ./bin/elasticsearch-certutil ca
This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.

The 'ca' mode generates a new 'certificate authority'
This will create a new X.509 certificate and private key that can be used
to sign certificate when running in 'cert' mode.

Use the 'ca-dn' option if you wish to configure the 'distinguished name'
of the certificate authority

By default the 'ca' mode produces a single PKCS#12 output file which holds:
    * The CA certificate
    * The CA's private key

If you elect to generate PEM format certificates (the -pem option), then the output will
be a zip file containing individual files for the CA certificate and private key

Please enter the desired output file [elastic-stack-ca.p12]: 【不用管,直接回车】
Enter password for elastic-stack-ca.p12 : 【输入密码】
[cjpt@localhost elasticsearch-7.3.2]$ ll
总用量 1888
......(省略其他文件)
-rw-------.  1 cjpt cjpt    2524 4月  19 16:28 elastic-stack-ca.p12

完成后会生成一个文件:elastic-stack-ca.p12

二、生成秘钥

./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12

​ 中间需要输入刚才设置的密码就直接输入就可以了,需要输入路径的地方不要输入地址,就直接回车,之后会生成一个文件:elastic-certificates.p12文件

三、将凭证迁移到指定目录

# 先创建目录
mkidr ./config/certificates
# 移动凭证至指定目录下
mv ./elastic-certificates.p12 ./config/certificates/
# 赋值权限,不然会出问题
chmod 777 ./config/certificates/elastic-certificates.p12

四、凭证移动至每一台集群下面

​ 此处省略各种scp,就是把elastic-certificates.p12这个文件移动到每一个es安装目录的相同路径下

五、修改配置文件(每一台es都需要添加)

vim ./config/elasticsearch.yml
# 输入如下的配置

# 跨域
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Type,Content-Length

xpack.security.enabled: true
xpack.security.authc.accept_default_password: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: 【es的安装路径】/config/certificates/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: 【es的安装路径】/config/certificates/elastic-certificates.p12

六、在各个节点上添加密码(每一台es都需要操作)

./bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password

输入密码:第一步中设置的密码,例如本样例中的123456

./bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password

输入密码:第一步中设置的密码,例如本样例中的123456

七、逐个启动节点

./bin/elasticsearch -d

启动后看日志,是否正常,若日志异常,则需要具体排查

八、设置密码

./bin/elasticsearch-setup-passwords  interactive
# 下面会要输入很多密码,都要自己能记住,以后要用
# 需要设置 elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user 这些用户的密码

九、浏览器查看9200端口

因为我是一台机器上装的集群,所以使用9201端口

访问地址为:http://【ip】:9200

image-20210419170942255

输入用户名和密码elastic/123456

image-20210419171603912

出现如上结果就是成功加上认证了

十、es-head访问认证的es

本来想修改es-head的源代码的,发现实在改不动,只能记录一下了

常用的es-head采用的连接为直接输入url

image-20210419171820000

但是此方法显然不支持带认证的es

所以此处需要带上账户名和密码:

http://192.168.31.160:9201/?auth_user=elastic&auth_password=123456

image-20210419171915681

此时再连接就可以发现连接正常了

image-20210419172149934



这篇关于Elasticsearch开启安全认证详细步骤的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程