Bootstrap

Elasticsearch 开启安全访问

Elasticsearch 安全访问

一、启用用户名密码

1、修改elasticsearch.yml配置文件

# 在elasticsearch.yml配置文件中添加下面配置
# 然后重启elasticsearch
xpack.security.enabled: true
xpack.license.self_generated.type: basic

2、设置用户名密码

# 打开控制台程序(windows环境下需要以管理员身份运行)
# 进入ES安装路径的bin目录下,输入下面命令
D:\DevelopmentTools\elasticsearch-7.14.1\bin> elasticsearch-setup-passwords interactive
Please confirm that you would like to continue [y/N]: y
# 输入用户elastic的密码,最少6位密码
Enter password for [elastic]: 
passwords must be at least [6] characters long
Try again.
Enter password for [elastic]: 
Reenter password for [elastic]: 
Passwords do not match.
Try again.
Enter password for [elastic]: 
Reenter password for [elastic]: 
# 输入用户kibana_system的密码
Enter password for [kibana_system]: 
Reenter password for [kibana_system]: 
# 输入用户logstash_system的密码
Enter password for [logstash_system]: 
Reenter password for [logstash_system]: 
# 输入用户beats_system的密码
Enter password for [beats_system]: 
Reenter password for [beats_system]: 
# 提示一下密码变更成功
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [elastic]

以上是手动设置密码,也可以自动生成随机密码,elasticsearch-setup-passwords auto

3、重启ES

4、修改Kibana配置文件

# 添加kibana_system账号密码
elasticsearch.username: "kibana_system"
elasticsearch.password: "123456"

5、重启Kibana

二、开启SLL访问

1、创建elastic-stack-ca.p12证书

# 进入ES根目录,输入下面的命令
D:\DevelopmentTools\elasticsearch-7.14.1> .\bin\elasticsearch-certutil ca # 生成elastic-stack-ca.p12文件
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 : # 给证书设置一个密码,然后回车,牢记此密码

2、生成elastic-certificates.p12证书

# 进入ES根目录,输入下面的命令
# 生成elastic-certificates.p12文件,供elasticsearch使用
D:\DevelopmentTools\elasticsearch-7.14.1> .\bin\elasticsearch-certutil cert --ca elastic-stack-ca.p12
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 'cert' mode generates X.509 certificate and private keys.
    * By default, this generates a single certificate and key for use
       on a single instance.
    * The '-multiple' option will prompt you to enter details for multiple
       instances and will generate a certificate and key for each one
    * The '-in' option allows for the certificate generation to be automated by describing
       the details of each instance in a YAML file

    * An instance is any piece of the Elastic Stack that requires an SSL certificate.
      Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats
      may all require a certificate and private key.
    * The minimum required value for each instance is a name. This can simply be the
      hostname, which will be used as the Common Name of the certificate. A full
      distinguished name may also be used.
    * A filename value may be required for each instance. This is necessary when the
      name would result in an invalid file or directory name. The name provided here
      is used as the directory name (within the zip) and the prefix for the key and
      certificate files. The filename is required if you are prompted and the name
      is not displayed in the prompt.
    * IP addresses and DNS names are optional. Multiple values can be specified as a
      comma separated string. If no IP addresses or DNS names are provided, you may
      disable hostname verification in your SSL configuration.

    * All certificates generated by this tool will be signed by a certificate authority (CA)
      unless the --self-signed command line option is specified.
      The tool can automatically generate a new CA for you, or you can provide your own with
      the --ca or --ca-cert command line options.

By default the 'cert' mode produces a single PKCS#12 output file which holds:
    * The instance certificate
    * The private key for the instance certificate
    * The CA certificate

If you specify any of the following options:
    * -pem (PEM formatted output)
    * -keep-ca-key (retain generated CA key)
    * -multiple (generate multiple certificates)
    * -in (generate certificates from an input file)
then the output will be be a zip file containing individual certificate/key files

Enter password for CA (elastic-stack-ca.p12) : # 输入上一步生成的CA证书的密码,回车
Please enter the desired output file [elastic-certificates.p12]: # p12文件名使用默认就可以,直接回车
Enter password for elastic-certificates.p12 :  # 此处让输入一个p12文件密码,可以留空 此处有坑,切记 

# 输入密码后会提示文件生成成功
Certificates written to D:\elasticsearch-7.14.1\elastic-certificates.p12

This file should be properly secured as it contains the private key for
your instance.

This file is a self contained file and can be copied and used 'as is'
For each Elastic product that you wish to configure, you should copy
this '.p12' file to the relevant configuration directory
and then follow the SSL configuration instructions in the product guide.

For client applications, you may only need to copy the CA certificate and
configure the client to trust this certificate.

请注意: Enter password for elastic-certificates.p12 : 这一步有坑,后面会讲到

3、生成newfile.crt.pem文件

# 生成newfile.crt.pem文件,供Kibana和filebeat使用,复制到各自对应目录下
D:\DevelopmentTools\elasticsearch-7.14.1> openssl pkcs12 -in elastic-stack-ca.p12 -out newfile.crt.pem -clcerts -nokeys
Enter Import Password:  # 输入elastic-certificates.p12证书密码,回车

4、生成certificate-bundle.zip文件

# 生成certificate-bundle.zip文件,包含ca/ca.crt,instance/instance.crt,instance/instance.key
D:\DevelopmentTools\elasticsearch-7.14.1> .\bin\elasticsearch-certutil cert --pem elastic-stack-ca.p12
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 'cert' mode generates X.509 certificate and private keys.
    * By default, this generates a single certificate and key for use
       on a single instance.
    * The '-multiple' option will prompt you to enter details for multiple
       instances and will generate a certificate and key for each one
    * The '-in' option allows for the certificate generation to be automated by describing
       the details of each instance in a YAML file

    * An instance is any piece of the Elastic Stack that requires an SSL certificate.
      Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats
      may all require a certificate and private key.
    * The minimum required value for each instance is a name. This can simply be the
      hostname, which will be used as the Common Name of the certificate. A full
      distinguished name may also be used.
    * A filename value may be required for each instance. This is necessary when the
      name would result in an invalid file or directory name. The name provided here
      is used as the directory name (within the zip) and the prefix for the key and
      certificate files. The filename is required if you are prompted and the name
      is not displayed in the prompt.
    * IP addresses and DNS names are optional. Multiple values can be specified as a
      comma separated string. If no IP addresses or DNS names are provided, you may
      disable hostname verification in your SSL configuration.

    * All certificates generated by this tool will be signed by a certificate authority (CA)
      unless the --self-signed command line option is specified.
      The tool can automatically generate a new CA for you, or you can provide your own with
      the --ca or --ca-cert command line options.

By default the 'cert' mode produces a single PKCS#12 output file which holds:
    * The instance certificate
    * The private key for the instance certificate
    * The CA certificate

If you specify any of the following options:
    * -pem (PEM formatted output)
    * -keep-ca-key (retain generated CA key)
    * -multiple (generate multiple certificates)
    * -in (generate certificates from an input file)
then the output will be be a zip file containing individual certificate/key files

Note: Generating certificates without providing a CA certificate is deprecated.
      A CA certificate will become mandatory in the next major release.

Please enter the desired output file [certificate-bundle.zip]:  # 文件名默认即可,直接回车

# certificate-bundle.zip 生成成功,在压缩文件中会包含这些文件
# ca/ca.crt
# instance/instance.crt
# instance/instance.key
Certificates written to D:\elasticsearch-7.14.1\certificate-bundle.zip

This file should be properly secured as it contains the private key for
your instance.

After unzipping the file, there will be a directory for each instance.
Each instance has a certificate and private key.
For each Elastic product that you wish to configure, you should copy
the certificate, key, and CA certificate to the relevant configuration directory
and then follow the SSL configuration instructions in the product guide.

For client applications, you may only need to copy the CA certificate and
configure the client to trust this certificate.

5、将证书文件复制到相应目录

1、将elastic-stack-ca.p12、elastic-certificates.p12证书复制到ES目录下的***[config\certs]***文件夹中

2、将newfile.crt.pem、certificate-bundle.zip文件复制到Kibana目录下的***[config\certs]***文件夹中

3、解压certificate-bundle.zip

6、修改elasticsearch.yml配置文件

xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.http.ssl.truststore.path: certs/elastic-certificates.p12

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

7、重启Elasticsearch

# 打开控制台程序,进入ES目录的bin文件夹下
D:\DevelopmentTools\elasticsearch-7.14.1\bin> elasticsearch -d  # -d表示不输出日志
"warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME"
WARNING: A terminally deprecated method in java.lang.System has been called
WARNING: System::setSecurityManager has been called by org.elasticsearch.bootstrap.Elasticsearch (file:/D:/DevelopmentTools/elasticsearch-7.14.1/lib/elasticsearch-7.14.1.jar)
WARNING: Please consider reporting this to the maintainers of org.elasticsearch.bootstrap.Elasticsearch
WARNING: System::setSecurityManager will be removed in a future release
WARNING: A terminally deprecated method in java.lang.System has been called
WARNING: System::setSecurityManager has been called by org.elasticsearch.bootstrap.Security (file:/D:/DevelopmentTools/elasticsearch-7.14.1/lib/elasticsearch-7.14.1.jar)
WARNING: Please consider reporting this to the maintainers of org.elasticsearch.bootstrap.Security
WARNING: System::setSecurityManager will be removed in a future release
uncaught exception in thread [main]
ElasticsearchSecurityException[failed to load SSL configuration [xpack.security.transport.ssl]]; nested: ElasticsearchException[failed to initialize SSL KeyManager]; nested: IOException[keystore password was incorrect]; nested: UnrecoverableKeyException[failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.];
Likely root cause: java.security.UnrecoverableKeyException: failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
        at java.base/sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:2159)
        at java.base/sun.security.util.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:221)
        at java.base/java.security.KeyStore.load(KeyStore.java:1473)
        at org.elasticsearch.xpack.core.ssl.TrustConfig.getStore(TrustConfig.java:98)
        at org.elasticsearch.xpack.core.ssl.StoreKeyConfig.createKeyManager(StoreKeyConfig.java:77)
        at org.elasticsearch.xpack.core.ssl.SSLService.createSslContext(SSLService.java:440)
        at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1219)
        at org.elasticsearch.xpack.core.ssl.SSLService.lambda$loadSSLConfigurations$5(SSLService.java:528)
        at java.base/java.util.HashMap.forEach(HashMap.java:1420)
        at java.base/java.util.Collections$UnmodifiableMap.forEach(Collections.java:1553)
        at org.elasticsearch.xpack.core.ssl.SSLService.loadSSLConfigurations(SSLService.java:526)
        at org.elasticsearch.xpack.core.ssl.SSLService.<init>(SSLService.java:144)
        at org.elasticsearch.xpack.core.XPackPlugin.createSSLService(XPackPlugin.java:459)
        at org.elasticsearch.xpack.core.XPackPlugin.createComponents(XPackPlugin.java:301)
        at org.elasticsearch.node.Node.lambda$new$18(Node.java:615)
        at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:273)
        at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
        at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
        at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
        at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
        at org.elasticsearch.node.Node.<init>(Node.java:619)
        at org.elasticsearch.node.Node.<init>(Node.java:281)
        at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:219)
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:219)
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:399)
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159)
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150)
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:75)
        <<<truncated>>>
For complete error details, refer to the log at D:\DevelopmentTools\elasticsearch-7.14.1\logs\elasticsearch.log

恭喜你,报错了,此处就是在生成elastic-certificates.p12证书的时候留下的坑,仔细阅读报错信息,发现是[failed to load SSL configuration [xpack.security.transport.ssl]]初始化异常,原因是[keystore password was incorrect]证书密码错误

**注意:**如果没有设置elastic-certificates.p12证书密码,恭喜你,成功了,此时ES已经成功开启https访问

8、设置证书密码(重点)

注意: 如果在生成elastic-certificates.p12证书时没有设置密码,此步骤可以忽略

# 打开控制台程序,进入ES目录的bin文件夹下
# 依次执行执行以下四句命令,并输入elastic-certificates.p12证书密码
# 其实也可以在elasticsearch.yml配置文件中以明文方式设置
D:\DevelopmentTools\elasticsearch-7.14.1\bin> elasticsearch-keystore add xpack.security.http.ssl.keystore.secure_password
D:\DevelopmentTools\elasticsearch-7.14.1\bin> elasticsearch-keystore add xpack.security.http.ssl.truststore.secure_password
D:\DevelopmentTools\elasticsearch-7.14.1\bin> elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
D:\DevelopmentTools\elasticsearch-7.14.1\bin> elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password

9、修改kibana.yml配置文件

# 添加下面配置项
elasticsearch.hosts: ["https://localhost:9200"]
elasticsearch.ssl.verificationMode: none
# 此处pem文件路径必须为绝对路径,否则无法正常启动Kibana
elasticsearch.ssl.certificateAuthorities: ["D:\\DevelopmentTools\\kibana-7.14.1-windows-x86_64\\config\\certs\\newfile.crt.pem"]

10、重启Kibana,大功告成

;