Elasticsearch 单节点安全配置与用户认证

Elasticsearch 单节点安全配置与用户认证

安全扫描时发现了一个高危漏洞:Elasticsearch 未授权访问 。在使用 Elasticsearch 构建搜索引擎或处理大规模数据时,需要启用基本的安全功能来防止未经授权的访问。本文将通过简单的配置步骤,为单节点 Elasticsearch 实现最基本的安全功能,包括启用 Elasticsearch 自带的安全功能和配置传输层 SSL,以确保数据传输的加密和访问权限的严格管理。 (elaticsearch版本7.9.2)


修改配置文件

编辑单节点的 elasticsearch.yml 文件,确保包含以下内容:

yaml 复制代码
# 启用 Elasticsearch 的安全功能
xpack.security.enabled: true

# 启用传输层 SSL
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.client_authentication: required

# 指定证书路径(使用默认生成的 P12 文件)
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12

生成证书

在 Elasticsearch 安装目录中,使用 elasticsearch-certutil 工具生成证书

生成 CA 文件

bash 复制代码
cd /bin
./elasticsearch-certutil ca

# (设置证书密码)可以直接回车选择不设置密码
Please enter the desired output file [elastic-stack-ca.p12]: 
Enter password for elastic-stack-ca.p12 :
  • 输出文件:elastic-stack-ca.p12

  • 选择密码(可留空)

生成节点证书

bash 复制代码
cd /bin
./elasticsearch-certutil cert --ca elastic-stack-ca.p12

# (设置证书密码)可以直接回车选择不设置密码
Enter password for CA (elastic-stack-ca.p12) : 
Please enter the desired output file [elastic-certificates.p12]: 
Enter password for elastic-certificates.p12 : 
  • 输出文件:elastic-certificates.p12

  • 可选择为证书设置密码

将证书复制到配置目录

将生成的 elastic-certificates.p12 文件移动到 Elasticsearch 的配置目录(通常是 $ES_HOME/config)下:

bash 复制代码
mv elastic-certificates.p12 $ES_HOME/config
# 赋权为es用户权限
chown es:es elastic-certificates.p12


(可选)存储证书密码

如果生成证书时设置了密码,需要将密码存储到 Elasticsearch 密钥库中:

bash 复制代码
./bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
./bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password

按照提示输入密码


启动 Elasticsearch

配置完成后,切换为 es 用户启动 Elasticsearch 或重启ES服务:

bash 复制代码
./elasticsearch

如果一切正常,日志中应显示以下内容:

复制代码
[2024-12-03T14:27:33,401][INFO ][o.e.x.s.s.SecurityStatusChangeListener] [es-xcu-node] Active license is now [BASIC]; Security is enabled

验证安全配置

设置用户密码

默认情况下,Elasticsearch 启用了几个内置用户(例如 elastic)。运行以下命令设置密码:

bash 复制代码
cd /bin
./elasticsearch-setup-passwords interactive

按提示为用户设置密码,特别是 elastic 用户。

访问 REST 接口

使用 curl 或其他工具测试:

bash 复制代码
# 未带认证信息访问会提示报错
curl http://localhost:9200
# 提示 401 错误,表明未授权

# 带认证信息的请求
curl -u elastic:设置的密码 http://localhost:9200

如果返回正常的 Elasticsearch 信息,则配置成功。

浏览器访问


Spring Boot 项目中配置 Elasticsearch 连接账户密码

1. 引入依赖

pom.xml 中添加 Elasticsearch 客户端依赖:

xml 复制代码
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.9.2</version>
</dependency>

2. 配置类

在 Spring Boot 项目中,通过 Java 配置类创建 RestHighLevelClient Bean:

java 复制代码
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticsearchConfig {

    @Value("${elasticsearch.host}")
    private String host;

    @Value("${elasticsearch.port}")
    private int port;

    @Value("${elasticsearch.username}")
    private String username;

    @Value("${elasticsearch.password}")
    private String password;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        // 配置用户名和密码
        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                AuthScope.ANY,
                new UsernamePasswordCredentials(username, password)
        );

        // 构建 RestClient
        RestClientBuilder restClientBuilder = RestClient.builder(
                new HttpHost(host, port)
        ).setHttpClientConfigCallback(httpClientBuilder -> 
                httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
        );

        return new RestHighLevelClient(restClientBuilder);
    }
}

3. 配置文件

application.ymlapplication.properties 中添加 Elasticsearch 的相关配置:

yaml 复制代码
elasticsearch:
  host: localhost
  port: 9200
  username: elastic
  password: elasticPWD

4. 使用示例

在需要使用 RestHighLevelClient 的地方直接注入并使用:

java 复制代码
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.MainResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ElasticsearchService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    public String getClusterInfo() {
        try {
            MainResponse response = restHighLevelClient.info(RequestOptions.DEFAULT);
            return response.getClusterName().toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "Error fetching cluster info";
        }
    }
}

5. 启动项目并验证

启动项目后,调用 ElasticsearchService 中的方法,验证是否成功连接到 Elasticsearch

相关推荐
智慧医院运行管理解决方案专家3 小时前
医院安全管理优化首选:中科医信安全生产双重预防管理系统
安全·医院管理·医院智慧安全管理·平安医院·智慧医院后勤
世界尽头与你5 小时前
CVE-2022-46463 Harbor public 镜像仓库信息泄露
安全·网络安全·渗透测试
十里-6 小时前
前端监控1-数据上报
前端·安全
wan_da_ren9 小时前
Docker安装Elasticsearch9.2.1与Kibana9.2.1 保姆教程(带验证版)
运维·docker·jenkins
杭州泽沃电子科技有限公司10 小时前
在线监测:为医药精细化工奠定安全、合规与质量基石
运维·人工智能·物联网·安全·智能监测
GIS数据转换器10 小时前
GIS+大模型助力安全风险精细化管理
大数据·网络·人工智能·安全·无人机
普普通通的南瓜11 小时前
IP证书在关键信息基础设施安全防护中的实践与挑战
网络·数据库·网络协议·tcp/ip·安全·ssl
合作小小程序员小小店11 小时前
桌面安全开发,桌面二进制%恶意行为拦截查杀%系统安全开发3.0,基于c/c++语言,mfc,win32,ring3,dll,hook,inject,无数据库
c语言·开发语言·c++·安全·系统安全
E***U94512 小时前
前端安全编程实践
前端·安全
数据堂官方账号12 小时前
行业洞见 | AI鉴伪:数据驱动的数字安全变革
人工智能·安全