前言

在es的早期版本中,没有免费提供安全认证的相关功能。
为了防止数据安全问题,一般的措施都是采用IP黑白名单,网络防火墙,
Nginx代理权限控制。
而从es6.8和7.1版本开始,es给我们免费提供了安全功能,也就是今天要介绍的X-Pack功能。


一、ES版本的安全

各版本支持的特性:
https://www.elastic.co/cn/subscriptions
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
安全性配置的官方文档:
https://www.elastic.co/guide/en/elasticsearch/reference/7.13/security-minimal-setup.html

二、ES的安全层级

在这里插入图片描述
说明:
目前ES的安全层级可以分为4层:
1、Minimal security (Elasticsearch Development)
最低安全方案,主要是开发环境使用。此配置通过为内置用户设置密码来防止对本地集群的未授权访问。
对于生产模式集群,最低安全方案是不够的。 如果您的集群有多个节点,您必须启用最低安全性,然后在节点之间配置传输层安全性 (TLS)。

2、basic security(Elasticsearch Production)
此方案通过添加传输层安全性 (TLS) 用于节点之间的通信,以最低安全要求为基础。 适用于生产环境的ES使用。
这一附加层要求节点验证安全证书,以防止未经授权的节点加入您的 Elasticsearch 集群。
Elasticsearch 和 Kibana 之间的外部 HTTP 流量不会被加密,但节点间通信将受到保护。

ES节点直接会验证安装证书,但是Elasticsearch 和 Kibana 之间的外部 HTTP 流量没有用证书保护。

3、basic security + TLS for REST
此方案建立在基本安全方案的基础上,并使用 TLS 保护所有 HTTP 流量。 除了在 Elasticsearch 集群的传输接口上配置 TLS 之外,您还可以在 HTTP 接口上为 Elasticsearch 和 Kibana 配置 TLS。

如果您需要在 HTTP 层上使用双向(双向)TLS,那么您需要配置双向认证加密。

然后,您将 Kibana 和 Beats 配置为使用 TLS 与 Elasticsearch 通信,以便对所有通信进行加密。 此级别的安全性很强,可确保进出集群的任何通信都是安全的。

总结:
ES的安全认证主要有3个级别的配置方案,高级的安全方案都是以低一级的安全方案为基础。

  • 最低方案:基于用户名和密码认证。
  • basic方案:ES节点间添加传输层安全性 (TLS) ,也就是节点采用证书认证
  • 更高的方案:TLS for REST,即http请求也都添加证书认证。

三、X-Pack开启步骤

1.Minimal security配置

在elasticsearch.yml配置文件中添加如下配置:

xpack.security.enabled: true

如果你只有es只有单个节点,还可以添加如下配置:

discovery.type: single-node

重启elasticsearch:
在这里插入图片描述
启动日志中发现,安全认证已经启动。

执行curl localhost:9200,提示权限不够

{
  "error": {
    "root_cause": [
      {
        "type": "security_exception",
        "reason": "missing authentication credentials for REST request [/]",
        "header": {
          "WWW-Authenticate": "Basic realm=\"security\" charset=\"UTF-8\""
        }
      }
    ],
    "type": "security_exception",
    "reason": "missing authentication credentials for REST request [/]",
    "header": {
      "WWW-Authenticate": "Basic realm=\"security\" charset=\"UTF-8\""
    }
  },
  "status": 401
}

内置用户配置密码:
在es进程启动的情况下,执行elasticsearch-setup-passwords工具类,为内置用户配置密码。

如果你希望手动设置密码,可以执行如下命令:

./bin/elasticsearch-setup-passwords interactive

如果你希望自动生产密码,可以执行如下命令:

./bin/elasticsearch-setup-passwords auto

执行结果如下:

Changed password for user apm_system
PASSWORD apm_system = GAkyp0bLv60B1VTqE8KH

Changed password for user kibana_system
PASSWORD kibana_system = u021T4dZgwBNTeAGg4k8

Changed password for user kibana
PASSWORD kibana = u021T4dZgwBNTeAGg4k8

Changed password for user logstash_system
PASSWORD logstash_system = 1xSET6NRTMwuts8ey2pD

Changed password for user beats_system
PASSWORD beats_system = ZgumYOBDA0RiYnv8L5WH

Changed password for user remote_monitoring_user
PASSWORD remote_monitoring_user = z1joG8rlS7ZwNvB2Jacj

Changed password for user elastic
PASSWORD elastic = AndTGV1WmKblRS9oSILp

注意⚠️:
为elastic用户设置密码后,不能再次运行elasticsearch-setup-passwords命令。

尝试使用内置用户elastic访问:

[root@Mobile ~]# curl -X GET -u "elastic:AndTGV1WmKblRS9oSILp"  "localhost:9200/?pretty"
{
  "name" : "node-1",
  "cluster_name" : "my-test",
  "cluster_uuid" : "M5EGtEmzRQG5ri3Yy_Wpxg",
  "version" : {
    "number" : "7.10.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "747e1cc71def077253878a59143c1f785afa92b9",
    "build_date" : "2021-01-13T00:42:12.435326Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

请求成功,说明我们的认证配置正确。

2.配置kibana

tar -zxvf kibana-7.10.2-linux-x86_64.tar.gz
##kibana进程需要和es进程通信,都采用esuser用户启动
chown -R esuser:esuser kibana-7.10.2-linux-x86_64
cd kibana-7.10.2-linux-x86_64/config
vi kibana.yml 

采用cat命名查看启动的配置项:

cat kibana.yml  | egrep -v "^$|#"
server.port: 5601
server.host: "0.0.0.0"
server.name: "my-kibana"
elasticsearch.hosts: ["http://localhost:9200"]
kibana.index: ".kibana"
elasticsearch.username: "kibana_system"
elasticsearch.password: "u021T4dZgwBNTeAGg4k8"
#kibana的界面使用中文显示
i18n.locale: "zh-CN"

注意⚠️:
启用 Elasticsearch 安全功能后,用户必须使用有效的用户名和密码登录 Kibana。
您将配置 Kibana 以使用内置的 kibana_system 用户和您之前创建的密码。 Kibana 执行一些需要使用 kibana_system 用户的后台任务。
此帐户不适用于个人用户,并且无权从浏览器登录 Kibana。 相反,您将以elastic超级用户身份登录 Kibana,使用此超级用户帐户来管理空间、创建新用户和分配角色。

采用kibana-keystore存储用户密码:
1、执行create创建kibana-keystore
2、执行add命令添加elasticsearch.password密钥到kibana-keystore中
3、修改kibana.yml 配置文件,去除elasticsearch.password配置。

[esuser@Mobile bin]$ ./kibana-keystore create
Created Kibana keystore in /usr/local/test/kibana-7.10.2-linux-x86_64/config/kibana.keystore
[esuser@Mobile bin]$ ./kibana-keystore add elasticsearch.password
Enter value for elasticsearch.password: ********************
[esuser@Mobile bin]$ cd ../config/
[esuser@Mobile config]$ vi kibana.yml 

切换成esuser用户,启动kibana

su esuser
./bin/kibana

后台启动,可以执行如下命令:

nohup ./bin/kibana  >>/dev/null 2>&1 &

访问服务器上kibana地址:http://192.168.100.240:5601/
使用内置用户elastic登录
在这里插入图片描述
登录成功:
在这里插入图片描述

在Management中打开开发工具界面:

GET _search
{
  "query": {
    "match_all": {}
  }
}

在这里插入图片描述
在这里插入图片描述
能正常进行请求操作。

3.basic security配置

在最低安全配置中添加密码保护后,您需要配置传输层安全 (TLS)。 传输层处理集群中节点之间的所有内部通信。

如果您的集群有多个节点,那么您必须在节点之间配置 TLS。 如果您不启用 TLS,生产模式集群将不会启动。

配置证书认证:
1、生成certificate authority (CA)

./bin/elasticsearch-certutil ca

说明:
可以选择输入一个ca的密码,在使用ca生成证书和私钥的时候需要用到。
集群必须验证这些证书的真实性。 推荐的方法是信任特定的证书颁发机构 (CA)。 当节点添加到您的集群时,它们必须使用由同一 CA 签署的证书。

2、生成证书和私钥
执行命令:

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

需要输入CA的密码,证书的输出位置,证书的密码

Enter password for CA (elastic-stack-ca.p12) : 
Please enter the desired output file [elastic-certificates.p12]: 
Enter password for elastic-certificates.p12 : 

在这里插入图片描述
3、拷贝证书elastic-certificates.p12到集群每个节点下的ES下的config/目录中

4、集群中每个节点上配置TLS
也就是配置SSL证书

修改elasticsearch.yml配置文件,新增如下配置:

#1、配置集群名称
cluster.name: my-test
#2、配置节点名称,注意唯一性
node.name: node-1
#3、配置证书
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate 
xpack.security.transport.ssl.client_authentication: required
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12

说明:
transport的安全认证只对节点间通信有效,采用rest风格的http请求时,只受用户名、密码认证控制。

5、设置密钥库密码
如果生成证书的时候使用了密码,那么一定要执行以下命令。

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

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

4.配置HTTPS

经过之前的步骤,我们实现了http请求的用户名、密码认证,
es集群节点间的ssl证书认证通信,基本能满足我们的生产环境安全要求。
如果希望加强外部http请求的网络安全,可以配置ES的HTTPS访问。

4.1 生成http证书

./bin/elasticsearch-certutil http

1、是否生成CSR,输入n
2、是否使用已经存在的CA,输入y
3、输入CA的路径,这里注意要使用绝对路径
4、输入CA的密码
5、输入证书过期时间,默认是5年
6、是给每个节点生成一个证书,还是所有节点公用一个证书。这里由于是本地测试,我选择公用一个证书,输入n
如果输入y,则会每个节点生成一个证书,每个证书拥有自己的私钥,对应自己的hostname和IP。
7、输入节点名称
8、输入hostname和IP
9、确认是否选项是否正确,输入y
10、是否开始生成证书,输入y
11、是否希望给证书设置密码,输入密码
12、生成zip文件保存的位置,默认就好
13、重复在每个节点上执行如上步骤

最后会在es的根目录,生成一个elasticsearch-ssl-http.zip文件,使用unzip命令解压后,是如下结构:

/elasticsearch
|_ README.txt
|_ http.p12
|_ sample-elasticsearch.yml 
/kibana
|_ README.txt
|_ elasticsearch-ca.pem
|_ sample-kibana.yml

4.2 配置ES的https

拷贝http.p12文件到ES每个节点上的config目录中

修改elasticsearch.yml配置文件,新增如下配置:

xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: http.p12

如果在生成http证书的时候设置了密码,一定要执行下面的命令保存密码到elasticsearch-keystore中。

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

重启ES后,会发现原来的curl命令不可用

curl -X GET -u "elastic:AndTGV1WmKblRS9oSILp"  "http://localhost:9200/?pretty"
curl: (52) Empty reply from server

改为https还是请求失败,原因是curl命令默认不支持https请求。

 curl -X GET -u "elastic:AndTGV1WmKblRS9oSILp"  "https://localhost:9200/?pretty"
curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.

更加提示,可以通过在curl请求中添加-k的参数,关闭证书认证。

curl -X GET -k -u "elastic:AndTGV1WmKblRS9oSILp"  "https://localhost:9200/?pretty"
{
  "name" : "node-1",
  "cluster_name" : "my-test",
  "cluster_uuid" : "M5EGtEmzRQG5ri3Yy_Wpxg",
  "version" : {
    "number" : "7.10.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "747e1cc71def077253878a59143c1f785afa92b9",
    "build_date" : "2021-01-13T00:42:12.435326Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

也可以浏览器上访问https://192.168.100.240:9200/
在这里插入图片描述
成功访问,说明ES的https配置成功。

4.3 配置Kibana到ES的https

1、将kibanaelasticsearch-ca.pem拷贝到kibana安装根目录下的config目录中

2、修改kibana.yml配置文件

#注意这里要使用绝对目录
elasticsearch.ssl.certificateAuthorities: /usr/local/test/kibana-7.10.2-linux-x86_64/config/elasticsearch-ca.pem
#这里改为https
elasticsearch.hosts: ["https://localhost:9200"]

3、重启kibana

lsof -i:5601
kill -9 pid
/kibana

4、尝试重新使用elastic用户登录kibana,访问正常,说明kibana到es的https请求配置成功。

4.4 配置HTTPS访问Kibana

1、生成p12证书
采用如下命令,生成p12证书

./bin/elasticsearch-certutil cert -name kibana-server -dns kibana.com,localhost,192.168.100.241

根据提示输入证书密码,这里需要注意⚠️,不能输入完全是数字的密码,一定要带有字符。

将生成的证书kibana-server.p12拷贝到kibana的config目录下

2、保存证书密码

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

3、修改kibana.yml配置文件
server.ssl.keystore.path: “/usr/local/test/kibana-7.10.2-linux-x86_64/config/kibana-server.p12”
server.ssl.enabled: true

4、重启kibana,使用https访问成功。

官网还有一种证书配置方式,根据csr生成crt证书。但是由于具体生成命令不清楚,没有成功。

./bin/elasticsearch-certutil csr -name kibana-server -dns kibana.com

生成csr-bundle.zip

解压 unzip csr-bundle.zip

Archive:  csr-bundle.zip
   creating: kibana-server/
  inflating: kibana-server/kibana-server.csr  
  inflating: kibana-server/kibana-server.key  

这里需要根据csr和key值从CA获取crt证书文件。?????

server.ssl.enabled: true
server.ssl.certificate: KBN_PATH_CONF/kibana-server.crt
server.ssl.key: KBN_PATH_CONF/kibana-server.key

四、异常记录:

问题一
keystore password was incorrect
原因:密码库密码不正确
在这里插入图片描述解决:

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

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

问题二:
ERROR: [1] bootstrap checks failed
[1]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
原因:discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes三个配置至少要配置一个。
解决:
修改elasticsearch.yml配置文件

discovery.seed_hosts: ["127.0.0.1"]
cluster.initial_master_nodes: ["node-1"]

问题三:
启动xpack.security.enabled=true后,必须设置xpack.security.transport.ssl.enabled=true

ERROR: [1] bootstrap checks failed
[1]: 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]
ERROR: Elasticsearch did not exit normally - check the logs at /usr/local/test/elasticsearch-7.10.2/logs/my-test.log

解决:
修改elasticsearch.yml配置文件

xpack.security.transport.ssl.enabled=true

问题四:
failed to establish trust with server at [127.0.0.1]; the server provided a certificate with subject name [CN=Elastic Certificate Tool Autogenerated CA] and fingerprint [50757f9c9dc63ef848c122261c4ab06a78119430]; the certificate does not have any subject alternative names; the certificate is self-issued; the [CN=Elastic Certificate Tool Autogenerated CA] certificate is not trusted in this ssl context ([(shared)])
说明:
这是一个警告信息,意思是提醒证书是自行签发的; [CN=Elastic Certificate Tool Autogenerated CA] 证书在此 ssl 上下文中不受信任 ([(shared)])。对功能没有影响,可以忽略。

问题五:
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
说明:
不能够发现证书路径。检测证书路径配置是否正确。


总结

主要介绍了通过开启X-Pack组件,实现对ES安全控制。
1、最初级的实现是用户名、账号
2、其次配置TLS,提升ES集群节点间的安全通信
3、最后,配置HTTPS的证书,提升所有外部http请求的安全性。

Logo

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

更多推荐