写点什么

基于 Kerberos 认证对接华为云 Elasticsearch

作者:大河
  • 2025-02-27
    湖南
  • 本文字数:3367 字

    阅读完需:约 11 分钟

基于Kerberos认证对接华为云Elasticsearch

可以通过华为官方提供的 Elasticsearch Java 客户端(基于 Elasticsearch 官方版本改造),实现基于 Kerberos 认证访问和操作华为云 Elasticsearch;亦可以使用更加通用的开源 Elasticsearch Java 客户端 bboss,实现基于Kerberos认证访问和操作华为云Elasticsearch


本文介绍使用 bboss 实现基于 Kerberos 认证访问和操作华为云 Elasticsearch 的方法。

1. bboss 介绍

bboss 是一个高性能高兼容性的 Elasticsearch java 客户端框架:



更多 bboss 介绍,可以访问文档了解:https://esdoc.bbossgroups.com/#/README

2. 准备工作

2.1 准备 Kerberos 认证配置文件

获取 Kerberos 配置文件 :从华为云获取 krb5.conf 和 jaas.conf 文件,这些文件由华为云 Elasticsearch 提供。


获取 keytab 文件 :从华为云获取 keytab 文件,该文件包含了客户端的凭据信息,文件由华为云 Elasticsearch 提供。

2.2 集成 bboss

集成 bboss 非常简单,只需在项目中导入 bboss 对应的 maven 坐标即可:


<dependency>    <groupId>com.bbossgroups.plugins</groupId>    <artifactId>bboss-datatran-jdbc</artifactId>    <version>7.3.5</version></dependency>
复制代码


实际 bboss 版本号可以参考文档获取:https://esdoc.bbossgroups.com/#/changelog

3. 配置和使用 Elasticsearch 数据源

在项目中导入 bboss maven 坐标,并准备好 Kerberos 认证相关的材料后,就可以实现基于 Kerberos 认证对接华为云 Elasticsearch 的功能,直接上代码。通过以下代码定义和初始化 Elasticsearch 数据源,并通过 ClientInterface 接口操作和访问华为云 Elasticsearch,验证集成是否成功:


    Map properties = new HashMap();        /**         * 配置Elasticsearch数据源参数,这里只设置必须的配置项,更多配置参考文件:         * https://gitee.com/bboss/elasticsearchdemo/blob/master/src/main/resources/application.properties         */        //定义Elasticsearch数据源名称:esDS,后续通过esDS获取对应数据源的客户端API操作和访问Elasticsearch        properties.put("elasticsearch.serverNames","esDS");        //es服务器地址和端口,多个用逗号分隔        //properties.put("esDS.elasticsearch.rest.hostNames","192.168.137.1:8200");
//开启https协议,华为云Elasticsearch一般会启用https,在bboss中,配置开启https协议的Elasticsearch节点地址时,需带上https://协议头; //更多https配置,可浏览后面参考资料中的【高性能elasticsearch ORM开发库使用介绍】了解 properties.put("esDS.elasticsearch.useHttps","true"); properties.put("esDS.elasticsearch.rest.hostNames","https://102.180.211.27:9280,https://102.180.211.27:9281,https://102.180.211.27:9282"); //是否在控制台打印dsl语句,log4j组件日志级别为INFO或者DEBUG properties.put("esDS.elasticsearch.showTemplate","true"); //集群节点自动发现,关闭服务发现机制 properties.put("esDS.elasticsearch.discoverHost","false"); //Kerberos安全认证配置--开始 properties.put("esDS.http.kerberos.serverRealmPath","/elasticsearch/serverrealm");//配置华为云Elasticsearch服务端Princpal查询服务地址 properties.put("esDS.http.kerberos.useSubjectCredsOnly","false"); //华为云Elasticsearch krb5.conf文件,由华为提供 properties.put("esDS.http.kerberos.krb5Location","C:/environment/es/8.13.2/elasticsearch-8.13.2/config/krb5.conf"); //华为云Elasticsearch jaas.conf文件,由华为提供 properties.put("esDS.http.kerberos.loginConfig","C:/environment/es/8.13.2/elasticsearch-8.13.2/config/jaas.conf");
//配置登录模块名称,与华为云Elasticsearch jaas.conf文件中的模块名称一致 properties.put("esDS.http.kerberos.loginContextName","ESClient"); //配置是否debug Kerberos认证详细日志 properties.put("esDS.http.kerberos.debug","true");
//Kerberos安全认证配置--结束 //启动和初始化Elasticsearch数据源 ElasticSearchBoot.boot(properties); //通过Elasticsearch数据源名称esDS获取对应数据源的客户端API,操作和访问Elasticsearch //可以反复根据数据源名称esDS,调用下面的方法获取ClientInterface接口实例,始终返回单实例多线程安全的ClientInterface对象 ClientInterface clientInterface = ElasticSearchHelper.getRestClientUtil("esDS"); //验证客户端:通过Elasticsearch rest服务获取ES集群信息 String result = clientInterface.executeHttp("/?pretty", ClientInterface.HTTP_GET); logger.info(result); //验证客户端:通过API获取ES集群配置参数 logger.info(clientInterface.getClusterSettings());
//验证客户端:通过API判断索引demo是否存在 boolean exist = clientInterface.existIndice("demo");
logger.info(exist+""); //验证客户端:通过API从索引demo获取文档id为1的文档数据(String报文) String doc = clientInterface.getDocument("demo","1");
logger.info(doc);
//验证客户端:通过API从索引demo获取文档id为1的文档数据(or mapping示例:返回Map结构的数据,亦可以转换为PO对象) Map mapdoc = clientInterface.getDocument("demo","1",Map.class);
复制代码


基于配置 Kerberos 认证实现代码非常简洁,只需在平常数据源参数配置的基础上,增加 Kerberos 认证相关的参数配置。上述代码中涉及的华为云 Kerberos 配置文件 krb5.conf 和 jaas.conf,由华为云 Elasticsearch 提供,这里不单独介绍,需要注意一下:http.kerberos.loginContextName 参数对应的值需与 jaas.conf 配置文件中认证模块名称一致,这里是 ESClient。


下面是一个 jaas.conf 配置内容样例:


ESClient {  com.sun.security.auth.module.Krb5LoginModule required  useKeyTab=true  keyTab="C:/environment/es/8.13.2/elasticsearch-8.13.2/config/elastic.keytab"  principal="elastic/admin@BBOSSGROUPS.COM"  useTicketCache=false  storeKey=true  debug=false;};
复制代码


其中的 elastic.keytab 文件由华为云 Elasticsearch 提供。更多 ClientInterface api 使用方法,可以访问下面参考资料中提供的链接了解。

4. 运行案例

本文对应的代码源码工程下载地址:


码云 https://gitee.com/bboss/eshelloword-booter


Github https://github.com/bbossgroups/eshelloword-booter


对应的 Kerberos 认证 Java Demo CustormInitAndBootKerberosAuth.java


可以下载案例源码工程,参考前面章节内容,将 Elasticsearch 地址和 Kerberos 相关配置修改为本地配置,然后执行案例代码,如输出如下信息,说明集成成功:


11:36:07.976 [main] INFO  org.frameworkset.elasticsearch.client.ElasticSearchRestClient - Elasticsearch Server Info:{  "name" : "EsNode1@125.214.131.185",  "cluster_name" : "elasticsearch_cluster",  "cluster_uuid" : "3ovPaNQvTgSmbTIHIeL54Q",  "version" : {    "number" : "7.10.2",    "build_flavor" : "oss",    "build_type" : "tar",    "build_hash" : "unknown",    "build_date" : "unknown",    "build_snapshot" : true,    "lucene_version" : "8.7.0",    "minimum_wire_compatibility_version" : "6.7.0",    "minimum_index_compatibility_version" : "6.0.0-beta1"  },  "tagline" : "You Know, for Search"}
复制代码


可以观看 bboss 环境搭建视频,搭建运行环境:https://mp.weixin.qq.com/s/RoJdxiPw_mnuhQpkqzY9QQ

5. 参考资料

Elasticsearch 文档增删改查操作介绍 https://esdoc.bbossgroups.com/#/document-crud


高性能 elasticsearch ORM 开发库使用介绍 https://esdoc.bbossgroups.com/#/development


快速开始 bboss https://esdoc.bbossgroups.com/#/quickstart


开发交流 https://esdoc.bbossgroups.com/#/supportus

用户头像

大河

关注

开源项目bboss作者 2022-08-15 加入

https://www.bbossgroups.com

评论

发布
暂无评论
基于Kerberos认证对接华为云Elasticsearch_elasticsearch_大河_InfoQ写作社区