Elasticsearch(一):单节点安装并开启ssl

news2024/9/24 19:19:15

单节点安装并开启ssl

  • 1、概述
  • 2、安装
    • 2.1、前期准备
    • 2.2、下载和解压Elasticsearch
    • 2.3、创建用户和配置权限
    • 2.4、生成证书
      • 2.4.1、创建CA(证书颁发机构)
      • 2.4.2、与CA签署证书(生成节点证书)
    • 2.5、 为Elasticsearch配置 证书
    • 2.6、启动 Elasticsearch
    • 2.7、测试连接
      • 2.7.1、连接被拒
      • 2.7.2、设置初始主结点
      • 2.7.3、开启ssl
      • 2.7.4、设置密码
      • 2.7.5、最终连通
  • 4、总结

大家好,我是欧阳方超,可以扫描下方二维码关注我的公众号“欧阳方超”,后续内容将在公众号首发。
在这里插入图片描述

在这里插入图片描述

1、概述

在现代应用程序中,数据安全性至关重要。为了保护 Elasticsearch 中的数据传输,我们可以通过启用 SSL/TLS 加密来确保数据的安全性。本文将指导您如何在单节点 Elasticsearch 环境中开启 SSL,并配置证书。

2、安装

2.1、前期准备

在开始安装之前,请确保您的CentOS 7系统已更新,并安装了Java Runtime Environment (JRE)。从 Elasticsearch 7.0 开始,要求使用 Java 11 或更高版本。如果系统安装的是Java 8,那么Elasticsearch会给出下面一个警告,并选择使用自带的 JDK。

[esuser@localhost bin]$ ./elasticsearch
warning: ignoring JAVA_HOME=/root/jdk1.8.0_202; using bundled JDK

2.2、下载和解压Elasticsearch

将elasticsearch安装包移动到合适的目录,并进行解压:

[root@localhost software]# pwd
/software
[root@localhost software]# tar -zxvf elasticsearch-8.7.0-linux-x86_64.tar.gz

2.3、创建用户和配置权限

出于安全考虑,不建议以root用户运行Elasticsearch。您需要创建一个新的用户并设置相应的权限。
创建用户:esuser

[root@localhost bin]# useradd esuser
[root@localhost bin]#

设置密码:

[root@localhost bin]# passwd esuser
Changing password for user esuser.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost bin]#

将es相关目录赋权给刚刚创建的esuser用户。

2.4、生成证书

2.4.1、创建CA(证书颁发机构)

ES中可以使用 elasticsearch-certutil命令生成证书,它的作用是负责生成CA并与CA签署证书。elasticsearch-certutil 可以创建一个证书颁发机构(CA)。CA 是一个可信任的实体,负责签署和验证证书。
下面是创建CA的命令及执行过程:

[esuser@localhost elasticsearch-8.7.0]$ ./bin/elasticsearch-certutil ca
warning: ignoring JAVA_HOME=/root/jdk1.8.0_202; using bundled JDK
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 :

该命令输出单一个PKCS#12文件,默认名称为elastic-stack-ca.p12,其中包含CA的公共证书和用于对证书签名的私钥。上面生成过程中文件名采用的默认的文件名——直接回车,密码也设置为空密码——直接回车。

2.4.2、与CA签署证书(生成节点证书)

接下来,使用生成的 CA 来为节点颁发证书:

./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12

下面是关键执行过程:

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)
    * -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 /software/elasticsearch-8.7.0/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.

在此过程中,首先需要输入 CA 的密码(生成CA时设置的密码为空,所以这里也直接回车),其次指定节点证书的名称(这里以默认文件名进行生成,所以也直接回车),最后为节点证书设置一个新密码(这里也设置为空,所以也直接回车)。过程结束后生成了一个名称为elastic-certificates.p12的节点证书。

2.5、 为Elasticsearch配置 证书

要在单节点的 Elasticsearch 环境中配置证书以启用 TLS 安全性,需要编辑 elasticsearch.yml 配置文件并指定证书信息,启用安全性和 SSL/TLS 设置。确保以下配置项存在并正确设置:

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

上面的配置是以生成的elastic-certificates.p12文件被移动到elasticsearch的config目录下为前提的。出于合理性考虑,把生成的CA证书elastic-stack-ca.p12也最好移动到elasticsearch的config目录下。

2.6、启动 Elasticsearch

完成配置后,启动 Elasticsearch:

./bin/elasticsearch

这种方式是前台启动,如果没有看到报错信息的话就算启动成功了。
后台启动方式:

./bin/elasticsearch -d

这种方式日志往命令行打印完后,会退出命令行。

2.7、测试连接

2.7.1、连接被拒

此时通过浏览器连接https://ip:9200,发现拒绝了连接:
在这里插入图片描述
这是因为没有在elasticsearch.yml配置文件中设置允许远程连接,将network.host改为如下值:

network.host: 0.0.0.0

2.7.2、设置初始主结点

再次以前台方式启动elasticsearch,比较遗憾的是报错了:

[2024-08-29T23:26:35,837][ERROR][o.e.b.Elasticsearch      ] [localhost.localdomain] node validation exception
[1] bootstrap checks failed. You must address the points described in the following [1] lines before starting Elasticsearch.
bootstrap check failure [1] of [1]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
ERROR: Elasticsearch did not exit normally - check the logs at /software/elasticsearch-8.7.0/logs/elasticsearch.log

discovery.seed_hosts、discovery.seed_providers 和 cluster.initial_master_nodes 这三个配置项在 Elasticsearch中用于集群发现和初始化,由于我们这里是单节点方式安装的,所以只配置 cluster.initial_master_nodes即可,比如可以把该项配置为如下形式:

cluster.initial_master_nodes: ["node-1"]

2.7.3、开启ssl

再次以前台方式启动elasticsearch,使用浏览器访问时又报错:
在这里插入图片描述
原因是未启用客户端与elasticsearch服务端通信的ssl,在配置文件中添加如下配置:

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

再次启动elasticsearch,浏览器再次访问与elasticsearch通信的接口,熟悉的画面终于出现了:
在这里插入图片描述
接着从“高级”可以进入登录页面:
在这里插入图片描述

2.7.4、设置密码

elasticsearch内置了一些用户,其中一个是elastic,但是密码是不知道,好在可以使用 elasticsearch-setup-passwords 工具以交互方式设置或修改内置用户的密码。命令如下:

[esuser@localhost elasticsearch-8.7.0]$ ./bin/elasticsearch-setup-passwords interactive

很遗憾也报错了:

warning: ignoring JAVA_HOME=/root/jdk1.8.0_202; using bundled JDK
01:30:33.206 [main] WARN  org.elasticsearch.common.ssl.DiagnosticTrustManager - failed to establish trust with server at [192.168.25.136]; the server provided a certificate with subject name [CN=instance], fingerprint [b8c0b762603c223ae3bcf8481bb5715954162805], no keyUsage and no extendedKeyUsage; the certificate is valid between [2024-08-30T02:58:59Z] and [2027-08-30T02:58:59Z] (current time is [2024-08-30T05:30:33.199963023Z], certificate dates are valid); the session uses cipher suite [TLS_AES_256_GCM_SHA384] and protocol [TLSv1.3]; the certificate does not have any subject alternative names; the certificate is issued by [CN=Elastic Certificate Tool Autogenerated CA]; the certificate is signed by (subject [CN=Elastic Certificate Tool Autogenerated CA] fingerprint [c3651acf6862859180807b3ce01037193102a5a0] {trusted issuer}) which is self-issued; the [CN=Elastic Certificate Tool Autogenerated CA] certificate is trusted in this ssl context ([xpack.security.http.ssl (with trust configuration: StoreTrustConfig{path=elastic-certificates.p12, password=<empty>, type=PKCS12, algorithm=PKIX})])
java.security.cert.CertificateException: No subject alternative names present

这是因为,证书缺少 Subject Alternative Names (SAN),这个问题以后再详细解释。要解决这个问题,需要使用之前生成的CA重新签发证书,并额外加上一些参数,具体命令如下:

./bin/elasticsearch-certutil cert --ca ./config/elastic-stack-ca.p12 --name instance --ip 192.168.25.136 --dns localhost

将新生成的结点证书(以上的命令会生成一个名为instance.p12的证书)移动到elasticsearch的confi中,并在elasticsearch.yml中重新配置证书的路径,重启elasticsearch,此时可以为elasticsearch用户设置密码了:

[root@localhost elasticsearch-8.7.0]# ./bin/elasticsearch-setup-passwords interactive
warning: ignoring JAVA_HOME=/root/jdk1.8.0_202; using bundled JDK
******************************************************************************
Note: The 'elasticsearch-setup-passwords' tool has been deprecated. This       command will be removed in a future release.
******************************************************************************

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]

可以看到此命令要求我们为包括elastic在内的所有用户设置密码,方便起见,这里密码全设置为123456。

2.7.5、最终连通

此时在登录页输入elastic/123456,终于通过接口见到了elasticsearch的信息:
在这里插入图片描述
至此,连接终于通了。
最后完整展示一下elasticsearch.yml中的配置信息:

# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
network.host: 0.0.0.0
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
http.port: 9200


xpack.security.enabled: true


xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: instance.p12
xpack.security.http.ssl.truststore.path: instance.p12


xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: instance.p12
xpack.security.transport.ssl.truststore.path: instance.p12

4、总结

通过以上步骤,已经成功在单节点的 Elasticsearch 环境中启用了 SSL 并配置了证书。这将确保数据在传输过程中的安全性,保护应用程序免受潜在的安全威胁。
我是欧阳方超,把事情做好了自然就有兴趣了,如果你喜欢我的文章,欢迎点赞、转发、评论加关注。我们下次见。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2090084.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

第4章-08-用Python Requests库模拟浏览器访问接口

🏆作者简介,黑夜开发者,CSDN领军人物,全栈领域优质创作者✌,CSDN博客专家,阿里云社区专家博主,2023年CSDN全站百大博主。 🏆数年电商行业从业经验,历任核心研发工程师,项目技术负责人。 🏆本文已收录于专栏:Web爬虫入门与实战精讲,后续完整更新内容如下。 文章…

【奔驰中国-注册安全分析报告】

前言 由于网站注册入口容易被黑客攻击&#xff0c;存在如下安全问题&#xff1a; 暴力破解密码&#xff0c;造成用户信息泄露短信盗刷的安全问题&#xff0c;影响业务及导致用户投诉带来经济损失&#xff0c;尤其是后付费客户&#xff0c;风险巨大&#xff0c;造成亏损无底洞…

什么是基于云的 SIEM

随着企业不断将业务迁移到数字世界&#xff0c;网络威胁的领域也在不断扩大&#xff0c;随着时间流逝&#xff0c;新的威胁不断出现&#xff0c;手段也变得更加巧妙。一个关键问题出现了&#xff1a;组织如何保护其敏感数据、资产和声誉免受网络威胁&#xff1f;这就是基于云的…

Linux中路由功能及ip_forward转发配置

操作系统中路由功能有着至关重要的作用&#xff0c;它决定了网络数据包如何在网络中传输&#xff0c;最终到达目的地。本文简要介绍Linux中的路由功能实现以及IP转发的配置&#xff0c;并验证在容器环境下配置net.ipv4.ip_forward的必要性&#xff0c;以加深理解。 1、Linux中路…

8.6 数据库基础技术-数据库的控制

并发控制 封锁协议 一级封锁协议 二级封锁协议 三级封锁协议 数据库故障和备份 真题 1

案例练习理解ThreadLocal以及应用场景

目录 案例练习 应用场景 ThreadLocal&#xff1a;用来解决多线程程序下并发问题&#xff0c;通过为每一个线程创建一份共享变量的副本保证线程之间的变量的访问和修改互不影响。 案例练习 1.三个销售卖小米SU7&#xff0c;求他们的总销售。使用CountDownLatch维护三个线程 …

跑腿代购系统开发:重塑便捷生活的新篇章

在快节奏的现代生活中&#xff0c;时间成为了最宝贵的资源之一。随着移动互联网技术的飞速发展&#xff0c;人们对于高效、便捷的生活服务需求日益增长&#xff0c;跑腿代购服务应运而生&#xff0c;并迅速成为连接消费者与日常所需商品及服务的重要桥梁。为了满足这一市场需求…

C++:继承用法详解~

在学完C的类和对象&#xff0c;并掌握了类的核心语法与基本用法之后&#xff1b;我们就得去学习一下继承的语法&#xff0c;与继承的用法。简单概括一下&#xff0c;继承是C中一种代码复用的手段&#xff0c;它允许我们&#xff0c;对已有的类&#xff0c;增添新的成员函数或变…

28 TreeView组件

Tkinter ttk.Treeview 组件使用指南 ttk.Treeview 是 Tkinter 的一个高级控件&#xff0c;用于显示和管理层次化数据。它类似于电子表格或列表视图&#xff0c;但提供了更丰富的功能&#xff0c;如可展开的节点、多列显示等。ttk 模块是 Tkinter 的一个扩展&#xff0c;提供了…

NVM安装及配置

一&#xff1a;下载nvm安装包 https://github.com/coreybutler/nvm-windows/releases 二&#xff1a;安装步骤 三&#xff1a;检查环境变量 &#xff08;1&#xff09;、检查用户变量和系统变量中是否有NVM_HOME和NVM_SYMLINK。一般情况下&#xff0c;安装nvm后&#xff0c;系…

Java分布式架构知识体系及知识体系图

Java分布式架构整体知识体系是一个庞大而复杂的领域&#xff0c;它涵盖了多个方面&#xff0c;旨在帮助开发者构建高性能、高可用、可扩展的分布式系统。以下是对Java分布式架构整体知识体系的概述&#xff1a; 一、分布式理论基础 CAP理论&#xff1a; 一致性&#xff08;Con…

GUI编程04:课堂练习及总结

本节内容视频链接&#xff1a;6、课堂练习讲解及总结_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1DJ411B75F?p6&vd_sourceb5775c3a4ea16a5306db9c7c1c1486b5 根据前三节学习到的Frame、Panel、Button知识&#xff0c;画出一下窗口界面&#xff1a; 实现代码如下…

避坑之:深信服AC跨三层取MAC(核心交换机是锐捷S7808C_RGOS 11.0(4)B2P1)

今天碰到一个奇怪的现象&#xff0c;深信服AC对接锐捷交换机做跨三层取MAC&#xff0c;怎么都获取不到。 一、坑1&#xff1a;交换机不回应snmp报文 1.1 排查锐捷交换机配置 配置上看着没有问题&#xff0c;重新配置了community 1.2 查看snmp报文是否通畅 我的笔记本是win10…

选对文档版本管理软件:10款工具详解

本篇文章中提到的工具包括&#xff1a;1.PingCode&#xff1b;2.Worktile&#xff1b;3.联想Filez&#xff1b;4.蓝凌云&#xff1b;5.阿里云盘&#xff1b;6.360亿方云&#xff1b;7.无忧企业文档&#xff1b;8.DocStar ECM&#xff1b;9.Dropbox Business&#xff1b;10.Shar…

APP 数据抓取 - Charles 抓包工具的使用(Charles 端口配置、CA 证书配置、Charles Android 模拟器配置)

前言说明 此文章是我在学习 Charles APP 抓包时编写&#xff0c;内容都是亲测有效&#xff0c;文章内容也有参考其他人&#xff0c;参考文章如下&#xff1a; Android 手机使用 charles 抓 https 请求&#xff08;保姆级教程&#xff09;网易 mumu 模拟器安装下载 charles 的…

JAVAEE初阶第二节——多线程基础(上)

系列文章目录 JAVAEE初阶第二节——多线程基础(上) 计算机的工作原理 认识线程&#xff08;Thread&#xff09;Thread 类及常见方法线程的状态 文章目录 系列文章目录JAVAEE初阶第二节——多线程基础(上) 计算机的工作原理 一.认识线程&#xff08;Thread&#xff09;1.概念 …

Leetcode面试经典150题-28.找出字符串第一个匹配项的下标

解法都在代码里&#xff0c;不懂就留言或者私信&#xff0c;比第一题稍微难点 用KMP解这个题简直就像大炮打蚂蚁&#xff0c;但是没办法&#xff0c;现在都是这么卷 package dataStructure.bigFactory;public class _28Strstr {public static int strStr(String s1, String s…

EasyCode实现完整CRUD + 分页封装

文章目录 1.创建一个表sys-user2.EasyCode 模板配置1.entity.java.vm2.dao.java.vm3.mapper.xml.vm4.service.java.vm5.serviceImpl.java.vm6.controller.java.vm7.PageInfo.java.vm8.PageResult.java.vm9.SunPageHelper.java.vm 3.EasyCode生成CRUD1.右键表&#xff0c;选择Ge…

Linux系统查看磁盘、内存使用情况、查看当前文件夹内文件详情:free、top、df、du、ls

Liunx系统查看内存使用情况 free&#xff1a;查看当前内存以及交换区内存使用情况&#xff0c;默认显示单位是比特&#xff0c;加上参数-h以易读的方式显示&#xff08;如KB、MB、G&#xff09;&#xff0c;在Linux系统中所有查询加上-h参数均以易读的方式显示。 资源管理器查看…

【C++ Primer Plus习题】7.5

问题: 解答: #include <iostream> using namespace std;int function(int n) {if (n 0)return 1;if (n 1)return 1;return n* function(n - 1); }int main() {int value 0;while (true){cout << "请输入数字:";cin >> value;cout << val…