前言

7.0以后es把基础的安全模块免费集成了。很棒。现在试试设置吧。
官网安全模块文档
其中包含了一个安全入门教程

正文

下面基本都是以我的操作为例,实际上可以按照官网文档进行其它的尝试。

一、设置账号密码

单节点

在elasticsearch.yml文件里增加配置

xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true

初始化密码需要在es启动的情况下进行设置,按照提示输入各个内置用户的密码。

[esuser@localhost elasticsearch-7.8.0]$./bin/elasticsearch -d
[esuser@localhost elasticsearch-7.8.0]$./bin/elasticsearch-setup-passwords interactive

集群

启用安全功能以后,必须使用TLS来确保节点之间的通信已加密。

1)创建ca

在安装目录下执行 ./bin/elasticsearch-certutil ca

[esuser@localhost elasticsearch-7.8.0]$ ./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

# 这里回车即可,默认会在当前安装目录下生成elastic-stack-ca.p12文件
Please enter the desired output file [elastic-stack-ca.p12]: 
# 这里可以回车也可以输入密码,输入密码的话,生成节点证书的时候要验证
Enter password for elastic-stack-ca.p12 : 
[esuser@localhost elasticsearch-7.8.0]$ ll
总用量 576
drwxr-xr-x.  2 esuser esuser   4096 12月 21 21:34 bin
drwxr-xr-x.  3 esuser esuser    199 12月 29 22:30 config
-rw-------.  1 esuser esuser   2527 12月 29 22:35 elastic-stack-ca.p12
drwxr-xr-x.  9 esuser esuser    107 6月  14 2020 jdk
drwxr-xr-x.  3 esuser esuser   4096 6月  14 2020 lib
-rw-r--r--.  1 esuser esuser  13675 6月  14 2020 LICENSE.txt
drwxr-xr-x.  2 esuser esuser   4096 12月 27 07:54 logs
drwxr-xr-x. 47 esuser esuser   4096 6月  14 2020 modules
-rw-r--r--.  1 esuser esuser 544318 6月  14 2020 NOTICE.txt
drwxr-xr-x.  2 esuser esuser      6 6月  14 2020 plugins
-rw-r--r--.  1 esuser esuser   8165 6月  14 2020 README.asciidoc

需要把elastic-stack-ca.p12复制到每一个节点路径下去执行第二步。

  1. 生成节点证书
[esuser@localhost elasticsearch-7.8.0]$ ./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
# 这里输入前面设定的密码,如果有的话;
Enter password for CA (elastic-stack-ca.p12) : 
Please enter the desired output file [elastic-certificates.p12]: 
# 这里生成密码的话,需要把密码加入密码库,在4)处有说明。
Enter password for elastic-certificates.p12 : 

Certificates written to /opt/spinfo/elasticsearch-7.8.0/elastic-certificates.p12

This file should be properly secured as it contains the private key for 
your instance.
  1. 修改配置,指定访问节点证书所需的信息
    将生成的elastic-certificates.p12 文件移至config目录下
[esuser@localhost elasticsearch-7.8.0]$ mv elastic-certificates.p12 ./config/

在elasticsearch.yml文件中增加以下

xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate 
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12 
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12 

4)如果节点证书配置密码的话,这里要加入密码库

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

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

4)重启es,初始化密码

[esuser@localhost elasticsearch-7.8.0]$./bin/elasticsearch -d
[esuser@localhost elasticsearch-7.8.0]$./bin/elasticsearch-setup-passwords interactive

通常不使用内置账号elastic进行访问,而是创建业务账号使用。
用户授权介绍
这是官网关于内置角色啊,自定义角色授权之类介绍的文档。

5)如果使用kibana的话,需要给kibana配置下内置用户。
修改kibana的配置文件,在kibana的安装目录下config/kibana.yml

# 放开配置
elasticsearch.username: "kibana_system"
elasticsearch.password: "your_password"

重启kibana。

如果不想在kibana的配置文件里明文保存账号密码,可以创建密钥库保存。

[esuser@localhost kibana-7.8.0-linux-x86_64]$ ./bin/kibana-keystore create
Created Kibana keystore in /opt/spinfo/kibana-7.8.0-linux-x86_64/data/kibana.keystore
[esuser@localhost kibana-7.8.0-linux-x86_64]$ ./bin/kibana-keystore add elasticsearch.username
Enter value for elasticsearch.username: *************
[esuser@localhost kibana-7.8.0-linux-x86_64]$ ./bin/kibana-keystore add elasticsearch.password
Enter value for elasticsearch.password: *************

6)如果使用logstash向es输出的话,也需要账号密码了

output {
  elasticsearch {
    hosts => "localhost:9200"
    manage_template => false
    index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
    user => "logstash_internal" 
    password => "your_password" 
  }
}

同样的,如果不想在配置文件里明文保存账号密码,可以设置密钥库。

set +o history
export LOGSTASH_KEYSTORE_PASS=mypassword 
set -o history
./bin/logstash-keystore create
./bin/logstash-keystore add ES_USER
./bin/logstash-keystore add ES_PWD

然后在配置文件里可以使用定义的user

output {
  elasticsearch {
    hosts => "localhost:9200"
    manage_template => false
    index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
    user => "${ES_USER}"
    password => "${ES_PWD}"
  }
}

二、es加密http通信

1) 生成证书

[esuser@localhost elasticsearch-7.8.0]$ bin/elasticsearch-certutil http

按照它的提示走

# 这里选择N,一会使用前面创建的ca
Generate a CSR? [y/N]N

Use an existing CA? [y/N]y

# 这里填前面的ca路径
CA Path: /opt/elasticsearch-7.8.0/elastic-stack-ca.p12
# 输入密码
Password for elastic-stack-ca.p12:
# 时间,它有说明 years (e.g. 3Y), months (e.g. 18M), or days (e.g. 90D)
For how long should your certificate be valid? [5y] 50y
# 下面的主机和ip也可以跳过

该命令生成一个zip文件,其中包含要在Elasticsearch和Kibana中使用的证书和密钥。每个文件夹都包含一个自述文件,说明如何使用这些文件。

2)更改配置
将zip解压,里面的 http.p12 复制到 es安装目录下的config文件夹里面。
在elasticsearch.yml文件里增加

# This turns on SSL for the HTTP (Rest) interface
xpack.security.http.ssl.enabled: true

# This configures the keystore to use for SSL on HTTP
xpack.security.http.ssl.keystore.path: "http.p12"

如果证书有密码,可以执行bin/elasticsearch-keystore add xpack.security.http.ssl.secure_key_passphrase将密码加入密码库。

对应的zip里面还有kibana证书,将elasticsearch-ca.pem复制到kibana安装目录下的config文件夹里面。
在kibana.yml文件里修改:

elasticsearch.ssl.certificateAuthorities: [ "config/elasticsearch-ca.pem" ]

# 注意这里,默认是full,会校验主机名,如果在生成证书的时候没有设置主机名,这里可以改成certificate
elasticsearch.ssl.verificationMode: certificate

三、kibana加密http通信

官网文档地址:https://www.elastic.co/guide/en/kibana/7.10/configuring-tls.html

在es安装目录下执行bin/elasticsearch-certutil cert -name kibana-server -dns localhost,127.0.0.1,会生成证书,注意哈,这是我们本地环境使用的,生产环境可以用csr模式发送给受信任的证书颁发机构以获取签名的证书。

将生成的kibana-server.p12复制到kibana安装目录下的config文件夹里面。
在kibana.yml文件里增加:

server.ssl.enabled: true
server.ssl.keystore.path: "config/kibana-server.p12"

如果创建证书时设置了密码,这里要加入密码库:

bin/kibana-keystore add server.ssl.keystore.password

想要了解更多,可以通过 elk相关知识地图 来找到你想要了解的部分进行查看。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐