当前位置:   article > 正文

Elasticsearch:配置 TLS/SSL 和 PKI 身份验证_elasticsearch ssl

elasticsearch ssl

当为使用生产许可证运行的集群启用 Elasticsearch 安全性时,必须使用 TLS/SSL 进行传输通信,并且必须正确设置。此外,一旦启用安全性,与 Elasticsearch 集群的所有通信都必须经过身份验证,包括来自 Kibana 和/或应用程序服务器的通信。

Kibana 和/或应用程序服务器向 Elasticsearch 集群进行身份验证的最简单方法是在其配置文件或源代码中嵌入用户名和密码。但是,在许多组织中,禁止在此类位置存储用户名和密码。在这种情况下,一种替代方法是使用公钥基础设施 (PKI - Public Key Infrastructure)(客户端证书)对 Elasticsearch 集群进行身份验证。

配置安全性以及 TLS/SSL 和 PKI 起初似乎令人生畏,因此本博客提供了有关如何的分步说明: 启用安全性;配置TLS/SSL;为内置用户设置密码;使用PKI进行认证;最后,如何使用 PKI 向 Elasticsearch 集群验证 Kibana。

启用安全

首先,我们按照我之前的教程来安装好 Elasticsearch 及 Kibana。我先直接按照默认的方式来进行安装,不做任何的配置:

接下来,我们修改 elasticsearch.yml 以及 kibana.ym 配置文件来完成安全的配置。详细的步骤我们可以参考我之前的文章 “Elasticsearch:设置 Elastic 账户安全”。请注意:如果你是针对多个节点的配置,那么你不需要在 elasticsearch.yml 中配置 discovery.type: single-node。我们需要记住超级用户 elastic 的密码以便在一下的步骤中使用。

等我们完成这一步,我们会发现,我们在 Kibana 的配置文件中还是使用如下类似的用户名及密码把这些安全的信息暴露在外:

config/kibana.yml

  1. elasticsearch.username: "kibana_system"
  2. elasticsearch.password: "password"

我们希望在如下的步骤中尽量不要这么做,因为这样的做法可能会造成安全的泄露。

TLS/SSL 加密

Elasticsearch 有两个级别的通信,传输通信和 http 通信。 传输协议用于 Elasticsearch 节点之间的内部通信,http 协议用于客户端到 Elasticsearch 集群的通信。 保护这些通信的安全将在以下段落中讨论。

传输 TLS/SSL 加密

传输协议用于 Elasticsearch 集群内节点之间的通信。因为 Elasticsearch 集群中的每个节点对于集群中的其他节点来说既是客户端又是服务器,所以所有传输证书必须既是客户端证书又是服务器证书。如果 TLS/SSL 证书没有定义扩展密钥用法(extended key usage),那么它们已经是事实上的客户端和服务器证书。如果传输证书确实有扩展密钥使用部分(企业环境中使用的 CA 签名证书通常是这种情况),那么它们必须明确启用 clientAuth 和 serverAuth。

Elasticsearch 附带一个名为 elasticsearch-certutil 的实用程序,可用于生成自签名证书,该证书可用于加密 Elasticsearch 集群内的内部通信。

以下命令可用于生成可用于传输通信的证书,如 Elasticsearch 中的加密通信页面中所述:

  1. $ bin/elasticsearch-certutil ca
  2. This tool assists you in the generation of X.509 certificates and certificate
  3. signing requests for use with SSL/TLS in the Elastic stack.
  4. The 'ca' mode generates a new 'certificate authority'
  5. This will create a new X.509 certificate and private key that can be used
  6. to sign certificate when running in 'cert' mode.
  7. Use the 'ca-dn' option if you wish to configure the 'distinguished name'
  8. of the certificate authority
  9. By default the 'ca' mode produces a single PKCS#12 output file which holds:
  10. * The CA certificate
  11. * The CA's private key
  12. If you elect to generate PEM format certificates (the -pem option), then the output will
  13. be a zip file containing individual files for the CA certificate and private key
  14. Please enter the desired output file [elastic-stack-ca.p12]:
  15. Enter password for elastic-stack-ca.p12 :

在上面,我们打入两个 enter。上面的命令在当前目录下生产一个叫做 elastic-stack-ca.p12 的文件。

  1. $ pwd
  2. /Users/liuxg/elastic0/elasticsearch-7.15.0
  3. $ ls
  4. LICENSE.txt config lib
  5. NOTICE.txt data logs
  6. README.asciidoc elastic-stack-ca.p12 modules
  7. bin jdk.app plugins

然后,我们打入如下的命令:

  1. $ bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
  2. This tool assists you in the generation of X.509 certificates and certificate
  3. signing requests for use with SSL/TLS in the Elastic stack.
  4. The 'cert' mode generates X.509 certificate and private keys.
  5. * By default, this generates a single certificate and key for use
  6. on a single instance.
  7. * The '-multiple' option will prompt you to enter details for multiple
  8. instances and will generate a certificate and key for each one
  9. * The '-in' option allows for the certificate generation to be automated by describing
  10. the details of each instance in a YAML file
  11. * An instance is any piece of the Elastic Stack that requires an SSL certificate.
  12. Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats
  13. may all require a certificate and private key.
  14. * The minimum required value for each instance is a name. This can simply be the
  15. hostname, which will be used as the Common Name of the certificate. A full
  16. distinguished name may also be used.
  17. * A filename value may be required for each instance. This is necessary when the
  18. name would result in an invalid file or directory name. The name provided here
  19. is used as the directory name (within the zip) and the prefix for the key and
  20. certificate files. The filename is required if you are prompted and the name
  21. is not displayed in the prompt.
  22. * IP addresses and DNS names are optional. Multiple values can be specified as a
  23. comma separated string. If no IP addresses or DNS names are provided, you may
  24. disable hostname verification in your SSL configuration.
  25. * All certificates generated by this tool will be signed by a certificate authority (CA)
  26. unless the --self-signed command line option is specified.
  27. The tool can automatically generate a new CA for you, or you can provide your own with
  28. the --ca or --ca-cert command line options.
  29. By default the 'cert' mode produces a single PKCS#12 output file which holds:
  30. * The instance certificate
  31. * The private key for the instance certificate
  32. * The CA certificate
  33. If you specify any of the following options:
  34. * -pem (PEM formatted output)
  35. * -keep-ca-key (retain generated CA key)
  36. * -multiple (generate multiple certificates)
  37. * -in (generate certificates from an input file)
  38. then the output will be be a zip file containing individual certificate/key files
  39. Enter password for CA (elastic-stack-ca.p12) :
  40. Please enter the desired output file [elastic-certificates.p12]:
  41. Enter password for elastic-certificates.p12 :
  42. Certificates written to /Users/liuxg/elastic0/elasticsearch-7.15.0/elastic-certificates.p12
  43. This file should be properly secured as it contains the private key for
  44. your instance.
  45. This file is a self contained file and can be copied and used 'as is'
  46. For each Elastic product that you wish to configure, you should copy
  47. this '.p12' file to the relevant configuration directory
  48. and then follow the SSL configuration instructions in the product guide.
  49. For client applications, you may only need to copy the CA certificate and
  50. configure the client to trust this certificate.

在上面的命令中,我们打入三个 enter 键。上面的命令在当前的目录中生产一个叫做 elastic-certificates.p12 的文件:

  1. $ pwd
  2. /Users/liuxg/elastic0/elasticsearch-7.15.0
  3. $ ls
  4. LICENSE.txt data logs
  5. NOTICE.txt elastic-certificates.p12 modules
  6. README.asciidoc elastic-stack-ca.p12 plugins
  7. bin jdk.app
  8. config lib

一旦执行了上述命令,我们将拥有可用于加密通信的 TLS/SSL 证书。

新创建的证书应复制到位于 config 目录中名为 certs 的子目录中。 然后将在 elasticsearch.yml 文件中指定证书,如下所示:

  1. $ pwd
  2. /Users/liuxg/elastic0/elasticsearch-7.15.0
  3. $ tree -L 2 ./config/
  4. ./config/
  5. ├── certs
  6. │   └── elastic-certificates.p12
  7. ├── elasticsearch.keystore
  8. ├── elasticsearch.yml
  9. ├── jvm.options
  10. ├── jvm.options.d
  11. ├── log4j2.properties
  12. ├── role_mapping.yml
  13. ├── roles.yml
  14. ├── users
  15. └── users_roles

我们在 config/elasticsearch.yml 文件中填入如下的配置:

  1. xpack.security.transport.ssl.enabled: true
  2. xpack.security.transport.ssl.verification_mode: certificate
  3. xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
  4. xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12

现在重新启动 Elasticsearch 集群中的所有节点以使上述更改生效。

HTTP TLS/SSL 加密

对于 HTTP 通信,Elasticsearch 节点将仅充当服务器,因此可以使用服务器证书。HTTP TLS/SSL 证书不需要启用客户端身份验证。

Elasticsearch 中有两种重要的网络通信机制需要了解:

  • HTTP:用于 HTTP 通信绑定的地址和端口,这是 Elasticsearch REST API 公开的方式
  • transport:用于集群内节点之间的内部通信

在许多情况下,HTTP 通信的证书将由公司 CA 签署。 值得注意的是,用于加密 HTTP 通信的证书可以完全独立于用于传输通信(transport communication)的证书。

为了减少本博客中的步骤数量,我们将使用与传输通信相同的证书进行 HTTP 通信。 这些在 config/elasticsearch.yml 文件中指定如下:

  1. xpack.security.http.ssl.enabled: true
  2. xpack.security.http.ssl.keystore.path: certs/elastic-certificates.p12
  3. xpack.security.http.ssl.truststore.path: certs/elastic-certificates.p12
  4. xpack.security.http.ssl.client_authentication: optional

如果这个时候,我们重新启动 Elasticsearch 的话,那么我们可以看到如下的日志信息:

 我们的 Kibana 再也连接不到 Elasticsearch 了。我们需要在如下的步骤中更进一步对它进行配置。

启用 PKI 身份验证

如配置 PKI 领域中所述,必须将以下内容添加到 config/elasticsearch.yml 文件中以允许 PKI 身份验证。

Elastic Stack 6.8 以前

xpack.security.authc.realms.pki1.type: pki

Elastic Stack 7.x

xpack.security.authc.realms.pki.pki1.order: 1

那么到目前位置,我们针对 config/elasticsearch.yml 所做的所有的配置总结如下:

config/elasticsearch.yml

  1. xpack.security.enabled: true
  2. xpack.security.transport.ssl.enabled: true
  3. xpack.security.transport.ssl.verification_mode: certificate
  4. xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
  5. xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12
  6. xpack.security.http.ssl.enabled: true
  7. xpack.security.http.ssl.keystore.path: certs/elastic-certificates.p12
  8. xpack.security.http.ssl.truststore.path: certs/elastic-certificates.p12
  9. xpack.security.http.ssl.client_authentication: optional
  10. xpack.security.authc.realms.pki.pki1.order: 1

我们需要把上面的配置添加到 config/elasticsearch.yml 文件的最后,并重新启动  Elasticsearch。当然,我们的 Kibana 如果没有做任何的修改,它是不会连接到 Elasticsearch 的。

创建客户端证书

将用于 PKI 身份验证的证书必须由与用于加密 HTTP 通信的证书相同的 CA 签名。 通常,这些将由组织内的官方 CA 签署。 但是,因为我们已经使用了自签名 CA,所以我们还使用我们之前保存为 elastic-stack-ca.p12 的相同自签名 CA 来签署我们的 HTTP 客户端证书。 我们可以为客户端身份验证创建证书,如下所示:

  1. $ pwd
  2. /Users/liuxg/elastic0/elasticsearch-7.15.0
  3. $ ls config/certs/
  4. elastic-certificates.p12 elastic-stack-ca.p12
  5. $ ./bin/elasticsearch-certutil cert --ca config/certs/elastic-stack-ca.p12 -name "CN=something,OU=Consulting Team,DC=mydomain,DC=com"
  6. This tool assists you in the generation of X.509 certificates and certificate
  7. signing requests for use with SSL/TLS in the Elastic stack.
  8. The 'cert' mode generates X.509 certificate and private keys.
  9. * By default, this generates a single certificate and key for use
  10. on a single instance.
  11. * The '-multiple' option will prompt you to enter details for multiple
  12. instances and will generate a certificate and key for each one
  13. * The '-in' option allows for the certificate generation to be automated by describing
  14. the details of each instance in a YAML file
  15. * An instance is any piece of the Elastic Stack that requires an SSL certificate.
  16. Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats
  17. may all require a certificate and private key.
  18. * The minimum required value for each instance is a name. This can simply be the
  19. hostname, which will be used as the Common Name of the certificate. A full
  20. distinguished name may also be used.
  21. * A filename value may be required for each instance. This is necessary when the
  22. name would result in an invalid file or directory name. The name provided here
  23. is used as the directory name (within the zip) and the prefix for the key and
  24. certificate files. The filename is required if you are prompted and the name
  25. is not displayed in the prompt.
  26. * IP addresses and DNS names are optional. Multiple values can be specified as a
  27. comma separated string. If no IP addresses or DNS names are provided, you may
  28. disable hostname verification in your SSL configuration.
  29. * All certificates generated by this tool will be signed by a certificate authority (CA)
  30. unless the --self-signed command line option is specified.
  31. The tool can automatically generate a new CA for you, or you can provide your own with
  32. the --ca or --ca-cert command line options.
  33. By default the 'cert' mode produces a single PKCS#12 output file which holds:
  34. * The instance certificate
  35. * The private key for the instance certificate
  36. * The CA certificate
  37. If you specify any of the following options:
  38. * -pem (PEM formatted output)
  39. * -keep-ca-key (retain generated CA key)
  40. * -multiple (generate multiple certificates)
  41. * -in (generate certificates from an input file)
  42. then the output will be be a zip file containing individual certificate/key files
  43. Enter password for CA (config/certs/elastic-stack-ca.p12) :
  44. Please enter the desired output file [CN=something,OU=Consulting Team,DC=mydomain,DC=com.p12]: client.p12
  45. Enter password for client.p12 :
  46. Certificates written to /Users/liuxg/elastic0/elasticsearch-7.15.0/client.p12
  47. This file should be properly secured as it contains the private key for
  48. your instance.
  49. This file is a self contained file and can be copied and used 'as is'
  50. For each Elastic product that you wish to configure, you should copy
  51. this '.p12' file to the relevant configuration directory
  52. and then follow the SSL configuration instructions in the product guide.
  53. For client applications, you may only need to copy the CA certificate and
  54. configure the client to trust this certificate.

我们可以在当前的目录中发现如下的一个叫做 client.p12 的文件:

  1. $ pwd
  2. /Users/liuxg/elastic0/elasticsearch-7.15.0
  3. $ ls
  4. LICENSE.txt config lib
  5. NOTICE.txt data logs
  6. README.asciidoc elastic-certificates.p12 modules
  7. bin elastic-stack-ca.p12 plugins
  8. client.p12 jdk.app

以上将创建一个名为 client.p12 的文件,其中包含对我们的 Elasticsearch 集群进行 PKI 身份验证所需的所有信息。 但是,为了使用此证书,将其分解为私钥、公共证书和 CA 证书是有帮助的。 这可以通过以下命令完成:

Private key

openssl pkcs12 -in client.p12 -nocerts -nodes > client.key
  1. $ openssl pkcs12 -in client.p12 -nocerts -nodes > client.key
  2. Enter Import Password:
  3. $ ls
  4. LICENSE.txt client.p12 jdk.app
  5. NOTICE.txt config lib
  6. README.asciidoc data logs
  7. bin elastic-certificates.p12 modules
  8. client.key elastic-stack-ca.p12 plugins

上面的命令将生成一个叫做 client.key 的文件。

Public Certificate

openssl pkcs12 -in client.p12 -clcerts -nokeys  > client.cer
  1. $ openssl pkcs12 -in client.p12 -clcerts -nokeys > client.cer
  2. Enter Import Password:
  3. $ ls
  4. LICENSE.txt client.p12 lib
  5. NOTICE.txt config logs
  6. README.asciidoc data modules
  7. bin elastic-certificates.p12 plugins
  8. client.cer elastic-stack-ca.p12
  9. client.key jdk.app

上面文件将生成一个叫做 client.cer 的文件。

CA  Certificate

  1. $ openssl pkcs12 -in client.p12 -cacerts -nokeys -chain > client-ca.cer
  2. Enter Import Password:
  3. $ ls
  4. LICENSE.txt client.key jdk.app
  5. NOTICE.txt client.p12 lib
  6. README.asciidoc config logs
  7. bin data modules
  8. client-ca.cer elastic-certificates.p12 plugins
  9. client.cer elastic-stack-ca.p12

上述命令将生成一个叫做 client-ca.cer 的文件。

我们接下来在 Kibana 的安装目录中创建一个叫做 config/certs 的目录,并把上面创建的三个文件移到该目录中去:

  1. $ pwd
  2. /Users/liuxg/elastic0/kibana-7.15.0-darwin-x86_64
  3. $ tree -L 2 config/certs
  4. config/certs
  5. ├── client-ca.cer
  6. ├── client.cer
  7. └── client.key

配置 Kibana 以对 Elasticsearch 进行身份验证

现在我们已经在 Elasticsearch 集群上启用了安全性,必须对与集群的通信进行身份验证。 因此,如果我们计划使用 Kibana 与集群交互,那么我们必须启用安全性并配置 Kibana 以通过 HTTPS 以 kibana 用户身份向集群进行身份验证。 由于我们尚未完全设置从 Kibana 到 Elasticsearch 集群的 PKI 身份验证,因此必须首先使用 kibana 用户和密码进行身份验证。 这可以通过 config/kibana.yml 文件中的以下几行来完成:

config/kibana.yml (6.8 and earlier)

  1. elasticsearch.url: "https://localhost:9200" #ensure https not http
  2. xpack.security.enabled: true
  3. elasticsearch.username: "kibana"
  4. elasticsearch.password: "your kibana password goes here"
  5. elasticsearch.ssl.certificateAuthorities: config/certs/client-ca.cer
  6. elasticsearch.ssl.verificationMode: certificate

config/kibana.yml (7.x)

  1. elasticsearch.hosts: "https://localhost:9200"
  2. xpack.security.enabled: true
  3. elasticsearch.username: "kibana_system"
  4. elasticsearch.password: "your kibana password goes here"
  5. elasticsearch.ssl.certificateAuthorities: config/certs/client-ca.cer
  6. elasticsearch.ssl.verificationMode: certificate

请注意:在上面,我们必须使用 https 来进行访问而不是默认的 http。确保我们将 localhost 更改为我们的 Elasticsearch 节点之一的名称,并且证书在 Kibana 文件夹内的 config/certs 目录中可用。

请注意,kibana 用户就像一个服务帐户,它在幕后工作以向 Elasticsearch 集群验证 Kibana 应用程序。 我们通常不会以 kibana 用户的身份直接登录 Elasticsearch 集群或 Kibana UI。

重新启动 Kibana 以使其以 kibana 用户身份向 Elasticsearch 集群进行身份验证。 我们现在应该能够以 elastic 内置超级用户身份通过 Kibana UI 登录。我们使用 http://localhost:5601 进行登录:

 

PKI 认证

我们可以使用三个新的客户端证书文件:

  1. $ pwd
  2. /Users/liuxg/elastic0/kibana-7.15.0-darwin-x86_64
  3. $ tree -L 2 config/certs/
  4. config/certs/
  5. ├── client-ca.cer
  6. ├── client.cer
  7. └── client.key

通过 curl 来测试对集群的 PKI 身份验证。 打开一个新终端,cd 到 Kibana 的 config/certs 目录,使用 curl 调用身份验证 API,如下所示。

  1. curl https://localhost:9200/_xpack/security/_authenticate?pretty \
  2. --key client.key --cert client.cer --cacert client-ca.cer -k -v

请务必将 localhost 替换为我们 Elasticsearch 集群中的节点名称,并确保使用 https(而不是 http)。 另请注意,-k 选项是必需的,因为我们没有使用指定的主机名创建证书,因此必须关闭主机名验证。

我们执行上面的指令后,我们在 Elasticsearch 的日志中可以看到如下的信息:

 它表明我们当前的 licsens 不支持 pki/pki1。我们需要启动试用功能。我们可以通过如下的命令:

curl -X POST "localhost:9200/_xpack/license/start_trial?acknowledge=true"

或者在 Kibana 的界面中,按照如下的步骤来进去启动:

 

 

 

这样就启动了试用许可的功能。更多关于白金版及黄金版的信息可以在 Elastic 的官方网站 Elasticsearch 官方定价信息:Elastic Cloud、托管型 Elasticsearch | Elastic 找到更多的信息。

启动试用后,重新运行上面的命令:

  1. $ curl https://localhost:9200/_xpack/security/_authenticate?pretty --key client.key --cert client.cer --cacert client-ca.cer -k -v
  2. * Trying ::1:9200...
  3. * Connected to localhost (::1) port 9200 (#0)
  4. * ALPN, offering http/1.1
  5. * successfully set certificate verify locations:
  6. * CAfile: client-ca.cer
  7. CApath: none
  8. * TLSv1.3 (OUT), TLS handshake, Client hello (1):
  9. * TLSv1.3 (IN), TLS handshake, Server hello (2):
  10. * TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
  11. * TLSv1.3 (IN), TLS handshake, Request CERT (13):
  12. * TLSv1.3 (IN), TLS handshake, Certificate (11):
  13. * TLSv1.3 (IN), TLS handshake, CERT verify (15):
  14. * TLSv1.3 (IN), TLS handshake, Finished (20):
  15. * TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
  16. * TLSv1.3 (OUT), TLS handshake, Certificate (11):
  17. * TLSv1.3 (OUT), TLS handshake, CERT verify (15):
  18. * TLSv1.3 (OUT), TLS handshake, Finished (20):
  19. * SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
  20. * ALPN, server did not agree to a protocol
  21. * Server certificate:
  22. * subject: CN=instance
  23. * start date: Sep 30 10:44:43 2021 GMT
  24. * expire date: Sep 29 10:44:43 2024 GMT
  25. * issuer: CN=Elastic Certificate Tool Autogenerated CA
  26. * SSL certificate verify ok.
  27. > GET /_xpack/security/_authenticate?pretty HTTP/1.1
  28. > Host: localhost:9200
  29. > User-Agent: curl/7.71.1
  30. > Accept: */*
  31. >
  32. * TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
  33. * Mark bundle as not supporting multiuse
  34. < HTTP/1.1 200 OK
  35. < X-elastic-product: Elasticsearch
  36. < Warning: 299 Elasticsearch-7.15.0-79d65f6e357953a5b3cbcc5e2c7c21073d89aa29 "[GET /_xpack/security/_authenticate] is deprecated! Use [GET /_security/_authenticate] instead."
  37. < content-type: application/json; charset=UTF-8
  38. < content-length: 376
  39. <
  40. {
  41. "username" : "something",
  42. "roles" : [ ],
  43. "full_name" : null,
  44. "email" : null,
  45. "metadata" : {
  46. "pki_dn" : "CN=something, OU=Consulting Team, DC=mydomain, DC=com"
  47. },
  48. "enabled" : true,
  49. "authentication_realm" : {
  50. "name" : "pki1",
  51. "type" : "pki"
  52. },
  53. "lookup_realm" : {
  54. "name" : "pki1",
  55. "type" : "pki"
  56. },
  57. "authentication_type" : "realm"
  58. }
  59. * Connection #0 to host localhost left intact

请注意,roles 值当前为空,这意味着尽管我们已通过 Elasticsearch 的身份验证,但我们无权执行任何操作。 身份验证是允许的,因为我们发送到集群的客户端证书是由与 Elasticsearch 节点使用的 http TLS/SSL 证书相同的 CA 签署的。 现在我们已经通过身份验证,我们需要授权这个用户能够做一些事情。

从身份验证 API 返回的 pki_dn 值将用于配置将分配给此证书的角色。

打开 Kibana UI,如果我们还没有这样做,请以弹性用户身份登录。 由于 elastic 用户具有超级用户权限,因此该用户可以为证书分配角色。 从 Kibana 中的 Dev Tools 执行以下命令,确保之前返回的 pki_dn 值复制到 dn 字段中,如下所示:

  1. PUT _xpack/security/role_mapping/kibana_certificate_authorization
  2. {
  3. "roles": [
  4. "kibana_system"
  5. ],
  6. "rules": {
  7. "field": {
  8. "dn": "CN=something, OU=Consulting Team, DC=mydomain, DC=com"
  9. }
  10. },
  11. "enabled": true
  12. }

执行完上面的操作后,我们可以在 Kibana 的界面看到一个额外的信息:

现在我们已经为这个证书分配了 kibana_system 角色,通过另一个对身份验证 API 的调用来验证它的设置是否正确:

  1. curl https://localhost:9200/_xpack/security/_authenticate?pretty \
  2. --key client.key --cert client.cer --cacert client-ca.cer -k -v

我们应该会看到以下响应,这表明我们现在已将 “kibana_system” 角色分配给此证书。

  1. $ curl https://localhost:9200/_xpack/security/_authenticate?pretty \
  2. > --key client.key --cert client.cer --cacert client-ca.cer -k -v
  3. * Trying ::1:9200...
  4. * Connected to localhost (::1) port 9200 (#0)
  5. * ALPN, offering http/1.1
  6. * successfully set certificate verify locations:
  7. * CAfile: client-ca.cer
  8. CApath: none
  9. * TLSv1.3 (OUT), TLS handshake, Client hello (1):
  10. * TLSv1.3 (IN), TLS handshake, Server hello (2):
  11. * TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
  12. * TLSv1.3 (IN), TLS handshake, Request CERT (13):
  13. * TLSv1.3 (IN), TLS handshake, Certificate (11):
  14. * TLSv1.3 (IN), TLS handshake, CERT verify (15):
  15. * TLSv1.3 (IN), TLS handshake, Finished (20):
  16. * TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
  17. * TLSv1.3 (OUT), TLS handshake, Certificate (11):
  18. * TLSv1.3 (OUT), TLS handshake, CERT verify (15):
  19. * TLSv1.3 (OUT), TLS handshake, Finished (20):
  20. * SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
  21. * ALPN, server did not agree to a protocol
  22. * Server certificate:
  23. * subject: CN=instance
  24. * start date: Sep 30 10:44:43 2021 GMT
  25. * expire date: Sep 29 10:44:43 2024 GMT
  26. * issuer: CN=Elastic Certificate Tool Autogenerated CA
  27. * SSL certificate verify ok.
  28. > GET /_xpack/security/_authenticate?pretty HTTP/1.1
  29. > Host: localhost:9200
  30. > User-Agent: curl/7.71.1
  31. > Accept: */*
  32. >
  33. * TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
  34. * Mark bundle as not supporting multiuse
  35. < HTTP/1.1 200 OK
  36. < X-elastic-product: Elasticsearch
  37. < Warning: 299 Elasticsearch-7.15.0-79d65f6e357953a5b3cbcc5e2c7c21073d89aa29 "[GET /_xpack/security/_authenticate] is deprecated! Use [GET /_security/_authenticate] instead."
  38. < content-type: application/json; charset=UTF-8
  39. < content-length: 398
  40. <
  41. {
  42. "username" : "something",
  43. "roles" : [
  44. "kibana_system"
  45. ],
  46. "full_name" : null,
  47. "email" : null,
  48. "metadata" : {
  49. "pki_dn" : "CN=something, OU=Consulting Team, DC=mydomain, DC=com"
  50. },
  51. "enabled" : true,
  52. "authentication_realm" : {
  53. "name" : "pki1",
  54. "type" : "pki"
  55. },
  56. "lookup_realm" : {
  57. "name" : "pki1",
  58. "type" : "pki"
  59. },
  60. "authentication_type" : "realm"
  61. }
  62. * Connection #0 to host localhost left intact

使用 PKI 向 Elasticsearch 集群验证 Kibana

现在我们已经测试了我们的客户端证书并为该证书分配了 “kibana_system” 角色,我们可以使用这个证书而不是用户名和密码来向 Elasticsearch 验证 Kibana。

从我们的 config/kibana.yml 文件中删除以下几行:

6.8 及以前

  1. elasticsearch.username: "kibana"
  2. elasticsearch.password: "XXXXXX"

或者

7.x:

  1. elasticsearch.username: "kibana_system"
  2. elasticsearch.password: "password"

确保将所有相关证书复制到 Kibana 的 config/certs 目录,并将以下几行添加到我们的 kibana.yml 文件中:

7.x

  1. elasticsearch.hosts: "https://localhost:9200"
  2. xpack.security.enabled: true
  3. elasticsearch.ssl.certificate: config/certs/client.cer
  4. elasticsearch.ssl.key: config/certs/client.key
  5. elasticsearch.ssl.certificateAuthorities: config/certs/client-ca.cer
  6. elasticsearch.ssl.verificationMode: certificate

6.8 及以前:

  1. elasticsearch.url: "https://localhost:9200" #ensure https
  2. xpack.security.enabled: true
  3. elasticsearch.ssl.certificate: config/certs/client.cer
  4. elasticsearch.ssl.key: config/certs/client.key
  5. elasticsearch.ssl.certificateAuthorities: config/certs/client-ca.cer
  6. elasticsearch.ssl.verificationMode: certificate

我们现在可以重新启动 Kibana,它应该对我们的 Elasticsearch 集群进行身份验证,无需任何嵌入的用户名和密码!

结论

在这篇博文中,我演示了如何启用安全性; 配置TLS/SSL; 为内置用户设置密码; 使用PKI进行认证; 最后,如何使用 PKI 向 Elasticsearch 集群验证 Kibana。

如果你对 Elasticsearch 的 PKI 身份验证或任何其他与 Elasticsearch 相关的主题有任何疑问,请查看我们的讨论论坛​​​​​​​以获得有价值的讨论、见解和信息。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/372339
推荐阅读
相关标签
  

闽ICP备14008679号