【kerberos】hadoop集群使用keytab认证的逻辑

一、背景:

haoop的kerberos认证核心是org.apache.hadoop.security.UserGroupInformation类。
UserGroupInformation一般有两种:(1)apache原生的(2)cdh hdp改良过的,即cloudera改良过的。
由此衍生出两个验证方法。
kerberos是有MIT研发出来的,mit也为kerberos开发了客户端。

MIT官网为kerberos规定的常用环境变量有如下:

KRB5CCNAME
Default name for the credentials cache file, in the form TYPE:residual. The type of the default cache may determine the availability of a cache collection. FILE is not a collection type; KEYRING, DIR, and KCM are.
If not set, the value of default_ccache_name from configuration files (see KRB5_CONFIG) will be used. If that is also not set, the default type is FILE, and the residual is the path /tmp/krb5cc_uid, where uid is the decimal user ID of the user.

KRB5_KTNAME
Specifies the location of the default keytab file, in the form TYPE:residual. If no type is present, the FILE type is assumed and residual is the pathname of the keytab file. If unset, DEFKTNAME will be used.

KRB5_CONFIG
Specifies the location of the Kerberos configuration file. The default is SYSCONFDIR/krb5.conf. Multiple filenames can be specified, separated by a colon; all files which are present will be read.

KRB5_KDC_PROFILE
Specifies the location of the KDC configuration file, which contains additional configuration directives for the Key Distribution Center daemon and associated programs. The default is LOCALSTATEDIR/krb5kdc/kdc.conf.

KRB5RCACHENAME
(New in release 1.18) Specifies the location of the default replay cache, in the form type:residual. The file2 type with a pathname residual specifies a replay cache file in the version-2 format in the specified location. The none type (residual is ignored) disables the replay cache. The dfl type (residual is ignored) indicates the default, which uses a file2 replay cache in a temporary directory. The default is dfl:.

KRB5RCACHETYPE
Specifies the type of the default replay cache, if KRB5RCACHENAME is unspecified. No residual can be specified, so none and dfl are the only useful types.

KRB5RCACHEDIR
Specifies the directory used by the dfl replay cache type. The default is the value of the TMPDIR environment variable, or /var/tmp if TMPDIR is not set.

KRB5_TRACE
Specifies a filename to write trace log output to. Trace logs can help illuminate decisions made internally by the Kerberos libraries. For example, env KRB5_TRACE=/dev/stderr kinit would send tracing information for kinit to /dev/stderr. The default is not to write trace log output anywhere.

KRB5_CLIENT_KTNAME
Default client keytab file name. If unset, DEFCKTNAME will be used).

KPROP_PORT
kprop port to use. Defaults to 754.

GSS_MECH_CONFIG
Specifies a filename containing GSSAPI mechanism module configuration. The default is to read SYSCONFDIR/gss/mech and files with a .conf suffix within the directory SYSCONFDIR/gss/mech.d.

以上环境变量常用的有
KRB5_CONFIG :krb5.conf或krb5.ini文件路径
KRB5CCNAME:kerberos cache文件路径(注:此文件可由MIT kerberos客户端生成)

二、具体认证步骤

1、krb5.conf信息配置

注意:UserGroupInformation中设置KRB5_CONFIG是没有用的,必须要设置java.security.krb5.conf

如下方法都可以:
(1)项目启动指定java vm变量:-Djava.security.krb5.conf=D:/xxx/xxx/krb5.conf
(2)程序中指定:System.setProperty("java.security.krb5.conf", "D:/xxx/xxx/krb5.conf")

如果不指定程序会找不到kdc,报异常,如下:

org.apache.hadoop.security.KerberosAuthException: failure to login: for principal: xxx@HADOOP.COM from keytab D:\xxx\xxx\xxx.keytab javax.security.auth.login.LoginException: null (68)
Caused by: javax.security.auth.login.LoginException: null (68)
Caused by: KrbException: null (68) 
Caused by: KrbException: Identifier doesn't match expected value (906)

2、hadoop conf信息配置

Hadoop configuration配置(类org.apache.hadoop.conf.Configuration
文档中明确了:会默认加载类路径下的core-default.xml文件内容。

Unless explicitly turned off, Hadoop by default specifies two resources, loaded in-order from the classpath:
core-default.xml: Read-only defaults for hadoop.
core-site.xml: Site-specific configuration for a given hadoop installation.

core-default.xml中含有hadoop的安全配置hadoop.security.authentication,在UserGroupInformation中依据此项配置,查询集群是否启动kerberos。
HADOOP_SECURITY_AUTHENTICATION路径如下:
org.apache.hadoop.fs.CommonConfigurationKeysPublic#HADOOP_SECURITY_AUTHENTICATION

  public static AuthenticationMethod getAuthenticationMethod(Configuration conf) {String value = conf.get(HADOOP_SECURITY_AUTHENTICATION, "simple");try {return Enum.valueOf(AuthenticationMethod.class,StringUtils.toUpperCase(value));} catch (IllegalArgumentException iae) {throw new IllegalArgumentException("Invalid attribute value for " +HADOOP_SECURITY_AUTHENTICATION + " of " + value);}}

所以如果在环境变量中配置了HADOOP_HOME或者HADOOP_CONF_DIR对于UserGroupInformation是没有用的。
必须将core-site.xml放在类路径下,或者直接调用org.apache.hadoop.security.UserGroupInformation#setConfiguration设置加载过core-site.xml的conf对象。

3、UserGroupInformation认证

3.1 、apache原生的UserGroupInformation验证:

UserGroupInformation类中使用静态变量存放hadoop conf和已认证用户信息,所以只需要程序中认证一次,不同类不需要传递认证的user,只需要都到UserGroupInformation取即可。

    private static Configuration conf;private static UserGroupInformation loginUser = null;private static String keytabPrincipal = null;private static String keytabFile = null;

调用org.apache.hadoop.security.UserGroupInformation#loginUserFromKeytab传入principal和keytab就可以完成认证。

3.2、cloudera改良过的UserGroupInformation验证:

当然,可以调用原生的loginUserFromKeytab也可以。

改良内容就是通过配置环境变量的方法,隐性完成kerberos用户认证。无需UserGroupInformation认证,在调用getLoginUser可以自动完成认证。
具体过程如下:
org.apache.hadoop.security.UserGroupInformation#getLoginUser方法获取用户

  public static UserGroupInformation getLoginUser() throws IOException {...if (loginUser == null) {UserGroupInformation newLoginUser = createLoginUser(null);... }}

实际是调用了doSubjectLogin(null, null)

  UserGroupInformation createLoginUser(Subject subject) throws IOException {UserGroupInformation realUser = doSubjectLogin(subject, null);...}

如下代码subject == null && params == null判断true

 private static UserGroupInformation doSubjectLogin(Subject subject, LoginParams params) throws IOException {ensureInitialized();// initial default login.if (subject == null && params == null) {params = LoginParams.getDefaults();}HadoopConfiguration loginConf = new HadoopConfiguration(params);try {HadoopLoginContext login = newLoginContext(authenticationMethod.getLoginAppName(), subject, loginConf);login.login();
...}

获取环境变量:KRB5PRINCIPALKRB5KEYTABKRB5CCNAME

  private static class LoginParams extends EnumMap<LoginParam,String>implements Parameters {...static LoginParams getDefaults() {LoginParams params = new LoginParams();params.put(LoginParam.PRINCIPAL, System.getenv("KRB5PRINCIPAL"));params.put(LoginParam.KEYTAB, System.getenv("KRB5KEYTAB"));params.put(LoginParam.CCACHE, System.getenv("KRB5CCNAME"));return params;}}

结果是利用环境变量设置的pricipal+keytab或者cache认证。
环境变量配置:
(1)每个程序单独配置:
-DKRB5PRINCIPAL=xxx -DKRB5KEYTAB=xxx 或者 -DKRB5CCNAME=xxx

(2)系统环境默认配置:
win环境如下:
在这里插入图片描述linux环境下/etc/profile添加:
export KRB5PRINCIPAL=xxx -DKRB5KEYTAB=xxx或者export KRB5CCNAME=xxx

完成以上配置就认证成功了,如下info日志信息:

2024-03-08 17:28:11 [org.apache.hadoop.security.UserGroupInformation] INFO : Login successful for user xxx@HADOOP.COM using keytab file D:\xxx\xxx.keytab

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

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

相关文章

Flink并行度

1、Task flink中每个算子就是一个Task&#xff0c;比如flatMap、map、sum是一个Task。 2、SubTask 算子有几个并行度SubTask的数量就是几&#xff0c;比如 3、算子并行度 算子并行度指的是每个算子的并行度&#xff0c;可用env.setParallelism(1);设置所有算子的并行度&am…

基于Jupyter快速入门Python,Numpy,Scipy,Matplotlib

文章目录 Jupyter 和 Colab 笔记本PythonPython 版本基础数据类型数字Numbers布尔值Booleans字符串Strings 容器列表List字典Dictionaries集合Sets元组Tuples 函数类 Numpy数组Array数组索引Array indexing数据类型DatatypesArray math广播Broadcasting Scipy图像操作MATLAB文件…

goby的安装和使用

简介 Goby是一款基于网络空间测绘技术的新一代网络安全工具&#xff0c;它通过给目标网络建立完整的资产知识库&#xff0c;进行网络安全事件应急与漏洞应急。 Goby可提供最全面的资产识别&#xff0c;目前预置了超过10万种规则识别引擎&#xff0c;能够针对硬件设备和软件业…

一键查看:大厂网站都用了啥技术栈,有图有真相。

本次我们采用Wappalyzer插件来看下国内大厂的网站都采用了什么技术架构&#xff0c;文章最后由Wappalyzer的安装方法。 今日头条网站 淘宝网站 哔哩哔哩 京东商城 花瓣网 CSDN 国务院 网易 58同城 腾讯网 如何安装Wappalyzer 用Edge浏览器即可

laravel 事件+队列 使用案例讲解分析

laravel 事件队列 使用案例讲解分析 场景:出发事件&#xff0c;在监听事件的监听器内引入队列机制&#xff0c;异步处理监听事件对应的业务 定义事件&#xff1a;目录app/Events(路径没有就手动建)下 新建文件DeomEvent.php <?phpnamespace App\Events;use Illuminate\B…

Java后端八股笔记

Java后端八股笔记 Redis八股 上两种都有可能导致脏数据 所以使用两次删除缓存的技术&#xff0c;延时是因为数据库有主从问题需要更新&#xff0c;无法达到完全的强一致性&#xff0c;只能达到控制一致性。 一般放入缓存中的数据都是读多写少的数据 业务逻辑代码&#x1f44…

9、JavaWeb-文件上传-配置文件

P146 案例-文件上传-简介 文件上传&#xff0c;将本地图片、视频等文件上传到服务器&#xff0c;供其他用户浏览下载的过程。 文件上传前端页面三要素&#xff1a; 如果前端表单的编码格式选择的默认编码方式x-www.form-urlencoded&#xff0c;则提交的文件仅仅是文件名&…

【C++ STL详解】——string类

目录 前言 一、string类对象的常见构造 二、string类对象的访问及遍历 1.下标【】&#xff08;底层operator【】函数&#xff09; ​编辑 2.迭代器 3.范围for 4.at 5.back和front 三、string类对象的容量操作 1.size 和 length 2.capacity 3.empty 4.clear 5.res…

【AIGC】Stable Diffusion的建模思想、训练预测方式快速

在这篇博客中&#xff0c;将会用机器学习入门级描述&#xff0c;来介绍Stable Diffusion的关键原理。目前&#xff0c;网络上的使用教程非常多&#xff0c;本篇中不会介绍如何部署、使用或者微调SD模型。也会尽量精简语言&#xff0c;无公式推导&#xff0c;旨在理解思想。让有…

JVM-虚拟机栈概述

背景&#xff1a;由于跨平台的设计&#xff0c;java指令都是根据栈来设计的。不同平台CPU架构不同&#xff0c;所以不能设计为基于寄存器。 栈是运行时单位&#xff0c;而堆是存储的单位。即&#xff1a;栈解决程序运行的问题&#xff0c;即程序如何执行&#xff0c;或者说如何…

JavaWeb——013SpringBootWeb综合案例(事务管理、AOP)

事务&AOP 目录 事务&AOP1. 事务管理1.1 事务回顾1.2 Spring事务管理1.2.1 案例1.2.2 原因分析1.2.3 Transactional注解 1.3 事务进阶1.3.1 rollbackFor1.3.3 propagation1.3.3.1 介绍1.3.3.2 案例 2. AOP基础2.1 AOP概述2.2 AOP快速入门2.3 AOP核心概念 3. AOP进阶3.1 …

ai直播数字人:AI大模型应用开发的神奇世界

当AI技术的发展走向一个新的高峰&#xff0c;AI直播数字人逐渐成为人们关注的焦点。这种全新的数字人形态&#xff0c;通过大模型应用开发&#xff0c;带来了一个神奇世界。 在这个神奇世界里&#xff0c;AI直播数字人可以展现出与真实人类相媲美的外貌和声音。通过先进的图像…

方法中单独使用return关键字

一、return关键字的单独使用 二、示例代码 public class ReturnDemo {public static void main(String[] args) {chu(10,0);chu(10,2);}public static void chu(int a,int b){if (b 0) {System.out.println("除法出错&#xff0c;除数不能为零");return;}System.ou…

消息队列-kafka-消息发送流程(源码跟踪) 与消息可靠性

官方网址 源码&#xff1a;https://kafka.apache.org/downloads 快速开始&#xff1a;https://kafka.apache.org/documentation/#gettingStarted springcloud整合 发送消息流程 主线程&#xff1a;主线程只负责组织消息&#xff0c;如果是同步发送会阻塞&#xff0c;如果是异…

学习笔记:顺序表和链表(一、顺序表)

首先来个导言&#xff1a; 1.数组的优势&#xff1a;下标的随机访问&#xff0c;物理空间连续。数组指针用[ ]或者 * , 结构体指针用 - > 2.书写习惯 test.c写出主体框架 QelList.c写出结构体、头文件、函数声明 QelList.c写出函数的实现 3.挪动&#xff1a;如果从前…

Oracle数据库日志文件

导读 本文讲解Oracle数据库的日志文件 Oracle数据库日志文件是数据库管理中至关重要的组成部分&#xff0c;它们记录了数据库系统中发生的各种事务和操作&#xff0c;以确保数据的一致性、完整性和可恢复性。在Oracle数据库中&#xff0c;主要有三种类型的日志文件&#xff1a;…

2024年AIGC发展现状

AIGC的发展现状呈现出一种蓬勃发展的态势。随着技术的不断进步和应用场景的不断拓展&#xff0c;AIGC正在逐渐成为引领新一轮科技革命和产业变革的重要力量。 在应用层创新方面&#xff0c;AIGC产业发展已经确定了明确的方向。AIGC应用正在率先在B端办公和生产力场景中落地&am…

APEX开发过程中需要注意的小细节3

【问题记录】在编辑数据信息时发现辩护的数据无法保存&#xff0c;提示 “ORA-01799: 列不能外部联接到子查询” 仅展示的数据来自主表&#xff0c;这部分都是关联子表重点内容&#xff0c;编辑时无法保存 于是想将扩展表作为主表&#xff0c;在主表进行修改试试 新的报错&a…

C#实现归并排序算法

C#实现归并排序算法 以下是 C# 中的归并排序算法实现示例&#xff1a; using System;class MergeSortAlgorithm {// 合并两个子数组static void Merge(int[] arr, int left, int mid, int right){// 计算左子数组和右子数组的长度int n1 mid - left 1;int n2 right - mid;/…

C语言从入门到精通 第十二章(程序的编译及链接)

写在前面&#xff1a; 本系列专栏主要介绍C语言的相关知识&#xff0c;思路以下面的参考链接教程为主&#xff0c;大部分笔记也出自该教程。除了参考下面的链接教程以外&#xff0c;笔者还参考了其它的一些C语言教材&#xff0c;笔者认为重要的部分大多都会用粗体标注&#xf…