Elastic:为 Elasticsearch 启动 HTTPS 访问

162 篇文章 172 订阅
90 篇文章 73 订阅

在今天的文章中,我们来介绍如何使我们的 Elasticsearch 启动 https 服务。这个在很多的场合是非常有用的。特别是在 Elastic SIEM 的安全领域,我们需要把 Elasticsearch 的访问变为 https 的访问,这样使得我们的数据更加安全可靠。

为 Elasticsearch 启动 https访问

安装 Elastic Stack

首先,我们可以按照之前的文章 “Elastic:菜鸟上手指南” 安装 Elasticsearch 及 Kibana。等我们安装好 Elasticsearch 和 Kibana 后,我们可以分别在 localhost:9200 及 localhost:5601 看到我们想要的输出:

为 Elasticsearch 启动安全

我们可以按照我之前的文章 “Elasticsearch:设置 Elastic 账户安全” 为我们的 Elasticsearch 设置安全。我们可以不创建新的用户,只使用默认的 super 用户 elastic。

等我们设置好上面的设置后,在我们启动 Kibana 时它要求我们输入用户名及密码来进行登录。如果你已经看到上面的画面,则表明我们的安全账户设置已经成功了。

生成 p12 证书

我们可以参照链接 “Generate certificates”,在 Elasticsearch 的安装目录下,使用如下的命令:

./bin/elasticsearch-certutil ca
$ pwd
/Users/liuxg/elastic9/elasticsearch-7.6.0
liuxg:elasticsearch-7.6.0 liuxg$ ./bin/elasticsearch-certutil ca
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.bouncycastle.jcajce.provider.drbg.DRBG (file:/Users/liuxg/elastic9/elasticsearch-7.6.0/lib/tools/security-cli/bcprov-jdk15on-1.61.jar) to constructor sun.security.provider.Sun()
WARNING: Please consider reporting this to the maintainers of org.bouncycastle.jcajce.provider.drbg.DRBG
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
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 : 

在上面我们接受缺省的文件名,并输入一个自己熟悉的密码(针对我的情况,我接受空)。我们在 Elasticsearch 的安装目录下,我们可以看见一个生产的证书文件:

$ ls
LICENSE.txt          config               lib
NOTICE.txt           data                 logs
README.asciidoc      elastic-stack-ca.p12 modules
bin                  jdk.app              plugins

我们接着运行如下的命令来生成一个证书:

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

上面的命令将使用我们的 CA 来生成一个证书 elastic-certificates.p12:

$ pwd
/Users/liuxg/elastic9/elasticsearch-7.6.0
liuxg:elasticsearch-7.6.0 liuxg$ ls
LICENSE.txt              data                     logs
NOTICE.txt               elastic-certificates.p12 modules
README.asciidoc          elastic-stack-ca.p12     plugins
bin                      jdk.app
config                   lib

我们把上面的 elastic-certificates.p12 证书拷入到 Elasticsearch 安装目录下的 config 子目录。

$ pwd
/Users/liuxg/elastic9/elasticsearch-7.6.0
liuxg:elasticsearch-7.6.0 liuxg$ ls config/
elastic-certificates.p12 jvm.options              roles.yml
elasticsearch.keystore   log4j2.properties        users
elasticsearch.yml        role_mapping.yml         users_roles

我们使用如下的命令:

openssl pkcs12 -in elastic-stack-ca.p12 -out newfile.crt.pem -clcerts -nokeys

它将生成一个叫做 newfile.crt.pem 的文件。我们把这个文件拷入到 Kibana 安装目录下的 config 子目录中:

$ pwd
/Users/liuxg/elastic9/kibana-7.6.0-darwin-x86_64
liuxg:kibana-7.6.0-darwin-x86_64 liuxg$ ls config/
apm.js                   kibana.yml
elastic-certificates.p12 newfile.crt.pem

配置 Elasticsearch

我们接下来配置在 Elasticsearch 中的 config/elasticsearch.yml。我们参照 Elastic 官方文档 “Encrypting communication in Elasticsearch”,我们添加如下的配置:

xpack.security.transport.ssl.enabled: true
xpack.security.http.ssl.enabled: true
xpack.security.authc.api_key.enabled: true
xpack.security.http.ssl.keystore.path: elastic-certificates.p12
xpack.security.http.ssl.truststore.path: elastic-certificates.p12

我们重新启动 Elasticsearch:

./bin/elasticsearch

这样我们的 Elasticsearch 已经成功地运行于 https 模式。我们在 chrome 中打入地址 https://localhost:9200,我们可以看到:

显然我们不可以再像以前那样访问本地9200口的地址了。当上面的页面出现后,我们打入如下的字符串(在chrome为当前窗口的情况下):

thissisunsafe

我们打入 elastic 及我们之前设定的 Elasticsearch 的密码,再点击 “Sign In”:

从上面我们可以看出来,我们的 Elasticsearch 的安装时正确的。

我们也可以使用 Safari 浏览器来打开:

我们点击 Show Details 按钮:

我们点击 visit this website 链接:

和上面一样,我们输入之前在创建安全账号时的用户名 elastic 及密码,那么我们就可以访问 Elasticsearch:

如果我们使用 Postman,我们可以通过在 “Settings” 里做如下的配置来避免证书的检查:

我们关掉上面的 SSL certificate verification 开关:

经过上面的设置后,我们可以在 Postman 中访问具有 https 的 Elasticsearch。关于如何使用 Postman 来访问 Elasticsearch,请参考我之前的文章 “Elastic:使用Postman来访问Elastic Stack”。

配置 Kibana

为了能够使我们的 Kibana 能够顺利地访问带有 https 的 Elasticsearch。我们也需要做相应的配置。我们打开 config/kibana.yml。添加如下的设置:

elasticsearch.hosts: ["https://localhost:9200"]
elasticsearch.ssl.certificateAuthorities: ["config/newfile.crt.pem"]
elasticsearch.ssl.verificationMode: none

在上面我们把之前生成的 newfile.crt.pem 的证书填入到上面的路径中,同时为了我们能够方便地访问,我们针对 kibana 不启用 verificationMode。

等我们配置完后,我们重新启动 Kibana:

我们输入 elastic 用户的密码,我们就可以进入到 Kibana 的界面中:

在上面我们可以看到我们已经成功地进入到 Kibana 的界面中了。

把 Beats 数据传入到 https 的 Elasticsearch中

由于引入了 https,那么我们该如何把我们的数据传入到 Elasticsearch 中呢?在导入数据时,我们必须把证书配置到 beats 的配置文件中。我们就以 filebeat 为例。我们在 Elasticsearch 的安装目录中,打入如下的命令:

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

上面的命令将会生成一个叫做 “certificate-bundle.zip” 的文件。

$ pwd
/Users/liuxg/elastic9/elasticsearch-7.6.0
liuxg:elasticsearch-7.6.0 liuxg$ ls
LICENSE.txt            certificate-bundle.zip lib
NOTICE.txt             config                 logs
README.asciidoc        data                   modules
bin                    jdk.app                plugins

我们可以把这个文件的一个进行解压,并把里面的 ca.crt 解压到 filebeat 的安装子目录中。

方法一

打开 filebeat 的配置文件 filebeat.yml,并添加证书信息:

filebeat.yml

output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["localhost:9200"]

  # Protocol - either `http` (default) or `https`.
  protocol: "https"

  # Authentication credentials - either API key or username/password.
  #api_key: "id:api_key"
  username: "elastic"
  password: "123456"
  ssl.certificate_authorities: ["/Users/liuxg/elastic9/filebeat-7.6.0-darwin-x86_64/ca.crt"]
  ssl.verification_mode: none

在上面,你需要填入自己的 username 及 password,同时也需要把上面的路径换成自己的证书路径。

方法二

我们也可以用如下的命令来生产自己的证书。我们在 Elasticsearch 的安装目录下,在已经生成上面的 elastic-stack-ca.p12 前提下,运行如下的命令:

openssl pkcs12 -in elastic-stack-ca.p12 -out newfile.crt.pem -clcerts -nokeys

上面的命令将生成一个叫做 newfile.crt.pem 的文件。我们把这个文件拷贝到 filebeat 的安装目录下,并修改我们的 filebeat.yml 如下:

output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["localhost:9200"]

  # Protocol - either `http` (default) or `https`.
  protocol: "https"

  # Authentication credentials - either API key or username/password.
  #api_key: "id:api_key"
  username: "elastic"
  password: "123456"
  ssl.certificate_authorities: ["/Users/liuxg/elastic9/filebeat-7.6.0-darwin-x86_64/newfile.crt.pem"]
  ssl.verification_mode: none

运行 Filebeat

修改完上面的配置后,我们启动 system 模块:

./filebeat modules enable system
./filebeat setup
$ ./filebeat setup
Overwriting ILM policy is disabled. Set `setup.ilm.overwrite:true` for enabling.

Index setup finished.
Loading dashboards (Kibana must be running and reachable)
Loaded dashboards
Setting up ML using setup --machine-learning is going to be removed in 8.0.0. Please use the ML app instead.
See more: https://www.elastic.co/guide/en/elastic-stack-overview/current/xpack-ml.html
Loaded machine learning job configurations
Loaded Ingest pipelines

我们可以通过如下的命令来运行 filebeat:

./filebeat -e

我们打开 Kibana:

点击上面的 [Filebeat System] Syslog dashboard ECS:

我们可以看到 filebeat 的数据成功地传入到 Elasticsearch 中了。

参考:

【1】Configure TLS | Elasticsearch Guide [master] | Elastic

【2】Generate certificates | Elasticsearch Guide [8.1] | Elastic

【3】How to setup TLS for Elasticsearch, Kibana, Logstash and Filebeat with offline install in Linux | Elastic Blog

【4】How to Secure Your Elastic Stack (Plus Kibana, Logstash and Beats) | Official Pythian®® Blog

【5】Configuring SSL, TLS, and HTTPS to secure Elasticsearch, Kibana, Beats, and Logstash | Elastic Blog

  • 11
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 44
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 44
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值