【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.mfbz.cn/a/442281.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

基于SpringBoot校园失物招领系统的设计与实现(程序+数据库+文档)

** &#x1f345;点赞收藏关注 → 私信领取本源代码、数据库&#x1f345; 本人在Java毕业设计领域有多年的经验&#xff0c;陆续会更新更多优质的Java实战项目&#xff0c;希望你能有所收获&#xff0c;少走一些弯路。&#x1f345;关注我不迷路&#x1f345;** 一、研究背景…

Linux操作系统的vim常用命令和vim 键盘图

在vi编辑器的命令模式下&#xff0c;命令的组成格式是&#xff1a;nnc。其中&#xff0c;字符c是命令&#xff0c;nn是整数值&#xff0c;它表示该命令将重复执行nn次&#xff0c;如果不给出重复次数的nn值&#xff0c;则命令将只执行一次。例如&#xff0c;在命令模式下按j键表…

RabbitMQ篇

1.初始MQ 1.1. 同步和异步通讯 微服务间通讯有同步和异步两种方式&#xff1a; 同步通讯&#xff1a;就像打电话&#xff0c;需要实时响应。 异步通讯&#xff1a;就像发邮件&#xff0c;不需要马上回复。 两种方式各有优劣&#xff0c;打电话可以立即得到响应&#xff0c;…

计算机网络——23网络层导论

网络层导论 网络层服务 在发送主机和接收主机对之间传送段&#xff08;segment&#xff09;在发送端将段封装到数据报中在接收端&#xff0c;将段上交给传输层实体网络层协议存在于每一个主机和路由器路由器检查每一个经过它的IP数据报的头部 网络层的关键功能 网络层功能 …

探秘WiFi 6技术:特性与优势解析

随着科技的飞速发展&#xff0c;无线网络技术也在不断演进&#xff0c;WiFi 6&#xff08;802.11ax&#xff09;作为最新一代的无线网络标准&#xff0c;正逐渐成为新一轮技术革新的引领者。本文将深入探讨WiFi 6模块的特性和优势&#xff0c;揭示其在无线通信领域的巨大潜力。…

(3)应用与信息

文章目录 前言 3.1 FlightDeck FrSky发射器应用程序 3.2 MAVLink2数据包签名(安全) 3.3 MAVLink高延迟协议 3.4 无线地面站连接中继器 1 概述 2 组件 3 设置 3.5 遥测无线电区域条例 3.6 用于OpenTX的Yaapu遥测脚本 前言 FlightDeck FrSky Transmitter AppMAVLink2 …

sizeof和strlen的详细万字解读

sizeof和strlen的对比 sizeof不是函数 侧面证明sizeof不是函数 如果是函数 应该需要有括号 不能落下来 strlen 只针对字符串 包含头文件 string.h 并且这个是个函数 随机数值 sizeof里面有表达式的话 表达式里面是不参与计算的 下面的s求出的是4 就是因为是不参与计算的 不…

重学SpringBoot3-WebMvcConfigurer接口

重学SpringBoot3-WebMvcConfigurer接口 WebMvcConfigurer基本信息为什么WebMvcConfigurer能配置底层行为实现WebMvcConfigurer举例1. 自定义格式化器和转换器2. 添加拦截器3. 配置静态资源4. 配置视图控制器 上一篇文章对 SpringMVC 重要配置类—— WebMvcAutoConfiguration 类…

httprunner结合pytest的自动化框架结构

2. 项目结构 2.1. 初代项目结构 3. 用例结构 - pytest 3.1. 单接口用例结构 # NOTE: Generated By HttpRunner v4.3.5 # FROM: testcases\data\data_20240229_test.json from httprunner import HttpRunner, Config, Step, RunRequestclass TestCaseData20240229Test(HttpRu…

数据集生成 YOLOV5 可训练的数据目录、并且可视化

1、前言 YOLOV5 训练数据的目录结构如下&#xff1a; 如果有测试集的话&#xff0c;也按照下面目录摆放即可 注意&#xff1a;这里的图片和标签文件名要严格对应&#xff01;&#xff01;后缀除外 关于YOLOv5介绍或者yolo格式的介绍参考之前专栏&#xff0c; 2、划分数据生成…

WPF 消息提示 类似toast方式

WPF里面的消息提示一般都是MessageBox.Show()&#xff0c;这种样式不是很好看&#xff0c;所以就想办法重新搞了一个类似弹出消息的功能。原理很简单&#xff0c;就是弹出一个新窗体&#xff0c;然后等几秒窗体自动关闭。 先上效果图&#xff1a; 新建一个MsgHelper.cs类&…

神经网络 梯度与神经元参数w、b关系;梯度与导数关系

参考&#xff1a;https://blog.csdn.net/weixin_44259490/article/details/90295146 概念 梯度与w的关系可以用梯度下降公式来表示&#xff1a;ww−α ∂ c o s t ∂ w \frac{\partial cost}{\partial w} ∂w∂cost​&#xff0c;其中w表示网络的权重&#xff0c; ∂ c o s t…

AI 辅助研发趋势 - 动动嘴完成代码?

目录 2024 年 AI 辅助研发趋势 方向一&#xff1a;AI辅助研发的技术进展 方向二&#xff1a;行业应用案例 方向三&#xff1a; 面临的挑战与机遇 方向四&#xff1a;未来趋势预测 方向五&#xff1a;与法规的影响 方向六&#xff1a;人才培养与教育 2024 年 AI 辅助研发…

141 Linux 系统编程18 ,线程,线程实现原理,ps –Lf 进程 查看

一 线程概念 什么是线程 LWP&#xff1a;light weight process 轻量级的进程&#xff0c;本质仍是进程(在Linux环境下) 进程&#xff1a;独立地址空间&#xff0c;拥有PCB 线程&#xff1a;有独立的PCB&#xff0c;但没有独立的地址空间(共享) 区别&#xff1a;在于是否共…

CorelDRAW Essentials2024使用简单易学的图形软件,让设计充满乐趣

CorelDRAW Essentials 2024使用简单易学的图形软件&#xff0c;让设计充满乐趣 创作引人注目的海报、卡片、社交媒体图片等。 增强功能&#xff01;支持文件导入/导出新增功能&#xff01;支持 WebP 文件&#xff0c;提高网页兼容性并优化图像交付增强功能&#xff01;显著的…

服务器租用和托管的区别

目前对于服务器要求相对高的企业会希望使用独立服务器来运行自己的网站&#xff0c;而在选择独立服务器业务事&#xff0c;是使用服务器托管还是服务器租用这两种方法时&#xff0c;许多刚进入网络或者传统行业的从业者&#xff0c;都不太了解什么是服务器&#xff0c;现在我来…

RabbitMQ 安装使用

文章目录 RabbitMQ 安装使用安装下载 Erlang下载 RabbitMQ 的服务安装好后看是否有 RabbitMQ 的服务开启管理 UIRabbitMQ 端口使用一览图 使用输出最简单的 Hello World&#xff01;生产者定义消费者消费消息小拓展 RabbitMQ 安装使用 安装 下载 Erlang RabbitMQ 是用这个语…

ai写作一键生成文章速度快

ai写作是一种基于人工智能技术的自动写作工具&#xff0c;它可以根据用户提供的主题或关键词&#xff0c;快速一键生成文章。与传统的手动写作相比&#xff0c;ai写作的速度更快&#xff0c;效率更高。下面小编就带大家一起来见识下ai写作一键生成文章的速度是如何之快&#xf…

string 底层模拟实现常用接口

目录 前言 什么是string? 为什么要学习使用string&#xff1f;string的优势&#xff1f; 因此&#xff0c;string类的成员变量也如图顺序表一样&#xff0c;如下图所示&#xff1a; 构造函数 拷贝构造 析构函数 size() 、capacity&#xff08;&#xff09; operato…

【C++】什么是类与对象?

&#x1f984;个人主页:修修修也 &#x1f38f;所属专栏:C ⚙️操作环境:Visual Studio 2022 目录 面向对象概述 封装 继承 多态 类 类是什么? C中类的引入 C中类的定义 类的两种定义方式: 1.声明和定义全部放在类体中 2.类声明与成员函数定义分别放在不同的工程文件中…
最新文章