es安全认证



执行命令:

elasticsearch-service.bat install

1

安装好之后,就可以执行自启动服务命令了

elasticsearch-service.bat start

bin\elasticsearch-certutil.bat ca

bin\elasticsearch-certutil.bat cert --ca elastic-stack-ca.p12

bin\elasticsearch-keystore.bat add xpack.security.transport.ssl.keystore.secure_password

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

bin\elasticsearch.bat -d

bin\elasticsearch-setup-passwords.bat interactive

 

kibana:

  1. elasticsearch.username: "elastic"
  2.  

    elasticsearch.password: "密码"

 

解决步骤

一、生成证书:

输入./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

1

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

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

# 先创建目录

mkdir ./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

1

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

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

1

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

七、逐个启动节点

./bin/elasticsearch -d

1

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

八、设置密码

./bin/elasticsearch-setup-passwords interactive

# 下面会要输入很多密码,都要自己能记住,以后要用

# 需要设置 elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user 这些用户的密码

1

2

3

九、浏览器查看9200端口

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

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

 

输入用户名和密码elastic/123456

 

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

十、es-head访问认证的es

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

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

 

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

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

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

 

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