Elasticsearch:保护你的 Elasticsearch 实例 - 如何使用带有内置证书的 Docker 镜像

1年前未命名76
Elasticsearch:保护你的 Elasticsearch 实例 - 如何使用带有内置证书的 Docker 镜像 Elastic 中国社区官方博客 于2023-02-27 14:02:04发布 113 收藏 1 分类专栏: Elasticsearch Elastic 文章标签: elasticsearch docker 搜索引擎 大数据 数据库 本文为博主原创文章,未经博主允许不得转载。 本文链接:https://blog.csdn.net/UbuntuTouch/article/details/129239072 版权 Elasticsearch 同时被 2 个专栏收录 457 篇文章 330 订阅 订阅专栏 Elastic 842 篇文章 578 订阅 订阅专栏

使用 docker 来构建 Elasticsearch 集群为开发者们带来了极大的方便。在我之前的文章中:

Elasticsearch:使用 Docker compose 来一键部署 Elastic Stack 8.x

Elasticsearch:如何在 Docker 上运行 Elasticsearch 8.x 进行本地开发

Elastic:使用 docker 来安装 Elastic Stack 8.0

我详细地描述了如何使用 Elastic 所提供的 docker 镜像来安装 Elasticsearch。在今天的文章中,我来详述如何来构建一个带有安全配置的 docker 镜像。希望这对你的开发有帮助。

Elasticsearch 是一个功能强大且流行的搜索引擎,被许多公司所使用。 但是,如果 Elasticsearch 实例没有得到适当的保护,它们可能容易受到安全威胁。 确保 Elasticsearch 实例安全的一种方法是使用证书。 在本文中,我们将讨论如何使用带有证书的 Elasticsearch Docker Image 来保护您的 Elasticsearch 实例。

第 1 部分:使用证书设置 Elasticsearch

首先,我们将使用已经包含证书的 Elasticsearch Docker 镜像。 我们将使用的 Docker Image 是最新的 Elasticsearch 8.6.2 版本,它预装了 Elasticsearch-certutil 工具。 该工具使我们能够为我们的 Elasticsearch 实例创建证书。

创建 Dockerfile:

我们使用如下的代码来创建一个 Dockerfile:

Dockerfile

FROM docker.elastic.co/elasticsearch/elasticsearch:8.6.2 RUN bin/elasticsearch-certutil ca --out config/elastic-stack-ca.p12 --pass "" RUN bin/elasticsearch-certutil cert --pass "" --days 1825 --ca config/elastic-stack-ca.p12 --ca-pass "" --out config/elastic-certificates.p12 $ pwd /Users/liuxg/data/sec_elasticsearch $ ls Dockerfile

构建 Docker 镜像

我们使用如下的命令来构建 Docker 镜像:

docker build -t elasticsearch-with-certs .

我们可以打开 Docker Desktop 来进行查看:

 

我们创建了一个名字称作为 elasticsearch-with-certs 的镜像。

运行 Docker 容器 

我们使用如下的命令来启动 docker 容器:

docker run -p 9200:9200 -p 9300:9300 -it --name elasticsearch elasticsearch-with-certs

在启动的过程中,我们可以看到 elastic 用户的密码及其他信息:

 我们可以通过如下的命令来查看正在运行的容器:

docker ps $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2dbc39f48668 elasticsearch-with-certs "/bin/tini -- /usr/l…" About a minute ago Up About a minute 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch

我们可以通过如下的命令来查看 elasticsearch 容器的日志信息:

docker logs -f elasticsearch

这个命令也可以让我们查看到 elastic 用户的密码:

 

在浏览器中验证 Elasticsearch 的运行

我们在 浏览器中输入 https://localhost:9200,并以上面所提供的 elastic 用户密码进行登录:

显然我们的登录是成功的。我们也可以使用如下的命令来进行验证:

curl -k -u elastic:v35KytDBs_2BLB4NSz2B https://localhost:9200 $ curl -k -u elastic:v35KytDBs_2BLB4NSz2B https://localhost:9200 { "name" : "2dbc39f48668", "cluster_name" : "docker-cluster", "cluster_uuid" : "4WtDSp8AQROd3lxNGjkEiQ", "version" : { "number" : "8.3.3", "build_flavor" : "default", "build_type" : "docker", "build_hash" : "801fed82df74dbe537f89b71b098ccaff88d2c56", "build_date" : "2022-07-23T19:30:09.227964828Z", "build_snapshot" : false, "lucene_version" : "9.2.0", "minimum_wire_compatibility_version" : "7.17.0", "minimum_index_compatibility_version" : "7.0.0" }, "tagline" : "You Know, for Search" }

请注意:针对你的情况,你需要修改上面的 elastic 用户密码进行验证。

现在我们已经启动并运行了带有证书的 Elasticsearch 实例,让我们更深入地了解证书的工作原理以及为什么它们对 Elasticsearch 安全很重要。

证书是验证服务器或客户端身份的数字文档。 在 Elasticsearch 中,证书用于在客户端和 Elasticsearch 集群之间建立安全连接。 通过使用证书,我们可以保证客户端和 Elasticsearch 集群之间传输的数据是加密的和安全的。

第 2 部分:了解 Elasticsearch 证书

证书在保护 Elasticsearch 实例方面起着至关重要的作用。 在本节中,我们将讨论与 Elasticsearch 证书相关的一些关键概念。

证书颁发机构 (CA):证书颁发机构负责颁发和管理证书。 在 Elasticsearch 中,我们使用 CA 创建根证书,用于签署和验证其他证书。根证书:根证书是验证服务器或客户端身份的数字文档。 在 Elasticsearch 中,我们使用根证书在客户端和 Elasticsearch 集群之间建立信任。服务器证书:服务器证书是验证 Elasticsearch 节点身份的数字文档。 在 Elasticsearch 中,我们使用服务器证书在客户端和 Elasticsearch 集群之间建立安全连接。客户端证书:客户端证书是一种数字文档,用于验证连接到 Elasticsearch 集群的客户端的身份。 在 Elasticsearch 中,我们使用客户端证书在客户端和 Elasticsearch 集群之间建立安全连接。

第 3 部分:创建 Elasticsearch 证书

现在我们了解了与 Elasticsearch 证书相关的关键概念,让我们使用 Elasticsearch-certutil 工具创建我们自己的证书。

创建 CA 证书

第一步是通过运行以下命令创建 CA 证书:

bin/elasticsearch-certutil ca --out config/elastic-stack-ca.p12 --pass "”

此命令在 config/elastic-stack-ca.p12 文件中创建一个 CA 证书。 --pass 标志用于为 CA 证书指定空密码。 不建议在生产环境中这样做,但在测试和开发环境中是可以接受的。

创建服务器及客户端证书

下一步是通过运行以下命令创建服务器和客户端证书:

bin/elasticsearch-certutil cert --pass "" --days 1825 --ca config/elastic-stack-ca.p12 --ca-pass "" --out config/elastic-certificates.p12

此命令创建两个证书:服务器证书和客户端证书。 --days 标志用于指定证书的有效期,设置为1825天(5年)。 --ca 标志用于指定 CA 证书文件的位置,--ca-pass 标志用于指定 CA 证书的密码。 最后,--out 标志用于指定包含服务器和客户端证书的输出文件的位置和名称。

第 4 部分:将 Elasticsearch 与证书结合使用

现在我们已经创建了我们的证书,我们可以使用它们来保护我们的 Elasticsearch 实例。 要使用证书,我们需要配置 Elasticsearch 以使用它们。 这可以通过将以下行添加到 elasticsearch.yml 配置文件来完成:

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

xpack.security.transport.ssl.enabled 设置启用 Elasticsearch 中的安全功能,xpack.security.transport.ssl.enabled 设置启用传输层通信的 SSL/TLS 加密。

xpack.security.transport.ssl.verification_mode 设置指定 Elasticsearch 应验证客户端提供的证书。 xpack.security.transport.ssl.keystore.path 设置指定服务器证书的位置,xpack.security.transport.ssl.truststore.path 设置指定 CA 证书的位置。

我们可以通过如下的命令登录正在运行的 Elasticsearch 容器,并查看 elasticsearch.yml 文件:

$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2dbc39f48668 elasticsearch-with-certs "/bin/tini -- /usr/l…" 22 minutes ago Up 22 minutes 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch $ docker exec -it elasticsearch /bin/bash elasticsearch@2dbc39f48668:~$ pwd /usr/share/elasticsearch elasticsearch@2dbc39f48668:~$ cat config/elasticsearch.yml cluster.name: "docker-cluster" network.host: 0.0.0.0 #----------------------- BEGIN SECURITY AUTO CONFIGURATION ----------------------- # # The following settings, TLS certificates, and keys have been automatically # generated to configure Elasticsearch security features on 27-02-2023 05:34:22 # # -------------------------------------------------------------------------------- # Enable security features xpack.security.enabled: true xpack.security.enrollment.enabled: true # Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents xpack.security.http.ssl: enabled: true keystore.path: certs/http.p12 # Enable encryption and mutual authentication between cluster nodes xpack.security.transport.ssl: enabled: true verification_mode: certificate keystore.path: certs/transport.p12 truststore.path: certs/transport.p12 # Create a new cluster with the current node only # Additional nodes can still join the cluster later cluster.initial_master_nodes: ["2dbc39f48668"] #----------------------- END SECURITY AUTO CONFIGURATION -------------------------

你也可以直接使用如下的命令来进行查看:

$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2dbc39f48668 elasticsearch-with-certs "/bin/tini -- /usr/l…" 26 minutes ago Up 25 minutes 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch $ docker exec elasticsearch cat /usr/share/elasticsearch/config/elasticsearch.yml cluster.name: "docker-cluster" network.host: 0.0.0.0 #----------------------- BEGIN SECURITY AUTO CONFIGURATION ----------------------- # # The following settings, TLS certificates, and keys have been automatically # generated to configure Elasticsearch security features on 27-02-2023 05:34:22 # # -------------------------------------------------------------------------------- # Enable security features xpack.security.enabled: true xpack.security.enrollment.enabled: true # Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents xpack.security.http.ssl: enabled: true keystore.path: certs/http.p12 # Enable encryption and mutual authentication between cluster nodes xpack.security.transport.ssl: enabled: true verification_mode: certificate keystore.path: certs/transport.p12 truststore.path: certs/transport.p12 # Create a new cluster with the current node only # Additional nodes can still join the cluster later cluster.initial_master_nodes: ["2dbc39f48668"] #----------------------- END SECURITY AUTO CONFIGURATION -------------------------

结论

Elasticsearch 是一个强大的搜索引擎,被许多公司广泛使用。 但是,保护 Elasticsearch 实例以防止安全威胁非常重要。 通过使用带有证书的 Elasticsearch Docker Image,我们可以轻松地保护我们的 Elasticsearch 实例。 证书在保护 Elasticsearch 实例方面起着至关重要的作用,我们已经讨论了与 Elasticsearch 证书相关的一些关键概念。 我们还介绍了如何使用 elasticsearch-certutil 工具创建证书以及如何使用它们来保护我们的 Elasticsearch 实例。

标签: [db:标签TAG]

相关文章

又发现一个ChatGPT国内镜像站,无次数限制也无广告

又发现一个ChatGPT国内镜像站,无次数限制也无广告...

第十四届蓝桥杯第三期模拟赛 【python】

第十四届蓝桥杯第三期模拟赛 【python】...

opencv图片处理

opencv图片处理...

python进阶——自动驾驶寻找车道

python进阶——自动驾驶寻找车道...

TCP协议原理一

TCP协议原理一...

狂飙Linux平台,软件部署大全

狂飙Linux平台,软件部署大全...