Elastic:为Elasticsearch启动https访问

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

安装Elastic Stack

首先,我们可以按照之前的文章“Elastic:菜鸟上手指南” 安装Elasticsearch及Kibana。等我们安装好Elasticsearch和Kibana后,我们可以分别在localhost:9200localhost: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: /Users/liuxg/elastic9/elasticsearch-7.6.0/config/elastic-certificates.p12
xpack.security.http.ssl.truststore.path: /Users/liuxg/elastic9/elasticsearch-7.6.0/config/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: ["/Users/liuxg/elastic9/kibana-7.6.0-darwin-x86_64/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】https://www.elastic.co/guide/en/elasticsearch/reference/master/configuring-tls.html

【2】https://www.elastic.co/guide/en/elasticsearch/reference/current/encrypting-communications-certificates.html

【3】https://www.elastic.co/blog/how-to-setup-tls-for-elasticsearch-kibana-logstash-filebeat-with-offline-install-in-linux

发布了517 篇原创文章 · 获赞 126 · 访问量 91万+

猜你喜欢

转载自blog.csdn.net/UbuntuTouch/article/details/105044365