网站安全认证(Elasticsearch如何开启安全认证)

开启 Elasticsearch 的授权认证,可以防止非法访问等安全漏洞问题,这里记录一下开启 Elasticsearch 安全认证的详细操作步骤。Elasticsearch版本这里使用的 Elasticsearch 版本是 7.10.1。操作步骤生成证书在 Elasticsearch 目录下执行 ./bin/elasticsearch-certutil ca,会遇到两个输入,第一个是输入输出文件的路径,可以直接回车,第二个是输入密码(这里设置为12345678),执行结束会在 Elasticsearch 目录里生成 elastic-stack-ca.p12 文件。zcj@localhost:~/elasticsearch-7.10.1$ ./bin/elasticsearch-certutil ca
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 目录下执行 ./bin/elasticsearch-certutil cert –ca elastic-stack-ca.p12,会遇到三个输入,第一个和第三个是输入密码,可以直接输入刚刚设置的密码(12345678),第二个是输入输出文件的路径,可以直接回车,执行结束会在 Elasticsearch 目录里生成 elastic-certificates.p12 文件。zcj@localhost:~/elasticsearch-7.10.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).
* 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) :
Please enter the desired output file [elastic-certificates.p12]:
Enter password for elastic-certificates.p12 :

Certificates written to /home/zcj/elasticsearch-7.10.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.移动凭证到指定目录将 elastic-certificates.p12 文件移动到指定目录。mkdir ./config/certificates
mv ./elastic-certificates.p12 ./config/certificates/
chmod 777 ./config/certificates/elastic-certificates.p12移动凭证到每一个ES节点使用 scp 命令把 elastic-certificates.p12 这个文件复制到每一个 Elasticsearch 节点的安装目录的相同目录下。修改每一个ES节点的配置文件修改每一个 Elasticsearch 节点的配置文件 elasticsearch.yml。# 跨域
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Type,Content-Length

# 认证
xpack.security.enabled: true
xpack.security.authc.accept_default_password: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: es的安装目录/config/certificates/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: es的安装目录/config/certificates/elastic-certificates.p12添加密码在集群的每一个 Elasticsearch 节点上添加密码,在每个节点的 Elasticsearch 目录下执行 ./bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password 和 ./bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password 两个命令。zcj@localhost:~/elasticsearch-7.10.1$ ./bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
The elasticsearch keystore does not exist. Do you want to create it? [y/N]y
Enter value for xpack.security.transport.ssl.keystore.secure_password:

zcj@localhost:~/elasticsearch-7.10.1$ ./bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
Enter value for xpack.security.transport.ssl.truststore.secure_password: 启动各个ES节点执行 ./bin/elasticsearch -d 启动各个 Elasticsearch 节点,如果已经在运行了,则停掉重启。设置内置用户的密码在 Elasticsearch 目录下执行 ./bin/elasticsearch-setup-passwords interactive,然后按提示输入各个内置用户的密码。zcj@localhost:~/elasticsearch-7.10.1$ ./bin/elasticsearch-setup-passwords interactive
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y

Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana_system]:
Reenter password for [kibana_system]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]验证是否配置成功通过浏览器打开 127.0.0.1:9200 验证,输入用户和密码。需要输入用户名和密码输入用户名和密码后访问正常通过在命令行终端输入 curl 127.0.0.1:9200 验证。# 直接请求
zcj@localhost:~/elasticsearch-7.10.1$ curl '127.0.0.1:9200?pretty'
{
"error" : {
"root_cause" : [
{
"type" : "security_exception",
"reason" : "missing authentication credentials for REST request [/?pretty]",
"header" : {
"WWW-Authenticate" : "Basic realm=\"security\" charset=\"UTF-8\""
}
}
],
"type" : "security_exception",
"reason" : "missing authentication credentials for REST request [/?pretty]",
"header" : {
"WWW-Authenticate" : "Basic realm=\"security\" charset=\"UTF-8\""
}
},
"status" : 401
}

# 带上用户和密码请求
zcj@localhost:~/elasticsearch-7.10.1$ curl 'elastic:[email protected]:9200?pretty'
{
"name" : "localhost",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "suOwjVffSnCuOggRreE2Zw",
"version" : {
"number" : "7.10.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "1c34507e66d7db1211f66f3513706fdf548736aa",
"build_date" : "2020-12-05T01:00:33.671820Z",
"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"
}使用RestHighLevelClient连接带有用户名和密码的ESimport org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;

import java.io.IOException;

public class ElasticsearchClient {

public static RestHighLevelClient restHighLevelClient = null;

public static RestHighLevelClient getRestHighLevelClient() {
if (restHighLevelClient == null) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password"));
restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")
).setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider))
);
}
return restHighLevelClient;
}

/**
* 【示例】检查指定索引是否存在
*/
public static Boolean indexExists() throws IOException {
GetIndexRequest request = new GetIndexRequest("index_name");
return getRestHighLevelClient().indices().exists(request, RequestOptions.DEFAULT);
}
}

本文出自快速备案,转载时请注明出处及相应链接。

本文永久链接: https://www.175ku.com/36395.html